Mercurial > cpython-lru_cache-weakref
annotate Objects/unicodeobject.c @ 82251:bb5a8564e186 2.7
Issue #13153: Tkinter functions now raise TclError instead of ValueError when
a unicode argument contains non-BMP character.
| author | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| date | Mon, 18 Feb 2013 13:00:08 +0200 |
| parents | 498b54e0e856 |
| children | 391e3a7db1a3 |
| rev | line source |
|---|---|
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1 /* |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 Unicode implementation based on original code by Fredrik Lundh, |
|
12646
17d1a3f1f085
Minimal change so I can add the rest of MAL's checkin message:
Fred Drake <fdrake@acm.org>
parents:
12645
diff
changeset
|
4 modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 Unicode Integration Proposal (see file Misc/unicode.txt). |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7 Major speed upgrades to the method implementations at the Reykjavik |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
8 NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
9 |
|
13828
254f8b7c4a83
Changing the CNRI copyright notice according to CNRI's instructions.
Guido van Rossum <guido@python.org>
parents:
13761
diff
changeset
|
10 Copyright (c) Corporation for National Research Initiatives. |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
16029
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
12 -------------------------------------------------------------------- |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
13 The original string type implementation is: |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
14 |
| 51216 | 15 Copyright (c) 1999 by Secret Labs AB |
| 16 Copyright (c) 1999 by Fredrik Lundh | |
|
16029
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
17 |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
18 By obtaining, using, and/or copying this software and/or its |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
19 associated documentation, you agree that you have read, understood, |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
20 and will comply with the following terms and conditions: |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
21 |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
22 Permission to use, copy, modify, and distribute this software and its |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
23 associated documentation for any purpose and without fee is hereby |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
24 granted, provided that the above copyright notice appears in all |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
25 copies, and that both that copyright notice and this permission notice |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
26 appear in supporting documentation, and that the name of Secret Labs |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
27 AB or the author not be used in advertising or publicity pertaining to |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
28 distribution of the software without specific, written prior |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
29 permission. |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
30 |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
31 SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
32 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
33 FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
34 ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
35 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
36 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
37 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
38 -------------------------------------------------------------------- |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
39 |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
40 */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
37702
9b22d8b7bcaa
Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents:
37668
diff
changeset
|
42 #define PY_SSIZE_T_CLEAN |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 #include "Python.h" |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 #include "unicodeobject.h" |
|
13209
b11e0a898de0
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13196
diff
changeset
|
46 #include "ucnhash.h" |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
48 #ifdef MS_WINDOWS |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
49 #include <windows.h> |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
50 #endif |
| 12329 | 51 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 /* Limit for the Unicode object free list */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
54 #define PyUnicode_MAXFREELIST 1024 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 /* Limit for the Unicode object free list stay alive optimization. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 The implementation will keep allocated Unicode memory intact for |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 all objects on the free list having a size less than this |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
60 limit. This reduces malloc() overhead for small Unicode objects. |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
62 At worst this will result in PyUnicode_MAXFREELIST * |
| 12329 | 63 (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 malloc()-overhead) bytes of unused garbage. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 Setting the limit to 0 effectively turns the feature off. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
| 12329 | 68 Note: This is an experimental feature ! If you get core dumps when |
| 69 using Unicode objects, turn this feature off. | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
| 12329 | 73 #define KEEPALIVE_SIZE_LIMIT 9 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 /* Endianness switches; defaults to little endian */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 #ifdef WORDS_BIGENDIAN |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 # define BYTEORDER_IS_BIG_ENDIAN |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 #else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 # define BYTEORDER_IS_LITTLE_ENDIAN |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
83 /* --- Globals ------------------------------------------------------------ |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
84 |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
85 NOTE: In the interpreter's initialization phase, some globals are currently |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
86 initialized dynamically as needed. In the process Unicode objects may |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
87 be created before the Unicode type is ready. |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
88 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
89 */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
91 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
92 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
93 extern "C" { |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
94 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
95 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 /* Free list for Unicode objects */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
97 static PyUnicodeObject *free_list = NULL; |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
98 static int numfree = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
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
|
100 /* The empty Unicode object is shared to improve performance. */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
101 static PyUnicodeObject *unicode_empty = NULL; |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
102 |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
103 #define _Py_RETURN_UNICODE_EMPTY() \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
104 do { \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
105 if (unicode_empty != NULL) \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
106 Py_INCREF(unicode_empty); \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
107 else { \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
108 unicode_empty = _PyUnicode_New(0); \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
109 if (unicode_empty != NULL) \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
110 Py_INCREF(unicode_empty); \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
111 } \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
112 return (PyObject *)unicode_empty; \ |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
113 } while (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
|
114 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
115 /* 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
|
116 shared as well. */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
117 static PyUnicodeObject *unicode_latin1[256] = {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
|
118 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
119 /* 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
|
120 parameter; it is initialized by _PyUnicode_Init(). |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
121 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
122 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
|
123 PyUnicode_GetDefaultEncoding() APIs to access this global. |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
124 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
125 */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
126 static char unicode_default_encoding[100 + 1] = "ascii"; |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
127 |
|
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
|
128 /* 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
|
129 const unsigned char _Py_ascii_whitespace[] = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
130 0, 0, 0, 0, 0, 0, 0, 0, |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
131 /* case 0x0009: * CHARACTER TABULATION */ |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
132 /* case 0x000A: * LINE FEED */ |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
133 /* case 0x000B: * LINE TABULATION */ |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
134 /* case 0x000C: * FORM FEED */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
135 /* case 0x000D: * CARRIAGE RETURN */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
136 0, 1, 1, 1, 1, 1, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
137 0, 0, 0, 0, 0, 0, 0, 0, |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
138 /* case 0x001C: * FILE SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
139 /* case 0x001D: * GROUP SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
140 /* case 0x001E: * RECORD SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
141 /* case 0x001F: * UNIT SEPARATOR */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
142 0, 0, 0, 0, 1, 1, 1, 1, |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
143 /* case 0x0020: * SPACE */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
144 1, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
145 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
146 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
147 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
148 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
149 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
150 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
151 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
152 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
153 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
154 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
155 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
156 0, 0, 0, 0, 0, 0, 0, 0 |
|
45053
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
157 }; |
|
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 |
|
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 /* 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
|
160 static unsigned char ascii_linebreak[] = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
161 0, 0, 0, 0, 0, 0, 0, 0, |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
162 /* 0x000A, * LINE FEED */ |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
163 /* 0x000B, * LINE TABULATION */ |
|
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
164 /* 0x000C, * FORM FEED */ |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
165 /* 0x000D, * CARRIAGE RETURN */ |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
166 0, 0, 1, 1, 1, 1, 0, 0, |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
167 0, 0, 0, 0, 0, 0, 0, 0, |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
168 /* 0x001C, * FILE SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
169 /* 0x001D, * GROUP SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
170 /* 0x001E, * RECORD SEPARATOR */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
171 0, 0, 0, 0, 1, 1, 1, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
172 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
173 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
174 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
175 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
176 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
177 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
178 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
179 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
180 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
181 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
182 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
183 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
184 0, 0, 0, 0, 0, 0, 0, 0 |
|
45053
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
185 }; |
|
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
|
186 |
|
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
|
187 |
|
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
|
188 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
|
189 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
|
190 { |
|
18324
3ba7a358ce02
use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents:
18298
diff
changeset
|
191 #ifdef Py_UNICODE_WIDE |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
192 return 0x10FFFF; |
|
18298
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
193 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
194 /* This is actually an illegal character, so it should |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
195 not be passed to unichr. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
196 return 0xFFFF; |
|
18298
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
197 #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
|
198 } |
|
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
199 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
200 /* --- Bloom Filters ----------------------------------------------------- */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
201 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
202 /* 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
|
203 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
|
204 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
|
205 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
206 /* 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
|
207 |
|
58297
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
208 #if LONG_BIT >= 128 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
209 #define BLOOM_WIDTH 128 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
210 #elif LONG_BIT >= 64 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
211 #define BLOOM_WIDTH 64 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
212 #elif LONG_BIT >= 32 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
213 #define BLOOM_WIDTH 32 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
214 #else |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
215 #error "LONG_BIT is smaller than 32" |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
216 #endif |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
217 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
218 #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
|
219 |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
220 static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
221 |
|
58297
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
222 #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
223 #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
224 |
| 51216 | 225 #define BLOOM_LINEBREAK(ch) \ |
| 226 ((ch) < 128U ? ascii_linebreak[(ch)] : \ | |
| 227 (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
|
228 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
229 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
|
230 { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
231 /* 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
|
232 |
|
58297
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
233 BLOOM_MASK mask; |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
234 Py_ssize_t i; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
235 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
236 mask = 0; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
237 for (i = 0; i < len; i++) |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
238 BLOOM_ADD(mask, ptr[i]); |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
239 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
240 return mask; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
241 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
242 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
243 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
|
244 { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
245 Py_ssize_t i; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
246 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
247 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
|
248 if (set[i] == chr) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
249 return 1; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
250 |
|
38270
1dc5657f1745
return 0 on misses, not -1.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38257
diff
changeset
|
251 return 0; |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
252 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
253 |
| 51216 | 254 #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
255 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
|
256 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 /* --- Unicode Object ----------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 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
|
260 int unicode_resize(register PyUnicodeObject *unicode, |
| 51216 | 261 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
|
262 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 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
|
264 |
| 12329 | 265 /* 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
|
266 if (unicode->length == length) |
| 51216 | 267 goto reset; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 |
|
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
|
269 /* 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
|
270 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
|
271 instead ! */ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
272 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
273 if (unicode == unicode_empty || |
| 51216 | 274 (unicode->length == 1 && |
| 275 unicode->str[0] < 256U && | |
| 276 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
|
277 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
|
278 "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
|
279 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
282 /* 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
|
283 The overallocation is also used by fastsearch, which assumes that it's |
| 38405 | 284 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
|
285 it contains). */ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
286 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 oldstr = unicode->str; |
| 45826 | 288 unicode->str = PyObject_REALLOC(unicode->str, |
| 51216 | 289 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
|
290 if (!unicode->str) { |
| 51216 | 291 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
|
292 PyErr_NoMemory(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 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
|
296 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
|
297 |
| 51216 | 298 reset: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 /* 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
|
300 if (unicode->defenc) { |
|
63503
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
63106
diff
changeset
|
301 Py_CLEAR(unicode->defenc); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 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
|
304 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 /* We allocate one more byte to make sure the string is |
|
63462
75b3edda3c2b
Recorded merge of revisions 83444 via svnmerge from
Georg Brandl <georg@python.org>
parents:
63434
diff
changeset
|
309 Ux0000 terminated; some code relies on that. |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 XXX This allocator could further be enhanced by assuring that the |
| 51216 | 312 free list never reduces its size below 1. |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 static |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
317 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
|
318 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 register PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 |
| 38426 | 321 /* 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
|
322 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
|
323 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
|
324 return unicode_empty; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
327 /* Ensure we won't overflow the size. */ |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
328 if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
329 return (PyUnicodeObject *)PyErr_NoMemory(); |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
330 } |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
331 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 /* 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
|
333 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
|
334 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
|
335 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
|
336 numfree--; |
| 51216 | 337 if (unicode->str) { |
| 338 /* Keep-Alive optimization: we only upsize the buffer, | |
| 339 never downsize it. */ | |
| 340 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
|
341 unicode_resize(unicode, length) < 0) { |
| 51216 | 342 PyObject_DEL(unicode->str); |
| 343 unicode->str = NULL; | |
| 344 } | |
| 345 } | |
|
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
|
346 else { |
| 51216 | 347 size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 348 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
|
349 } |
|
c29f3135a20c
Fix a mis-indentation in _PyUnicode_New() that caused me to stare at
Guido van Rossum <guido@python.org>
parents:
18051
diff
changeset
|
350 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
|
351 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 else { |
| 51216 | 353 size_t new_size; |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
354 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
|
355 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 return NULL; |
| 51216 | 357 new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 358 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
|
359 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 |
| 12511 | 361 if (!unicode->str) { |
| 51216 | 362 PyErr_NoMemory(); |
| 363 goto onError; | |
| 12511 | 364 } |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
365 /* 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
|
366 * 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
|
367 * 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
|
368 * 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
|
369 * 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
|
370 * 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
|
371 */ |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
372 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
|
373 unicode->str[length] = 0; |
|
37668
4342e441b11c
Remove another INT_MAX limitation
Martin v. Löwis <martin@v.loewis.de>
parents:
37662
diff
changeset
|
374 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
|
375 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
|
376 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
|
377 return unicode; |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
378 |
| 51216 | 379 onError: |
|
48117
a94f136b5283
Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48114
diff
changeset
|
380 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
a94f136b5283
Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48114
diff
changeset
|
381 _Py_DEC_REFTOTAL; |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
382 _Py_ForgetReference((PyObject *)unicode); |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
383 PyObject_Del(unicode); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
384 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 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
|
388 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
|
389 { |
|
21262
e8fea95a82a4
Fix for #489669 (Neil Norwitz): memory leak in test_descr (unicode).
Guido van Rossum <guido@python.org>
parents:
21082
diff
changeset
|
390 if (PyUnicode_CheckExact(unicode) && |
| 51216 | 391 numfree < PyUnicode_MAXFREELIST) { |
| 12329 | 392 /* Keep-Alive optimization */ |
| 51216 | 393 if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 394 PyObject_DEL(unicode->str); | |
| 395 unicode->str = NULL; | |
| 396 unicode->length = 0; | |
| 397 } | |
| 398 if (unicode->defenc) { | |
|
63503
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
63106
diff
changeset
|
399 Py_CLEAR(unicode->defenc); |
| 51216 | 400 } |
| 401 /* 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
|
402 *(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
|
403 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
|
404 numfree++; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 else { |
| 51216 | 407 PyObject_DEL(unicode->str); |
| 408 Py_XDECREF(unicode->defenc); | |
| 409 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
|
410 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 |
|
50305
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
413 static |
|
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
414 int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
415 { |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
416 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
|
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 /* 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
|
419 if (unicode == NULL) { |
| 51216 | 420 PyErr_BadInternalCall(); |
| 421 return -1; | |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
422 } |
|
50305
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
423 v = *unicode; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43932
diff
changeset
|
424 if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
| 51216 | 425 PyErr_BadInternalCall(); |
| 426 return -1; | |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
427 } |
|
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 |
|
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 /* 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
|
430 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
|
431 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
|
432 if (v->length != length && |
| 51216 | 433 (v == unicode_empty || v->length == 1)) { |
| 434 PyUnicodeObject *w = _PyUnicode_New(length); | |
| 435 if (w == NULL) | |
| 436 return -1; | |
| 437 Py_UNICODE_COPY(w->str, v->str, | |
| 438 length < v->length ? length : v->length); | |
| 439 Py_DECREF(*unicode); | |
| 440 *unicode = w; | |
| 441 return 0; | |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
442 } |
|
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 |
|
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 /* 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
|
445 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
|
446 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
|
447 } |
|
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 |
|
50305
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
449 int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
|
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
450 { |
|
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
451 return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
|
e673632e1885
Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
452 } |
|
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
|
453 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
| 51216 | 455 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
|
456 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
459 /* 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
|
460 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
|
461 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
|
462 |
| 51216 | 463 /* Optimization for empty strings */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
464 if (size == 0) |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
465 _Py_RETURN_UNICODE_EMPTY(); |
| 51216 | 466 |
| 467 /* Single character Unicode objects in the Latin-1 range are | |
| 468 shared when using this constructor */ | |
| 469 if (size == 1 && *u < 256) { | |
| 470 unicode = unicode_latin1[*u]; | |
| 471 if (!unicode) { | |
| 472 unicode = _PyUnicode_New(1); | |
| 473 if (!unicode) | |
| 474 return NULL; | |
| 475 unicode->str[0] = *u; | |
| 476 unicode_latin1[*u] = unicode; | |
| 477 } | |
| 478 Py_INCREF(unicode); | |
| 479 return (PyObject *)unicode; | |
| 480 } | |
|
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
|
481 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
482 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 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
|
484 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 /* 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
|
488 if (u != NULL) |
| 51216 | 489 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
|
490 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 |
|
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
|
494 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
|
495 { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type 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 PyUnicodeObject *unicode; |
|
46383
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46333
diff
changeset
|
497 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
498 if (size < 0) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
499 PyErr_SetString(PyExc_SystemError, |
| 51216 | 500 "Negative size passed to PyUnicode_FromStringAndSize"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
501 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
502 } |
|
46383
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46333
diff
changeset
|
503 |
|
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
|
504 /* 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
|
505 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
|
506 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
|
507 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
|
508 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
|
509 |
| 51216 | 510 /* Optimization for empty strings */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
511 if (size == 0) |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
512 _Py_RETURN_UNICODE_EMPTY(); |
| 51216 | 513 |
| 514 /* Single characters are shared when using this constructor. | |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
515 Restrict to ASCII, since the input must be UTF-8. */ |
| 51216 | 516 if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 517 unicode = unicode_latin1[Py_CHARMASK(*u)]; | |
| 518 if (!unicode) { | |
| 519 unicode = _PyUnicode_New(1); | |
| 520 if (!unicode) | |
| 521 return NULL; | |
| 522 unicode->str[0] = Py_CHARMASK(*u); | |
| 523 unicode_latin1[Py_CHARMASK(*u)] = unicode; | |
| 524 } | |
| 525 Py_INCREF(unicode); | |
| 526 return (PyObject *)unicode; | |
| 527 } | |
|
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
|
528 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
529 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
|
530 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
531 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
532 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
|
533 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
|
534 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
|
535 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
536 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
|
537 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
538 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
539 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
|
540 { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
541 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
|
542 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
|
543 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
|
544 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
|
545 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
546 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
547 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
|
548 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type 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 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 #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
|
551 |
|
52210
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
552 #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
553 # define CONVERT_WCHAR_TO_SURROGATES |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
554 #endif |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
555 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
556 #ifdef CONVERT_WCHAR_TO_SURROGATES |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
557 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
558 /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
559 to convert from UTF32 to UTF16. */ |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
560 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
561 PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
562 Py_ssize_t size) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
563 { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
564 PyUnicodeObject *unicode; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
565 register Py_ssize_t i; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
566 Py_ssize_t alloc; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
567 const wchar_t *orig_w; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
568 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
569 if (w == NULL) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
570 PyErr_BadInternalCall(); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
571 return NULL; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
572 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
573 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
574 alloc = size; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
575 orig_w = w; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
576 for (i = size; i > 0; i--) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
577 if (*w > 0xFFFF) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
578 alloc++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
579 w++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
580 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
581 w = orig_w; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
582 unicode = _PyUnicode_New(alloc); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
583 if (!unicode) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
584 return NULL; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
585 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
586 /* Copy the wchar_t data into the new object */ |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
587 { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
588 register Py_UNICODE *u; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
589 u = PyUnicode_AS_UNICODE(unicode); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
590 for (i = size; i > 0; i--) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
591 if (*w > 0xFFFF) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
592 wchar_t ordinal = *w++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
593 ordinal -= 0x10000; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
594 *u++ = 0xD800 | (ordinal >> 10); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
595 *u++ = 0xDC00 | (ordinal & 0x3FF); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
596 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
597 else |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
598 *u++ = *w++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
599 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
600 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
601 return (PyObject *)unicode; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
602 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
603 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
604 #else |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
605 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
606 PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 51216 | 607 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
|
608 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
610 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 if (w == NULL) { |
| 51216 | 612 PyErr_BadInternalCall(); |
| 613 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
614 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
616 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
|
617 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
618 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
619 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
620 /* 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
|
621 #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
|
622 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
|
623 #else |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
624 { |
| 51216 | 625 register Py_UNICODE *u; |
| 626 register Py_ssize_t i; | |
| 627 u = PyUnicode_AS_UNICODE(unicode); | |
| 628 for (i = size; i > 0; i--) | |
| 629 *u++ = *w++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
630 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
631 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
632 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
633 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
634 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
635 |
|
52210
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
636 #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
637 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
638 #undef CONVERT_WCHAR_TO_SURROGATES |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
639 |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
640 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
|
641 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
|
642 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
643 *fmt++ = '%'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
644 if (width) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
645 if (zeropad) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
646 *fmt++ = '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
647 fmt += sprintf(fmt, "%d", width); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
648 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
649 if (precision) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
650 fmt += sprintf(fmt, ".%d", precision); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
651 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
652 *fmt++ = 'l'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
653 else if (size_tflag) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
654 char *f = PY_FORMAT_SIZE_T; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
655 while (*f) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
656 *fmt++ = *f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
657 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
658 *fmt++ = c; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
659 *fmt = '\0'; |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
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 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type 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 #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
|
663 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type 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 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
|
665 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
|
666 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
667 va_list count; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
668 Py_ssize_t callcount = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
669 PyObject **callresults = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
670 PyObject **callresult = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
671 Py_ssize_t n = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
672 int width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
673 int precision = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
674 int zeropad; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
675 const char* f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
676 Py_UNICODE *s; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
677 PyObject *string; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
678 /* used by sprintf */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
679 char buffer[21]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
680 /* use abuffer instead of buffer, if we need more space |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
681 * (which can happen if there's a format specifier with width). */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
682 char *abuffer = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
683 char *realbuffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
684 Py_ssize_t abuffersize = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
685 char fmt[60]; /* should be enough for %0width.precisionld */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
686 const char *copy; |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
687 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type 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 #ifdef VA_LIST_IS_ARRAY |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
689 Py_MEMCPY(count, vargs, sizeof(va_list)); |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
690 #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
|
691 #ifdef __va_copy |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
692 __va_copy(count, vargs); |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
693 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
694 count = vargs; |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
695 #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
|
696 #endif |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
697 /* step 1: count the number of %S/%R/%s format specifications |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
698 * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
699 * objects once during step 3 and put the result in an array) */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
700 for (f = format; *f; f++) { |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
701 if (*f == '%') { |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
702 if (*(f+1)=='%') |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
703 continue; |
|
53723
5ade0de74b5a
Merged revisions 72263 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53721
diff
changeset
|
704 if (*(f+1)=='S' || *(f+1)=='R') |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
705 ++callcount; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
706 while (isdigit((unsigned)*f)) |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
707 width = (width*10) + *f++ - '0'; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
708 while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
709 ; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
710 if (*f == 's') |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
711 ++callcount; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
712 } |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
713 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
714 /* step 2: allocate memory for the results of |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
715 * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
716 if (callcount) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
717 callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
718 if (!callresults) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
719 PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
720 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
721 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
722 callresult = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
723 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
724 /* step 3: figure out how large a buffer we need */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
725 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
726 if (*f == '%') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
727 const char* p = f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
728 width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
729 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
730 width = (width*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
731 while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
732 ; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
733 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
734 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
735 * they don't affect the amount of space we reserve. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
736 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
737 if ((*f == 'l' || *f == 'z') && |
| 51216 | 738 (f[1] == 'd' || f[1] == 'u')) |
| 739 ++f; | |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
740 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
741 switch (*f) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
742 case 'c': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
743 (void)va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
744 /* fall through... */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
745 case '%': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
746 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
747 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
748 case 'd': case 'u': case 'i': case 'x': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
749 (void) va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
750 /* 20 bytes is enough to hold a 64-bit |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
751 integer. Decimal takes the most space. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
752 This isn't enough for octal. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
753 If a width is specified we need more |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
754 (which we allocate later). */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
755 if (width < 20) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
756 width = 20; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
757 n += width; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
758 if (abuffersize < width) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
759 abuffersize = width; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
760 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
761 case 's': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
762 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
763 /* UTF-8 */ |
|
53780
5878ca784a84
#5929: fix signedness warning.
Georg Brandl <georg@python.org>
parents:
53740
diff
changeset
|
764 const char *s = va_arg(count, const char*); |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
765 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
766 if (!str) |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
767 goto fail; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
768 n += PyUnicode_GET_SIZE(str); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
769 /* Remember the str and switch to the next slot */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
770 *callresult++ = str; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
771 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
772 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
773 case 'U': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
774 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
775 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
776 assert(obj && PyUnicode_Check(obj)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
777 n += PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
778 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
779 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
780 case 'V': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
781 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
782 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
783 const char *str = va_arg(count, const char *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
784 assert(obj || str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
785 assert(!obj || PyUnicode_Check(obj)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
786 if (obj) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
787 n += PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
788 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
789 n += strlen(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
790 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
791 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
792 case 'S': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
793 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
794 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
795 PyObject *str; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
796 assert(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
797 str = PyObject_Str(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
798 if (!str) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
799 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
800 n += PyUnicode_GET_SIZE(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
801 /* Remember the str and switch to the next slot */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
802 *callresult++ = str; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
803 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
804 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
805 case 'R': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
806 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
807 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
808 PyObject *repr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
809 assert(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
810 repr = PyObject_Repr(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
811 if (!repr) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
812 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
813 n += PyUnicode_GET_SIZE(repr); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
814 /* Remember the repr and switch to the next slot */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
815 *callresult++ = repr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
816 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
817 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
818 case 'p': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
819 (void) va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
820 /* maximum 64-bit pointer representation: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
821 * 0xffffffffffffffff |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
822 * so 19 characters is enough. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
823 * XXX I count 18 -- what's the extra for? |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
824 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
825 n += 19; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
826 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
827 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
828 /* if we stumble upon an unknown |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
829 formatting code, copy the rest of |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
830 the format string to the output |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
831 string. (we cannot just skip the |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
832 code, since there's no way to know |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
833 what's in the argument list) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
834 n += strlen(p); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
835 goto expand; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
836 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
837 } else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
838 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
839 } |
| 51216 | 840 expand: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
841 if (abuffersize > 20) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
842 abuffer = PyObject_Malloc(abuffersize); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
843 if (!abuffer) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
844 PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
845 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
846 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
847 realbuffer = abuffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
848 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
849 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
850 realbuffer = buffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
851 /* step 4: fill the buffer */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
852 /* Since we've analyzed how much space we need for the worst case, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
853 we don't have to resize the string. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
854 There can be no errors beyond this point. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
855 string = PyUnicode_FromUnicode(NULL, n); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
856 if (!string) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
857 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
858 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
859 s = PyUnicode_AS_UNICODE(string); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
860 callresult = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
861 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
862 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
863 if (*f == '%') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
864 const char* p = f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
865 int longflag = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
866 int size_tflag = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
867 zeropad = (*f == '0'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
868 /* parse the width.precision part */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
869 width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
870 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
871 width = (width*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
872 precision = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
873 if (*f == '.') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
874 f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
875 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
876 precision = (precision*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
877 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
878 /* handle the long flag, but only for %ld and %lu. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
879 others can be added when necessary. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
880 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
881 longflag = 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
882 ++f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
883 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
884 /* handle the size_t flag. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
885 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
886 size_tflag = 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
887 ++f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
888 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
889 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
890 switch (*f) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
891 case 'c': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
892 *s++ = va_arg(vargs, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
893 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
894 case 'd': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
895 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
896 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
897 sprintf(realbuffer, fmt, va_arg(vargs, long)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
898 else if (size_tflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
899 sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
900 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
901 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
902 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
903 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
904 case 'u': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
905 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
906 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
907 sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
908 else if (size_tflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
909 sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
910 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
911 sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
912 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
913 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
914 case 'i': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
915 makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
916 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
917 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
918 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
919 case 'x': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
920 makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
921 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
922 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
923 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
924 case 's': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
925 { |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
926 /* unused, since we already have the result */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
927 (void) va_arg(vargs, char *); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
928 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
929 PyUnicode_GET_SIZE(*callresult)); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
930 s += PyUnicode_GET_SIZE(*callresult); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
931 /* We're done with the unicode()/repr() => forget it */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
932 Py_DECREF(*callresult); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
933 /* switch to next unicode()/repr() result */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
934 ++callresult; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
935 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
936 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
937 case 'U': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
938 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
939 PyObject *obj = va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
940 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
941 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
942 s += size; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
943 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
944 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
945 case 'V': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
946 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
947 PyObject *obj = va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
948 const char *str = va_arg(vargs, const char *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
949 if (obj) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
950 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
951 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
952 s += size; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
953 } else { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
954 appendstring(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
955 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
956 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
957 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
958 case 'S': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
959 case 'R': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
960 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
961 Py_UNICODE *ucopy; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
962 Py_ssize_t usize; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
963 Py_ssize_t upos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
964 /* unused, since we already have the result */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
965 (void) va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
966 ucopy = PyUnicode_AS_UNICODE(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
967 usize = PyUnicode_GET_SIZE(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
968 for (upos = 0; upos<usize;) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
969 *s++ = ucopy[upos++]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
970 /* We're done with the unicode()/repr() => forget it */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
971 Py_DECREF(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
972 /* switch to next unicode()/repr() result */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
973 ++callresult; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
974 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
975 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
976 case 'p': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
977 sprintf(buffer, "%p", va_arg(vargs, void*)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
978 /* %p is ill-defined: ensure leading 0x. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
979 if (buffer[1] == 'X') |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
980 buffer[1] = 'x'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
981 else if (buffer[1] != 'x') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
982 memmove(buffer+2, buffer, strlen(buffer)+1); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
983 buffer[0] = '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
984 buffer[1] = 'x'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
985 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
986 appendstring(buffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
987 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
988 case '%': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
989 *s++ = '%'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
990 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
991 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
992 appendstring(p); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
993 goto end; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
994 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
995 } else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
996 *s++ = *f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
997 } |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
998 |
| 51216 | 999 end: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1000 if (callresults) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1001 PyObject_Free(callresults); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1002 if (abuffer) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1003 PyObject_Free(abuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1004 PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1005 return string; |
| 51216 | 1006 fail: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1007 if (callresults) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1008 PyObject **callresult2 = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1009 while (callresult2 < callresult) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1010 Py_DECREF(*callresult2); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1011 ++callresult2; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1012 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1013 PyObject_Free(callresults); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1014 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1015 if (abuffer) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1016 PyObject_Free(abuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1017 return NULL; |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1018 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1019 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1020 #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
|
1021 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1022 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
|
1023 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
|
1024 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1025 PyObject* ret; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1026 va_list vargs; |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1027 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1028 #ifdef HAVE_STDARG_PROTOTYPES |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1029 va_start(vargs, format); |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1030 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1031 va_start(vargs); |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1032 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1033 ret = PyUnicode_FromFormatV(format, vargs); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1034 va_end(vargs); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1035 return ret; |
|
44945
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1036 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1037 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1038 Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 51216 | 1039 wchar_t *w, |
| 1040 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
|
1041 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1042 if (unicode == NULL) { |
| 51216 | 1043 PyErr_BadInternalCall(); |
| 1044 return -1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1045 } |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1046 |
|
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1047 /* 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
|
1048 if (size > PyUnicode_GET_SIZE(unicode)) |
| 51216 | 1049 size = PyUnicode_GET_SIZE(unicode) + 1; |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1050 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 #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
|
1052 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
|
1053 #else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1054 { |
| 51216 | 1055 register Py_UNICODE *u; |
| 1056 register Py_ssize_t i; | |
| 1057 u = PyUnicode_AS_UNICODE(unicode); | |
| 1058 for (i = size; i > 0; i--) | |
| 1059 *w++ = *u++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1060 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1062 |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1063 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
|
1064 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
|
1065 else |
| 51216 | 1066 return size; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1067 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1068 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1069 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1070 |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1071 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
|
1072 { |
|
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
|
1073 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
|
1074 |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1075 #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
|
1076 if (ordinal < 0 || ordinal > 0x10ffff) { |
| 51216 | 1077 PyErr_SetString(PyExc_ValueError, |
| 1078 "unichr() arg not in range(0x110000) " | |
| 1079 "(wide Python build)"); | |
| 1080 return NULL; | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1081 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1082 #else |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1083 if (ordinal < 0 || ordinal > 0xffff) { |
| 51216 | 1084 PyErr_SetString(PyExc_ValueError, |
| 1085 "unichr() arg not in range(0x10000) " | |
| 1086 "(narrow Python build)"); | |
| 1087 return NULL; | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1088 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1089 #endif |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1090 |
|
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
|
1091 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
|
1092 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
|
1093 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1094 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1095 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
|
1096 { |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1097 /* XXX Perhaps we should make this API an alias of |
| 51216 | 1098 PyObject_Unicode() instead ?! */ |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1099 if (PyUnicode_CheckExact(obj)) { |
| 51216 | 1100 Py_INCREF(obj); |
| 1101 return obj; | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1102 } |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1103 if (PyUnicode_Check(obj)) { |
| 51216 | 1104 /* For a Unicode subtype that's not a Unicode object, |
| 1105 return a true Unicode object with the same data. */ | |
| 1106 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), | |
| 1107 PyUnicode_GET_SIZE(obj)); | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1108 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1109 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
|
1110 } |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1111 |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1112 PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 51216 | 1113 const char *encoding, |
| 1114 const char *errors) | |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1115 { |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1116 const char *s = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1117 Py_ssize_t len; |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1118 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
|
1119 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1120 if (obj == NULL) { |
| 51216 | 1121 PyErr_BadInternalCall(); |
| 1122 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1123 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1124 |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1125 #if 0 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1126 /* 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
|
1127 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
|
1128 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
|
1129 Unicode subclasses. |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1130 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1131 NOTE: This API should really only be used for object which |
| 51216 | 1132 represent *encoded* Unicode ! |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1133 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1134 */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1135 if (PyUnicode_Check(obj)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1136 if (encoding) { |
| 51216 | 1137 PyErr_SetString(PyExc_TypeError, |
| 1138 "decoding Unicode is not supported"); | |
| 1139 return NULL; | |
| 1140 } | |
| 1141 return PyObject_Unicode(obj); | |
| 1142 } | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1143 #else |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1144 if (PyUnicode_Check(obj)) { |
| 51216 | 1145 PyErr_SetString(PyExc_TypeError, |
| 1146 "decoding Unicode is not supported"); | |
| 1147 return NULL; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1148 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1149 #endif |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1150 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1151 /* Coerce object */ |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1152 if (PyString_Check(obj)) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1153 s = PyString_AS_STRING(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1154 len = PyString_GET_SIZE(obj); |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46062
diff
changeset
|
1155 } |
|
47291
04bd3690fb1f
First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents:
46908
diff
changeset
|
1156 else if (PyByteArray_Check(obj)) { |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46062
diff
changeset
|
1157 /* Python 2.x specific */ |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46062
diff
changeset
|
1158 PyErr_Format(PyExc_TypeError, |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46062
diff
changeset
|
1159 "decoding bytearray is not supported"); |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46062
diff
changeset
|
1160 return NULL; |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46062
diff
changeset
|
1161 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1162 else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 51216 | 1163 /* Overwrite the error message with something more useful in |
| 1164 case of a TypeError. */ | |
| 1165 if (PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 1166 PyErr_Format(PyExc_TypeError, | |
| 1167 "coercing to Unicode: need string or buffer, " | |
| 1168 "%.80s found", | |
| 1169 Py_TYPE(obj)->tp_name); | |
| 1170 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
|
1171 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1172 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1173 /* Convert to Unicode */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
1174 if (len == 0) |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
1175 _Py_RETURN_UNICODE_EMPTY(); |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
1176 |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
1177 v = PyUnicode_Decode(s, len, encoding, errors); |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1178 return v; |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1179 |
| 51216 | 1180 onError: |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1181 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1182 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1183 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1184 PyObject *PyUnicode_Decode(const char *s, |
| 51216 | 1185 Py_ssize_t size, |
| 1186 const char *encoding, | |
| 1187 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
|
1188 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1189 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
|
1190 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1191 if (encoding == NULL) |
| 51216 | 1192 encoding = PyUnicode_GetDefaultEncoding(); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1193 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1194 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1195 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
|
1196 return PyUnicode_DecodeUTF8(s, size, errors); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1197 else if (strcmp(encoding, "latin-1") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1198 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
|
1199 #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
|
1200 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
|
1201 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
|
1202 #endif |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1203 else if (strcmp(encoding, "ascii") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1204 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
|
1205 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1206 /* 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
|
1207 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
|
1208 if (buffer == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1209 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1210 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
|
1211 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1212 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1213 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
|
1214 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
|
1215 "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
|
1216 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
|
1217 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1218 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1219 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1220 Py_DECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1221 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
|
1222 |
| 51216 | 1223 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1224 Py_XDECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1225 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1226 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1227 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1228 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
|
1229 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1230 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1231 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1232 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1233 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1234 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1235 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1236 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1237 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1238 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1239 if (encoding == NULL) |
| 51216 | 1240 encoding = PyUnicode_GetDefaultEncoding(); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1241 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1242 /* 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
|
1243 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
|
1244 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1245 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1246 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1247 |
| 51216 | 1248 onError: |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1249 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1250 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1251 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1252 PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 51216 | 1253 Py_ssize_t size, |
| 1254 const char *encoding, | |
| 1255 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
|
1256 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1257 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
|
1258 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1259 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
|
1260 if (unicode == NULL) |
| 51216 | 1261 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1262 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
|
1263 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1264 return v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1265 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1266 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1267 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
|
1268 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1269 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1270 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1271 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1272 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1273 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1274 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1275 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1276 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1277 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1278 if (encoding == NULL) |
| 51216 | 1279 encoding = PyUnicode_GetDefaultEncoding(); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1280 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1281 /* 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
|
1282 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
|
1283 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1284 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1285 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1286 |
| 51216 | 1287 onError: |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1288 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1289 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1290 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1291 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
|
1292 const char *encoding, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1293 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1294 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1295 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
|
1296 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1297 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
|
1298 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1299 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1300 } |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1301 |
|
30441
76e998aee644
On c.l.py, 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 if (encoding == NULL) |
| 51216 | 1303 encoding = PyUnicode_GetDefaultEncoding(); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1304 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1305 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1306 if (errors == NULL) { |
| 51216 | 1307 if (strcmp(encoding, "utf-8") == 0) |
| 1308 return PyUnicode_AsUTF8String(unicode); | |
| 1309 else if (strcmp(encoding, "latin-1") == 0) | |
| 1310 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
|
1311 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 51216 | 1312 else if (strcmp(encoding, "mbcs") == 0) |
| 1313 return PyUnicode_AsMBCSString(unicode); | |
|
29706
a4fead0e24f8
Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents:
29641
diff
changeset
|
1314 #endif |
| 51216 | 1315 else if (strcmp(encoding, "ascii") == 0) |
| 1316 return PyUnicode_AsASCIIString(unicode); | |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1317 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1318 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1319 /* 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
|
1320 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
|
1321 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1322 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1323 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
|
1324 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
|
1325 "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
|
1326 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
|
1327 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1328 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1329 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1330 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
|
1331 |
| 51216 | 1332 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1333 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1334 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1335 |
|
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
|
1336 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 51216 | 1337 const char *errors) |
|
13837
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1338 { |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1339 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
|
1340 |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1341 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
|
1342 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
|
1343 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
|
1344 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
|
1345 ((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
|
1346 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
|
1347 } |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1348 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1349 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
|
1350 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1351 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
|
1352 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1353 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1354 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1355 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
|
1356 |
| 51216 | 1357 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1358 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1359 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1360 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1361 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
|
1362 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1363 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
|
1364 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1365 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1366 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1367 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
|
1368 |
| 51216 | 1369 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1370 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1371 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1372 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
1373 const char *PyUnicode_GetDefaultEncoding(void) |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1374 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1375 return unicode_default_encoding; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1376 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1377 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1378 int PyUnicode_SetDefaultEncoding(const char *encoding) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1379 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1380 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
|
1381 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1382 /* 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
|
1383 loads the encoding into the codec registry cache. */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1384 v = _PyCodec_Lookup(encoding); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1385 if (v == NULL) |
| 51216 | 1386 goto onError; |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1387 Py_DECREF(v); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1388 strncpy(unicode_default_encoding, |
| 51216 | 1389 encoding, |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
1390 sizeof(unicode_default_encoding) - 1); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1391 return 0; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1392 |
| 51216 | 1393 onError: |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1394 return -1; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1395 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1396 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1397 /* 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
|
1398 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
|
1399 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
|
1400 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
|
1401 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
|
1402 */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1403 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1404 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1405 int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 51216 | 1406 const char *encoding, const char *reason, |
| 1407 const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, | |
| 1408 Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | |
| 1409 PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1410 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1411 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
|
1412 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1413 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
|
1414 PyObject *repunicode = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1415 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
|
1416 Py_ssize_t requiredsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1417 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
|
1418 Py_UNICODE *repptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1419 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
|
1420 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
|
1421 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1422 if (*errorHandler == NULL) { |
| 51216 | 1423 *errorHandler = PyCodec_LookupError(errors); |
| 1424 if (*errorHandler == NULL) | |
| 1425 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
|
1426 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1427 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1428 if (*exceptionObject == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1429 *exceptionObject = PyUnicodeDecodeError_Create( |
| 51216 | 1430 encoding, input, insize, *startinpos, *endinpos, reason); |
| 1431 if (*exceptionObject == NULL) | |
| 1432 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
|
1433 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1434 else { |
| 51216 | 1435 if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1436 goto onError; | |
| 1437 if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) | |
| 1438 goto onError; | |
| 1439 if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | |
| 1440 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
|
1441 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1442 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1443 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
|
1444 if (restuple == NULL) |
| 51216 | 1445 goto onError; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1446 if (!PyTuple_Check(restuple)) { |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
52411
diff
changeset
|
1447 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 1448 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
|
1449 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1450 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 51216 | 1451 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
|
1452 if (newpos<0) |
| 51216 | 1453 newpos = insize+newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1454 if (newpos<0 || newpos>insize) { |
| 51216 | 1455 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1456 goto onError; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1457 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1458 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1459 /* 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
|
1460 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
|
1461 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
|
1462 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
|
1463 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
|
1464 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
|
1465 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
|
1466 if (requiredsize > outsize) { |
| 51216 | 1467 if (requiredsize<2*outsize) |
| 1468 requiredsize = 2*outsize; | |
| 1469 if (_PyUnicode_Resize(output, requiredsize) < 0) | |
| 1470 goto onError; | |
| 1471 *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1472 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1473 *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
|
1474 *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
|
1475 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
|
1476 *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
|
1477 *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
|
1478 /* 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
|
1479 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
|
1480 |
| 51216 | 1481 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
|
1482 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
|
1483 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
|
1484 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1485 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1486 /* --- UTF-7 Codec -------------------------------------------------------- */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1487 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1488 /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1489 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1490 /* Three simple macros defining base-64. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1491 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1492 /* Is c a base-64 character? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1493 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1494 #define IS_BASE64(c) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1495 (isalnum(c) || (c) == '+' || (c) == '/') |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1496 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1497 /* given that c is a base-64 character, what is its base-64 value? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1498 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1499 #define FROM_BASE64(c) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1500 (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1501 ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1502 ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1503 (c) == '+' ? 62 : 63) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1504 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1505 /* What is the base-64 character of the bottom 6 bits of n? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1506 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1507 #define TO_BASE64(n) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1508 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1509 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1510 /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1511 * decoded as itself. We are permissive on decoding; the only ASCII |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1512 * byte not decoding to itself is the + which begins a base64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1513 * string. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1514 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1515 #define DECODE_DIRECT(c) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1516 ((c) <= 127 && (c) != '+') |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1517 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1518 /* The UTF-7 encoder treats ASCII characters differently according to |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1519 * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1520 * the above). See RFC2152. This array identifies these different |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1521 * sets: |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1522 * 0 : "Set D" |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1523 * alphanumeric and '(),-./:? |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1524 * 1 : "Set O" |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1525 * !"#$%&*;<=>@[]^_`{|} |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1526 * 2 : "whitespace" |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1527 * ht nl cr sp |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1528 * 3 : special (must be base64 encoded) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1529 * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1530 */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1531 |
|
30441
76e998aee644
On c.l.py, 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 static |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1533 char utf7_category[128] = { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1534 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1535 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1536 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1537 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1538 /* sp ! " # $ % & ' ( ) * + , - . / */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1539 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1540 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1542 /* @ A B C D E F G H I J K L M N O */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1543 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1544 /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1546 /* ` a b c d e f g h i j k l m n o */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1547 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1548 /* p q r s t u v w x y z { | } ~ del */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1549 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1550 }; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1551 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1552 /* ENCODE_DIRECT: this character should be encoded as itself. The |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1553 * answer depends on whether we are encoding set O as itself, and also |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1554 * on whether we are encoding whitespace as itself. RFC2152 makes it |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1555 * clear that the answers to these questions vary between |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1556 * applications, so this code needs to be flexible. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1557 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1558 #define ENCODE_DIRECT(c, directO, directWS) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1559 ((c) < 128 && (c) > 0 && \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1560 ((utf7_category[(c)] == 0) || \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1561 (directWS && (utf7_category[(c)] == 2)) || \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1562 (directO && (utf7_category[(c)] == 1)))) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1563 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1564 PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 51216 | 1565 Py_ssize_t size, |
| 1566 const char *errors) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1567 { |
|
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
|
1568 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
|
1569 } |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1570 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1571 /* The decoder. The only state we preserve is our read position, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1572 * i.e. how many characters we have consumed. So if we end in the |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1573 * middle of a shift sequence we have to back off the read position |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1574 * and the output to the beginning of the sequence, otherwise we lose |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1575 * all the shift state (seen bits, number of bits seen, high |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1576 * surrogate). */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1577 |
|
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
|
1578 PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 51216 | 1579 Py_ssize_t size, |
| 1580 const char *errors, | |
| 1581 Py_ssize_t *consumed) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1582 { |
|
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 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1584 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1585 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1586 Py_ssize_t outpos; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1587 const char *e; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1588 PyUnicodeObject *unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1589 Py_UNICODE *p; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1590 const char *errmsg = ""; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1591 int inShift = 0; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1592 Py_UNICODE *shiftOutStart; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1593 unsigned int base64bits = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1594 unsigned long base64buffer = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1595 Py_UNICODE surrogate = 0; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1596 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
|
1597 PyObject *exc = NULL; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1598 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1599 unicode = _PyUnicode_New(size); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1600 if (!unicode) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1601 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
|
1602 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
|
1603 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
|
1604 *consumed = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1605 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
|
1606 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1607 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1608 p = unicode->str; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1609 shiftOutStart = p; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1610 e = s + size; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1611 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1612 while (s < e) { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1613 Py_UNICODE ch = (unsigned char) *s; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1614 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1615 if (inShift) { /* in a base-64 section */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1616 if (IS_BASE64(ch)) { /* consume a base-64 character */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1617 base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1618 base64bits += 6; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1619 s++; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1620 if (base64bits >= 16) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1621 /* we have enough bits for a UTF-16 value */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1622 Py_UNICODE outCh = (Py_UNICODE) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1623 (base64buffer >> (base64bits-16)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1624 base64bits -= 16; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1625 base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1626 if (surrogate) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1627 /* expecting a second surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1628 if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1629 #ifdef Py_UNICODE_WIDE |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1630 *p++ = (((surrogate & 0x3FF)<<10) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1631 | (outCh & 0x3FF)) + 0x10000; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1632 #else |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1633 *p++ = surrogate; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1634 *p++ = outCh; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1635 #endif |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1636 surrogate = 0; |
|
73554
050772822bde
Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents:
71867
diff
changeset
|
1637 continue; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1638 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1639 else { |
|
73554
050772822bde
Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents:
71867
diff
changeset
|
1640 *p++ = surrogate; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1641 surrogate = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1642 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1643 } |
|
73554
050772822bde
Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents:
71867
diff
changeset
|
1644 if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1645 /* first surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1646 surrogate = outCh; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1647 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1648 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1649 *p++ = outCh; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1650 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1651 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1652 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1653 else { /* now leaving a base-64 section */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1654 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1655 s++; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1656 if (surrogate) { |
|
73554
050772822bde
Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents:
71867
diff
changeset
|
1657 *p++ = surrogate; |
|
050772822bde
Issue #13333: The UTF-7 decoder now accepts lone surrogates
Antoine Pitrou <solipsis@pitrou.net>
parents:
71867
diff
changeset
|
1658 surrogate = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1659 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1660 if (base64bits > 0) { /* left-over bits */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1661 if (base64bits >= 6) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1662 /* We've seen at least one base-64 character */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1663 errmsg = "partial character in shift sequence"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1664 goto utf7Error; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1665 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1666 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1667 /* Some bits remain; they should be zero */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1668 if (base64buffer != 0) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1669 errmsg = "non-zero padding bits in shift sequence"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1670 goto utf7Error; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1671 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1672 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1673 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1674 if (ch != '-') { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1675 /* '-' is absorbed; other terminating |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1676 characters are preserved */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1677 *p++ = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1678 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1679 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1680 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1681 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
|
1682 startinpos = s-starts; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1683 s++; /* consume '+' */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1684 if (s < e && *s == '-') { /* '+-' encodes '+' */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1685 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1686 *p++ = '+'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1687 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1688 else { /* begin base64-encoded section */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1689 inShift = 1; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1690 shiftOutStart = p; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1691 base64bits = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1692 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1693 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1694 else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1695 *p++ = ch; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1696 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1697 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1698 else { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1699 startinpos = s-starts; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1700 s++; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1701 errmsg = "unexpected special character"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1702 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1703 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1704 continue; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1705 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
|
1706 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
|
1707 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
|
1708 if (unicode_decode_call_errorhandler( |
| 51216 | 1709 errors, &errorHandler, |
| 1710 "utf7", errmsg, | |
| 1711 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 1712 &unicode, &outpos, &p)) | |
| 1713 goto onError; | |
|
19998
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 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1716 /* end of string */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1717 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1718 if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1719 /* if we're in an inconsistent state, that's an error */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1720 if (surrogate || |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1721 (base64bits >= 6) || |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1722 (base64bits > 0 && base64buffer != 0)) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1723 outpos = p-PyUnicode_AS_UNICODE(unicode); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1724 endinpos = size; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1725 if (unicode_decode_call_errorhandler( |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1726 errors, &errorHandler, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1727 "utf7", "unterminated shift sequence", |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1728 starts, size, &startinpos, &endinpos, &exc, &s, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1729 &unicode, &outpos, &p)) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1730 goto onError; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1731 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1732 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1733 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1734 /* return state */ |
|
43932
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1735 if (consumed) { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1736 if (inShift) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1737 p = shiftOutStart; /* back off output */ |
|
43932
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1738 *consumed = startinpos; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1739 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1740 else { |
|
43932
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1741 *consumed = s-starts; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1742 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1743 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1744 |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1745 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
|
1746 goto onError; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1747 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1748 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
|
1749 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1750 return (PyObject *)unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1751 |
| 51216 | 1752 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
|
1753 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
|
1754 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1755 Py_DECREF(unicode); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1756 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1757 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1758 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1759 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1760 PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 51216 | 1761 Py_ssize_t size, |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1762 int base64SetO, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1763 int base64WhiteSpace, |
| 51216 | 1764 const char *errors) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1765 { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1766 PyObject *v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1767 /* It might be possible to tighten this worst case */ |
|
55133
38d6e4e51af0
Grow the allocated buffer in PyUnicode_EncodeUTF7 to avoid buffer overrun.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54600
diff
changeset
|
1768 Py_ssize_t allocated = 8 * size; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1769 int inShift = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1770 Py_ssize_t i = 0; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1771 unsigned int base64bits = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1772 unsigned long base64buffer = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1773 char * out; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1774 char * start; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1775 |
|
55133
38d6e4e51af0
Grow the allocated buffer in PyUnicode_EncodeUTF7 to avoid buffer overrun.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54600
diff
changeset
|
1776 if (allocated / 8 != size) |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
1777 return PyErr_NoMemory(); |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
1778 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1779 if (size == 0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1780 return PyString_FromStringAndSize(NULL, 0); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1781 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1782 v = PyString_FromStringAndSize(NULL, allocated); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1783 if (v == NULL) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1784 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1785 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1786 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
|
1787 for (;i < size; ++i) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1788 Py_UNICODE ch = s[i]; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1789 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1790 if (inShift) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1791 if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1792 /* shifting out */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1793 if (base64bits) { /* output remaining bits */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1794 *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1795 base64buffer = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1796 base64bits = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1797 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1798 inShift = 0; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1799 /* Characters not in the BASE64 set implicitly unshift the sequence |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1800 so no '-' is required, except if the character is itself a '-' */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1801 if (IS_BASE64(ch) || ch == '-') { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1802 *out++ = '-'; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1803 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1804 *out++ = (char) ch; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1805 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1806 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1807 goto encode_char; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1808 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1809 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1810 else { /* not in a shift sequence */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1811 if (ch == '+') { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1812 *out++ = '+'; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1813 *out++ = '-'; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1814 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1815 else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1816 *out++ = (char) ch; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1817 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1818 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1819 *out++ = '+'; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1820 inShift = 1; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1821 goto encode_char; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1822 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1823 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1824 continue; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1825 encode_char: |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1826 #ifdef Py_UNICODE_WIDE |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1827 if (ch >= 0x10000) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1828 /* code first surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1829 base64bits += 16; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1830 base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1831 while (base64bits >= 6) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1832 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1833 base64bits -= 6; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1834 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1835 /* prepare second surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1836 ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1837 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1838 #endif |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1839 base64bits += 16; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1840 base64buffer = (base64buffer << 16) | ch; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1841 while (base64bits >= 6) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1842 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1843 base64bits -= 6; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1844 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1845 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1846 if (base64bits) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1847 *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1848 if (inShift) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1849 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1850 |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
1851 if (_PyString_Resize(&v, out - start)) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
1852 return NULL; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1853 return v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1854 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1855 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1856 #undef IS_BASE64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1857 #undef FROM_BASE64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1858 #undef TO_BASE64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1859 #undef DECODE_DIRECT |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1860 #undef ENCODE_DIRECT |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1861 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1862 /* --- UTF-8 Codec -------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1863 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1864 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1865 char utf8_code_length[256] = { |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1866 /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1867 illegal prefix. See RFC 3629 for details */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1868 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1869 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
79590
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
78792
diff
changeset
|
1870 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1871 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1872 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1873 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
|
1874 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1875 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1876 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1877 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
|
1878 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1879 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1880 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1881 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1882 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1883 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1884 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1885 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1886 PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 51216 | 1887 Py_ssize_t size, |
| 1888 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
|
1889 { |
|
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
|
1890 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
|
1891 } |
|
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
|
1892 |
|
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
|
1893 PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
| 51216 | 1894 Py_ssize_t size, |
| 1895 const char *errors, | |
| 1896 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
|
1897 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1898 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
|
1899 int n; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1900 int k; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1901 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1902 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1903 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
|
1904 const char *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1905 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1906 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
|
1907 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
|
1908 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
|
1909 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
|
1910 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1911 /* 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
|
1912 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1913 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
|
1914 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1915 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
|
1916 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
|
1917 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
|
1918 *consumed = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1919 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
|
1920 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1921 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1922 /* 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
|
1923 p = unicode->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1924 e = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1925 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1926 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
|
1927 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
|
1928 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1929 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
|
1930 *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
|
1931 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1932 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1933 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1934 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1935 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
|
1936 |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1937 if (s + n > e) { |
| 51216 | 1938 if (consumed) |
| 1939 break; | |
| 1940 else { | |
| 1941 errmsg = "unexpected end of data"; | |
| 1942 startinpos = s-starts; | |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1943 endinpos = startinpos+1; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1944 for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1945 endinpos++; |
| 51216 | 1946 goto utf8Error; |
| 1947 } | |
| 1948 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1949 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1950 switch (n) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1951 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1952 case 0: |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1953 errmsg = "invalid start byte"; |
| 51216 | 1954 startinpos = s-starts; |
| 1955 endinpos = startinpos+1; | |
| 1956 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1957 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1958 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
|
1959 errmsg = "internal error"; |
| 51216 | 1960 startinpos = s-starts; |
| 1961 endinpos = startinpos+1; | |
| 1962 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1963 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1964 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
|
1965 if ((s[1] & 0xc0) != 0x80) { |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1966 errmsg = "invalid continuation byte"; |
| 51216 | 1967 startinpos = s-starts; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1968 endinpos = startinpos + 1; |
| 51216 | 1969 goto utf8Error; |
| 1970 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1971 ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1972 assert ((ch > 0x007F) && (ch <= 0x07FF)); |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1973 *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
|
1974 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1975 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1976 case 3: |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1977 /* XXX: surrogates shouldn't be valid UTF-8! |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1978 see http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1979 (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1980 Uncomment the 2 lines below to make them invalid, |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1981 codepoints: d800-dfff; UTF-8: \xed\xa0\x80-\xed\xbf\xbf. */ |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1982 if ((s[1] & 0xc0) != 0x80 || |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1983 (s[2] & 0xc0) != 0x80 || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1984 ((unsigned char)s[0] == 0xE0 && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1985 (unsigned char)s[1] < 0xA0)/* || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1986 ((unsigned char)s[0] == 0xED && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1987 (unsigned char)s[1] > 0x9F)*/) { |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1988 errmsg = "invalid continuation byte"; |
| 51216 | 1989 startinpos = s-starts; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1990 endinpos = startinpos + 1; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1991 |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1992 /* if s[1] first two bits are 1 and 0, then the invalid |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1993 continuation byte is s[2], so increment endinpos by 1, |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1994 if not, s[1] is invalid and endinpos doesn't need to |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1995 be incremented. */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1996 if ((s[1] & 0xC0) == 0x80) |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1997 endinpos++; |
| 51216 | 1998 goto utf8Error; |
| 1999 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2000 ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2001 assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2002 *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
|
2003 break; |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2004 |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2005 case 4: |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2006 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
|
2007 (s[2] & 0xc0) != 0x80 || |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2008 (s[3] & 0xc0) != 0x80 || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2009 ((unsigned char)s[0] == 0xF0 && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2010 (unsigned char)s[1] < 0x90) || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2011 ((unsigned char)s[0] == 0xF4 && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2012 (unsigned char)s[1] > 0x8F)) { |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2013 errmsg = "invalid continuation byte"; |
| 51216 | 2014 startinpos = s-starts; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2015 endinpos = startinpos + 1; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2016 if ((s[1] & 0xC0) == 0x80) { |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2017 endinpos++; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2018 if ((s[2] & 0xC0) == 0x80) |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2019 endinpos++; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2020 } |
| 51216 | 2021 goto utf8Error; |
| 2022 } | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2023 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
|
2024 ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2025 assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2026 |
|
18324
3ba7a358ce02
use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents:
18298
diff
changeset
|
2027 #ifdef Py_UNICODE_WIDE |
| 51216 | 2028 *p++ = (Py_UNICODE)ch; |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2029 #else |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2030 /* 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
|
2031 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2032 /* 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
|
2033 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
|
2034 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2035 /* 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
|
2036 *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
|
2037 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2038 /* 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
|
2039 *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
|
2040 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2041 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2042 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2043 s += n; |
| 51216 | 2044 continue; |
| 2045 | |
| 2046 utf8Error: | |
| 2047 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2048 if (unicode_decode_call_errorhandler( | |
| 2049 errors, &errorHandler, | |
| 2050 "utf8", errmsg, | |
| 2051 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2052 &unicode, &outpos, &p)) | |
| 2053 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2054 } |
|
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
|
2055 if (consumed) |
| 51216 | 2056 *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
|
2057 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2058 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
2059 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
|
2060 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2061 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2062 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
|
2063 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
|
2064 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2065 |
| 51216 | 2066 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
|
2067 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
|
2068 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
|
2069 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2070 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2071 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2072 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2073 /* 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
|
2074 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
|
2075 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
|
2076 the excess memory at the end. |
| 23203 | 2077 */ |
|
23198
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
2078 PyObject * |
|
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
2079 PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 51216 | 2080 Py_ssize_t size, |
| 2081 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
|
2082 { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2083 #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
|
2084 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2085 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
|
2086 PyObject *v; /* result string object */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2087 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
|
2088 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
|
2089 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
|
2090 char stackbuf[MAX_SHORT_UNICHARS * 4]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2091 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2092 assert(s != NULL); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2093 assert(size >= 0); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2094 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2095 if (size <= MAX_SHORT_UNICHARS) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2096 /* 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
|
2097 * 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
|
2098 * turns out we need. |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2099 */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2100 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
|
2101 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
|
2102 p = stackbuf; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2103 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2104 else { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2105 /* 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
|
2106 nallocated = size * 4; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2107 if (nallocated / 4 != size) /* overflow! */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2108 return PyErr_NoMemory(); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2109 v = PyString_FromStringAndSize(NULL, nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2110 if (v == NULL) |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2111 return NULL; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2112 p = PyString_AS_STRING(v); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2113 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2114 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2115 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
|
2116 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
|
2117 |
| 23203 | 2118 if (ch < 0x80) |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2119 /* Encode ASCII */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2120 *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
|
2121 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2122 else if (ch < 0x0800) { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2123 /* Encode Latin-1 */ |
| 21884 | 2124 *p++ = (char)(0xc0 | (ch >> 6)); |
| 2125 *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
|
2126 } |
|
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
|
2127 else { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2128 /* Encode UCS2 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2129 if (ch < 0x10000) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2130 /* Special case: check for high surrogate */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2131 if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2132 Py_UCS4 ch2 = s[i]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2133 /* 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
|
2134 form a UCS4 value */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2135 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 23203 | 2136 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
|
2137 i++; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2138 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
|
2139 } |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2140 /* 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
|
2141 } |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2142 *p++ = (char)(0xe0 | (ch >> 12)); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2143 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2144 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2145 continue; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2146 } |
| 51216 | 2147 encodeUCS4: |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2148 /* Encode UCS4 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2149 *p++ = (char)(0xf0 | (ch >> 18)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2150 *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2151 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2152 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2153 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2154 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2155 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2156 if (v == NULL) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2157 /* 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
|
2158 nneeded = p - stackbuf; |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2159 assert(nneeded <= nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2160 v = PyString_FromStringAndSize(stackbuf, nneeded); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2161 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2162 else { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2163 /* 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
|
2164 nneeded = p - PyString_AS_STRING(v); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2165 assert(nneeded <= nallocated); |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
2166 if (_PyString_Resize(&v, nneeded)) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
2167 return NULL; |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2168 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2169 return v; |
| 23203 | 2170 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2171 #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
|
2172 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2173 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2174 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
|
2175 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2176 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
|
2177 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2178 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2179 } |
|
14059
d0348a26b093
PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents:
13966
diff
changeset
|
2180 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2181 PyUnicode_GET_SIZE(unicode), |
| 2182 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2183 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2184 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2185 /* --- 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
|
2186 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2187 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2188 PyUnicode_DecodeUTF32(const char *s, |
| 51216 | 2189 Py_ssize_t size, |
| 2190 const char *errors, | |
| 2191 int *byteorder) | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2192 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2193 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
|
2194 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2195 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2196 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2197 PyUnicode_DecodeUTF32Stateful(const char *s, |
| 51216 | 2198 Py_ssize_t size, |
| 2199 const char *errors, | |
| 2200 int *byteorder, | |
| 2201 Py_ssize_t *consumed) | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2202 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2203 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
|
2204 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
|
2205 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
|
2206 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
|
2207 PyUnicodeObject *unicode; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2208 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
|
2209 #ifndef Py_UNICODE_WIDE |
|
62257
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2210 int pairs = 0; |
|
62302
73e5354f92e7
fix warning with ucs4
Benjamin Peterson <benjamin@python.org>
parents:
62255
diff
changeset
|
2211 const unsigned char *qq; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2212 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2213 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
|
2214 #endif |
|
62302
73e5354f92e7
fix warning with ucs4
Benjamin Peterson <benjamin@python.org>
parents:
62255
diff
changeset
|
2215 const unsigned char *q, *e; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2216 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
|
2217 const char *errmsg = ""; |
|
42650
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2218 /* 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
|
2219 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2220 int iorder[] = {0, 1, 2, 3}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2221 #else |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2222 int iorder[] = {3, 2, 1, 0}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2223 #endif |
|
42652
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2224 PyObject *errorHandler = NULL; |
|
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2225 PyObject *exc = NULL; |
|
79590
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
78792
diff
changeset
|
2226 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2227 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
|
2228 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
|
2229 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2230 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2231 bo = *byteorder; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2232 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2233 /* 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
|
2234 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
|
2235 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
|
2236 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
|
2237 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
|
2238 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
|
2239 const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 51216 | 2240 (q[iorder[1]] << 8) | q[iorder[0]]; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2241 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 51216 | 2242 if (bom == 0x0000FEFF) { |
| 2243 q += 4; | |
| 2244 bo = -1; | |
| 2245 } | |
| 2246 else if (bom == 0xFFFE0000) { | |
| 2247 q += 4; | |
| 2248 bo = 1; | |
| 2249 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2250 #else |
| 51216 | 2251 if (bom == 0x0000FEFF) { |
| 2252 q += 4; | |
| 2253 bo = 1; | |
| 2254 } | |
| 2255 else if (bom == 0xFFFE0000) { | |
| 2256 q += 4; | |
| 2257 bo = -1; | |
| 2258 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2259 #endif |
| 51216 | 2260 } |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2261 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2262 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2263 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
|
2264 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2265 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
|
2266 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
|
2267 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
|
2268 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
|
2269 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2270 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
|
2271 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2272 iorder[0] = 3; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2273 iorder[1] = 2; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2274 iorder[2] = 1; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2275 iorder[3] = 0; |
|
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 |
|
62257
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2278 /* On narrow builds we split characters outside the BMP into two |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2279 codepoints => count how much extra space we need. */ |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2280 #ifndef Py_UNICODE_WIDE |
|
81326
3570e04f4ea9
Fix out of bound read in UTF-32 decoder on "narrow Unicode" builds.
Serhiy Storchaka <storchaka@gmail.com>
parents:
80478
diff
changeset
|
2281 for (qq = q; e - qq >= 4; qq += 4) |
|
62257
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2282 if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2283 pairs++; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2284 #endif |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2285 |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2286 /* This might be one to much, because of a BOM */ |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2287 unicode = _PyUnicode_New((size+3)/4+pairs); |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2288 if (!unicode) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2289 return NULL; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2290 if (size == 0) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2291 return (PyObject *)unicode; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2292 |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2293 /* Unpack UTF-32 encoded data */ |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2294 p = unicode->str; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2295 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2296 while (q < e) { |
| 51216 | 2297 Py_UCS4 ch; |
| 2298 /* remaining bytes at the end? (size should be divisible by 4) */ | |
| 2299 if (e-q<4) { | |
| 2300 if (consumed) | |
| 2301 break; | |
| 2302 errmsg = "truncated data"; | |
| 2303 startinpos = ((const char *)q)-starts; | |
| 2304 endinpos = ((const char *)e)-starts; | |
| 2305 goto utf32Error; | |
| 2306 /* The remaining input chars are ignored if the callback | |
| 2307 chooses to skip the input */ | |
| 2308 } | |
| 2309 ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | |
| 2310 (q[iorder[1]] << 8) | q[iorder[0]]; | |
| 2311 | |
| 2312 if (ch >= 0x110000) | |
| 2313 { | |
| 2314 errmsg = "codepoint not in range(0x110000)"; | |
| 2315 startinpos = ((const char *)q)-starts; | |
| 2316 endinpos = startinpos+4; | |
| 2317 goto utf32Error; | |
| 2318 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2319 #ifndef Py_UNICODE_WIDE |
| 51216 | 2320 if (ch >= 0x10000) |
| 2321 { | |
| 2322 *p++ = 0xD800 | ((ch-0x10000) >> 10); | |
| 2323 *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); | |
| 2324 } | |
| 2325 else | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2326 #endif |
| 51216 | 2327 *p++ = ch; |
| 2328 q += 4; | |
| 2329 continue; | |
| 2330 utf32Error: | |
| 2331 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2332 if (unicode_decode_call_errorhandler( | |
| 2333 errors, &errorHandler, | |
| 2334 "utf32", errmsg, | |
|
56034
0ff37e200292
Merged revisions 74869 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55751
diff
changeset
|
2335 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 51216 | 2336 &unicode, &outpos, &p)) |
| 2337 goto onError; | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2338 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2339 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2340 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2341 *byteorder = bo; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2342 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2343 if (consumed) |
| 51216 | 2344 *consumed = (const char *)q-starts; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2345 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2346 /* Adjust length */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2347 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
|
2348 goto onError; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2349 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2350 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
|
2351 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
|
2352 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
|
2353 |
| 51216 | 2354 onError: |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2355 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
|
2356 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
|
2357 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
|
2358 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2359 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2360 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2361 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2362 PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 51216 | 2363 Py_ssize_t size, |
| 2364 const char *errors, | |
| 2365 int byteorder) | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2366 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2367 PyObject *v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2368 unsigned char *p; |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2369 Py_ssize_t nsize, bytesize; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2370 #ifndef Py_UNICODE_WIDE |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2371 Py_ssize_t i, pairs; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2372 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2373 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
|
2374 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2375 /* 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
|
2376 #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
|
2377 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
|
2378 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2379 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
|
2380 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2381 |
| 51216 | 2382 #define STORECHAR(CH) \ |
| 2383 do { \ | |
| 2384 p[iorder[3]] = ((CH) >> 24) & 0xff; \ | |
| 2385 p[iorder[2]] = ((CH) >> 16) & 0xff; \ | |
| 2386 p[iorder[1]] = ((CH) >> 8) & 0xff; \ | |
| 2387 p[iorder[0]] = (CH) & 0xff; \ | |
| 2388 p += 4; \ | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2389 } while(0) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2390 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2391 /* 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
|
2392 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
|
2393 #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
|
2394 for (i = pairs = 0; i < size-1; i++) |
| 51216 | 2395 if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2396 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) | |
| 2397 pairs++; | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2398 #endif |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2399 nsize = (size - pairs + (byteorder == 0)); |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2400 bytesize = nsize * 4; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2401 if (bytesize / 4 != nsize) |
| 51216 | 2402 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2403 v = PyString_FromStringAndSize(NULL, bytesize); |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2404 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
|
2405 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2406 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
2407 p = (unsigned char *)PyString_AS_STRING(v); |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2408 if (byteorder == 0) |
| 51216 | 2409 STORECHAR(0xFEFF); |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2410 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
|
2411 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2412 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2413 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
|
2414 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2415 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
|
2416 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
|
2417 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
|
2418 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
|
2419 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2420 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
|
2421 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2422 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
|
2423 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
|
2424 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
|
2425 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
|
2426 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2427 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2428 while (size-- > 0) { |
| 51216 | 2429 Py_UCS4 ch = *s++; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2430 #ifndef Py_UNICODE_WIDE |
| 51216 | 2431 if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2432 Py_UCS4 ch2 = *s; | |
| 2433 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | |
| 2434 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | |
| 2435 s++; | |
| 2436 size--; | |
| 2437 } | |
| 2438 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2439 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2440 STORECHAR(ch); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2441 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2442 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2443 #undef STORECHAR |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2444 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2445 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2446 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
|
2447 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2448 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
|
2449 PyErr_BadArgument(); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2450 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2451 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2452 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2453 PyUnicode_GET_SIZE(unicode), |
| 2454 NULL, | |
| 2455 0); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2456 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2457 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2458 /* --- UTF-16 Codec ------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2459 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2460 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2461 PyUnicode_DecodeUTF16(const char *s, |
| 51216 | 2462 Py_ssize_t size, |
| 2463 const char *errors, | |
| 2464 int *byteorder) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2465 { |
|
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
|
2466 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
|
2467 } |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
2468 |
|
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
|
2469 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
|
2470 PyUnicode_DecodeUTF16Stateful(const char *s, |
| 51216 | 2471 Py_ssize_t size, |
| 2472 const char *errors, | |
| 2473 int *byteorder, | |
| 2474 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
|
2475 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2476 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2477 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2478 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2479 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
|
2480 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2481 Py_UNICODE *p; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2482 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
|
2483 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
|
2484 const char *errmsg = ""; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2485 /* 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
|
2486 #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
|
2487 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
|
2488 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2489 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
|
2490 #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
|
2491 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
|
2492 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
|
2493 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2494 /* 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
|
2495 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2496 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
|
2497 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2498 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2499 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2500 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2501 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2502 /* 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
|
2503 p = unicode->str; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2504 q = (unsigned char *)s; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2505 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
|
2506 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2507 if (byteorder) |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2508 bo = *byteorder; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2509 |
|
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
|
2510 /* 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
|
2511 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
|
2512 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
|
2513 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
|
2514 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
|
2515 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
|
2516 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
|
2517 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 51216 | 2518 if (bom == 0xFEFF) { |
| 2519 q += 2; | |
| 2520 bo = -1; | |
| 2521 } | |
| 2522 else if (bom == 0xFFFE) { | |
| 2523 q += 2; | |
| 2524 bo = 1; | |
| 2525 } | |
|
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
|
2526 #else |
| 51216 | 2527 if (bom == 0xFEFF) { |
| 2528 q += 2; | |
| 2529 bo = 1; | |
| 2530 } | |
| 2531 else if (bom == 0xFFFE) { | |
| 2532 q += 2; | |
| 2533 bo = -1; | |
| 2534 } | |
|
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
|
2535 #endif |
| 51216 | 2536 } |
|
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
|
2537 } |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2538 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2539 if (bo == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2540 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2541 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2542 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2543 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2544 else if (bo == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2545 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2546 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2547 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2548 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2549 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2550 while (q < e) { |
| 51216 | 2551 Py_UNICODE ch; |
| 2552 /* remaining bytes at the end? (size should be even) */ | |
| 2553 if (e-q<2) { | |
| 2554 if (consumed) | |
| 2555 break; | |
| 2556 errmsg = "truncated data"; | |
| 2557 startinpos = ((const char *)q)-starts; | |
| 2558 endinpos = ((const char *)e)-starts; | |
| 2559 goto utf16Error; | |
| 2560 /* The remaining input chars are ignored if the callback | |
| 2561 chooses to skip the input */ | |
| 2562 } | |
| 2563 ch = (q[ihi] << 8) | q[ilo]; | |
| 2564 | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2565 q += 2; |
| 51216 | 2566 |
| 2567 if (ch < 0xD800 || ch > 0xDFFF) { | |
| 2568 *p++ = ch; | |
| 2569 continue; | |
| 2570 } | |
| 2571 | |
| 2572 /* UTF-16 code pair: */ | |
|
78208
4cadf91aaddd
Issue #14579: Fix error handling bug in the utf-16 decoder.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77292
diff
changeset
|
2573 if (e - q < 2) { |
|
81328
f2353e74b335
Issue #11461: Fix the incremental UTF-16 decoder. Original patch by
Serhiy Storchaka <storchaka@gmail.com>
parents:
81326
diff
changeset
|
2574 q -= 2; |
|
f2353e74b335
Issue #11461: Fix the incremental UTF-16 decoder. Original patch by
Serhiy Storchaka <storchaka@gmail.com>
parents:
81326
diff
changeset
|
2575 if (consumed) |
|
f2353e74b335
Issue #11461: Fix the incremental UTF-16 decoder. Original patch by
Serhiy Storchaka <storchaka@gmail.com>
parents:
81326
diff
changeset
|
2576 break; |
| 51216 | 2577 errmsg = "unexpected end of data"; |
|
81328
f2353e74b335
Issue #11461: Fix the incremental UTF-16 decoder. Original patch by
Serhiy Storchaka <storchaka@gmail.com>
parents:
81326
diff
changeset
|
2578 startinpos = ((const char *)q)-starts; |
| 51216 | 2579 endinpos = ((const char *)e)-starts; |
| 2580 goto utf16Error; | |
| 2581 } | |
| 2582 if (0xD800 <= ch && ch <= 0xDBFF) { | |
| 2583 Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | |
| 2584 q += 2; | |
| 2585 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
|
2586 #ifndef Py_UNICODE_WIDE |
| 51216 | 2587 *p++ = ch; |
| 2588 *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
|
2589 #else |
| 51216 | 2590 *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
|
2591 #endif |
| 51216 | 2592 continue; |
| 2593 } | |
| 2594 else { | |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2595 errmsg = "illegal UTF-16 surrogate"; |
| 51216 | 2596 startinpos = (((const char *)q)-4)-starts; |
| 2597 endinpos = startinpos+2; | |
| 2598 goto utf16Error; | |
| 2599 } | |
| 2600 | |
| 2601 } | |
| 2602 errmsg = "illegal encoding"; | |
| 2603 startinpos = (((const char *)q)-2)-starts; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2604 endinpos = startinpos+2; |
| 51216 | 2605 /* Fall through to report the error */ |
| 2606 | |
| 2607 utf16Error: | |
| 2608 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2609 if (unicode_decode_call_errorhandler( | |
| 2610 errors, &errorHandler, | |
| 2611 "utf16", errmsg, | |
| 2612 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, | |
| 2613 &unicode, &outpos, &p)) | |
| 2614 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2615 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2616 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2617 if (byteorder) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2618 *byteorder = bo; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2619 |
|
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
|
2620 if (consumed) |
| 51216 | 2621 *consumed = (const char *)q-starts; |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
2622 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2623 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
2624 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
|
2625 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2626 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2627 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
|
2628 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
|
2629 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2630 |
| 51216 | 2631 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2632 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
|
2633 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
|
2634 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
|
2635 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2636 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2637 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2638 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2639 PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 51216 | 2640 Py_ssize_t size, |
| 2641 const char *errors, | |
| 2642 int byteorder) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2643 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2644 PyObject *v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2645 unsigned char *p; |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2646 Py_ssize_t nsize, bytesize; |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2647 #ifdef Py_UNICODE_WIDE |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2648 Py_ssize_t i, pairs; |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2649 #else |
|
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2650 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
|
2651 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2652 /* 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
|
2653 #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
|
2654 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
|
2655 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2656 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
|
2657 #endif |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2658 |
| 51216 | 2659 #define STORECHAR(CH) \ |
| 2660 do { \ | |
| 2661 p[ihi] = ((CH) >> 8) & 0xff; \ | |
| 2662 p[ilo] = (CH) & 0xff; \ | |
| 2663 p += 2; \ | |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2664 } 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
|
2665 |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2666 #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
|
2667 for (i = pairs = 0; i < size; i++) |
| 51216 | 2668 if (s[i] >= 0x10000) |
| 2669 pairs++; | |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2670 #endif |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2671 /* 2 * (size + pairs + (byteorder == 0)) */ |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2672 if (size > PY_SSIZE_T_MAX || |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2673 size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 51216 | 2674 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2675 nsize = size + pairs + (byteorder == 0); |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2676 bytesize = nsize * 2; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2677 if (bytesize / 2 != nsize) |
| 51216 | 2678 return PyErr_NoMemory(); |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2679 v = PyString_FromStringAndSize(NULL, bytesize); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2680 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2681 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2682 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2683 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
|
2684 if (byteorder == 0) |
| 51216 | 2685 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
|
2686 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
|
2687 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2688 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2689 if (byteorder == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2690 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2691 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2692 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2693 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2694 else if (byteorder == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2695 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2696 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2697 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2698 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2699 |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2700 while (size-- > 0) { |
| 51216 | 2701 Py_UNICODE ch = *s++; |
| 2702 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
|
2703 #ifdef Py_UNICODE_WIDE |
| 51216 | 2704 if (ch >= 0x10000) { |
| 2705 ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | |
| 2706 ch = 0xD800 | ((ch-0x10000) >> 10); | |
| 2707 } | |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2708 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2709 STORECHAR(ch); |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2710 if (ch2) |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2711 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
|
2712 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2713 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2714 #undef STORECHAR |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2715 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2716 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2717 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
|
2718 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2719 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
|
2720 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2721 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2722 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2723 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2724 PyUnicode_GET_SIZE(unicode), |
| 2725 NULL, | |
| 2726 0); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2727 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2728 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2729 /* --- Unicode Escape Codec ----------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2730 |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2731 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
|
2732 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2733 PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 51216 | 2734 Py_ssize_t size, |
| 2735 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
|
2736 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2737 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2738 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2739 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2740 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
|
2741 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
|
2742 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
|
2743 const char *end; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2744 char* message; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2745 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
|
2746 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
|
2747 PyObject *exc = NULL; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2748 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2749 /* 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
|
2750 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
|
2751 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
|
2752 (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
|
2753 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
|
2754 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
|
2755 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2756 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2757 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2758 return (PyObject *)v; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2759 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2760 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
|
2761 end = s + size; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2762 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2763 while (s < end) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2764 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
|
2765 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
|
2766 int digits; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2767 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2768 /* 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
|
2769 if (*s != '\\') { |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2770 *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
|
2771 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2772 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
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 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
|
2775 /* \ - Escapes */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2776 s++; |
|
43754
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2777 c = *s++; |
|
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2778 if (s > end) |
|
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2779 c = '\0'; /* Invalid after \ */ |
|
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2780 switch (c) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2781 |
| 51216 | 2782 /* \x escapes */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2783 case '\n': break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2784 case '\\': *p++ = '\\'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2785 case '\'': *p++ = '\''; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2786 case '\"': *p++ = '\"'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2787 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
|
2788 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
|
2789 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
|
2790 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
|
2791 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
|
2792 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
|
2793 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
|
2794 |
| 51216 | 2795 /* \OOO (octal) escapes */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2796 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
|
2797 case '4': case '5': case '6': case '7': |
| 12544 | 2798 x = s[-1] - '0'; |
|
43754
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2799 if (s < end && '0' <= *s && *s <= '7') { |
| 12544 | 2800 x = (x<<3) + *s++ - '0'; |
|
43754
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2801 if (s < end && '0' <= *s && *s <= '7') |
| 12544 | 2802 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
|
2803 } |
| 12544 | 2804 *p++ = x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2805 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2806 |
| 51216 | 2807 /* hex escapes */ |
| 2808 /* \xXX */ | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2809 case 'x': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2810 digits = 2; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2811 message = "truncated \\xXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2812 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2813 |
| 51216 | 2814 /* \uXXXX */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2815 case 'u': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2816 digits = 4; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2817 message = "truncated \\uXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2818 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2819 |
| 51216 | 2820 /* \UXXXXXXXX */ |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2821 case 'U': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2822 digits = 8; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2823 message = "truncated \\UXXXXXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2824 hexescape: |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2825 chr = 0; |
|
81803
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2826 if (end - s < digits) { |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2827 /* count only hex digits */ |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2828 for (; s < end; ++s) { |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2829 c = (unsigned char)*s; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2830 if (!Py_ISXDIGIT(c)) |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2831 goto error; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2832 } |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2833 goto 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
|
2834 } |
|
81803
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2835 for (; digits--; ++s) { |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2836 c = (unsigned char)*s; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2837 if (!Py_ISXDIGIT(c)) |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2838 goto error; |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2839 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
|
2840 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
|
2841 chr += c - '0'; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2842 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
|
2843 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
|
2844 else |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2845 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
|
2846 } |
|
30653
3b47d6057151
Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30441
diff
changeset
|
2847 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
|
2848 /* _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
|
2849 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
|
2850 break; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2851 store: |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2852 /* 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
|
2853 if (chr <= 0xffff) |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2854 /* UCS-2 character */ |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2855 *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
|
2856 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
|
2857 /* UCS-4 character. Either store directly, or as |
| 22521 | 2858 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
|
2859 #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
|
2860 *p++ = chr; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2861 #else |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2862 chr -= 0x10000L; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2863 *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
|
2864 *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
|
2865 #endif |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2866 } else { |
|
81803
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2867 message = "illegal Unicode character"; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2868 goto error; |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2869 } |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2870 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2871 |
| 51216 | 2872 /* \N{name} */ |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2873 case 'N': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2874 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
|
2875 if (ucnhash_CAPI == NULL) { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2876 /* load the unicode data module */ |
|
59998
d11fbc796499
Backported PyCapsule from 3.1, and converted most uses of
Larry Hastings <larry@hastings.org>
parents:
59895
diff
changeset
|
2877 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2878 if (ucnhash_CAPI == NULL) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2879 goto ucnhashError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2880 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2881 if (*s == '{') { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2882 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
|
2883 /* 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
|
2884 while (*s != '}' && s < end) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2885 s++; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2886 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
|
2887 /* 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
|
2888 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
|
2889 s++; |
|
81625
f4d30d1a529e
Issue #16335: Fix integer overflow in unicode-escape decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81590
diff
changeset
|
2890 if (s - start - 1 <= INT_MAX && |
|
f4d30d1a529e
Issue #16335: Fix integer overflow in unicode-escape decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81590
diff
changeset
|
2891 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
|
2892 goto store; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2893 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2894 } |
|
81803
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2895 goto error; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2896 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2897 default: |
| 22521 | 2898 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
|
2899 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
|
2900 s--; |
|
81803
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2901 goto error; |
| 22521 | 2902 } |
| 2903 else { | |
| 2904 *p++ = '\\'; | |
| 2905 *p++ = (unsigned char)s[-1]; | |
| 2906 } | |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2907 break; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2908 } |
|
81803
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2909 continue; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2910 |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2911 error: |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2912 endinpos = s-starts; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2913 outpos = p-PyUnicode_AS_UNICODE(v); |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2914 if (unicode_decode_call_errorhandler( |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2915 errors, &errorHandler, |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2916 "unicodeescape", message, |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2917 starts, size, &startinpos, &endinpos, &exc, &s, |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2918 &v, &outpos, &p)) |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2919 goto onError; |
|
a242ac99161f
Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81748
diff
changeset
|
2920 continue; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2921 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
2922 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
|
2923 goto onError; |
|
30160
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2924 Py_XDECREF(errorHandler); |
|
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2925 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
|
2926 return (PyObject *)v; |
| 22521 | 2927 |
| 51216 | 2928 ucnhashError: |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2929 PyErr_SetString( |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2930 PyExc_UnicodeError, |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2931 "\\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
|
2932 ); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2933 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
|
2934 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
|
2935 Py_XDECREF(exc); |
|
16064
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2936 return NULL; |
|
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2937 |
| 51216 | 2938 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2939 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
|
2940 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
|
2941 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
|
2942 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2943 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2944 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2945 /* 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
|
2946 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2947 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
|
2948 appropriate. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2949 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2950 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2951 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
2952 Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 51216 | 2953 Py_ssize_t size, |
| 2954 Py_UNICODE ch) | |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2955 { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2956 /* 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
|
2957 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2958 while (size-- > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2959 if (*s == ch) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2960 return s; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2961 s++; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2962 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2963 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2964 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2965 } |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
2966 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2967 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2968 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
|
2969 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
|
2970 int quotes) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2971 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2972 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2973 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
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 static const char *hexdigit = "0123456789abcdef"; |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2976 #ifdef Py_UNICODE_WIDE |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2977 const Py_ssize_t expandsize = 10; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2978 #else |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2979 const Py_ssize_t expandsize = 6; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2980 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2981 |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2982 /* 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
|
2983 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
|
2984 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
|
2985 */ |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2986 /* Initial allocation is based on the longest-possible unichr |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2987 escape. |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2988 |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2989 In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2990 unichr, so in this case it's the longest unichr escape. In |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2991 narrow (UTF-16) builds this is five chars per source unichr |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2992 since there are two unichrs in the surrogate pair, so in narrow |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2993 (UTF-16) builds it's not the longest unichr escape. |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2994 |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2995 In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2996 so in the narrow (UTF-16) build case it's the longest unichr |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2997 escape. |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2998 */ |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2999 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3000 if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 51216 | 3001 return PyErr_NoMemory(); |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3002 |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3003 repr = PyString_FromStringAndSize(NULL, |
| 51216 | 3004 2 |
| 3005 + expandsize*size | |
| 3006 + 1); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3007 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3008 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3009 |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3010 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
|
3011 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3012 if (quotes) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3013 *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
|
3014 *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
|
3015 !findchar(s, size, '"')) ? '"' : '\''; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3016 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3017 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3018 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
|
3019 |
|
36056
f1ae11457d34
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents:
35999
diff
changeset
|
3020 /* 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
|
3021 if ((quotes && |
| 51216 | 3022 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
|
3023 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3024 *p++ = (char) ch; |
| 51216 | 3025 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
|
3026 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3027 |
|
18773
743f8e1d953c
#ifdef out generation of \U escapes unless Py_UNICODE_WIDE. This
Guido van Rossum <guido@python.org>
parents:
18324
diff
changeset
|
3028 #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
|
3029 /* 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
|
3030 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
|
3031 *p++ = '\\'; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3032 *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
|
3033 *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
|
3034 *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
|
3035 *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
|
3036 *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
|
3037 *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
|
3038 *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
|
3039 *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
|
3040 *p++ = hexdigit[ch & 0x0000000F]; |
| 51216 | 3041 continue; |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3042 } |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3043 #else |
| 51216 | 3044 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3045 else if (ch >= 0xD800 && ch < 0xDC00) { | |
| 3046 Py_UNICODE ch2; | |
| 3047 Py_UCS4 ucs; | |
| 3048 | |
| 3049 ch2 = *s++; | |
| 3050 size--; | |
| 3051 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | |
| 3052 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | |
| 3053 *p++ = '\\'; | |
| 3054 *p++ = 'U'; | |
| 3055 *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; | |
| 3056 *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; | |
| 3057 *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; | |
| 3058 *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; | |
| 3059 *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; | |
| 3060 *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; | |
| 3061 *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; | |
| 3062 *p++ = hexdigit[ucs & 0x0000000F]; | |
| 3063 continue; | |
| 3064 } | |
| 3065 /* Fall through: isolated surrogates are copied as-is */ | |
| 3066 s--; | |
| 3067 size++; | |
| 3068 } | |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3069 #endif |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3070 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3071 /* 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
|
3072 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
|
3073 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3074 *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
|
3075 *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
|
3076 *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
|
3077 *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
|
3078 *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
|
3079 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3080 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3081 /* 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
|
3082 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
|
3083 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3084 *p++ = 't'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3085 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3086 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
|
3087 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3088 *p++ = 'n'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3089 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3090 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
|
3091 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3092 *p++ = 'r'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3093 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3094 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3095 /* 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
|
3096 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
|
3097 *p++ = '\\'; |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3098 *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
|
3099 *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
|
3100 *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
|
3101 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3102 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3103 /* 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
|
3104 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3105 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3106 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3107 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
|
3108 *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
|
3109 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3110 *p = '\0'; |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3111 if (_PyString_Resize(&repr, p - PyString_AS_STRING(repr))) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3112 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3113 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3114 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3115 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3116 PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 51216 | 3117 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
|
3118 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3119 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
|
3120 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3121 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3122 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
|
3123 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3124 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
|
3125 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3126 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3127 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3128 return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3129 PyUnicode_GET_SIZE(unicode)); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3130 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3131 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3132 /* --- 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
|
3133 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3134 PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 51216 | 3135 Py_ssize_t size, |
| 3136 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
|
3137 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3138 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3139 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3140 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3141 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
|
3142 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
|
3143 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
|
3144 const char *end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3145 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
|
3146 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
|
3147 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
|
3148 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3149 /* 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
|
3150 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
|
3151 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
|
3152 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
|
3153 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
|
3154 if (v == NULL) |
| 51216 | 3155 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3156 if (size == 0) |
| 51216 | 3157 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
|
3158 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
|
3159 end = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3160 while (s < end) { |
| 51216 | 3161 unsigned char c; |
| 3162 Py_UCS4 x; | |
| 3163 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
|
3164 int count; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3165 |
| 51216 | 3166 /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3167 if (*s != '\\') { | |
| 3168 *p++ = (unsigned char)*s++; | |
| 3169 continue; | |
| 3170 } | |
| 3171 startinpos = s-starts; | |
| 3172 | |
| 3173 /* \u-escapes are only interpreted iff the number of leading | |
| 3174 backslashes if odd */ | |
| 3175 bs = s; | |
| 3176 for (;s < end;) { | |
| 3177 if (*s != '\\') | |
| 3178 break; | |
| 3179 *p++ = (unsigned char)*s++; | |
| 3180 } | |
| 3181 if (((s - bs) & 1) == 0 || | |
| 3182 s >= end || | |
| 3183 (*s != 'u' && *s != 'U')) { | |
| 3184 continue; | |
| 3185 } | |
| 3186 p--; | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3187 count = *s=='u' ? 4 : 8; |
| 51216 | 3188 s++; |
| 3189 | |
| 3190 /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | |
| 3191 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 3192 for (x = 0, i = 0; i < count; ++i, ++s) { | |
| 3193 c = (unsigned char)*s; | |
| 3194 if (!isxdigit(c)) { | |
| 3195 endinpos = s-starts; | |
| 3196 if (unicode_decode_call_errorhandler( | |
| 3197 errors, &errorHandler, | |
| 3198 "rawunicodeescape", "truncated \\uXXXX", | |
| 3199 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3200 &v, &outpos, &p)) | |
| 3201 goto onError; | |
| 3202 goto nextByte; | |
| 3203 } | |
| 3204 x = (x<<4) & ~0xF; | |
| 3205 if (c >= '0' && c <= '9') | |
| 3206 x += c - '0'; | |
| 3207 else if (c >= 'a' && c <= 'f') | |
| 3208 x += 10 + c - 'a'; | |
| 3209 else | |
| 3210 x += 10 + c - 'A'; | |
| 3211 } | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3212 if (x <= 0xffff) |
| 51216 | 3213 /* UCS-2 character */ |
| 3214 *p++ = (Py_UNICODE) x; | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3215 else if (x <= 0x10ffff) { |
| 51216 | 3216 /* UCS-4 character. Either store directly, or as |
| 3217 surrogate pair. */ | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3218 #ifdef Py_UNICODE_WIDE |
| 51216 | 3219 *p++ = (Py_UNICODE) x; |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3220 #else |
| 51216 | 3221 x -= 0x10000L; |
| 3222 *p++ = 0xD800 + (Py_UNICODE) (x >> 10); | |
| 3223 *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3224 #endif |
|
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3225 } else { |
|
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3226 endinpos = s-starts; |
|
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3227 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
|
3228 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
|
3229 errors, &errorHandler, |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3230 "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 51216 | 3231 starts, size, &startinpos, &endinpos, &exc, &s, |
| 3232 &v, &outpos, &p)) | |
| 3233 goto onError; | |
| 3234 } | |
| 3235 nextByte: | |
| 3236 ; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3237 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3238 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 51216 | 3239 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
|
3240 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
|
3241 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
|
3242 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
|
3243 |
| 51216 | 3244 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3245 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
|
3246 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
|
3247 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
|
3248 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3249 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3250 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3251 PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 51216 | 3252 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
|
3253 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3254 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3255 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3256 char *q; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3257 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3258 static const char *hexdigit = "0123456789abcdef"; |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3259 #ifdef Py_UNICODE_WIDE |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3260 const Py_ssize_t expandsize = 10; |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3261 #else |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3262 const Py_ssize_t expandsize = 6; |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3263 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3264 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3265 if (size > PY_SSIZE_T_MAX / expandsize) |
| 51216 | 3266 return PyErr_NoMemory(); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3267 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3268 repr = PyString_FromStringAndSize(NULL, expandsize * size); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3269 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3270 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
|
3271 if (size == 0) |
| 51216 | 3272 return repr; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3273 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3274 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
|
3275 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3276 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
|
3277 #ifdef Py_UNICODE_WIDE |
| 51216 | 3278 /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3279 if (ch >= 0x10000) { | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3280 *p++ = '\\'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3281 *p++ = 'U'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3282 *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
|
3283 *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
|
3284 *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
|
3285 *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
|
3286 *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
|
3287 *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
|
3288 *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
|
3289 *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
|
3290 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3291 else |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3292 #else |
| 51216 | 3293 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3294 if (ch >= 0xD800 && ch < 0xDC00) { | |
| 3295 Py_UNICODE ch2; | |
| 3296 Py_UCS4 ucs; | |
| 3297 | |
| 3298 ch2 = *s++; | |
| 3299 size--; | |
| 3300 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | |
| 3301 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | |
| 3302 *p++ = '\\'; | |
| 3303 *p++ = 'U'; | |
| 3304 *p++ = hexdigit[(ucs >> 28) & 0xf]; | |
| 3305 *p++ = hexdigit[(ucs >> 24) & 0xf]; | |
| 3306 *p++ = hexdigit[(ucs >> 20) & 0xf]; | |
| 3307 *p++ = hexdigit[(ucs >> 16) & 0xf]; | |
| 3308 *p++ = hexdigit[(ucs >> 12) & 0xf]; | |
| 3309 *p++ = hexdigit[(ucs >> 8) & 0xf]; | |
| 3310 *p++ = hexdigit[(ucs >> 4) & 0xf]; | |
| 3311 *p++ = hexdigit[ucs & 0xf]; | |
| 3312 continue; | |
| 3313 } | |
| 3314 /* Fall through: isolated surrogates are copied as-is */ | |
| 3315 s--; | |
| 3316 size++; | |
| 3317 } | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3318 #endif |
| 51216 | 3319 /* Map 16-bit characters to '\uxxxx' */ |
| 3320 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
|
3321 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3322 *p++ = 'u'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3323 *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
|
3324 *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
|
3325 *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
|
3326 *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
|
3327 } |
| 51216 | 3328 /* Copy everything else as-is */ |
| 3329 else | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3330 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3331 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3332 *p = '\0'; |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3333 if (_PyString_Resize(&repr, p - q)) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3334 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3335 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3336 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3337 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3338 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
|
3339 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3340 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3341 PyErr_BadArgument(); |
| 3342 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3343 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3344 return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3345 PyUnicode_GET_SIZE(unicode)); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3346 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3347 |
| 35485 | 3348 /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3349 | |
| 3350 PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, | |
| 51216 | 3351 Py_ssize_t size, |
| 3352 const char *errors) | |
| 35485 | 3353 { |
| 3354 const char *starts = s; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3355 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3356 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3357 Py_ssize_t outpos; |
| 35485 | 3358 PyUnicodeObject *v; |
| 3359 Py_UNICODE *p; | |
| 3360 const char *end; | |
| 3361 const char *reason; | |
| 3362 PyObject *errorHandler = NULL; | |
| 3363 PyObject *exc = NULL; | |
| 3364 | |
|
36236
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3365 #ifdef Py_UNICODE_WIDE |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3366 Py_UNICODE unimax = PyUnicode_GetMax(); |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3367 #endif |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3368 |
|
39994
798935e1295e
Forward-port of r52136: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39743
diff
changeset
|
3369 /* XXX overflow detection missing */ |
| 35485 | 3370 v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3371 if (v == NULL) | |
| 51216 | 3372 goto onError; |
| 35485 | 3373 if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 51216 | 3374 return (PyObject *)v; |
| 35485 | 3375 p = PyUnicode_AS_UNICODE(v); |
| 3376 end = s + size; | |
| 3377 | |
| 3378 while (s < end) { | |
|
82050
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3379 if (end-s < Py_UNICODE_SIZE) { |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3380 endinpos = end-starts; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3381 reason = "truncated input"; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3382 goto error; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3383 } |
|
38182
d8355cccff4a
Patch #1488312, Fix memory alignment problem on SPARC in unicode. Will backport
Neal Norwitz <nnorwitz@gmail.com>
parents:
37775
diff
changeset
|
3384 memcpy(p, s, sizeof(Py_UNICODE)); |
|
82050
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3385 #ifdef Py_UNICODE_WIDE |
| 35485 | 3386 /* We have to sanity check the raw data, otherwise doom looms for |
| 3387 some malformed UCS-4 data. */ | |
|
82050
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3388 if (*p > unimax || *p < 0) { |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3389 endinpos = s - starts + Py_UNICODE_SIZE; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3390 reason = "illegal code point (> 0x10FFFF)"; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3391 goto error; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3392 } |
| 51216 | 3393 #endif |
|
82050
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3394 p++; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3395 s += Py_UNICODE_SIZE; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3396 continue; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3397 |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3398 error: |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3399 startinpos = s - starts; |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3400 outpos = p - PyUnicode_AS_UNICODE(v); |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3401 if (unicode_decode_call_errorhandler( |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3402 errors, &errorHandler, |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3403 "unicode_internal", reason, |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3404 starts, size, &startinpos, &endinpos, &exc, &s, |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3405 &v, &outpos, &p)) { |
|
498b54e0e856
Issue #17043: The unicode-internal decoder no longer read past the end of
Serhiy Storchaka <storchaka@gmail.com>
parents:
81803
diff
changeset
|
3406 goto onError; |
| 35485 | 3407 } |
| 3408 } | |
| 3409 | |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3410 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 35485 | 3411 goto onError; |
| 3412 Py_XDECREF(errorHandler); | |
| 3413 Py_XDECREF(exc); | |
| 3414 return (PyObject *)v; | |
| 3415 | |
| 51216 | 3416 onError: |
| 35485 | 3417 Py_XDECREF(v); |
| 3418 Py_XDECREF(errorHandler); | |
| 3419 Py_XDECREF(exc); | |
| 3420 return NULL; | |
| 3421 } | |
| 3422 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3423 /* --- Latin-1 Codec ------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3424 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3425 PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 51216 | 3426 Py_ssize_t size, |
| 3427 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
|
3428 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3429 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3430 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
|
3431 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3432 /* 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
|
3433 if (size == 1) { |
| 51216 | 3434 Py_UNICODE r = *(unsigned char*)s; |
| 3435 return PyUnicode_FromUnicode(&r, 1); | |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3436 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3437 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3438 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
|
3439 if (v == NULL) |
| 51216 | 3440 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3441 if (size == 0) |
| 51216 | 3442 return (PyObject *)v; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3443 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
|
3444 while (size-- > 0) |
| 51216 | 3445 *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
|
3446 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
|
3447 |
| 51216 | 3448 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3449 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3450 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3451 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3452 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3453 /* 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
|
3454 static void make_encode_exception(PyObject **exceptionObject, |
| 51216 | 3455 const char *encoding, |
| 3456 const Py_UNICODE *unicode, Py_ssize_t size, | |
| 3457 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3458 const char *reason) | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3459 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3460 if (*exceptionObject == NULL) { |
| 51216 | 3461 *exceptionObject = PyUnicodeEncodeError_Create( |
| 3462 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
|
3463 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3464 else { |
| 51216 | 3465 if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3466 goto onError; | |
| 3467 if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | |
| 3468 goto onError; | |
| 3469 if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | |
| 3470 goto onError; | |
| 3471 return; | |
| 3472 onError: | |
| 3473 Py_DECREF(*exceptionObject); | |
| 3474 *exceptionObject = NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3475 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3476 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3477 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3478 /* 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
|
3479 static void raise_encode_exception(PyObject **exceptionObject, |
| 51216 | 3480 const char *encoding, |
| 3481 const Py_UNICODE *unicode, Py_ssize_t size, | |
| 3482 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3483 const char *reason) | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3484 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3485 make_encode_exception(exceptionObject, |
| 51216 | 3486 encoding, unicode, size, startpos, endpos, reason); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3487 if (*exceptionObject != NULL) |
| 51216 | 3488 PyCodec_StrictErrors(*exceptionObject); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3489 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3490 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3491 /* 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
|
3492 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
|
3493 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
|
3494 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
|
3495 static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 51216 | 3496 PyObject **errorHandler, |
| 3497 const char *encoding, const char *reason, | |
| 3498 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | |
| 3499 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3500 Py_ssize_t *newpos) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3501 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3502 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
|
3503 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3504 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
|
3505 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
|
3506 |
|
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 (*errorHandler == NULL) { |
| 51216 | 3508 *errorHandler = PyCodec_LookupError(errors); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3509 if (*errorHandler == NULL) |
| 51216 | 3510 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3511 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3512 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3513 make_encode_exception(exceptionObject, |
| 51216 | 3514 encoding, unicode, size, startpos, endpos, reason); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3515 if (*exceptionObject == NULL) |
| 51216 | 3516 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3517 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3518 restuple = PyObject_CallFunctionObjArgs( |
| 51216 | 3519 *errorHandler, *exceptionObject, NULL); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3520 if (restuple == NULL) |
| 51216 | 3521 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3522 if (!PyTuple_Check(restuple)) { |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
52411
diff
changeset
|
3523 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 3524 Py_DECREF(restuple); |
| 3525 return NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3526 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3527 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 51216 | 3528 &resunicode, newpos)) { |
| 3529 Py_DECREF(restuple); | |
| 3530 return NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3531 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3532 if (*newpos<0) |
| 51216 | 3533 *newpos = size+*newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3534 if (*newpos<0 || *newpos>size) { |
| 51216 | 3535 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3536 Py_DECREF(restuple); | |
| 3537 return NULL; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3538 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3539 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
|
3540 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
|
3541 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
|
3542 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3543 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3544 static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 51216 | 3545 Py_ssize_t size, |
| 3546 const char *errors, | |
| 3547 int limit) | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3548 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3549 /* 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
|
3550 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
|
3551 /* 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
|
3552 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
|
3553 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
|
3554 /* 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
|
3555 /* 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
|
3556 /* 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
|
3557 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
|
3558 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3559 Py_ssize_t respos = 0; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3560 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
|
3561 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
|
3562 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
|
3563 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
|
3564 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
|
3565 /* 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
|
3566 * -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
|
3567 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
|
3568 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3569 /* 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
|
3570 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
|
3571 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
|
3572 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
|
3573 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
|
3574 if (size == 0) |
| 51216 | 3575 return res; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3576 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
|
3577 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
|
3578 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3579 while (p<endp) { |
| 51216 | 3580 Py_UNICODE c = *p; |
| 3581 | |
| 3582 /* can we encode this? */ | |
| 3583 if (c<limit) { | |
| 3584 /* no overflow check, because we know that the space is enough */ | |
| 3585 *str++ = (char)c; | |
| 3586 ++p; | |
| 3587 } | |
| 3588 else { | |
| 3589 Py_ssize_t unicodepos = p-startp; | |
| 3590 Py_ssize_t requiredsize; | |
| 3591 PyObject *repunicode; | |
| 3592 Py_ssize_t repsize; | |
| 3593 Py_ssize_t newpos; | |
| 3594 Py_ssize_t respos; | |
| 3595 Py_UNICODE *uni2; | |
| 3596 /* startpos for collecting unencodable chars */ | |
| 3597 const Py_UNICODE *collstart = p; | |
| 3598 const Py_UNICODE *collend = p; | |
| 3599 /* find all unecodable characters */ | |
| 3600 while ((collend < endp) && ((*collend)>=limit)) | |
| 3601 ++collend; | |
| 3602 /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | |
| 3603 if (known_errorHandler==-1) { | |
| 3604 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 3605 known_errorHandler = 1; | |
| 3606 else if (!strcmp(errors, "replace")) | |
| 3607 known_errorHandler = 2; | |
| 3608 else if (!strcmp(errors, "ignore")) | |
| 3609 known_errorHandler = 3; | |
| 3610 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 3611 known_errorHandler = 4; | |
| 3612 else | |
| 3613 known_errorHandler = 0; | |
| 3614 } | |
| 3615 switch (known_errorHandler) { | |
| 3616 case 1: /* strict */ | |
| 3617 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | |
| 3618 goto onError; | |
| 3619 case 2: /* replace */ | |
| 3620 while (collstart++<collend) | |
| 3621 *str++ = '?'; /* fall through */ | |
| 3622 case 3: /* ignore */ | |
| 3623 p = collend; | |
| 3624 break; | |
| 3625 case 4: /* xmlcharrefreplace */ | |
| 3626 respos = str-PyString_AS_STRING(res); | |
| 3627 /* determine replacement size (temporarily (mis)uses p) */ | |
| 3628 for (p = collstart, repsize = 0; p < collend; ++p) { | |
| 3629 if (*p<10) | |
| 3630 repsize += 2+1+1; | |
| 3631 else if (*p<100) | |
| 3632 repsize += 2+2+1; | |
| 3633 else if (*p<1000) | |
| 3634 repsize += 2+3+1; | |
| 3635 else if (*p<10000) | |
| 3636 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
|
3637 #ifndef Py_UNICODE_WIDE |
| 51216 | 3638 else |
| 3639 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
|
3640 #else |
| 51216 | 3641 else if (*p<100000) |
| 3642 repsize += 2+5+1; | |
| 3643 else if (*p<1000000) | |
| 3644 repsize += 2+6+1; | |
| 3645 else | |
| 3646 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
|
3647 #endif |
| 51216 | 3648 } |
| 3649 requiredsize = respos+repsize+(endp-collend); | |
| 3650 if (requiredsize > ressize) { | |
| 3651 if (requiredsize<2*ressize) | |
| 3652 requiredsize = 2*ressize; | |
| 3653 if (_PyString_Resize(&res, requiredsize)) | |
| 3654 goto onError; | |
| 3655 str = PyString_AS_STRING(res) + respos; | |
| 3656 ressize = requiredsize; | |
| 3657 } | |
| 3658 /* generate replacement (temporarily (mis)uses p) */ | |
| 3659 for (p = collstart; p < collend; ++p) { | |
| 3660 str += sprintf(str, "&#%d;", (int)*p); | |
| 3661 } | |
| 3662 p = collend; | |
| 3663 break; | |
| 3664 default: | |
| 3665 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | |
| 3666 encoding, reason, startp, size, &exc, | |
| 3667 collstart-startp, collend-startp, &newpos); | |
| 3668 if (repunicode == NULL) | |
| 3669 goto onError; | |
|
51213
cd5db43dbb65
fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents:
51212
diff
changeset
|
3670 /* need more space? (at least enough for what we have+the |
|
cd5db43dbb65
fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents:
51212
diff
changeset
|
3671 replacement+the rest of the string, so we won't have to |
|
cd5db43dbb65
fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents:
51212
diff
changeset
|
3672 check space for encodable characters) */ |
| 51216 | 3673 respos = str-PyString_AS_STRING(res); |
| 3674 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 3675 requiredsize = respos+repsize+(endp-collend); | |
| 3676 if (requiredsize > ressize) { | |
| 3677 if (requiredsize<2*ressize) | |
| 3678 requiredsize = 2*ressize; | |
| 3679 if (_PyString_Resize(&res, requiredsize)) { | |
| 3680 Py_DECREF(repunicode); | |
| 3681 goto onError; | |
| 3682 } | |
| 3683 str = PyString_AS_STRING(res) + respos; | |
| 3684 ressize = requiredsize; | |
| 3685 } | |
| 3686 /* check if there is anything unencodable in the replacement | |
| 3687 and copy it to the output */ | |
| 3688 for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { | |
| 3689 c = *uni2; | |
| 3690 if (c >= limit) { | |
| 3691 raise_encode_exception(&exc, encoding, startp, size, | |
| 3692 unicodepos, unicodepos+1, reason); | |
| 3693 Py_DECREF(repunicode); | |
| 3694 goto onError; | |
| 3695 } | |
| 3696 *str = (char)c; | |
| 3697 } | |
| 3698 p = startp + newpos; | |
| 3699 Py_DECREF(repunicode); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3700 } |
| 51216 | 3701 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3702 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3703 /* 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
|
3704 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
|
3705 if (respos<ressize) |
| 51216 | 3706 /* If this falls res will be NULL */ |
| 3707 _PyString_Resize(&res, respos); | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3708 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
|
3709 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
|
3710 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
|
3711 |
| 51216 | 3712 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
|
3713 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
|
3714 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
|
3715 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
|
3716 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3717 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3718 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3719 PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 51216 | 3720 Py_ssize_t size, |
| 3721 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
|
3722 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3723 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
|
3724 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3725 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3726 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
|
3727 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3728 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3729 PyErr_BadArgument(); |
| 3730 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3731 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3732 return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3733 PyUnicode_GET_SIZE(unicode), |
| 3734 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3735 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3736 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3737 /* --- 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
|
3738 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3739 PyObject *PyUnicode_DecodeASCII(const char *s, |
| 51216 | 3740 Py_ssize_t size, |
| 3741 const char *errors) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3742 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3743 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
|
3744 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3745 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3746 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3747 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3748 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
|
3749 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
|
3750 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
|
3751 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
|
3752 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3753 /* 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
|
3754 if (size == 1 && *(unsigned char*)s < 128) { |
| 51216 | 3755 Py_UNICODE r = *(unsigned char*)s; |
| 3756 return PyUnicode_FromUnicode(&r, 1); | |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3757 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
3758 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3759 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
|
3760 if (v == NULL) |
| 51216 | 3761 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3762 if (size == 0) |
| 51216 | 3763 return (PyObject *)v; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3764 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
|
3765 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
|
3766 while (s < e) { |
| 51216 | 3767 register unsigned char c = (unsigned char)*s; |
| 3768 if (c < 128) { | |
| 3769 *p++ = c; | |
| 3770 ++s; | |
| 3771 } | |
| 3772 else { | |
| 3773 startinpos = s-starts; | |
| 3774 endinpos = startinpos + 1; | |
| 3775 outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | |
| 3776 if (unicode_decode_call_errorhandler( | |
| 3777 errors, &errorHandler, | |
| 3778 "ascii", "ordinal not in range(128)", | |
| 3779 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3780 &v, &outpos, &p)) | |
| 3781 goto onError; | |
| 3782 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3783 } |
| 12329 | 3784 if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
| 51216 | 3785 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 3786 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
|
3787 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
|
3788 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
|
3789 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
|
3790 |
| 51216 | 3791 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3792 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
|
3793 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
|
3794 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
|
3795 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3796 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3797 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3798 PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 51216 | 3799 Py_ssize_t size, |
| 3800 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
|
3801 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3802 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
|
3803 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3804 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3805 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
|
3806 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3807 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3808 PyErr_BadArgument(); |
| 3809 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3810 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3811 return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3812 PyUnicode_GET_SIZE(unicode), |
| 3813 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3814 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3815 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
3816 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 12193 | 3817 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3818 /* --- MBCS codecs for Windows -------------------------------------------- */ |
| 12193 | 3819 |
|
52254
b8934dd9267d
Merged revisions 70499 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
51216
diff
changeset
|
3820 #if SIZEOF_INT < SIZEOF_SIZE_T |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3821 #define NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3822 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3823 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3824 /* 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
|
3825 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
|
3826 b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 51216 | 3827 encodings, see IsDBCSLeadByteEx documentation. */ |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3828 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3829 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
|
3830 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3831 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
|
3832 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3833 if (IsDBCSLeadByte(*curr)) { |
| 51216 | 3834 const char *prev = CharPrev(s, curr); |
| 3835 return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3836 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3837 return 0; |
|
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 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3840 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3841 * 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
|
3842 * 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
|
3843 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3844 static int decode_mbcs(PyUnicodeObject **v, |
| 51216 | 3845 const char *s, /* MBCS string */ |
| 3846 int size, /* sizeof MBCS string */ | |
| 3847 int final) | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3848 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3849 Py_UNICODE *p; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3850 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
|
3851 int usize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3852 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3853 assert(size >= 0); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3854 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3855 /* 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
|
3856 if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 51216 | 3857 --size; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3858 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3859 /* 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
|
3860 if (size > 0) { |
| 51216 | 3861 usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3862 if (usize == 0) { | |
| 3863 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3864 return -1; | |
| 3865 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3866 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3867 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3868 if (*v == NULL) { |
| 51216 | 3869 /* Create unicode object */ |
| 3870 *v = _PyUnicode_New(usize); | |
| 3871 if (*v == NULL) | |
| 3872 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3873 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3874 else { |
| 51216 | 3875 /* Extend unicode object */ |
| 3876 n = PyUnicode_GET_SIZE(*v); | |
| 3877 if (_PyUnicode_Resize(v, n + usize) < 0) | |
| 3878 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3879 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3880 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3881 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3882 if (size > 0) { |
| 51216 | 3883 p = PyUnicode_AS_UNICODE(*v) + n; |
| 3884 if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { | |
| 3885 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3886 return -1; | |
| 3887 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3888 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3889 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3890 return size; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3891 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3892 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3893 PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 51216 | 3894 Py_ssize_t size, |
| 3895 const char *errors, | |
| 3896 Py_ssize_t *consumed) | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3897 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3898 PyUnicodeObject *v = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3899 int done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3900 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3901 if (consumed) |
| 51216 | 3902 *consumed = 0; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3903 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3904 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3905 retry: |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3906 if (size > INT_MAX) |
| 51216 | 3907 done = decode_mbcs(&v, s, INT_MAX, 0); |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3908 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3909 #endif |
| 51216 | 3910 done = decode_mbcs(&v, s, (int)size, !consumed); |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3911 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3912 if (done < 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3913 Py_XDECREF(v); |
| 51216 | 3914 return NULL; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3915 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3916 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3917 if (consumed) |
| 51216 | 3918 *consumed += done; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3919 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3920 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3921 if (size > INT_MAX) { |
| 51216 | 3922 s += done; |
| 3923 size -= done; | |
| 3924 goto retry; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3925 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3926 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3927 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3928 return (PyObject *)v; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3929 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3930 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3931 PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 51216 | 3932 Py_ssize_t size, |
| 3933 const char *errors) | |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3934 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3935 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
|
3936 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3937 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3938 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3939 * 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
|
3940 * 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
|
3941 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3942 static int encode_mbcs(PyObject **repr, |
| 51216 | 3943 const Py_UNICODE *p, /* unicode */ |
| 3944 int size) /* size of unicode */ | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3945 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3946 int mbcssize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3947 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
|
3948 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3949 assert(size >= 0); |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3950 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3951 /* 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
|
3952 if (size > 0) { |
| 51216 | 3953 mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3954 if (mbcssize == 0) { | |
| 3955 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3956 return -1; | |
| 3957 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3958 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3959 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3960 if (*repr == NULL) { |
| 51216 | 3961 /* Create string object */ |
| 3962 *repr = PyString_FromStringAndSize(NULL, mbcssize); | |
| 3963 if (*repr == NULL) | |
| 3964 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3965 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3966 else { |
| 51216 | 3967 /* Extend string object */ |
| 3968 n = PyString_Size(*repr); | |
| 3969 if (_PyString_Resize(repr, n + mbcssize) < 0) | |
| 3970 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3971 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3972 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3973 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3974 if (size > 0) { |
| 51216 | 3975 char *s = PyString_AS_STRING(*repr) + n; |
| 3976 if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { | |
| 3977 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3978 return -1; | |
| 3979 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3980 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3981 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3982 return 0; |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3983 } |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3984 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3985 PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 51216 | 3986 Py_ssize_t size, |
| 3987 const char *errors) | |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3988 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3989 PyObject *repr = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3990 int ret; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3991 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3992 #ifdef NEED_RETRY |
| 51216 | 3993 retry: |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3994 if (size > INT_MAX) |
| 51216 | 3995 ret = encode_mbcs(&repr, p, INT_MAX); |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3996 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3997 #endif |
| 51216 | 3998 ret = encode_mbcs(&repr, p, (int)size); |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3999 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4000 if (ret < 0) { |
| 51216 | 4001 Py_XDECREF(repr); |
| 4002 return NULL; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4003 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4004 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4005 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4006 if (size > INT_MAX) { |
| 51216 | 4007 p += INT_MAX; |
| 4008 size -= INT_MAX; | |
| 4009 goto retry; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4010 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4011 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4012 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4013 return repr; |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4014 } |
| 12193 | 4015 |
|
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
|
4016 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
|
4017 { |
|
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
|
4018 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
|
4019 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
|
4020 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
|
4021 } |
|
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
|
4022 return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 4023 PyUnicode_GET_SIZE(unicode), |
| 4024 NULL); | |
|
29706
a4fead0e24f8
Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents:
29641
diff
changeset
|
4025 } |
|
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
|
4026 |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4027 #undef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4028 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
4029 #endif /* MS_WINDOWS */ |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4030 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4031 /* --- Character Mapping Codec -------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4032 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4033 PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 51216 | 4034 Py_ssize_t size, |
| 4035 PyObject *mapping, | |
| 4036 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
|
4037 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4038 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4039 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4040 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4041 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
|
4042 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
|
4043 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4044 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4045 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
|
4046 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
|
4047 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
|
4048 Py_UNICODE *mapstring = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4049 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
|
4050 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4051 /* 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
|
4052 if (mapping == NULL) |
| 51216 | 4053 return PyUnicode_DecodeLatin1(s, size, errors); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4054 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4055 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
|
4056 if (v == NULL) |
| 51216 | 4057 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4058 if (size == 0) |
| 51216 | 4059 return (PyObject *)v; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4060 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
|
4061 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
|
4062 if (PyUnicode_CheckExact(mapping)) { |
| 51216 | 4063 mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4064 maplen = PyUnicode_GET_SIZE(mapping); | |
| 4065 while (s < e) { | |
| 4066 unsigned char ch = *s; | |
| 4067 Py_UNICODE x = 0xfffe; /* illegal value */ | |
| 4068 | |
| 4069 if (ch < maplen) | |
| 4070 x = mapstring[ch]; | |
| 4071 | |
| 4072 if (x == 0xfffe) { | |
| 4073 /* undefined mapping */ | |
| 4074 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 4075 startinpos = s-starts; | |
| 4076 endinpos = startinpos+1; | |
| 4077 if (unicode_decode_call_errorhandler( | |
| 4078 errors, &errorHandler, | |
| 4079 "charmap", "character maps to <undefined>", | |
| 4080 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 4081 &v, &outpos, &p)) { | |
| 4082 goto onError; | |
| 4083 } | |
| 4084 continue; | |
| 4085 } | |
| 4086 *p++ = x; | |
| 4087 ++s; | |
| 4088 } | |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4089 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4090 else { |
| 51216 | 4091 while (s < e) { |
| 4092 unsigned char ch = *s; | |
| 4093 PyObject *w, *x; | |
| 4094 | |
| 4095 /* Get mapping (char ordinal -> integer, Unicode char or None) */ | |
| 4096 w = PyInt_FromLong((long)ch); | |
| 4097 if (w == NULL) | |
| 4098 goto onError; | |
| 4099 x = PyObject_GetItem(mapping, w); | |
| 4100 Py_DECREF(w); | |
| 4101 if (x == NULL) { | |
| 4102 if (PyErr_ExceptionMatches(PyExc_LookupError)) { | |
| 4103 /* No mapping found means: mapping is undefined. */ | |
| 4104 PyErr_Clear(); | |
|
81517
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4105 goto Undefined; |
| 51216 | 4106 } else |
| 4107 goto onError; | |
| 4108 } | |
| 4109 | |
| 4110 /* Apply mapping */ | |
|
81517
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4111 if (x == Py_None) |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4112 goto Undefined; |
| 51216 | 4113 if (PyInt_Check(x)) { |
| 4114 long value = PyInt_AS_LONG(x); | |
|
81517
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4115 if (value == 0xFFFE) |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4116 goto Undefined; |
|
80478
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4117 if (value < 0 || value > 0x10FFFF) { |
| 51216 | 4118 PyErr_SetString(PyExc_TypeError, |
|
80478
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4119 "character mapping must be in range(0x110000)"); |
| 51216 | 4120 Py_DECREF(x); |
| 4121 goto onError; | |
| 4122 } | |
|
80478
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4123 |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4124 #ifndef Py_UNICODE_WIDE |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4125 if (value > 0xFFFF) { |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4126 /* see the code for 1-n mapping below */ |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4127 if (extrachars < 2) { |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4128 /* resize first */ |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4129 Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4130 Py_ssize_t needed = 10 - extrachars; |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4131 extrachars += needed; |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4132 /* XXX overflow detection missing */ |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4133 if (_PyUnicode_Resize(&v, |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4134 PyUnicode_GET_SIZE(v) + needed) < 0) { |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4135 Py_DECREF(x); |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4136 goto onError; |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4137 } |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4138 p = PyUnicode_AS_UNICODE(v) + oldpos; |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4139 } |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4140 value -= 0x10000; |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4141 *p++ = 0xD800 | (value >> 10); |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4142 *p++ = 0xDC00 | (value & 0x3FF); |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4143 extrachars -= 2; |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4144 } |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4145 else |
|
c7ce91756472
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Antoine Pitrou <solipsis@pitrou.net>
parents:
80075
diff
changeset
|
4146 #endif |
| 51216 | 4147 *p++ = (Py_UNICODE)value; |
| 4148 } | |
| 4149 else if (PyUnicode_Check(x)) { | |
| 4150 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | |
| 4151 | |
|
81517
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4152 if (targetsize == 1) { |
| 51216 | 4153 /* 1-1 mapping */ |
|
81517
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4154 Py_UNICODE value = *PyUnicode_AS_UNICODE(x); |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4155 if (value == 0xFFFE) |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4156 goto Undefined; |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4157 *p++ = value; |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4158 } |
| 51216 | 4159 else if (targetsize > 1) { |
| 4160 /* 1-n mapping */ | |
| 4161 if (targetsize > extrachars) { | |
| 4162 /* resize first */ | |
| 4163 Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | |
| 4164 Py_ssize_t needed = (targetsize - extrachars) + \ | |
| 4165 (targetsize << 2); | |
| 4166 extrachars += needed; | |
| 4167 /* XXX overflow detection missing */ | |
| 4168 if (_PyUnicode_Resize(&v, | |
| 4169 PyUnicode_GET_SIZE(v) + needed) < 0) { | |
| 4170 Py_DECREF(x); | |
| 4171 goto onError; | |
| 4172 } | |
| 4173 p = PyUnicode_AS_UNICODE(v) + oldpos; | |
| 4174 } | |
| 4175 Py_UNICODE_COPY(p, | |
| 4176 PyUnicode_AS_UNICODE(x), | |
| 4177 targetsize); | |
| 4178 p += targetsize; | |
| 4179 extrachars -= targetsize; | |
| 4180 } | |
| 4181 /* 1-0 mapping: skip the character */ | |
| 4182 } | |
| 4183 else { | |
| 4184 /* wrong return value */ | |
| 4185 PyErr_SetString(PyExc_TypeError, | |
| 4186 "character mapping must return integer, None or unicode"); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4187 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4188 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4189 } |
| 51216 | 4190 Py_DECREF(x); |
| 4191 ++s; | |
|
81517
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4192 continue; |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4193 Undefined: |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4194 /* undefined mapping */ |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4195 Py_XDECREF(x); |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4196 outpos = p-PyUnicode_AS_UNICODE(v); |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4197 startinpos = s-starts; |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4198 endinpos = startinpos+1; |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4199 if (unicode_decode_call_errorhandler( |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4200 errors, &errorHandler, |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4201 "charmap", "character maps to <undefined>", |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4202 starts, size, &startinpos, &endinpos, &exc, &s, |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4203 &v, &outpos, &p)) { |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4204 goto onError; |
|
33a8ef498b1e
Issue #14850: Now a chamap decoder treates U+FFFE as "undefined mapping"
Serhiy Storchaka <storchaka@gmail.com>
parents:
81328
diff
changeset
|
4205 } |
| 51216 | 4206 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4207 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4208 if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 51216 | 4209 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4210 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
|
4211 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
|
4212 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
|
4213 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
|
4214 |
| 51216 | 4215 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
|
4216 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
|
4217 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
|
4218 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4219 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4220 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4221 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4222 /* Charmap encoding: the lookup table */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4223 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4224 struct encoding_map{ |
| 51216 | 4225 PyObject_HEAD |
| 4226 unsigned char level1[32]; | |
| 4227 int count2, count3; | |
| 4228 unsigned char level23[1]; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4229 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4230 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4231 static PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4232 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
|
4233 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4234 struct encoding_map *map = (struct encoding_map*)obj; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4235 return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4236 128*map->count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4237 } |
|
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 static PyMethodDef encoding_map_methods[] = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4240 {"size", encoding_map_size, METH_NOARGS, |
| 51216 | 4241 PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4242 { 0 } | |
|
38586
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 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4245 static void |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4246 encoding_map_dealloc(PyObject* o) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4247 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4248 PyObject_FREE(o); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4249 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4250 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4251 static PyTypeObject EncodingMapType = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4252 PyVarObject_HEAD_INIT(NULL, 0) |
| 51216 | 4253 "EncodingMap", /*tp_name*/ |
| 4254 sizeof(struct encoding_map), /*tp_basicsize*/ | |
| 4255 0, /*tp_itemsize*/ | |
| 4256 /* methods */ | |
| 4257 encoding_map_dealloc, /*tp_dealloc*/ | |
| 4258 0, /*tp_print*/ | |
| 4259 0, /*tp_getattr*/ | |
| 4260 0, /*tp_setattr*/ | |
| 4261 0, /*tp_compare*/ | |
| 4262 0, /*tp_repr*/ | |
| 4263 0, /*tp_as_number*/ | |
| 4264 0, /*tp_as_sequence*/ | |
| 4265 0, /*tp_as_mapping*/ | |
| 4266 0, /*tp_hash*/ | |
| 4267 0, /*tp_call*/ | |
| 4268 0, /*tp_str*/ | |
| 4269 0, /*tp_getattro*/ | |
| 4270 0, /*tp_setattro*/ | |
| 4271 0, /*tp_as_buffer*/ | |
| 4272 Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
| 4273 0, /*tp_doc*/ | |
| 4274 0, /*tp_traverse*/ | |
| 4275 0, /*tp_clear*/ | |
| 4276 0, /*tp_richcompare*/ | |
| 4277 0, /*tp_weaklistoffset*/ | |
| 4278 0, /*tp_iter*/ | |
| 4279 0, /*tp_iternext*/ | |
| 4280 encoding_map_methods, /*tp_methods*/ | |
| 4281 0, /*tp_members*/ | |
| 4282 0, /*tp_getset*/ | |
| 4283 0, /*tp_base*/ | |
| 4284 0, /*tp_dict*/ | |
| 4285 0, /*tp_descr_get*/ | |
| 4286 0, /*tp_descr_set*/ | |
| 4287 0, /*tp_dictoffset*/ | |
| 4288 0, /*tp_init*/ | |
| 4289 0, /*tp_alloc*/ | |
| 4290 0, /*tp_new*/ | |
| 4291 0, /*tp_free*/ | |
| 4292 0, /*tp_is_gc*/ | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4293 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4294 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4295 PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4296 PyUnicode_BuildEncodingMap(PyObject* string) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4297 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4298 Py_UNICODE *decode; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4299 PyObject *result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4300 struct encoding_map *mresult; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4301 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4302 int need_dict = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4303 unsigned char level1[32]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4304 unsigned char level2[512]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4305 unsigned char *mlevel1, *mlevel2, *mlevel3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4306 int count2 = 0, count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4307 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4308 if (!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
|
4309 PyErr_BadArgument(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4310 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4311 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4312 decode = PyUnicode_AS_UNICODE(string); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4313 memset(level1, 0xFF, sizeof level1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4314 memset(level2, 0xFF, sizeof level2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4315 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4316 /* 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
|
4317 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
|
4318 a mapping dictionary. */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4319 if (decode[0] != 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4320 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4321 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
|
4322 int l1, l2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4323 if (decode[i] == 0 |
| 51216 | 4324 #ifdef Py_UNICODE_WIDE |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4325 || decode[i] > 0xFFFF |
| 51216 | 4326 #endif |
| 4327 ) { | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4328 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4329 break; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4330 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4331 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4332 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4333 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4334 l1 = decode[i] >> 11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4335 l2 = decode[i] >> 7; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4336 if (level1[l1] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4337 level1[l1] = count2++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4338 if (level2[l2] == 0xFF) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4339 level2[l2] = count3++; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4340 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4341 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4342 if (count2 >= 0xFF || count3 >= 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4343 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4344 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4345 if (need_dict) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4346 PyObject *result = PyDict_New(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4347 PyObject *key, *value; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4348 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4349 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4350 for (i = 0; i < 256; i++) { |
|
61204
55a593cc3280
Remove an unneeded variable and assignment.
Brett Cannon <bcannon@gmail.com>
parents:
60203
diff
changeset
|
4351 value = NULL; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4352 key = PyInt_FromLong(decode[i]); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4353 value = PyInt_FromLong(i); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4354 if (!key || !value) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4355 goto failed1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4356 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
|
4357 goto failed1; |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4358 Py_DECREF(key); |
|
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4359 Py_DECREF(value); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4360 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4361 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4362 failed1: |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4363 Py_XDECREF(key); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4364 Py_XDECREF(value); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4365 Py_DECREF(result); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4366 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4367 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4368 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4369 /* Create a three-level trie */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4370 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
|
4371 16*count2 + 128*count3 - 1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4372 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4373 return PyErr_NoMemory(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4374 PyObject_Init(result, &EncodingMapType); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4375 mresult = (struct encoding_map*)result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4376 mresult->count2 = count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4377 mresult->count3 = count3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4378 mlevel1 = mresult->level1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4379 mlevel2 = mresult->level23; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4380 mlevel3 = mresult->level23 + 16*count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4381 memcpy(mlevel1, level1, 32); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4382 memset(mlevel2, 0xFF, 16*count2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4383 memset(mlevel3, 0, 128*count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4384 count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4385 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
|
4386 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
|
4387 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4388 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4389 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4390 o1 = decode[i]>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4391 o2 = (decode[i]>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4392 i2 = 16*mlevel1[o1] + o2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4393 if (mlevel2[i2] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4394 mlevel2[i2] = count3++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4395 o3 = decode[i] & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4396 i3 = 128*mlevel2[i2] + o3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4397 mlevel3[i3] = i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4398 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4399 return result; |
|
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 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4403 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
|
4404 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4405 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
|
4406 int l1 = c>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4407 int l2 = (c>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4408 int l3 = c & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4409 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4410 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4411 #ifdef Py_UNICODE_WIDE |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4412 if (c > 0xFFFF) { |
| 51216 | 4413 return -1; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4414 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4415 #endif |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4416 if (c == 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4417 return 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4418 /* level 1*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4419 i = map->level1[l1]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4420 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4421 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4422 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4423 /* level 2*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4424 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
|
4425 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4426 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4427 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4428 /* level 3 */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4429 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
|
4430 if (i == 0) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4431 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4432 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4433 return i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4434 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4435 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4436 /* 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
|
4437 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
|
4438 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
|
4439 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
|
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 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
|
4442 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
|
4443 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4444 if (w == NULL) |
| 51216 | 4445 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4446 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
|
4447 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
|
4448 if (x == NULL) { |
| 51216 | 4449 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4450 /* No mapping found means: mapping is undefined. */ | |
| 4451 PyErr_Clear(); | |
| 4452 x = Py_None; | |
| 4453 Py_INCREF(x); | |
| 4454 return x; | |
| 4455 } else | |
| 4456 return NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4457 } |
|
27175
47b134bb51c3
Fix charmapencode_lookup(), so that a None value in the mapping
Walter Dörwald <walter@livinglogic.de>
parents:
27170
diff
changeset
|
4458 else if (x == Py_None) |
| 51216 | 4459 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
|
4460 else if (PyInt_Check(x)) { |
| 51216 | 4461 long value = PyInt_AS_LONG(x); |
| 4462 if (value < 0 || value > 255) { | |
| 4463 PyErr_SetString(PyExc_TypeError, | |
| 4464 "character mapping must be in range(256)"); | |
| 4465 Py_DECREF(x); | |
| 4466 return NULL; | |
| 4467 } | |
| 4468 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
|
4469 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4470 else if (PyString_Check(x)) |
| 51216 | 4471 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
|
4472 else { |
| 51216 | 4473 /* wrong return value */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4474 PyErr_SetString(PyExc_TypeError, |
| 51216 | 4475 "character mapping must return integer, None or str"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4476 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4477 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4478 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4479 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4480 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4481 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4482 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
|
4483 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4484 Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4485 /* exponentially overallocate to minimize reallocations */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4486 if (requiredsize < 2*outsize) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4487 requiredsize = 2*outsize; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4488 if (_PyString_Resize(outobj, requiredsize)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4489 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4490 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4491 return 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4492 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4493 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4494 typedef enum charmapencode_result { |
| 51216 | 4495 enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4496 }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
|
4497 /* 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
|
4498 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
|
4499 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
|
4500 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
|
4501 (in which case no character was written) or NULL, if a |
| 35827 | 4502 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
|
4503 static |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4504 charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 51216 | 4505 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
|
4506 { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4507 PyObject *rep; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4508 char *outstart; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4509 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
|
4510 |
|
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
|
4511 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
|
4512 int res = encoding_map_lookup(c, mapping); |
| 51216 | 4513 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
|
4514 if (res == -1) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4515 return enc_FAILED; |
| 51216 | 4516 if (outsize<requiredsize) |
| 4517 if (!charmapencode_resize(outobj, outpos, requiredsize)) | |
| 4518 return enc_EXCEPTION; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4519 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4520 outstart[(*outpos)++] = (char)res; |
| 4521 return enc_SUCCESS; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4522 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4523 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4524 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
|
4525 if (rep==NULL) |
| 51216 | 4526 return enc_EXCEPTION; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4527 else if (rep==Py_None) { |
| 51216 | 4528 Py_DECREF(rep); |
| 4529 return enc_FAILED; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4530 } else { |
| 51216 | 4531 if (PyInt_Check(rep)) { |
| 4532 Py_ssize_t requiredsize = *outpos+1; | |
| 4533 if (outsize<requiredsize) | |
| 4534 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | |
| 4535 Py_DECREF(rep); | |
| 4536 return enc_EXCEPTION; | |
| 4537 } | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4538 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4539 outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4540 } | |
| 4541 else { | |
| 4542 const char *repchars = PyString_AS_STRING(rep); | |
| 4543 Py_ssize_t repsize = PyString_GET_SIZE(rep); | |
| 4544 Py_ssize_t requiredsize = *outpos+repsize; | |
| 4545 if (outsize<requiredsize) | |
| 4546 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | |
| 4547 Py_DECREF(rep); | |
| 4548 return enc_EXCEPTION; | |
| 4549 } | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4550 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4551 memcpy(outstart + *outpos, repchars, repsize); |
| 4552 *outpos += repsize; | |
| 4553 } | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4554 } |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4555 Py_DECREF(rep); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4556 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
|
4557 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4558 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4559 /* 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
|
4560 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
|
4561 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4562 int charmap_encoding_error( |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4563 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
|
4564 PyObject **exceptionObject, |
|
30149
59bad7efff02
Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents:
29706
diff
changeset
|
4565 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
|
4566 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
|
4567 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4568 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
|
4569 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4570 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
|
4571 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
|
4572 /* startpos for collecting unencodable chars */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4573 Py_ssize_t collstartpos = *inpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4574 Py_ssize_t collendpos = *inpos+1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4575 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
|
4576 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
|
4577 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
|
4578 charmapencode_result x; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4579 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4580 /* 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
|
4581 while (collendpos < size) { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4582 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
|
4583 if (Py_TYPE(mapping) == &EncodingMapType) { |
| 51216 | 4584 int res = encoding_map_lookup(p[collendpos], mapping); |
| 4585 if (res != -1) | |
| 4586 break; | |
| 4587 ++collendpos; | |
| 4588 continue; | |
| 4589 } | |
| 4590 | |
| 4591 rep = charmapencode_lookup(p[collendpos], mapping); | |
| 4592 if (rep==NULL) | |
| 4593 return -1; | |
| 4594 else if (rep!=Py_None) { | |
| 4595 Py_DECREF(rep); | |
| 4596 break; | |
| 4597 } | |
| 4598 Py_DECREF(rep); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4599 ++collendpos; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
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 /* 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
|
4602 * (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
|
4603 if (*known_errorHandler==-1) { |
| 51216 | 4604 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4605 *known_errorHandler = 1; | |
| 4606 else if (!strcmp(errors, "replace")) | |
| 4607 *known_errorHandler = 2; | |
| 4608 else if (!strcmp(errors, "ignore")) | |
| 4609 *known_errorHandler = 3; | |
| 4610 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 4611 *known_errorHandler = 4; | |
| 4612 else | |
| 4613 *known_errorHandler = 0; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4614 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4615 switch (*known_errorHandler) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4616 case 1: /* strict */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4617 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4618 return -1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4619 case 2: /* replace */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4620 for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 51216 | 4621 x = charmapencode_output('?', mapping, res, respos); |
| 4622 if (x==enc_EXCEPTION) { | |
| 4623 return -1; | |
| 4624 } | |
| 4625 else if (x==enc_FAILED) { | |
| 4626 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4627 return -1; | |
| 4628 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4629 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4630 /* fall through */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4631 case 3: /* ignore */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4632 *inpos = collendpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4633 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4634 case 4: /* xmlcharrefreplace */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4635 /* generate replacement (temporarily (mis)uses p) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4636 for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 51216 | 4637 char buffer[2+29+1+1]; |
| 4638 char *cp; | |
| 4639 sprintf(buffer, "&#%d;", (int)p[collpos]); | |
| 4640 for (cp = buffer; *cp; ++cp) { | |
| 4641 x = charmapencode_output(*cp, mapping, res, respos); | |
| 4642 if (x==enc_EXCEPTION) | |
| 4643 return -1; | |
| 4644 else if (x==enc_FAILED) { | |
| 4645 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4646 return -1; | |
| 4647 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4648 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4649 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4650 *inpos = collendpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4651 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4652 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4653 repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
| 51216 | 4654 encoding, reason, p, size, exceptionObject, |
| 4655 collstartpos, collendpos, &newpos); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4656 if (repunicode == NULL) |
| 51216 | 4657 return -1; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4658 /* generate replacement */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4659 repsize = PyUnicode_GET_SIZE(repunicode); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4660 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 51216 | 4661 x = charmapencode_output(*uni2, mapping, res, respos); |
| 4662 if (x==enc_EXCEPTION) { | |
| 4663 return -1; | |
| 4664 } | |
| 4665 else if (x==enc_FAILED) { | |
| 4666 Py_DECREF(repunicode); | |
| 4667 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4668 return -1; | |
| 4669 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4670 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4671 *inpos = newpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4672 Py_DECREF(repunicode); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4673 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4674 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4675 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4676 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4677 PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 51216 | 4678 Py_ssize_t size, |
| 4679 PyObject *mapping, | |
| 4680 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
|
4681 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4682 /* 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
|
4683 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
|
4684 /* current input position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4685 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
|
4686 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4687 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
|
4688 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
|
4689 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
|
4690 /* 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
|
4691 * -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
|
4692 * 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
|
4693 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
|
4694 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4695 /* 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
|
4696 if (mapping == NULL) |
| 51216 | 4697 return PyUnicode_EncodeLatin1(p, size, errors); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4698 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4699 /* 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
|
4700 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
|
4701 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
|
4702 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
|
4703 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
|
4704 if (size == 0) |
| 51216 | 4705 return res; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4706 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4707 while (inpos<size) { |
| 51216 | 4708 /* try to encode it */ |
| 4709 charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); | |
| 4710 if (x==enc_EXCEPTION) /* error */ | |
| 4711 goto onError; | |
| 4712 if (x==enc_FAILED) { /* unencodable character */ | |
| 4713 if (charmap_encoding_error(p, size, &inpos, mapping, | |
| 4714 &exc, | |
| 4715 &known_errorHandler, &errorHandler, errors, | |
| 4716 &res, &respos)) { | |
| 4717 goto onError; | |
| 4718 } | |
| 4719 } | |
| 4720 else | |
| 4721 /* done with this character => adjust input position */ | |
| 4722 ++inpos; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4723 } |
|
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 /* 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
|
4726 if (respos<PyString_GET_SIZE(res)) { |
| 51216 | 4727 if (_PyString_Resize(&res, respos)) |
| 4728 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
|
4729 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4730 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
|
4731 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
|
4732 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
|
4733 |
| 51216 | 4734 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
|
4735 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
|
4736 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
|
4737 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
|
4738 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4739 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4740 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4741 PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 51216 | 4742 PyObject *mapping) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4743 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4744 if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 51216 | 4745 PyErr_BadArgument(); |
| 4746 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4747 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4748 return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 4749 PyUnicode_GET_SIZE(unicode), |
| 4750 mapping, | |
| 4751 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4752 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4753 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4754 /* 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
|
4755 static void make_translate_exception(PyObject **exceptionObject, |
| 51216 | 4756 const Py_UNICODE *unicode, Py_ssize_t size, |
| 4757 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4758 const char *reason) | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4759 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4760 if (*exceptionObject == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4761 *exceptionObject = PyUnicodeTranslateError_Create( |
| 51216 | 4762 unicode, size, startpos, endpos, reason); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4763 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4764 else { |
| 51216 | 4765 if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4766 goto onError; | |
| 4767 if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | |
| 4768 goto onError; | |
| 4769 if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | |
| 4770 goto onError; | |
| 4771 return; | |
| 4772 onError: | |
| 4773 Py_DECREF(*exceptionObject); | |
| 4774 *exceptionObject = NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4775 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4776 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4777 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4778 /* 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
|
4779 static void raise_translate_exception(PyObject **exceptionObject, |
| 51216 | 4780 const Py_UNICODE *unicode, Py_ssize_t size, |
| 4781 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4782 const char *reason) | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4783 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4784 make_translate_exception(exceptionObject, |
| 51216 | 4785 unicode, size, startpos, endpos, reason); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4786 if (*exceptionObject != NULL) |
| 51216 | 4787 PyCodec_StrictErrors(*exceptionObject); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4788 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4789 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4790 /* 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
|
4791 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
|
4792 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
|
4793 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
|
4794 static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 51216 | 4795 PyObject **errorHandler, |
| 4796 const char *reason, | |
| 4797 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | |
| 4798 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4799 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
|
4800 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4801 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
|
4802 |
|
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4803 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
|
4804 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
|
4805 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
|
4806 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4807 if (*errorHandler == NULL) { |
| 51216 | 4808 *errorHandler = PyCodec_LookupError(errors); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4809 if (*errorHandler == NULL) |
| 51216 | 4810 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4811 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4812 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4813 make_translate_exception(exceptionObject, |
| 51216 | 4814 unicode, size, startpos, endpos, reason); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4815 if (*exceptionObject == NULL) |
| 51216 | 4816 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4817 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4818 restuple = PyObject_CallFunctionObjArgs( |
| 51216 | 4819 *errorHandler, *exceptionObject, NULL); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4820 if (restuple == NULL) |
| 51216 | 4821 return NULL; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4822 if (!PyTuple_Check(restuple)) { |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
52411
diff
changeset
|
4823 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 4824 Py_DECREF(restuple); |
| 4825 return NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4826 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4827 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 51216 | 4828 &resunicode, &i_newpos)) { |
| 4829 Py_DECREF(restuple); | |
| 4830 return NULL; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4831 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4832 if (i_newpos<0) |
| 51216 | 4833 *newpos = size+i_newpos; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4834 else |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4835 *newpos = i_newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4836 if (*newpos<0 || *newpos>size) { |
| 51216 | 4837 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4838 Py_DECREF(restuple); | |
| 4839 return NULL; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4840 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4841 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
|
4842 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
|
4843 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
|
4844 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4845 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4846 /* 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
|
4847 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
|
4848 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
|
4849 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
|
4850 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
|
4851 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4852 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
|
4853 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
|
4854 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4855 if (w == NULL) |
| 51216 | 4856 return -1; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4857 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
|
4858 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
|
4859 if (x == NULL) { |
| 51216 | 4860 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4861 /* No mapping found means: use 1:1 mapping. */ | |
| 4862 PyErr_Clear(); | |
| 4863 *result = NULL; | |
| 4864 return 0; | |
| 4865 } else | |
| 4866 return -1; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4867 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4868 else if (x == Py_None) { |
| 51216 | 4869 *result = x; |
| 4870 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4871 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4872 else if (PyInt_Check(x)) { |
| 51216 | 4873 long value = PyInt_AS_LONG(x); |
| 4874 long max = PyUnicode_GetMax(); | |
| 4875 if (value < 0 || value > max) { | |
| 4876 PyErr_Format(PyExc_TypeError, | |
| 4877 "character mapping must be in range(0x%lx)", max+1); | |
| 4878 Py_DECREF(x); | |
| 4879 return -1; | |
| 4880 } | |
| 4881 *result = x; | |
| 4882 return 0; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4883 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4884 else if (PyUnicode_Check(x)) { |
| 51216 | 4885 *result = x; |
| 4886 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4887 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4888 else { |
| 51216 | 4889 /* wrong return value */ |
| 4890 PyErr_SetString(PyExc_TypeError, | |
| 4891 "character mapping must return integer, None or unicode"); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4892 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4893 return -1; |
|
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 /* ensure that *outobj is at least requiredsize characters long, |
| 51216 | 4897 if not reallocate and adjust various state variables. |
| 4898 Return 0 on success, -1 on error */ | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4899 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4900 int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
| 51216 | 4901 Py_ssize_t requiredsize) |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4902 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4903 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
|
4904 if (requiredsize > oldsize) { |
| 51216 | 4905 /* remember old output position */ |
| 4906 Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); | |
| 4907 /* exponentially overallocate to minimize reallocations */ | |
| 4908 if (requiredsize < 2 * oldsize) | |
| 4909 requiredsize = 2 * oldsize; | |
| 4910 if (PyUnicode_Resize(outobj, requiredsize) < 0) | |
| 4911 return -1; | |
| 4912 *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4913 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4914 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
|
4915 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4916 /* 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
|
4917 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
|
4918 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
|
4919 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
|
4920 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
|
4921 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
|
4922 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4923 int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 51216 | 4924 Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 4925 PyObject **res) | |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4926 { |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4927 if (charmaptranslate_lookup(*curinp, mapping, res)) |
| 51216 | 4928 return -1; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4929 if (*res==NULL) { |
| 51216 | 4930 /* not found => default to 1:1 mapping */ |
| 4931 *(*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
|
4932 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4933 else if (*res==Py_None) |
| 51216 | 4934 ; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4935 else if (PyInt_Check(*res)) { |
| 51216 | 4936 /* no overflow check, because we know that the space is enough */ |
| 4937 *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4938 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4939 else if (PyUnicode_Check(*res)) { |
| 51216 | 4940 Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 4941 if (repsize==1) { | |
| 4942 /* no overflow check, because we know that the space is enough */ | |
| 4943 *(*outp)++ = *PyUnicode_AS_UNICODE(*res); | |
| 4944 } | |
| 4945 else if (repsize!=0) { | |
| 4946 /* more than one character */ | |
| 4947 Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + | |
| 4948 (insize - (curinp-startinp)) + | |
| 4949 repsize - 1; | |
| 4950 if (charmaptranslate_makespace(outobj, outp, requiredsize)) | |
| 4951 return -1; | |
| 4952 memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); | |
| 4953 *outp += repsize; | |
| 4954 } | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4955 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4956 else |
| 51216 | 4957 return -1; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4958 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
|
4959 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4960 |
|
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 *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 51216 | 4962 Py_ssize_t size, |
| 4963 PyObject *mapping, | |
| 4964 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
|
4965 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4966 /* 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
|
4967 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
|
4968 /* 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
|
4969 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
|
4970 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
|
4971 /* 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
|
4972 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
|
4973 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4974 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
|
4975 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
|
4976 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
|
4977 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
|
4978 /* 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
|
4979 * -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
|
4980 * 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
|
4981 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
|
4982 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4983 if (mapping == NULL) { |
| 51216 | 4984 PyErr_BadArgument(); |
| 4985 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4986 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4987 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4988 /* 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
|
4989 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
|
4990 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
|
4991 if (res == NULL) |
| 51216 | 4992 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4993 if (size == 0) |
| 51216 | 4994 return res; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4995 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
|
4996 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4997 while (p<endp) { |
| 51216 | 4998 /* try to encode it */ |
| 4999 PyObject *x = NULL; | |
| 5000 if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { | |
| 5001 Py_XDECREF(x); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5002 goto onError; |
| 51216 | 5003 } |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5004 Py_XDECREF(x); |
| 51216 | 5005 if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5006 ++p; | |
| 5007 else { /* untranslatable character */ | |
| 5008 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | |
| 5009 Py_ssize_t repsize; | |
| 5010 Py_ssize_t newpos; | |
| 5011 Py_UNICODE *uni2; | |
| 5012 /* startpos for collecting untranslatable chars */ | |
| 5013 const Py_UNICODE *collstart = p; | |
| 5014 const Py_UNICODE *collend = p+1; | |
| 5015 const Py_UNICODE *coll; | |
| 5016 | |
| 5017 /* find all untranslatable characters */ | |
| 5018 while (collend < endp) { | |
| 5019 if (charmaptranslate_lookup(*collend, mapping, &x)) | |
| 5020 goto onError; | |
| 5021 Py_XDECREF(x); | |
| 5022 if (x!=Py_None) | |
| 5023 break; | |
| 5024 ++collend; | |
| 5025 } | |
| 5026 /* cache callback name lookup | |
| 5027 * (if not done yet, i.e. it's the first error) */ | |
| 5028 if (known_errorHandler==-1) { | |
| 5029 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 5030 known_errorHandler = 1; | |
| 5031 else if (!strcmp(errors, "replace")) | |
| 5032 known_errorHandler = 2; | |
| 5033 else if (!strcmp(errors, "ignore")) | |
| 5034 known_errorHandler = 3; | |
| 5035 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 5036 known_errorHandler = 4; | |
| 5037 else | |
| 5038 known_errorHandler = 0; | |
| 5039 } | |
| 5040 switch (known_errorHandler) { | |
| 5041 case 1: /* strict */ | |
| 5042 raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5043 goto onError; |
| 51216 | 5044 case 2: /* replace */ |
| 5045 /* No need to check for space, this is a 1:1 replacement */ | |
| 5046 for (coll = collstart; coll<collend; ++coll) | |
| 5047 *str++ = '?'; | |
| 5048 /* fall through */ | |
| 5049 case 3: /* ignore */ | |
| 5050 p = collend; | |
| 5051 break; | |
| 5052 case 4: /* xmlcharrefreplace */ | |
| 5053 /* generate replacement (temporarily (mis)uses p) */ | |
| 5054 for (p = collstart; p < collend; ++p) { | |
| 5055 char buffer[2+29+1+1]; | |
| 5056 char *cp; | |
| 5057 sprintf(buffer, "&#%d;", (int)*p); | |
| 5058 if (charmaptranslate_makespace(&res, &str, | |
| 5059 (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) | |
| 5060 goto onError; | |
| 5061 for (cp = buffer; *cp; ++cp) | |
| 5062 *str++ = *cp; | |
| 5063 } | |
| 5064 p = collend; | |
| 5065 break; | |
| 5066 default: | |
| 5067 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | |
| 5068 reason, startp, size, &exc, | |
| 5069 collstart-startp, collend-startp, &newpos); | |
| 5070 if (repunicode == NULL) | |
| 5071 goto onError; | |
| 5072 /* generate replacement */ | |
| 5073 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 5074 if (charmaptranslate_makespace(&res, &str, | |
| 5075 (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { | |
| 5076 Py_DECREF(repunicode); | |
| 5077 goto onError; | |
| 5078 } | |
| 5079 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) | |
| 5080 *str++ = *uni2; | |
| 5081 p = startp + newpos; | |
| 5082 Py_DECREF(repunicode); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5083 } |
| 51216 | 5084 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5085 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5086 /* 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
|
5087 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
|
5088 if (respos<PyUnicode_GET_SIZE(res)) { |
| 51216 | 5089 if (PyUnicode_Resize(&res, respos) < 0) |
| 5090 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
|
5091 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5092 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
|
5093 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
|
5094 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
|
5095 |
| 51216 | 5096 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
|
5097 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
|
5098 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
|
5099 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
|
5100 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5101 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5102 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5103 PyObject *PyUnicode_Translate(PyObject *str, |
| 51216 | 5104 PyObject *mapping, |
| 5105 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
|
5106 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5107 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
|
5108 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5109 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
|
5110 if (str == NULL) |
| 51216 | 5111 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5112 result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 51216 | 5113 PyUnicode_GET_SIZE(str), |
| 5114 mapping, | |
| 5115 errors); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5116 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5117 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
|
5118 |
| 51216 | 5119 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5120 Py_XDECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5121 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5122 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5123 |
|
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
|
5124 /* --- 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
|
5125 |
|
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
|
5126 int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 51216 | 5127 Py_ssize_t length, |
| 5128 char *output, | |
| 5129 const char *errors) | |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5130 { |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5131 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
|
5132 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
|
5133 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
|
5134 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
|
5135 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
|
5136 /* 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
|
5137 * -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
|
5138 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
|
5139 |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5140 if (output == NULL) { |
| 51216 | 5141 PyErr_BadArgument(); |
| 5142 return -1; | |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5143 } |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5144 |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5145 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
|
5146 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
|
5147 while (p < end) { |
| 51216 | 5148 register Py_UNICODE ch = *p; |
| 5149 int decimal; | |
| 5150 PyObject *repunicode; | |
| 5151 Py_ssize_t repsize; | |
| 5152 Py_ssize_t newpos; | |
| 5153 Py_UNICODE *uni2; | |
| 5154 Py_UNICODE *collstart; | |
| 5155 Py_UNICODE *collend; | |
| 5156 | |
| 5157 if (Py_UNICODE_ISSPACE(ch)) { | |
| 5158 *output++ = ' '; | |
| 5159 ++p; | |
| 5160 continue; | |
| 5161 } | |
| 5162 decimal = Py_UNICODE_TODECIMAL(ch); | |
| 5163 if (decimal >= 0) { | |
| 5164 *output++ = '0' + decimal; | |
| 5165 ++p; | |
| 5166 continue; | |
| 5167 } | |
| 5168 if (0 < ch && ch < 256) { | |
| 5169 *output++ = (char)ch; | |
| 5170 ++p; | |
| 5171 continue; | |
| 5172 } | |
| 5173 /* All other characters are considered unencodable */ | |
| 5174 collstart = p; | |
|
73682
0cd197f13400
Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73554
diff
changeset
|
5175 for (collend = p+1; collend < end; collend++) { |
| 51216 | 5176 if ((0 < *collend && *collend < 256) || |
|
73682
0cd197f13400
Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73554
diff
changeset
|
5177 Py_UNICODE_ISSPACE(*collend) || |
|
0cd197f13400
Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73554
diff
changeset
|
5178 0 <= Py_UNICODE_TODECIMAL(*collend)) |
| 51216 | 5179 break; |
| 5180 } | |
| 5181 /* cache callback name lookup | |
| 5182 * (if not done yet, i.e. it's the first error) */ | |
| 5183 if (known_errorHandler==-1) { | |
| 5184 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 5185 known_errorHandler = 1; | |
| 5186 else if (!strcmp(errors, "replace")) | |
| 5187 known_errorHandler = 2; | |
| 5188 else if (!strcmp(errors, "ignore")) | |
| 5189 known_errorHandler = 3; | |
| 5190 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 5191 known_errorHandler = 4; | |
| 5192 else | |
| 5193 known_errorHandler = 0; | |
| 5194 } | |
| 5195 switch (known_errorHandler) { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5196 case 1: /* strict */ |
| 51216 | 5197 raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5198 goto onError; |
| 51216 | 5199 case 2: /* replace */ |
| 5200 for (p = collstart; p < collend; ++p) | |
| 5201 *output++ = '?'; | |
| 5202 /* fall through */ | |
| 5203 case 3: /* ignore */ | |
| 5204 p = collend; | |
| 5205 break; | |
| 5206 case 4: /* xmlcharrefreplace */ | |
| 5207 /* generate replacement (temporarily (mis)uses p) */ | |
| 5208 for (p = collstart; p < collend; ++p) | |
| 5209 output += sprintf(output, "&#%d;", (int)*p); | |
| 5210 p = collend; | |
| 5211 break; | |
| 5212 default: | |
| 5213 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | |
| 5214 encoding, reason, s, length, &exc, | |
| 5215 collstart-s, collend-s, &newpos); | |
| 5216 if (repunicode == NULL) | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5217 goto onError; |
| 51216 | 5218 /* generate replacement */ |
| 5219 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 5220 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | |
| 5221 Py_UNICODE ch = *uni2; | |
| 5222 if (Py_UNICODE_ISSPACE(ch)) | |
| 5223 *output++ = ' '; | |
| 5224 else { | |
| 5225 decimal = Py_UNICODE_TODECIMAL(ch); | |
| 5226 if (decimal >= 0) | |
| 5227 *output++ = '0' + decimal; | |
| 5228 else if (0 < ch && ch < 256) | |
| 5229 *output++ = (char)ch; | |
| 5230 else { | |
| 5231 Py_DECREF(repunicode); | |
| 5232 raise_encode_exception(&exc, encoding, | |
| 5233 s, length, collstart-s, collend-s, reason); | |
| 5234 goto onError; | |
| 5235 } | |
| 5236 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5237 } |
| 51216 | 5238 p = s + newpos; |
| 5239 Py_DECREF(repunicode); | |
| 5240 } | |
|
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
|
5241 } |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5242 /* 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
|
5243 *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
|
5244 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
|
5245 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
|
5246 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
|
5247 |
| 51216 | 5248 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
|
5249 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
|
5250 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
|
5251 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
|
5252 } |
|
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
|
5253 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5254 /* --- Helpers ------------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5255 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
5256 #include "stringlib/unicodedefs.h" |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38347
diff
changeset
|
5257 #include "stringlib/fastsearch.h" |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5258 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5259 #include "stringlib/count.h" |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5260 #include "stringlib/find.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5261 #include "stringlib/partition.h" |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5262 #include "stringlib/split.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5263 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5264 /* helper macro to fixup start/end slice values */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5265 #define ADJUST_INDICES(start, end, len) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5266 if (end > len) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5267 end = len; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5268 else if (end < 0) { \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5269 end += len; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5270 if (end < 0) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5271 end = 0; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5272 } \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5273 if (start < 0) { \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5274 start += len; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5275 if (start < 0) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5276 start = 0; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5277 } |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5278 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5279 Py_ssize_t PyUnicode_Count(PyObject *str, |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5280 PyObject *substr, |
|
38347
6d8615f9f1cc
use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents:
38339
diff
changeset
|
5281 Py_ssize_t start, |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5282 Py_ssize_t end) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5283 { |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5284 Py_ssize_t result; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5285 PyUnicodeObject* str_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5286 PyUnicodeObject* sub_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5287 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5288 str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5289 if (!str_obj) |
| 51216 | 5290 return -1; |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5291 sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5292 if (!sub_obj) { |
| 51216 | 5293 Py_DECREF(str_obj); |
| 5294 return -1; | |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5295 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5296 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5297 ADJUST_INDICES(start, end, str_obj->length); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5298 result = stringlib_count( |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5299 str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5300 PY_SSIZE_T_MAX |
|
38275
43b1474b512b
needforspeed: use "fastsearch" for count and findstring helpers. this
Fredrik Lundh <fredrik@pythonware.com>
parents:
38274
diff
changeset
|
5301 ); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
5302 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5303 Py_DECREF(sub_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5304 Py_DECREF(str_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5305 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5306 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5307 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5308 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5309 Py_ssize_t PyUnicode_Find(PyObject *str, |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5310 PyObject *sub, |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5311 Py_ssize_t start, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5312 Py_ssize_t end, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5313 int direction) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5314 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5315 Py_ssize_t result; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5316 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5317 str = PyUnicode_FromObject(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5318 if (!str) |
| 51216 | 5319 return -2; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5320 sub = PyUnicode_FromObject(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5321 if (!sub) { |
| 51216 | 5322 Py_DECREF(str); |
| 5323 return -2; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5324 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5325 |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5326 if (direction > 0) |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5327 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5328 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
|
5329 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
|
5330 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5331 ); |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5332 else |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5333 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5334 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
|
5335 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
|
5336 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5337 ); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5338 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5339 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5340 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5341 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5342 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5343 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5344 |
|
30441
76e998aee644
On c.l.py, 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 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5346 int tailmatch(PyUnicodeObject *self, |
| 51216 | 5347 PyUnicodeObject *substring, |
| 5348 Py_ssize_t start, | |
| 5349 Py_ssize_t end, | |
| 5350 int direction) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5351 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5352 if (substring->length == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5353 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5354 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5355 ADJUST_INDICES(start, end, self->length); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5356 end -= substring->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5357 if (end < start) |
| 51216 | 5358 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5359 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5360 if (direction > 0) { |
| 51216 | 5361 if (Py_UNICODE_MATCH(self, end, substring)) |
| 5362 return 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5363 } else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5364 if (Py_UNICODE_MATCH(self, start, substring)) |
| 51216 | 5365 return 1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5366 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5367 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5368 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5369 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5370 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5371 Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
| 51216 | 5372 PyObject *substr, |
| 5373 Py_ssize_t start, | |
| 5374 Py_ssize_t end, | |
| 5375 int direction) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5376 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5377 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
|
5378 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5379 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
|
5380 if (str == NULL) |
| 51216 | 5381 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5382 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
|
5383 if (substr == NULL) { |
| 51216 | 5384 Py_DECREF(str); |
| 5385 return -1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5386 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5387 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5388 result = tailmatch((PyUnicodeObject *)str, |
| 51216 | 5389 (PyUnicodeObject *)substr, |
| 5390 start, end, direction); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5391 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5392 Py_DECREF(substr); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5393 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5394 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5395 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5396 /* 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
|
5397 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
|
5398 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5399 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5400 PyObject *fixup(PyUnicodeObject *self, |
| 51216 | 5401 int (*fixfct)(PyUnicodeObject *s)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5402 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5403 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5404 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5405 |
|
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
|
5406 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
|
5407 if (u == NULL) |
| 51216 | 5408 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
|
5409 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
5410 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
|
5411 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5412 if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
| 51216 | 5413 /* fixfct should return TRUE if it modified the buffer. If |
| 5414 FALSE, return a reference to the original buffer instead | |
| 5415 (to save space, not time) */ | |
| 5416 Py_INCREF(self); | |
| 5417 Py_DECREF(u); | |
| 5418 return (PyObject*) self; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5419 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5420 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5421 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5422 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5423 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5424 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
|
5425 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5426 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
|
5427 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
|
5428 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
|
5429 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5430 while (len-- > 0) { |
| 51216 | 5431 register Py_UNICODE ch; |
| 5432 | |
| 5433 ch = Py_UNICODE_TOUPPER(*s); | |
| 5434 if (ch != *s) { | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5435 status = 1; |
| 51216 | 5436 *s = ch; |
| 5437 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5438 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5439 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5440 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5441 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5442 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5443 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5444 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5445 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
|
5446 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5447 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
|
5448 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
|
5449 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
|
5450 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5451 while (len-- > 0) { |
| 51216 | 5452 register Py_UNICODE ch; |
| 5453 | |
| 5454 ch = Py_UNICODE_TOLOWER(*s); | |
| 5455 if (ch != *s) { | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5456 status = 1; |
| 51216 | 5457 *s = ch; |
| 5458 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5459 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5460 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5461 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5462 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5463 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5464 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5465 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5466 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
|
5467 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5468 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
|
5469 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
|
5470 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
|
5471 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5472 while (len-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5473 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
|
5474 *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
|
5475 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5476 } 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
|
5477 *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
|
5478 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5479 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5480 s++; |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5483 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5484 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5485 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5486 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5487 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
|
5488 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5489 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
|
5490 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
|
5491 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
|
5492 |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5493 if (len == 0) |
| 51216 | 5494 return 0; |
|
71867
eab17979a586
#12266: Fix str.capitalize() to correctly uppercase/lowercase titlecased and cased non-letter characters.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
71529
diff
changeset
|
5495 if (!Py_UNICODE_ISUPPER(*s)) { |
| 51216 | 5496 *s = Py_UNICODE_TOUPPER(*s); |
| 5497 status = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5498 } |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5499 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5500 while (--len > 0) { |
|
71867
eab17979a586
#12266: Fix str.capitalize() to correctly uppercase/lowercase titlecased and cased non-letter characters.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
71529
diff
changeset
|
5501 if (!Py_UNICODE_ISLOWER(*s)) { |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5502 *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
|
5503 status = 1; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5504 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5505 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5506 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5507 return status; |
|
11968
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5510 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5511 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
|
5512 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5513 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
|
5514 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
|
5515 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
|
5516 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5517 /* 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
|
5518 if (PyUnicode_GET_SIZE(self) == 1) { |
| 51216 | 5519 Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5520 if (*p != ch) { | |
| 5521 *p = ch; | |
| 5522 return 1; | |
| 5523 } | |
| 5524 else | |
| 5525 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5526 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5527 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5528 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
|
5529 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
|
5530 for (; p < e; p++) { |
| 51216 | 5531 register const Py_UNICODE ch = *p; |
| 5532 | |
| 5533 if (previous_is_cased) | |
| 5534 *p = Py_UNICODE_TOLOWER(ch); | |
| 5535 else | |
| 5536 *p = Py_UNICODE_TOTITLE(ch); | |
| 5537 | |
| 5538 if (Py_UNICODE_ISLOWER(ch) || | |
| 5539 Py_UNICODE_ISUPPER(ch) || | |
| 5540 Py_UNICODE_ISTITLE(ch)) | |
| 5541 previous_is_cased = 1; | |
| 5542 else | |
| 5543 previous_is_cased = 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5544 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5545 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5546 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5547 |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5548 PyObject * |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5549 PyUnicode_Join(PyObject *separator, PyObject *seq) |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5550 { |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5551 PyObject *internal_separator = NULL; |
|
33499
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5552 const Py_UNICODE blank = ' '; |
|
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5553 const Py_UNICODE *sep = ␣ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5554 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
|
5555 PyUnicodeObject *res = NULL; /* the result */ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5556 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
|
5557 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
|
5558 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
|
5559 PyObject *fseq; /* PySequence_Fast(seq) */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5560 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
|
5561 PyObject *item; |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5562 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
|
5563 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5564 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
|
5565 if (fseq == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5566 return NULL; |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5567 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5568 |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5569 /* 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
|
5570 * 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
|
5571 * 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
|
5572 * 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
|
5573 * 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
|
5574 * is invariant. |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5575 */ |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5576 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
|
5577 /* 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
|
5578 if (seqlen == 0) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5579 res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5580 goto Done; |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5581 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5582 /* 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
|
5583 if (seqlen == 1) { |
| 51216 | 5584 item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5585 if (PyUnicode_CheckExact(item)) { | |
| 5586 Py_INCREF(item); | |
| 5587 res = (PyUnicodeObject *)item; | |
| 5588 goto Done; | |
| 5589 } | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5590 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5591 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5592 /* 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
|
5593 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
|
5594 /* Set up sep and seplen -- they're needed. */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5595 if (separator == NULL) { |
| 51216 | 5596 sep = ␣ |
| 5597 seplen = 1; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5598 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5599 else { |
| 51216 | 5600 internal_separator = PyUnicode_FromObject(separator); |
| 5601 if (internal_separator == NULL) | |
| 5602 goto onError; | |
| 5603 sep = PyUnicode_AS_UNICODE(internal_separator); | |
| 5604 seplen = PyUnicode_GET_SIZE(internal_separator); | |
| 5605 /* In case PyUnicode_FromObject() mutated seq. */ | |
| 5606 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
|
5607 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5608 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5609 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5610 /* Get space. */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5611 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
|
5612 if (res == NULL) |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5613 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
|
5614 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
|
5615 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
|
5616 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5617 for (i = 0; i < seqlen; ++i) { |
| 51216 | 5618 Py_ssize_t itemlen; |
| 5619 Py_ssize_t new_res_used; | |
| 5620 | |
| 5621 item = PySequence_Fast_GET_ITEM(fseq, i); | |
| 5622 /* Convert item to Unicode. */ | |
| 5623 if (! PyUnicode_Check(item) && ! PyString_Check(item)) { | |
| 5624 PyErr_Format(PyExc_TypeError, | |
| 5625 "sequence item %zd: expected string or Unicode," | |
| 5626 " %.80s found", | |
| 5627 i, Py_TYPE(item)->tp_name); | |
| 5628 goto onError; | |
| 5629 } | |
| 5630 item = PyUnicode_FromObject(item); | |
| 5631 if (item == NULL) | |
| 5632 goto onError; | |
| 5633 /* We own a reference to item from here on. */ | |
| 5634 | |
| 5635 /* In case PyUnicode_FromObject() mutated seq. */ | |
| 5636 seqlen = PySequence_Fast_GET_SIZE(fseq); | |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5637 |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5638 /* Make sure we have enough space for the separator and the item. */ |
| 51216 | 5639 itemlen = PyUnicode_GET_SIZE(item); |
| 5640 new_res_used = res_used + itemlen; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5641 if (new_res_used < 0) |
| 51216 | 5642 goto Overflow; |
| 5643 if (i < seqlen - 1) { | |
| 5644 new_res_used += seplen; | |
| 5645 if (new_res_used < 0) | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5646 goto Overflow; |
| 51216 | 5647 } |
| 5648 if (new_res_used > res_alloc) { | |
| 5649 /* double allocated size until it's big enough */ | |
| 5650 do { | |
| 5651 res_alloc += res_alloc; | |
| 5652 if (res_alloc <= 0) | |
| 5653 goto Overflow; | |
| 5654 } while (new_res_used > res_alloc); | |
| 5655 if (_PyUnicode_Resize(&res, res_alloc) < 0) { | |
| 5656 Py_DECREF(item); | |
| 5657 goto onError; | |
| 5658 } | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5659 res_p = PyUnicode_AS_UNICODE(res) + res_used; |
| 51216 | 5660 } |
| 5661 | |
| 5662 /* Copy item, and maybe the separator. */ | |
| 5663 Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); | |
| 5664 res_p += itemlen; | |
| 5665 if (i < seqlen - 1) { | |
| 5666 Py_UNICODE_COPY(res_p, sep, seplen); | |
| 5667 res_p += seplen; | |
| 5668 } | |
| 5669 Py_DECREF(item); | |
| 5670 res_used = new_res_used; | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5671 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5672 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5673 /* 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
|
5674 * 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
|
5675 */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5676 if (_PyUnicode_Resize(&res, res_used) < 0) |
| 51216 | 5677 goto onError; |
| 5678 | |
| 5679 Done: | |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5680 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
|
5681 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
|
5682 return (PyObject *)res; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5683 |
| 51216 | 5684 Overflow: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5685 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
|
5686 "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
|
5687 Py_DECREF(item); |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5688 /* fall through */ |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5689 |
| 51216 | 5690 onError: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5691 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
|
5692 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
|
5693 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
|
5694 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5695 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5696 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5697 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
|
5698 PyUnicodeObject *pad(PyUnicodeObject *self, |
| 51216 | 5699 Py_ssize_t left, |
| 5700 Py_ssize_t right, | |
| 5701 Py_UNICODE fill) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5702 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5703 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5704 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5705 if (left < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5706 left = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5707 if (right < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5708 right = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5709 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5710 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
|
5711 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5712 return self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5713 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5714 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5715 if (left > PY_SSIZE_T_MAX - self->length || |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
5716 right > PY_SSIZE_T_MAX - (left + self->length)) { |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5717 PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5718 return NULL; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5719 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5720 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
|
5721 if (u) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5722 if (left) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5723 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
|
5724 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
|
5725 if (right) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5726 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
|
5727 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5728 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5729 return u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5730 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5731 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5732 PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5733 { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5734 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5735 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5736 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
|
5737 if (string == NULL) |
| 51216 | 5738 return NULL; |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5739 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5740 list = stringlib_splitlines( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5741 (PyObject*) string, PyUnicode_AS_UNICODE(string), |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5742 PyUnicode_GET_SIZE(string), keepends); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5743 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5744 Py_DECREF(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5745 return list; |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5746 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5747 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5748 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5749 PyObject *split(PyUnicodeObject *self, |
| 51216 | 5750 PyUnicodeObject *substring, |
| 5751 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
|
5752 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5753 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5754 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
|
5755 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5756 if (substring == NULL) |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5757 return stringlib_split_whitespace( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5758 (PyObject*) self, self->str, self->length, maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5759 ); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5760 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5761 return stringlib_split( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5762 (PyObject*) self, self->str, self->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5763 substring->str, substring->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5764 maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5765 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5766 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5767 |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5768 static |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5769 PyObject *rsplit(PyUnicodeObject *self, |
| 51216 | 5770 PyUnicodeObject *substring, |
| 5771 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
|
5772 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5773 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5774 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
|
5775 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5776 if (substring == NULL) |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5777 return stringlib_rsplit_whitespace( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5778 (PyObject*) self, self->str, self->length, maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5779 ); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5780 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5781 return stringlib_rsplit( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5782 (PyObject*) self, self->str, self->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5783 substring->str, substring->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5784 maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5785 ); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5786 } |
|
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 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5789 PyObject *replace(PyUnicodeObject *self, |
| 51216 | 5790 PyUnicodeObject *str1, |
| 5791 PyUnicodeObject *str2, | |
| 5792 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
|
5793 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5794 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5795 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5796 if (maxcount < 0) |
| 51216 | 5797 maxcount = PY_SSIZE_T_MAX; |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5798 else if (maxcount == 0 || self->length == 0) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5799 goto nothing; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5800 |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5801 if (str1->length == str2->length) { |
|
58290
d51358645e60
Fix Windows build (re r77461)
Antoine Pitrou <solipsis@pitrou.net>
parents:
58288
diff
changeset
|
5802 Py_ssize_t i; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5803 /* same length */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5804 if (str1->length == 0) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5805 goto nothing; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5806 if (str1->length == 1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5807 /* replace characters */ |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5808 Py_UNICODE u1, u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5809 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
|
5810 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5811 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
|
5812 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5813 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5814 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
|
5815 u1 = str1->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5816 u2 = str2->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5817 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
|
5818 if (u->str[i] == u1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5819 if (--maxcount < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5820 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5821 u->str[i] = u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5822 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5823 } else { |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5824 i = stringlib_find( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5825 self->str, self->length, str1->str, str1->length, 0 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5826 ); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5827 if (i < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5828 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5829 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
|
5830 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5831 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5832 Py_UNICODE_COPY(u->str, self->str, self->length); |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5833 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5834 /* change everything in-place, starting with this one */ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5835 Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5836 i += str1->length; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5837 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5838 while ( --maxcount > 0) { |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5839 i = stringlib_find(self->str+i, self->length-i, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5840 str1->str, str1->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5841 i); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5842 if (i == -1) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5843 break; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5844 Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5845 i += str1->length; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5846 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5847 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5848 } else { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5849 |
|
61204
55a593cc3280
Remove an unneeded variable and assignment.
Brett Cannon <bcannon@gmail.com>
parents:
60203
diff
changeset
|
5850 Py_ssize_t n, i, j; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5851 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
|
5852 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5853 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5854 /* replace strings */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5855 n = stringlib_count(self->str, self->length, str1->str, str1->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5856 maxcount); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5857 if (n == 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5858 goto nothing; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5859 /* 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
|
5860 delta = (str2->length - str1->length); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5861 if (delta == 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5862 new_size = self->length; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5863 } else { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5864 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
|
5865 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
|
5866 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5867 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5868 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5869 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5870 new_size = self->length + product; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5871 if (new_size < 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5872 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5873 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5874 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5875 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5876 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5877 u = _PyUnicode_New(new_size); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5878 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5879 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5880 i = 0; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5881 p = u->str; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5882 if (str1->length > 0) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5883 while (n-- > 0) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5884 /* look for next match */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5885 j = stringlib_find(self->str+i, self->length-i, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5886 str1->str, str1->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5887 i); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5888 if (j == -1) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5889 break; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5890 else if (j > i) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5891 /* copy unchanged part [i:j] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5892 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
|
5893 p += j - i; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5894 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5895 /* copy substitution string */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5896 if (str2->length > 0) { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5897 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
|
5898 p += str2->length; |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5899 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5900 i = j + str1->length; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5901 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5902 if (i < self->length) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5903 /* copy tail [i:] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5904 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
|
5905 } else { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5906 /* interleave */ |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5907 while (n > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5908 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
|
5909 p += str2->length; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5910 if (--n <= 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5911 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5912 *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
|
5913 } |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5914 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
|
5915 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5916 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5917 return (PyObject *) u; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5918 |
| 51216 | 5919 nothing: |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5920 /* 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
|
5921 if (PyUnicode_CheckExact(self)) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5922 Py_INCREF(self); |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5923 return (PyObject *) self; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5924 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5925 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
|
5926 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5927 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5928 /* --- Unicode Object Methods --------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5929 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
5930 PyDoc_STRVAR(title__doc__, |
| 51216 | 5931 "S.title() -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5932 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5933 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
|
5934 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
|
5935 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5936 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
|
5937 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
|
5938 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5939 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
|
5940 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5941 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
5942 PyDoc_STRVAR(capitalize__doc__, |
| 51216 | 5943 "S.capitalize() -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5944 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5945 Return a capitalized version of S, i.e. make the first character\n\ |
|
62803
e84d51644144
Merged revisions 82573 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
62518
diff
changeset
|
5946 have upper case and the rest lower case."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5947 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5948 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
|
5949 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
|
5950 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5951 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
|
5952 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5953 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5954 #if 0 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
5955 PyDoc_STRVAR(capwords__doc__, |
| 51216 | 5956 "S.capwords() -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5957 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5958 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
|
5959 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
|
5960 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5961 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
|
5962 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
|
5963 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5964 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5965 PyObject *item; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5966 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
|
5967 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5968 /* Split into words */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5969 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
|
5970 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5971 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5972 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5973 /* Capitalize each word */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5974 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
|
5975 item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 51216 | 5976 fixcapitalize); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5977 if (item == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5978 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5979 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
|
5980 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
|
5981 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5982 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5983 /* 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
|
5984 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
|
5985 |
| 51216 | 5986 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5987 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5988 return (PyObject *)item; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5989 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5990 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5991 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
5992 /* 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
|
5993 |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
5994 static int |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
5995 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
|
5996 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5997 Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5998 PyObject *uniobj; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5999 Py_UNICODE *unistr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6000 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6001 uniobj = PyUnicode_FromObject(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6002 if (uniobj == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6003 PyErr_SetString(PyExc_TypeError, |
| 51216 | 6004 "The fill character cannot be converted to Unicode"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6005 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6006 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6007 if (PyUnicode_GET_SIZE(uniobj) != 1) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6008 PyErr_SetString(PyExc_TypeError, |
| 51216 | 6009 "The fill character must be exactly one character long"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6010 Py_DECREF(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6011 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6012 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6013 unistr = PyUnicode_AS_UNICODE(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6014 *fillcharloc = unistr[0]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6015 Py_DECREF(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6016 return 1; |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6017 } |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
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(center__doc__, |
| 51216 | 6020 "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
|
6021 \n\ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6022 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
|
6023 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
|
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 * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6026 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
|
6027 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6028 Py_ssize_t marg, left; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6029 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
|
6030 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6031 |
|
36532
0c096be27000
Use correct PyArg_Parse format char for Py_ssize_t in unicode.center().
Thomas Wouters <thomas@python.org>
parents:
36519
diff
changeset
|
6032 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
|
6033 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6034 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
6035 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
|
6036 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6037 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6038 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6039 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6040 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
|
6041 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
|
6042 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6043 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
|
6044 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6045 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6046 #if 0 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6047 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6048 /* 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
|
6049 module. The basic comparison should compare ordinals on a naive |
|
56812
a1b0936b027c
Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56034
diff
changeset
|
6050 basis (this is what Java does and thus Jython too). */ |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6051 |
| 13337 | 6052 /* speedy UTF-16 code point order comparison */ |
| 6053 /* gleaned from: */ | |
| 6054 /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | |
| 6055 | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
6056 static short utf16Fixup[32] = |
| 13337 | 6057 { |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
6058 0, 0, 0, 0, 0, 0, 0, 0, |
| 13337 | 6059 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
|
6060 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
|
6061 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
| 13337 | 6062 }; |
| 6063 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6064 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6065 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
|
6066 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6067 Py_ssize_t len1, len2; |
| 13337 | 6068 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6069 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
|
6070 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
|
6071 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6072 len1 = str1->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6073 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
|
6074 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6075 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
|
6076 Py_UNICODE c1, c2; |
| 13337 | 6077 |
| 6078 c1 = *s1++; | |
| 6079 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
|
6080 |
| 51216 | 6081 if (c1 > (1<<11) * 26) |
| 6082 c1 += utf16Fixup[c1>>11]; | |
| 6083 if (c2 > (1<<11) * 26) | |
| 13337 | 6084 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
|
6085 /* 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
|
6086 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6087 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
|
6088 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
|
6089 |
| 13337 | 6090 len1--; len2--; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6091 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6092 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6093 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
|
6094 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6095 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6096 #else |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6097 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6098 static int |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6099 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
|
6100 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6101 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
|
6102 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6103 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
|
6104 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
|
6105 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6106 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
|
6107 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
|
6108 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6109 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
|
6110 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
|
6111 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6112 c1 = *s1++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6113 c2 = *s2++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6114 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6115 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
|
6116 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
|
6117 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6118 len1--; len2--; |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6119 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6120 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6121 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
|
6122 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6123 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6124 #endif |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6125 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6126 int PyUnicode_Compare(PyObject *left, |
| 51216 | 6127 PyObject *right) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6128 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6129 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
|
6130 int result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6131 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6132 /* 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
|
6133 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
|
6134 if (u == NULL) |
| 51216 | 6135 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6136 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
|
6137 if (v == NULL) |
| 51216 | 6138 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6139 |
|
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
|
6140 /* 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
|
6141 if (v == u) { |
| 51216 | 6142 Py_DECREF(u); |
| 6143 Py_DECREF(v); | |
| 6144 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6145 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6146 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6147 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
|
6148 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6149 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6150 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6151 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6152 |
| 51216 | 6153 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6154 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6155 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6156 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6157 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6158 |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6159 PyObject *PyUnicode_RichCompare(PyObject *left, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6160 PyObject *right, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6161 int op) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6162 { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6163 int result; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6164 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6165 result = PyUnicode_Compare(left, right); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6166 if (result == -1 && PyErr_Occurred()) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6167 goto onError; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6168 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6169 /* Convert the return value to a Boolean */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6170 switch (op) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6171 case Py_EQ: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6172 result = (result == 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6173 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6174 case Py_NE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6175 result = (result != 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6176 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6177 case Py_LE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6178 result = (result <= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6179 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6180 case Py_GE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6181 result = (result >= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6182 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6183 case Py_LT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6184 result = (result == -1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6185 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6186 case Py_GT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6187 result = (result == 1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6188 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6189 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6190 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6191 |
| 51216 | 6192 onError: |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6193 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6194 /* Standard case |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6195 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6196 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
|
6197 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
|
6198 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
|
6199 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
|
6200 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
|
6201 chance. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6202 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6203 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6204 if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6205 PyErr_Clear(); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6206 Py_INCREF(Py_NotImplemented); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6207 return Py_NotImplemented; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6208 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6209 if (op != Py_EQ && op != Py_NE) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6210 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6211 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6212 /* Equality comparison. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6213 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6214 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
|
6215 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
|
6216 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6217 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6218 if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6219 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6220 PyErr_Clear(); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6221 if (PyErr_Warn(PyExc_UnicodeWarning, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6222 (op == Py_EQ) ? |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6223 "Unicode equal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6224 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6225 "interpreting them as being unequal" : |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6226 "Unicode unequal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6227 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6228 "interpreting them as being unequal" |
| 51216 | 6229 ) < 0) |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6230 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6231 result = (op == Py_NE); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6232 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6233 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6234 |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6235 int PyUnicode_Contains(PyObject *container, |
| 51216 | 6236 PyObject *element) |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6237 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6238 PyObject *str, *sub; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6239 int result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6240 |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6241 /* Coerce the two arguments */ |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6242 sub = PyUnicode_FromObject(element); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6243 if (!sub) { |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6244 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6245 } |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6246 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6247 str = PyUnicode_FromObject(container); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6248 if (!str) { |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6249 Py_DECREF(sub); |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6250 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6251 } |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6252 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6253 result = stringlib_contains_obj(str, sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6254 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6255 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6256 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6257 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6258 return result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6259 } |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6260 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6261 /* 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
|
6262 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6263 PyObject *PyUnicode_Concat(PyObject *left, |
| 51216 | 6264 PyObject *right) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6265 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6266 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
|
6267 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6268 /* 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
|
6269 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
|
6270 if (u == NULL) |
| 51216 | 6271 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6272 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
|
6273 if (v == NULL) |
| 51216 | 6274 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6275 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6276 /* Shortcuts */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6277 if (v == unicode_empty) { |
| 51216 | 6278 Py_DECREF(v); |
| 6279 return (PyObject *)u; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6280 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6281 if (u == unicode_empty) { |
| 51216 | 6282 Py_DECREF(u); |
| 6283 return (PyObject *)v; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6284 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6285 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6286 /* 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
|
6287 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
|
6288 if (w == NULL) |
| 51216 | 6289 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6290 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
|
6291 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
|
6292 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6293 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6294 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6295 return (PyObject *)w; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6296 |
| 51216 | 6297 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6298 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6299 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6300 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6301 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6302 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6303 PyDoc_STRVAR(count__doc__, |
| 51216 | 6304 "S.count(sub[, start[, end]]) -> int\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6305 \n\ |
|
38236
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38182
diff
changeset
|
6306 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
|
6307 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
|
6308 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
|
6309 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6310 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6311 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
|
6312 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6313 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6314 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
|
6315 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
|
6316 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6317 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6318 if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6319 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6320 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6321 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
6322 ADJUST_INDICES(start, end, self->length); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6323 result = PyInt_FromSsize_t( |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6324 stringlib_count(self->str + start, end - start, |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
6325 substring->str, substring->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
6326 PY_SSIZE_T_MAX) |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6327 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6328 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6329 Py_DECREF(substring); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6330 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6331 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6332 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6333 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6334 PyDoc_STRVAR(encode__doc__, |
| 51216 | 6335 "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
|
6336 \n\ |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6337 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
|
6338 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
|
6339 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
|
6340 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
|
6341 '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
|
6342 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
|
6343 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6344 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6345 unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6346 { |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6347 static char *kwlist[] = {"encoding", "errors", 0}; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6348 char *encoding = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6349 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6350 PyObject *v; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6351 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6352 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6353 kwlist, &encoding, &errors)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6354 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6355 v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6356 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6357 goto onError; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6358 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
|
6359 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6360 "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
|
6361 "(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
|
6362 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
|
6363 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6364 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6365 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6366 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6367 |
| 51216 | 6368 onError: |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6369 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6370 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6371 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6372 PyDoc_STRVAR(decode__doc__, |
| 51216 | 6373 "S.decode([encoding[,errors]]) -> string or unicode\n\ |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6374 \n\ |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6375 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
|
6376 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
|
6377 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
|
6378 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 75052 | 6379 as well as any other name registered with codecs.register_error that is\n\ |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6380 able to handle UnicodeDecodeErrors."); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6381 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6382 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6383 unicode_decode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6384 { |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6385 static char *kwlist[] = {"encoding", "errors", 0}; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6386 char *encoding = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6387 char *errors = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6388 PyObject *v; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6389 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6390 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6391 kwlist, &encoding, &errors)) |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6392 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6393 v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6394 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6395 goto onError; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6396 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
|
6397 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6398 "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
|
6399 "(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
|
6400 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
|
6401 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6402 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6403 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6404 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6405 |
| 51216 | 6406 onError: |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6407 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6408 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6409 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6410 PyDoc_STRVAR(expandtabs__doc__, |
| 51216 | 6411 "S.expandtabs([tabsize]) -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6412 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6413 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
|
6414 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
|
6415 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6416 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6417 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
|
6418 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6419 Py_UNICODE *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6420 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6421 Py_UNICODE *q; |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6422 Py_UNICODE *qe; |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6423 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
|
6424 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6425 int tabsize = 8; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6426 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6427 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 51216 | 6428 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6429 |
|
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
|
6430 /* First pass: determine size of output string */ |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6431 i = 0; /* chars up to and including most recent \n or \r */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6432 j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6433 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
|
6434 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
|
6435 if (*p == '\t') { |
| 51216 | 6436 if (tabsize > 0) { |
| 6437 incr = tabsize - (j % tabsize); /* cannot overflow */ | |
| 6438 if (j > PY_SSIZE_T_MAX - incr) | |
| 6439 goto overflow1; | |
| 6440 j += incr; | |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6441 } |
| 51216 | 6442 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6443 else { |
| 51216 | 6444 if (j > PY_SSIZE_T_MAX - 1) |
| 6445 goto overflow1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6446 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6447 if (*p == '\n' || *p == '\r') { |
| 51216 | 6448 if (i > PY_SSIZE_T_MAX - j) |
| 6449 goto overflow1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6450 i += j; |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6451 j = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6452 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6453 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6454 |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6455 if (i > PY_SSIZE_T_MAX - j) |
| 51216 | 6456 goto overflow1; |
|
42005
2bd9417dacd5
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
39994
diff
changeset
|
6457 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6458 /* 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
|
6459 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
|
6460 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6461 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6462 |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6463 j = 0; /* same as in first pass */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6464 q = u->str; /* next output char */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6465 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
|
6466 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6467 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
|
6468 if (*p == '\t') { |
| 51216 | 6469 if (tabsize > 0) { |
| 6470 i = tabsize - (j % tabsize); | |
| 6471 j += i; | |
| 6472 while (i--) { | |
| 6473 if (q >= qe) | |
| 6474 goto overflow2; | |
| 6475 *q++ = ' '; | |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6476 } |
| 51216 | 6477 } |
| 6478 } | |
| 6479 else { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6480 if (q >= qe) |
| 51216 | 6481 goto overflow2; |
| 6482 *q++ = *p; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6483 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6484 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
|
6485 j = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6486 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6487 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6488 return (PyObject*) u; |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6489 |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6490 overflow2: |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6491 Py_DECREF(u); |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6492 overflow1: |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6493 PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6494 return NULL; |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6497 PyDoc_STRVAR(find__doc__, |
| 51216 | 6498 "S.find(sub [,start [,end]]) -> int\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6499 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6500 Return the lowest index in S where substring sub is found,\n\ |
|
71529
e266415cf42c
merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents:
69562
diff
changeset
|
6501 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
|
6502 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
|
6503 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6504 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
|
6505 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6506 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6507 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
|
6508 { |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6509 PyUnicodeObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6510 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
|
6511 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6512 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
|
6513 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6514 if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6515 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6516 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6517 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6518 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6519 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
|
6520 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
|
6521 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6522 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6523 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6524 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6525 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6526 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
|
6527 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6528 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6529 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6530 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
|
6531 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6532 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
|
6533 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
|
6534 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6535 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6536 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6537 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
|
6538 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6539 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6540 static long |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6541 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
|
6542 { |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6543 /* 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
|
6544 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
|
6545 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
|
6546 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
|
6547 dictionary keys. */ |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6548 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6549 register Py_ssize_t len; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6550 register Py_UNICODE *p; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6551 register long x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6552 |
|
76189
a11a2bbd8241
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
75135
diff
changeset
|
6553 #ifdef Py_DEBUG |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75100
diff
changeset
|
6554 assert(_Py_HashSecret_Initialized); |
|
76189
a11a2bbd8241
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
75135
diff
changeset
|
6555 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6556 if (self->hash != -1) |
| 51216 | 6557 return self->hash; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6558 len = PyUnicode_GET_SIZE(self); |
|
75100
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6559 /* |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6560 We make the hash of the empty string be 0, rather than using |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6561 (prefix ^ suffix), since this slightly obfuscates the hash secret |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6562 */ |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6563 if (len == 0) { |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6564 self->hash = 0; |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6565 return 0; |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6566 } |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6567 p = PyUnicode_AS_UNICODE(self); |
|
75100
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6568 x = _Py_HashSecret.prefix; |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6569 x ^= *p << 7; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6570 while (--len >= 0) |
| 51216 | 6571 x = (1000003*x) ^ *p++; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6572 x ^= PyUnicode_GET_SIZE(self); |
|
75100
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
68255
diff
changeset
|
6573 x ^= _Py_HashSecret.suffix; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6574 if (x == -1) |
| 51216 | 6575 x = -2; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6576 self->hash = x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6577 return x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6578 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6579 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6580 PyDoc_STRVAR(index__doc__, |
| 51216 | 6581 "S.index(sub [,start [,end]]) -> int\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6582 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6583 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
|
6584 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6585 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6586 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
|
6587 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6588 Py_ssize_t result; |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6589 PyUnicodeObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
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; |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6592 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6593 if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6594 &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 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6605 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6606 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
|
6607 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6608 } |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6609 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6610 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
|
6611 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6612 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6613 PyDoc_STRVAR(islower__doc__, |
| 51216 | 6614 "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
|
6615 \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
|
6616 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
|
6617 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
|
6618 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6619 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
|
6620 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
|
6621 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6622 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
|
6623 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
|
6624 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6625 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6626 /* 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
|
6627 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6628 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
|
6629 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6630 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6631 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6632 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6633 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6634 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
|
6635 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6636 for (; p < e; p++) { |
| 51216 | 6637 register const Py_UNICODE ch = *p; |
| 6638 | |
| 6639 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | |
| 6640 return PyBool_FromLong(0); | |
| 6641 else if (!cased && Py_UNICODE_ISLOWER(ch)) | |
| 6642 cased = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6643 } |
|
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
|
6644 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
|
6645 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6646 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6647 PyDoc_STRVAR(isupper__doc__, |
| 51216 | 6648 "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
|
6649 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6650 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
|
6651 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
|
6652 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6653 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
|
6654 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
|
6655 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6656 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
|
6657 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
|
6658 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6659 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6660 /* 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
|
6661 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6662 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
|
6663 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6664 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6665 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6666 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6667 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6668 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
|
6669 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6670 for (; p < e; p++) { |
| 51216 | 6671 register const Py_UNICODE ch = *p; |
| 6672 | |
| 6673 if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | |
| 6674 return PyBool_FromLong(0); | |
| 6675 else if (!cased && Py_UNICODE_ISUPPER(ch)) | |
| 6676 cased = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6677 } |
|
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
|
6678 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
|
6679 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6680 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6681 PyDoc_STRVAR(istitle__doc__, |
| 51216 | 6682 "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
|
6683 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6684 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
|
6685 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
|
6686 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
|
6687 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
|
6688 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6689 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
|
6690 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
|
6691 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6692 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
|
6693 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
|
6694 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
|
6695 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6696 /* 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
|
6697 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6698 return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6699 (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
|
6700 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6701 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6702 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6703 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6704 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6705 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
|
6706 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6707 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
|
6708 for (; p < e; p++) { |
| 51216 | 6709 register const Py_UNICODE ch = *p; |
| 6710 | |
| 6711 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | |
| 6712 if (previous_is_cased) | |
| 6713 return PyBool_FromLong(0); | |
| 6714 previous_is_cased = 1; | |
| 6715 cased = 1; | |
| 6716 } | |
| 6717 else if (Py_UNICODE_ISLOWER(ch)) { | |
| 6718 if (!previous_is_cased) | |
| 6719 return PyBool_FromLong(0); | |
| 6720 previous_is_cased = 1; | |
| 6721 cased = 1; | |
| 6722 } | |
| 6723 else | |
| 6724 previous_is_cased = 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6725 } |
|
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
|
6726 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
|
6727 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6728 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6729 PyDoc_STRVAR(isspace__doc__, |
| 51216 | 6730 "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
|
6731 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6732 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
|
6733 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
|
6734 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6735 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
|
6736 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
|
6737 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6738 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
|
6739 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
|
6740 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6741 /* 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
|
6742 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6743 Py_UNICODE_ISSPACE(*p)) |
| 6744 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
|
6745 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6746 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6747 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6748 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6749 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6750 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
|
6751 for (; p < e; p++) { |
| 51216 | 6752 if (!Py_UNICODE_ISSPACE(*p)) |
| 6753 return PyBool_FromLong(0); | |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6754 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6755 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
|
6756 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6757 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6758 PyDoc_STRVAR(isalpha__doc__, |
| 51216 | 6759 "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
|
6760 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6761 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
|
6762 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
|
6763 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6764 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
|
6765 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
|
6766 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6767 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
|
6768 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
|
6769 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6770 /* 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
|
6771 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6772 Py_UNICODE_ISALPHA(*p)) |
| 6773 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
|
6774 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6775 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6776 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6777 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
|
6778 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6779 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
|
6780 for (; p < e; p++) { |
| 51216 | 6781 if (!Py_UNICODE_ISALPHA(*p)) |
| 6782 return PyBool_FromLong(0); | |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6783 } |
|
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(1); |
|
13356
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6785 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6786 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6787 PyDoc_STRVAR(isalnum__doc__, |
| 51216 | 6788 "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
|
6789 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6790 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
|
6791 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
|
6792 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6793 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
|
6794 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
|
6795 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6796 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
|
6797 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
|
6798 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6799 /* 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
|
6800 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6801 Py_UNICODE_ISALNUM(*p)) |
| 6802 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
|
6803 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6804 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6805 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6806 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
|
6807 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6808 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
|
6809 for (; p < e; p++) { |
| 51216 | 6810 if (!Py_UNICODE_ISALNUM(*p)) |
| 6811 return PyBool_FromLong(0); | |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6812 } |
|
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(1); |
|
13356
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6814 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6815 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6816 PyDoc_STRVAR(isdecimal__doc__, |
| 51216 | 6817 "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
|
6818 \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
|
6819 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
|
6820 False otherwise."); |
|
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 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
|
6823 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
|
6824 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6825 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
|
6826 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
|
6827 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6828 /* 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
|
6829 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6830 Py_UNICODE_ISDECIMAL(*p)) |
| 6831 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
|
6832 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6833 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6834 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6835 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6836 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6837 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
|
6838 for (; p < e; p++) { |
| 51216 | 6839 if (!Py_UNICODE_ISDECIMAL(*p)) |
| 6840 return PyBool_FromLong(0); | |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6841 } |
|
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(1); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6843 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6844 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6845 PyDoc_STRVAR(isdigit__doc__, |
| 51216 | 6846 "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
|
6847 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6848 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
|
6849 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
|
6850 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6851 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
|
6852 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
|
6853 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6854 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
|
6855 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
|
6856 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6857 /* 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
|
6858 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6859 Py_UNICODE_ISDIGIT(*p)) |
| 6860 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
|
6861 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6862 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6863 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6864 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6865 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6866 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
|
6867 for (; p < e; p++) { |
| 51216 | 6868 if (!Py_UNICODE_ISDIGIT(*p)) |
| 6869 return PyBool_FromLong(0); | |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6870 } |
|
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(1); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6872 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6873 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6874 PyDoc_STRVAR(isnumeric__doc__, |
| 51216 | 6875 "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
|
6876 \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
|
6877 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
|
6878 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6879 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6880 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
|
6881 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
|
6882 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6883 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
|
6884 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
|
6885 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6886 /* 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
|
6887 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6888 Py_UNICODE_ISNUMERIC(*p)) |
| 6889 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
|
6890 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6891 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6892 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6893 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6894 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6895 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
|
6896 for (; p < e; p++) { |
| 51216 | 6897 if (!Py_UNICODE_ISNUMERIC(*p)) |
| 6898 return PyBool_FromLong(0); | |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6899 } |
|
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(1); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6901 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6902 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6903 PyDoc_STRVAR(join__doc__, |
|
56824
b7b8970cd5a5
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56812
diff
changeset
|
6904 "S.join(iterable) -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6905 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6906 Return a string which is the concatenation of the strings in the\n\ |
|
56824
b7b8970cd5a5
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56812
diff
changeset
|
6907 iterable. The separator between elements is S."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6908 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6909 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
|
6910 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
|
6911 { |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
6912 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
|
6913 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6914 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6915 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
|
6916 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
|
6917 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6918 return self->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6919 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6920 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6921 PyDoc_STRVAR(ljust__doc__, |
| 51216 | 6922 "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
|
6923 \n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49386
diff
changeset
|
6924 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
|
6925 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
|
6926 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6927 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6928 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
|
6929 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
6930 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
|
6931 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6932 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
6933 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
|
6934 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6935 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
6936 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
|
6937 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6938 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6939 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6940 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6941 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
|
6942 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6943 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6944 PyDoc_STRVAR(lower__doc__, |
| 51216 | 6945 "S.lower() -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6946 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6947 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
|
6948 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6949 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
|
6950 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
|
6951 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6952 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
|
6953 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6954 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6955 #define LEFTSTRIP 0 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6956 #define RIGHTSTRIP 1 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6957 #define BOTHSTRIP 2 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6958 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6959 /* Arrays indexed by above */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6960 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
|
6961 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6962 #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
|
6963 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6964 /* 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
|
6965 PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6966 _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
|
6967 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6968 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6969 Py_ssize_t len = PyUnicode_GET_SIZE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6970 Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6971 Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6972 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
|
6973 |
| 51216 | 6974 BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
6975 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6976 i = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6977 if (striptype != RIGHTSTRIP) { |
| 51216 | 6978 while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6979 i++; | |
| 6980 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6981 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6982 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6983 j = len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6984 if (striptype != LEFTSTRIP) { |
| 51216 | 6985 do { |
| 6986 j--; | |
| 6987 } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); | |
| 6988 j++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6989 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6990 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6991 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 51216 | 6992 Py_INCREF(self); |
| 6993 return (PyObject*)self; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6994 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6995 else |
| 51216 | 6996 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
|
6997 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6998 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6999 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7000 static PyObject * |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7001 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
|
7002 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7003 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7004 Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7005 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7006 i = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7007 if (striptype != RIGHTSTRIP) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7008 while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7009 i++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7010 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7011 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7012 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7013 j = len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7014 if (striptype != LEFTSTRIP) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7015 do { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7016 j--; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7017 } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7018 j++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7019 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7020 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7021 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7022 Py_INCREF(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7023 return (PyObject*)self; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7024 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7025 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7026 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
|
7027 } |
|
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 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7030 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7031 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
|
7032 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7033 PyObject *sep = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7034 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7035 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7036 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7037 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7038 if (sep != NULL && sep != Py_None) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7039 if (PyUnicode_Check(sep)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7040 return _PyUnicode_XStrip(self, striptype, sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7041 else if (PyString_Check(sep)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7042 PyObject *res; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7043 sep = PyUnicode_FromObject(sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7044 if (sep==NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7045 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7046 res = _PyUnicode_XStrip(self, striptype, sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7047 Py_DECREF(sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7048 return res; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7049 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7050 else { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7051 PyErr_Format(PyExc_TypeError, |
| 51216 | 7052 "%s arg must be None, unicode or str", |
| 7053 STRIPNAME(striptype)); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7054 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7055 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7056 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7057 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7058 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
|
7059 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7060 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7061 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7062 PyDoc_STRVAR(strip__doc__, |
| 51216 | 7063 "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
|
7064 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7065 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
|
7066 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
|
7067 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
|
7068 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
|
7069 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7070 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7071 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
|
7072 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7073 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7074 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7075 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7076 return do_argstrip(self, BOTHSTRIP, args); |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7077 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7078 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7079 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7080 PyDoc_STRVAR(lstrip__doc__, |
| 51216 | 7081 "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
|
7082 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7083 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
|
7084 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
|
7085 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
|
7086 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7087 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7088 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
|
7089 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7090 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7091 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7092 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7093 return do_argstrip(self, LEFTSTRIP, args); |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7094 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7095 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7096 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7097 PyDoc_STRVAR(rstrip__doc__, |
| 51216 | 7098 "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
|
7099 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7100 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
|
7101 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
|
7102 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
|
7103 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7104 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7105 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
|
7106 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7107 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7108 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7109 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7110 return do_argstrip(self, RIGHTSTRIP, args); |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7111 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7112 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7113 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7114 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7115 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
|
7116 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7117 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7118 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7119 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
|
7120 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
|
7121 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7122 if (len < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7123 len = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7124 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7125 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
|
7126 /* 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
|
7127 Py_INCREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7128 return (PyObject*) str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7129 } |
|
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
|
7130 |
|
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
|
7131 /* 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
|
7132 * 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
|
7133 */ |
|
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
|
7134 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
|
7135 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
|
7136 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
|
7137 "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
|
7138 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
|
7139 } |
|
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
|
7140 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
|
7141 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
|
7142 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
|
7143 "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
|
7144 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
|
7145 } |
|
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
|
7146 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
|
7147 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7148 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7149 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7150 p = u->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7151 |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7152 if (str->length == 1 && len > 0) { |
|
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7153 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
|
7154 } else { |
| 51216 | 7155 Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7156 if (done < nchars) { | |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7157 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
|
7158 done = str->length; |
| 51216 | 7159 } |
| 7160 while (done < nchars) { | |
|
46062
09adff3d6691
Try to fix a bunch of compiler warnings on Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46059
diff
changeset
|
7161 Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
|
38239
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7162 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
|
7163 done += n; |
| 51216 | 7164 } |
|
38239
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7165 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7166 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7167 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7168 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7169 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7170 PyObject *PyUnicode_Replace(PyObject *obj, |
| 51216 | 7171 PyObject *subobj, |
| 7172 PyObject *replobj, | |
| 7173 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
|
7174 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7175 PyObject *self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7176 PyObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7177 PyObject *str2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7178 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7179 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7180 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
|
7181 if (self == NULL) |
| 51216 | 7182 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7183 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
|
7184 if (str1 == NULL) { |
| 51216 | 7185 Py_DECREF(self); |
| 7186 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7187 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7188 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
|
7189 if (str2 == NULL) { |
| 51216 | 7190 Py_DECREF(self); |
| 7191 Py_DECREF(str1); | |
| 7192 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7193 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
7194 result = replace((PyUnicodeObject *)self, |
| 51216 | 7195 (PyUnicodeObject *)str1, |
| 7196 (PyUnicodeObject *)str2, | |
| 7197 maxcount); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7198 Py_DECREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7199 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7200 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7201 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7202 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7203 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7204 PyDoc_STRVAR(replace__doc__, |
|
62520
014e7c00dc28
Merged revisions 82248 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62257
diff
changeset
|
7205 "S.replace(old, new[, count]) -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7206 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7207 Return a copy of S with all occurrences of substring\n\ |
|
47388
d6639cd9575f
#2999: fix name of third parameter in unicode.replace()'s docstring.
Georg Brandl <georg@python.org>
parents:
47294
diff
changeset
|
7208 old replaced by new. If the optional argument count is\n\ |
|
d6639cd9575f
#2999: fix name of third parameter in unicode.replace()'s docstring.
Georg Brandl <georg@python.org>
parents:
47294
diff
changeset
|
7209 given, only the first count occurrences are replaced."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7210 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7211 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7212 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
|
7213 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7214 PyUnicodeObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7215 PyUnicodeObject *str2; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7216 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
|
7217 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7218 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7219 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
|
7220 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7221 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
|
7222 if (str1 == NULL) |
| 51216 | 7223 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7224 str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7225 if (str2 == NULL) { |
| 51216 | 7226 Py_DECREF(str1); |
| 7227 return NULL; | |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7228 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7229 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7230 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
|
7231 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7232 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7233 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7234 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7235 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7236 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7237 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7238 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
|
7239 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7240 return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 7241 PyUnicode_GET_SIZE(unicode), |
| 7242 1); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7243 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7244 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7245 PyDoc_STRVAR(rfind__doc__, |
| 51216 | 7246 "S.rfind(sub [,start [,end]]) -> int\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7247 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7248 Return the highest index in S where substring sub is found,\n\ |
|
71529
e266415cf42c
merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents:
69562
diff
changeset
|
7249 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
|
7250 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
|
7251 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7252 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
|
7253 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7254 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7255 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
|
7256 { |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7257 PyUnicodeObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7258 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
|
7259 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7260 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
|
7261 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7262 if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7263 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7264 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7265 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7266 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7267 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
|
7268 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
|
7269 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7270 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7271 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7272 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7273 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7274 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
|
7275 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7276 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7277 PyDoc_STRVAR(rindex__doc__, |
| 51216 | 7278 "S.rindex(sub [,start [,end]]) -> int\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7279 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7280 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
|
7281 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7282 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7283 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
|
7284 { |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7285 PyUnicodeObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7286 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
|
7287 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7288 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
|
7289 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7290 if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7291 &start, &end)) |
|
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; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7293 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7294 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7295 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
|
7296 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
|
7297 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7298 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7299 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7300 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7301 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7302 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7303 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
|
7304 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7305 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7306 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
|
7307 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7308 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7309 PyDoc_STRVAR(rjust__doc__, |
| 51216 | 7310 "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
|
7311 \n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49386
diff
changeset
|
7312 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
|
7313 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
|
7314 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7315 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7316 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
|
7317 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7318 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
|
7319 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7320 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7321 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
|
7322 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7323 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7324 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
|
7325 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7326 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7327 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7328 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7329 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
|
7330 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7331 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7332 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7333 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
|
7334 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7335 /* standard clamping */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7336 if (start < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7337 start = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7338 if (end < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7339 end = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7340 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
|
7341 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
|
7342 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
|
7343 /* 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
|
7344 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7345 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7346 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7347 if (start > end) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7348 start = end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7349 /* copy slice */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7350 return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 51216 | 7351 end - start); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7352 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7353 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7354 PyObject *PyUnicode_Split(PyObject *s, |
| 51216 | 7355 PyObject *sep, |
| 7356 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
|
7357 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7358 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
|
7359 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7360 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
|
7361 if (s == NULL) |
| 51216 | 7362 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7363 if (sep != NULL) { |
| 51216 | 7364 sep = PyUnicode_FromObject(sep); |
| 7365 if (sep == NULL) { | |
| 7366 Py_DECREF(s); | |
| 7367 return NULL; | |
| 7368 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7369 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7370 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7371 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
|
7372 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7373 Py_DECREF(s); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7374 Py_XDECREF(sep); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7375 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7376 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7377 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7378 PyDoc_STRVAR(split__doc__, |
| 51216 | 7379 "S.split([sep [,maxsplit]]) -> list of strings\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7380 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7381 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
|
7382 delimiter string. If maxsplit is given, at most maxsplit\n\ |
|
46871
877783333eae
#2809: elaborate str.split docstring a bit.
Georg Brandl <georg@python.org>
parents:
46391
diff
changeset
|
7383 splits are done. If sep is not specified or is None, any\n\ |
|
46908
ed2c1888e143
#2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents:
46871
diff
changeset
|
7384 whitespace string is a separator and empty strings are\n\ |
|
ed2c1888e143
#2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents:
46871
diff
changeset
|
7385 removed from the result."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7386 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7387 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7388 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
|
7389 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7390 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7391 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7392 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7393 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
|
7394 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7395 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7396 if (substring == Py_None) |
| 51216 | 7397 return split(self, NULL, maxcount); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7398 else if (PyUnicode_Check(substring)) |
| 51216 | 7399 return split(self, (PyUnicodeObject *)substring, maxcount); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7400 else |
| 51216 | 7401 return PyUnicode_Split((PyObject *)self, substring, maxcount); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7402 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7403 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7404 PyObject * |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7405 PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7406 { |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7407 PyObject* str_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7408 PyObject* sep_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7409 PyObject* out; |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7410 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7411 str_obj = PyUnicode_FromObject(str_in); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7412 if (!str_obj) |
| 51216 | 7413 return NULL; |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7414 sep_obj = PyUnicode_FromObject(sep_in); |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7415 if (!sep_obj) { |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7416 Py_DECREF(str_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7417 return NULL; |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7418 } |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7419 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7420 out = stringlib_partition( |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7421 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
|
7422 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
|
7423 ); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7424 |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7425 Py_DECREF(sep_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7426 Py_DECREF(str_obj); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7427 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7428 return out; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7429 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7430 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7431 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7432 PyObject * |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7433 PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7434 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7435 PyObject* str_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7436 PyObject* sep_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7437 PyObject* out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7438 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7439 str_obj = PyUnicode_FromObject(str_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7440 if (!str_obj) |
| 51216 | 7441 return NULL; |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7442 sep_obj = PyUnicode_FromObject(sep_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7443 if (!sep_obj) { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7444 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7445 return NULL; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7446 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7447 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7448 out = stringlib_rpartition( |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7449 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
|
7450 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
|
7451 ); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7452 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7453 Py_DECREF(sep_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7454 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7455 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7456 return out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7457 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7458 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7459 PyDoc_STRVAR(partition__doc__, |
| 51216 | 7460 "S.partition(sep) -> (head, sep, tail)\n\ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7461 \n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49386
diff
changeset
|
7462 Search for the separator sep in S, and return the part before it,\n\ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7463 the separator itself, and the part after it. If the separator is not\n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49386
diff
changeset
|
7464 found, return S and two empty strings."); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7465 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7466 static PyObject* |
|
38338
958f91684209
needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents:
38335
diff
changeset
|
7467 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
|
7468 { |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7469 return PyUnicode_Partition((PyObject *)self, separator); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7470 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7471 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7472 PyDoc_STRVAR(rpartition__doc__, |
|
58488
ff66324f52a1
Merged revisions 77743 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56824
diff
changeset
|
7473 "S.rpartition(sep) -> (head, sep, tail)\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7474 \n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49386
diff
changeset
|
7475 Search for the separator sep in S, starting at the end of S, and return\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7476 the part before it, the separator itself, and the part after it. If the\n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49386
diff
changeset
|
7477 separator is not found, return two empty strings and S."); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7478 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7479 static PyObject* |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7480 unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7481 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7482 return PyUnicode_RPartition((PyObject *)self, separator); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7483 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7484 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7485 PyObject *PyUnicode_RSplit(PyObject *s, |
| 51216 | 7486 PyObject *sep, |
| 7487 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
|
7488 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7489 PyObject *result; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7490 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7491 s = PyUnicode_FromObject(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7492 if (s == NULL) |
| 51216 | 7493 return NULL; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7494 if (sep != NULL) { |
| 51216 | 7495 sep = PyUnicode_FromObject(sep); |
| 7496 if (sep == NULL) { | |
| 7497 Py_DECREF(s); | |
| 7498 return NULL; | |
| 7499 } | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7500 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7501 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7502 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
|
7503 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7504 Py_DECREF(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7505 Py_XDECREF(sep); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7506 return result; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7507 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7508 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7509 PyDoc_STRVAR(rsplit__doc__, |
| 51216 | 7510 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7511 \n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7512 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
|
7513 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
|
7514 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
|
7515 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
|
7516 is a separator."); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7517 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7518 static PyObject* |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7519 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
|
7520 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7521 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7522 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7523 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7524 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
|
7525 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7526 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7527 if (substring == Py_None) |
| 51216 | 7528 return rsplit(self, NULL, maxcount); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7529 else if (PyUnicode_Check(substring)) |
| 51216 | 7530 return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7531 else |
| 51216 | 7532 return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7533 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7534 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7535 PyDoc_STRVAR(splitlines__doc__, |
|
77292
2fbee0b741f7
Improve tooltips for splitlines() by showing that the default for keepends is False.
Raymond Hettinger <python@rcn.com>
parents:
76190
diff
changeset
|
7536 "S.splitlines(keepends=False) -> 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
|
7537 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7538 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12369 | 7539 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
|
7540 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
|
7541 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7542 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7543 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
|
7544 { |
| 12369 | 7545 int keepends = 0; |
| 7546 | |
| 7547 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
|
7548 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7549 |
| 12369 | 7550 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
|
7551 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7552 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7553 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7554 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
|
7555 { |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
7556 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
|
7557 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7558 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7559 PyDoc_STRVAR(swapcase__doc__, |
| 51216 | 7560 "S.swapcase() -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7561 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7562 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
|
7563 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
|
7564 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7565 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
|
7566 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
|
7567 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7568 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
|
7569 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7570 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7571 PyDoc_STRVAR(translate__doc__, |
| 51216 | 7572 "S.translate(table) -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7573 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7574 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
|
7575 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
|
7576 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
|
7577 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
|
7578 are deleted."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7579 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7580 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
|
7581 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
|
7582 { |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
7583 return PyUnicode_TranslateCharmap(self->str, |
| 51216 | 7584 self->length, |
| 7585 table, | |
| 7586 "ignore"); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7587 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7588 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7589 PyDoc_STRVAR(upper__doc__, |
| 51216 | 7590 "S.upper() -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7591 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7592 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
|
7593 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7594 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
|
7595 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
|
7596 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7597 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
|
7598 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7599 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7600 PyDoc_STRVAR(zfill__doc__, |
| 51216 | 7601 "S.zfill(width) -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7602 \n\ |
|
49115
6f08278bf8b1
Fix varname in docstring. #3822.
Georg Brandl <georg@python.org>
parents:
48117
diff
changeset
|
7603 Pad a numeric string S with zeros on the left, to fill a field\n\ |
|
6f08278bf8b1
Fix varname in docstring. #3822.
Georg Brandl <georg@python.org>
parents:
48117
diff
changeset
|
7604 of the specified width. The string S is never truncated."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7605 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7606 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7607 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
|
7608 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7609 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
|
7610 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7611 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7612 Py_ssize_t width; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7613 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
|
7614 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7615 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7616 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
|
7617 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
|
7618 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
|
7619 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
|
7620 } |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23045
diff
changeset
|
7621 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
|
7622 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
|
7623 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
|
7624 PyUnicode_GET_SIZE(self) |
| 51216 | 7625 ); |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7628 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
|
7629 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7630 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
|
7631 |
|
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
|
7632 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
|
7633 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
|
7634 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7635 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
|
7636 /* 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
|
7637 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
|
7638 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
|
7639 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7640 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7641 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7642 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7643 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7644 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7645 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
|
7646 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
|
7647 { |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
7648 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
|
7649 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7650 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7651 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7652 PyDoc_STRVAR(startswith__doc__, |
| 51216 | 7653 "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
|
7654 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7655 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
|
7656 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
|
7657 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
|
7658 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
|
7659 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7660 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7661 unicode_startswith(PyUnicodeObject *self, |
| 51216 | 7662 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7663 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7664 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7665 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7666 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
|
7667 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
|
7668 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
|
7669 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7670 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
| 51216 | 7671 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
|
7672 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
|
7673 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
|
7674 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
|
7675 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 51216 | 7676 PyTuple_GET_ITEM(subobj, i)); |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7677 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
|
7678 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
|
7679 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
|
7680 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
|
7681 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
|
7682 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
|
7683 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7684 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7685 /* 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
|
7686 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
|
7687 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7688 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7689 if (substring == NULL) { |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7690 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7691 PyErr_Format(PyExc_TypeError, "startswith first arg must be str, " |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7692 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); |
| 51216 | 7693 return NULL; |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7694 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7695 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
|
7696 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
|
7697 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
|
7698 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7699 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7700 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7701 PyDoc_STRVAR(endswith__doc__, |
| 51216 | 7702 "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
|
7703 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7704 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
|
7705 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
|
7706 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
|
7707 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
|
7708 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7709 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7710 unicode_endswith(PyUnicodeObject *self, |
| 51216 | 7711 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7712 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7713 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7714 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7715 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
|
7716 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
|
7717 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
|
7718 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7719 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
| 51216 | 7720 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
|
7721 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
|
7722 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
|
7723 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
|
7724 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 51216 | 7725 PyTuple_GET_ITEM(subobj, i)); |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7726 if (substring == NULL) |
| 51216 | 7727 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
|
7728 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
|
7729 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
|
7730 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
|
7731 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
|
7732 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7733 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7734 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
|
7735 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7736 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7737 if (substring == NULL) { |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7738 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7739 PyErr_Format(PyExc_TypeError, "endswith first arg must be str, " |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7740 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); |
| 51216 | 7741 return NULL; |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7742 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7743 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
|
7744 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
|
7745 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
|
7746 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7747 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7748 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7749 /* 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
|
7750 #include "stringlib/string_format.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7751 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7752 PyDoc_STRVAR(format__doc__, |
| 51216 | 7753 "S.format(*args, **kwargs) -> unicode\n\ |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7754 \n\ |
|
66044
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63468
diff
changeset
|
7755 Return a formatted version of S, using substitutions from args and kwargs.\n\ |
|
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63468
diff
changeset
|
7756 The substitutions are identified by braces ('{' and '}')."); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7757 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7758 static PyObject * |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7759 unicode__format__(PyObject *self, PyObject *args) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7760 { |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7761 PyObject *format_spec; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7762 PyObject *result = NULL; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7763 PyObject *tmp = NULL; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7764 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7765 /* If 2.x, convert format_spec to the same type as value */ |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7766 /* This is to allow things like u''.format('') */ |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7767 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7768 goto done; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7769 if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7770 PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
| 51216 | 7771 "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7772 goto done; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7773 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7774 tmp = PyObject_Unicode(format_spec); |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7775 if (tmp == NULL) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7776 goto done; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7777 format_spec = tmp; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7778 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7779 result = _PyUnicode_FormatAdvanced(self, |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7780 PyUnicode_AS_UNICODE(format_spec), |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7781 PyUnicode_GET_SIZE(format_spec)); |
| 51216 | 7782 done: |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7783 Py_XDECREF(tmp); |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7784 return result; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7785 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7786 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7787 PyDoc_STRVAR(p_format__doc__, |
| 51216 | 7788 "S.__format__(format_spec) -> unicode\n\ |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7789 \n\ |
|
66044
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63468
diff
changeset
|
7790 Return a formatted version of S as described by format_spec."); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7791 |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7792 static PyObject * |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7793 unicode__sizeof__(PyUnicodeObject *v) |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7794 { |
|
47922
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47543
diff
changeset
|
7795 return PyInt_FromSsize_t(sizeof(PyUnicodeObject) + |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47543
diff
changeset
|
7796 sizeof(Py_UNICODE) * (v->length + 1)); |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7797 } |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7798 |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7799 PyDoc_STRVAR(sizeof__doc__, |
| 51216 | 7800 "S.__sizeof__() -> size of S in memory, in bytes\n\ |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7801 \n\ |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7802 "); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7803 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7804 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7805 unicode_getnewargs(PyUnicodeObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7806 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7807 return Py_BuildValue("(u#)", v->str, v->length); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7808 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7809 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7810 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7811 static PyMethodDef unicode_methods[] = { |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
7812 {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
7813 {"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
|
7814 {"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
|
7815 {"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
|
7816 {"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
|
7817 {"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
|
7818 {"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
|
7819 {"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
|
7820 {"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
|
7821 {"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
|
7822 {"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
|
7823 {"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
|
7824 {"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
|
7825 {"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
|
7826 {"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
|
7827 {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
7828 {"decode", (PyCFunction) unicode_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
7829 /* {"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
|
7830 {"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
|
7831 {"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
|
7832 {"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
|
7833 {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7834 {"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
|
7835 {"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
|
7836 {"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
|
7837 {"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
|
7838 {"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
|
7839 {"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
|
7840 {"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
|
7841 {"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
|
7842 {"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
|
7843 {"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
|
7844 {"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
|
7845 {"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
|
7846 {"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
|
7847 {"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
|
7848 {"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
|
7849 {"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
|
7850 {"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
|
7851 {"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
|
7852 {"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
|
7853 {"__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
|
7854 {"_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
|
7855 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7856 {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7857 #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
|
7858 {"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
|
7859 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7860 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7861 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7862 /* 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
|
7863 {"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
|
7864 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7865 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7866 {"__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
|
7867 {NULL, NULL} |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7868 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7869 |
|
26274
958d07085dcf
Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
7870 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
|
7871 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
|
7872 { |
| 51216 | 7873 if (!PyUnicode_Check(v)) { |
| 7874 Py_INCREF(Py_NotImplemented); | |
| 7875 return Py_NotImplemented; | |
| 7876 } | |
| 7877 return PyUnicode_Format(v, w); | |
|
26274
958d07085dcf
Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
7878 } |
|
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
|
7879 |
|
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
|
7880 static PyNumberMethods unicode_as_number = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7881 0, /*nb_add*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7882 0, /*nb_subtract*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7883 0, /*nb_multiply*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7884 0, /*nb_divide*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7885 unicode_mod, /*nb_remainder*/ |
|
26274
958d07085dcf
Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
7886 }; |
|
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
|
7887 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7888 static PySequenceMethods unicode_as_sequence = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7889 (lenfunc) unicode_length, /* sq_length */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7890 PyUnicode_Concat, /* sq_concat */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7891 (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7892 (ssizeargfunc) unicode_getitem, /* sq_item */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7893 (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7894 0, /* sq_ass_item */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7895 0, /* sq_ass_slice */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7896 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
|
7897 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7898 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7899 static PyObject* |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7900 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
|
7901 { |
|
39491
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
7902 if (PyIndex_Check(item)) { |
|
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
7903 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
|
7904 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7905 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7906 if (i < 0) |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7907 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
|
7908 return unicode_getitem(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7909 } else if (PySlice_Check(item)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7910 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
|
7911 Py_UNICODE* source_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7912 Py_UNICODE* result_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7913 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7914 |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7915 if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
| 51216 | 7916 &start, &stop, &step, &slicelength) < 0) { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7917 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7918 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7919 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7920 if (slicelength <= 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7921 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
|
7922 } 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
|
7923 PyUnicode_CheckExact(self)) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7924 Py_INCREF(self); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7925 return (PyObject *)self; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7926 } else if (step == 1) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7927 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
|
7928 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7929 source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
| 45826 | 7930 result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 7931 sizeof(Py_UNICODE)); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7932 |
| 51216 | 7933 if (result_buf == NULL) |
| 7934 return PyErr_NoMemory(); | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7935 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7936 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
|
7937 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
|
7938 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
7939 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7940 result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 45826 | 7941 PyObject_FREE(result_buf); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7942 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7943 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7944 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7945 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
|
7946 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7947 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7948 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7949 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7950 static PyMappingMethods unicode_as_mapping = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7951 (lenfunc)unicode_length, /* mp_length */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7952 (binaryfunc)unicode_subscript, /* mp_subscript */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7953 (objobjargproc)0, /* mp_ass_subscript */ |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7954 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7955 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7956 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
|
7957 unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 51216 | 7958 Py_ssize_t index, |
| 7959 const void **ptr) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7960 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7961 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7962 PyErr_SetString(PyExc_SystemError, |
| 51216 | 7963 "accessing non-existent unicode segment"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7964 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7965 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7966 *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
|
7967 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
|
7968 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7969 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7970 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7971 unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
| 51216 | 7972 const void **ptr) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7973 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7974 PyErr_SetString(PyExc_TypeError, |
| 51216 | 7975 "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
|
7976 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7977 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7978 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7979 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7980 unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 51216 | 7981 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
|
7982 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7983 if (lenp) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7984 *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
|
7985 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7986 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7987 |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
7988 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
|
7989 unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 51216 | 7990 Py_ssize_t index, |
| 7991 const void **ptr) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7992 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7993 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
|
7994 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7995 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7996 PyErr_SetString(PyExc_SystemError, |
| 51216 | 7997 "accessing non-existent unicode segment"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7998 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7999 } |
|
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
|
8000 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
|
8001 if (str == NULL) |
| 51216 | 8002 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8003 *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
|
8004 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
|
8005 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8006 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8007 /* 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
|
8008 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8009 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8010 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
|
8011 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8012 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
|
8013 if (argidx < arglen) { |
| 51216 | 8014 (*p_argidx)++; |
| 8015 if (arglen < 0) | |
| 8016 return args; | |
| 8017 else | |
| 8018 return PyTuple_GetItem(args, argidx); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8019 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8020 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8021 "not enough arguments for format string"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8022 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8023 } |
|
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 #define F_LJUST (1<<0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8026 #define F_SIGN (1<<1) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8027 #define F_BLANK (1<<2) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8028 #define F_ALT (1<<3) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8029 #define F_ZERO (1<<4) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8030 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8031 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
|
8032 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
|
8033 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8034 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8035 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
|
8036 for (i = len - 1; i >= 0; i--) |
| 51216 | 8037 buffer[i] = (Py_UNICODE) charbuffer[i]; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8038 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8039 return len; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8040 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8041 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8042 static int |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8043 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
|
8044 { |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8045 Py_ssize_t result; |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8046 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8047 PyOS_snprintf((char *)buffer, len, format, x); |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8048 result = strtounicode(buffer, (char *)buffer); |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8049 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
|
8050 } |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8051 |
|
24736
ff05eef3a6d1
Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents:
24703
diff
changeset
|
8052 /* 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
|
8053 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
|
8054 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
|
8055 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8056 /* Returns a new reference to a PyUnicode object, or NULL on failure. */ |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8057 |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8058 static PyObject * |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8059 formatfloat(PyObject *v, int flags, int prec, int type) |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8060 { |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8061 char *p; |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8062 PyObject *result; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8063 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
|
8064 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8065 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
|
8066 if (x == -1.0 && PyErr_Occurred()) |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8067 return NULL; |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8068 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8069 if (prec < 0) |
| 51216 | 8070 prec = 6; |
|
52410
f05b6d9adbe6
Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
8071 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8072 p = PyOS_double_to_string(x, type, prec, |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8073 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8074 if (p == NULL) |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8075 return NULL; |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8076 result = PyUnicode_FromStringAndSize(p, strlen(p)); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8077 PyMem_Free(p); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8078 return result; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8079 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8080 |
|
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
|
8081 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
|
8082 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
|
8083 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8084 char *buf; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8085 int i, len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8086 PyObject *str; /* temporary string object. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8087 PyUnicodeObject *result; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8088 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8089 str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8090 if (!str) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8091 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8092 result = _PyUnicode_New(len); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8093 if (!result) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8094 Py_DECREF(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8095 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8096 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8097 for (i = 0; i < len; i++) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8098 result->str[i] = buf[i]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8099 result->str[len] = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8100 Py_DECREF(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8101 return (PyObject*)result; |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14492
diff
changeset
|
8102 } |
|
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
|
8103 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8104 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8105 formatint(Py_UNICODE *buf, |
| 51216 | 8106 size_t buflen, |
| 8107 int flags, | |
| 8108 int prec, | |
| 8109 int type, | |
| 8110 PyObject *v) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8111 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8112 /* 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
|
8113 * 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
|
8114 * + 1 + 1 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8115 * = 24 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8116 */ |
|
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
|
8117 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
|
8118 char *sign; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8119 long x; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8120 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8121 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
|
8122 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
|
8123 return -1; |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8124 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
|
8125 type = 'd'; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8126 } |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8127 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
|
8128 sign = "-"; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8129 else |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8130 sign = ""; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8131 if (prec < 0) |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8132 prec = 1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8133 |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8134 /* 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
|
8135 * 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
|
8136 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8137 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
|
8138 PyErr_SetString(PyExc_OverflowError, |
| 51216 | 8139 "formatted integer is too long (precision too large?)"); |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8140 return -1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8141 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8142 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8143 if ((flags & F_ALT) && |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8144 (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
|
8145 /* 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
|
8146 * 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
|
8147 * - 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
|
8148 * 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
|
8149 * %#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
|
8150 * hex() function |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8151 * - 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
|
8152 * 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
|
8153 * (Metrowerks, Compaq Tru64) |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8154 * - 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
|
8155 * 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
|
8156 * 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
|
8157 * |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8158 * 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
|
8159 * 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
|
8160 * of %#x/%#X. |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8161 * |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8162 * 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
|
8163 * 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
|
8164 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8165 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
|
8166 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
|
8167 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8168 else { |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8169 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
|
8170 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
|
8171 prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8172 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8173 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
|
8174 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
|
8175 else |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8176 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
|
8177 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8178 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8179 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8180 formatchar(Py_UNICODE *buf, |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8181 size_t buflen, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8182 PyObject *v) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8183 { |
|
59138
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8184 PyObject *unistr; |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8185 char *str; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8186 /* 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
|
8187 if (PyUnicode_Check(v)) { |
| 51216 | 8188 if (PyUnicode_GET_SIZE(v) != 1) |
| 8189 goto onError; | |
| 8190 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
|
8191 } |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8192 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8193 else if (PyString_Check(v)) { |
| 51216 | 8194 if (PyString_GET_SIZE(v) != 1) |
| 8195 goto onError; | |
|
59138
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8196 /* #7649: "u'%c' % char" should behave like "u'%s' % char" and fail |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8197 with a UnicodeDecodeError if 'char' is not decodable with the |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8198 default encoding (usually ASCII, but it might be something else) */ |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8199 str = PyString_AS_STRING(v); |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8200 if ((unsigned char)str[0] > 0x7F) { |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8201 /* the char is not ASCII; try to decode the string using the |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8202 default encoding and return -1 to let the UnicodeDecodeError |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8203 be raised if the string can't be decoded */ |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8204 unistr = PyUnicode_Decode(str, 1, NULL, "strict"); |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8205 if (unistr == NULL) |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8206 return -1; |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8207 buf[0] = PyUnicode_AS_UNICODE(unistr)[0]; |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8208 Py_DECREF(unistr); |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8209 } |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8210 else |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8211 buf[0] = (Py_UNICODE)str[0]; |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8212 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8213 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8214 else { |
| 51216 | 8215 /* Integer input truncated to a character */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8216 long x; |
| 51216 | 8217 x = PyInt_AsLong(v); |
| 8218 if (x == -1 && PyErr_Occurred()) | |
| 8219 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
|
8220 #ifdef Py_UNICODE_WIDE |
| 51216 | 8221 if (x < 0 || x > 0x10ffff) { |
| 8222 PyErr_SetString(PyExc_OverflowError, | |
| 8223 "%c arg not in range(0x110000) " | |
| 8224 "(wide Python build)"); | |
| 8225 return -1; | |
| 8226 } | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8227 #else |
| 51216 | 8228 if (x < 0 || x > 0xffff) { |
| 8229 PyErr_SetString(PyExc_OverflowError, | |
| 8230 "%c arg not in range(0x10000) " | |
| 8231 "(narrow Python build)"); | |
| 8232 return -1; | |
| 8233 } | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8234 #endif |
| 51216 | 8235 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
|
8236 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8237 buf[1] = '\0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8238 return 1; |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8239 |
| 51216 | 8240 onError: |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8241 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8242 "%c requires int or char"); |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8243 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8244 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8245 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8246 /* 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
|
8247 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8248 FORMATBUFLEN is the length of the buffer in which the ints & |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8249 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
|
8250 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
|
8251 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
|
8252 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
|
8253 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8254 #define FORMATBUFLEN (size_t)120 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8255 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8256 PyObject *PyUnicode_Format(PyObject *format, |
| 51216 | 8257 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8258 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8259 Py_UNICODE *fmt, *res; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8260 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
|
8261 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
|
8262 PyUnicodeObject *result = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8263 PyObject *dict = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8264 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
|
8265 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8266 if (format == NULL || args == NULL) { |
| 51216 | 8267 PyErr_BadInternalCall(); |
| 8268 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8269 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8270 uformat = PyUnicode_FromObject(format); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8271 if (uformat == NULL) |
| 51216 | 8272 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8273 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
|
8274 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
|
8275 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8276 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
|
8277 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
|
8278 if (result == NULL) |
| 51216 | 8279 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8280 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
|
8281 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8282 if (PyTuple_Check(args)) { |
| 51216 | 8283 arglen = PyTuple_Size(args); |
| 8284 argidx = 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8285 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8286 else { |
| 51216 | 8287 arglen = -1; |
| 8288 argidx = -2; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8289 } |
|
78792
2801bf875a24
use the stricter PyMapping_Check (closes #15801)
Benjamin Peterson <benjamin@python.org>
parents:
78208
diff
changeset
|
8290 if (PyMapping_Check(args) && !PyTuple_Check(args) && |
|
26164
63a08639dae2
Fix SF # 635969, No error "not all arguments converted"
Neal Norwitz <nnorwitz@gmail.com>
parents:
25895
diff
changeset
|
8291 !PyObject_TypeCheck(args, &PyBaseString_Type)) |
| 51216 | 8292 dict = args; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8293 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8294 while (--fmtcnt >= 0) { |
| 51216 | 8295 if (*fmt != '%') { |
| 8296 if (--rescnt < 0) { | |
| 8297 rescnt = fmtcnt + 100; | |
| 8298 reslen += rescnt; | |
| 8299 if (_PyUnicode_Resize(&result, reslen) < 0) | |
| 8300 goto onError; | |
| 8301 res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; | |
| 8302 --rescnt; | |
| 8303 } | |
| 8304 *res++ = *fmt++; | |
| 8305 } | |
| 8306 else { | |
| 8307 /* Got a format specifier */ | |
| 8308 int flags = 0; | |
| 8309 Py_ssize_t width = -1; | |
| 8310 int prec = -1; | |
| 8311 Py_UNICODE c = '\0'; | |
| 8312 Py_UNICODE fill; | |
| 8313 int isnumok; | |
| 8314 PyObject *v = NULL; | |
| 8315 PyObject *temp = NULL; | |
| 8316 Py_UNICODE *pbuf; | |
| 8317 Py_UNICODE sign; | |
| 8318 Py_ssize_t len; | |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8319 Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{int,char}() */ |
| 51216 | 8320 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8321 fmt++; |
| 51216 | 8322 if (*fmt == '(') { |
| 8323 Py_UNICODE *keystart; | |
| 8324 Py_ssize_t keylen; | |
| 8325 PyObject *key; | |
| 8326 int pcount = 1; | |
| 8327 | |
| 8328 if (dict == NULL) { | |
| 8329 PyErr_SetString(PyExc_TypeError, | |
| 8330 "format requires a mapping"); | |
| 8331 goto onError; | |
| 8332 } | |
| 8333 ++fmt; | |
| 8334 --fmtcnt; | |
| 8335 keystart = fmt; | |
| 8336 /* Skip over balanced parentheses */ | |
| 8337 while (pcount > 0 && --fmtcnt >= 0) { | |
| 8338 if (*fmt == ')') | |
| 8339 --pcount; | |
| 8340 else if (*fmt == '(') | |
| 8341 ++pcount; | |
| 8342 fmt++; | |
| 8343 } | |
| 8344 keylen = fmt - keystart - 1; | |
| 8345 if (fmtcnt < 0 || pcount > 0) { | |
| 8346 PyErr_SetString(PyExc_ValueError, | |
| 8347 "incomplete format key"); | |
| 8348 goto onError; | |
| 8349 } | |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8350 #if 0 |
| 51216 | 8351 /* keys are converted to strings using UTF-8 and |
| 8352 then looked up since Python uses strings to hold | |
| 8353 variables names etc. in its namespaces and we | |
| 8354 wouldn't want to break common idioms. */ | |
| 8355 key = PyUnicode_EncodeUTF8(keystart, | |
| 8356 keylen, | |
| 8357 NULL); | |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8358 #else |
| 51216 | 8359 key = PyUnicode_FromUnicode(keystart, keylen); |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8360 #endif |
| 51216 | 8361 if (key == NULL) |
| 8362 goto onError; | |
| 8363 if (args_owned) { | |
| 8364 Py_DECREF(args); | |
| 8365 args_owned = 0; | |
| 8366 } | |
| 8367 args = PyObject_GetItem(dict, key); | |
| 8368 Py_DECREF(key); | |
| 8369 if (args == NULL) { | |
| 8370 goto onError; | |
| 8371 } | |
| 8372 args_owned = 1; | |
| 8373 arglen = -1; | |
| 8374 argidx = -2; | |
| 8375 } | |
| 8376 while (--fmtcnt >= 0) { | |
| 8377 switch (c = *fmt++) { | |
| 8378 case '-': flags |= F_LJUST; continue; | |
| 8379 case '+': flags |= F_SIGN; continue; | |
| 8380 case ' ': flags |= F_BLANK; continue; | |
| 8381 case '#': flags |= F_ALT; continue; | |
| 8382 case '0': flags |= F_ZERO; continue; | |
| 8383 } | |
| 8384 break; | |
| 8385 } | |
| 8386 if (c == '*') { | |
| 8387 v = getnextarg(args, arglen, &argidx); | |
| 8388 if (v == NULL) | |
| 8389 goto onError; | |
| 8390 if (!PyInt_Check(v)) { | |
| 8391 PyErr_SetString(PyExc_TypeError, | |
| 8392 "* wants int"); | |
| 8393 goto onError; | |
| 8394 } | |
|
81590
d544873d62e9
Issue #15989: Fix several occurrences of integer overflow
Serhiy Storchaka <storchaka@gmail.com>
parents:
81517
diff
changeset
|
8395 width = PyInt_AsSsize_t(v); |
|
d544873d62e9
Issue #15989: Fix several occurrences of integer overflow
Serhiy Storchaka <storchaka@gmail.com>
parents:
81517
diff
changeset
|
8396 if (width == -1 && PyErr_Occurred()) |
|
d544873d62e9
Issue #15989: Fix several occurrences of integer overflow
Serhiy Storchaka <storchaka@gmail.com>
parents:
81517
diff
changeset
|
8397 goto onError; |
| 51216 | 8398 if (width < 0) { |
| 8399 flags |= F_LJUST; | |
| 8400 width = -width; | |
| 8401 } | |
| 8402 if (--fmtcnt >= 0) | |
| 8403 c = *fmt++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8404 } |
| 51216 | 8405 else if (c >= '0' && c <= '9') { |
| 8406 width = c - '0'; | |
| 8407 while (--fmtcnt >= 0) { | |
| 8408 c = *fmt++; | |
| 8409 if (c < '0' || c > '9') | |
| 8410 break; | |
|
80014
21fb1767e185
Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents:
79590
diff
changeset
|
8411 if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
| 51216 | 8412 PyErr_SetString(PyExc_ValueError, |
| 8413 "width too big"); | |
| 8414 goto onError; | |
| 8415 } | |
| 8416 width = width*10 + (c - '0'); | |
| 8417 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8418 } |
| 51216 | 8419 if (c == '.') { |
| 8420 prec = 0; | |
| 8421 if (--fmtcnt >= 0) | |
| 8422 c = *fmt++; | |
| 8423 if (c == '*') { | |
| 8424 v = getnextarg(args, arglen, &argidx); | |
| 8425 if (v == NULL) | |
| 8426 goto onError; | |
| 8427 if (!PyInt_Check(v)) { | |
| 8428 PyErr_SetString(PyExc_TypeError, | |
| 8429 "* wants int"); | |
| 8430 goto onError; | |
| 8431 } | |
|
81590
d544873d62e9
Issue #15989: Fix several occurrences of integer overflow
Serhiy Storchaka <storchaka@gmail.com>
parents:
81517
diff
changeset
|
8432 prec = _PyInt_AsInt(v); |
|
d544873d62e9
Issue #15989: Fix several occurrences of integer overflow
Serhiy Storchaka <storchaka@gmail.com>
parents:
81517
diff
changeset
|
8433 if (prec == -1 && PyErr_Occurred()) |
|
d544873d62e9
Issue #15989: Fix several occurrences of integer overflow
Serhiy Storchaka <storchaka@gmail.com>
parents:
81517
diff
changeset
|
8434 goto onError; |
| 51216 | 8435 if (prec < 0) |
| 8436 prec = 0; | |
| 8437 if (--fmtcnt >= 0) | |
| 8438 c = *fmt++; | |
| 8439 } | |
| 8440 else if (c >= '0' && c <= '9') { | |
| 8441 prec = c - '0'; | |
| 8442 while (--fmtcnt >= 0) { | |
|
63106
742fdf7f6540
Merged revisions 82980 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents:
62711
diff
changeset
|
8443 c = *fmt++; |
| 51216 | 8444 if (c < '0' || c > '9') |
| 8445 break; | |
|
80014
21fb1767e185
Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents:
79590
diff
changeset
|
8446 if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
| 51216 | 8447 PyErr_SetString(PyExc_ValueError, |
| 8448 "prec too big"); | |
| 8449 goto onError; | |
| 8450 } | |
| 8451 prec = prec*10 + (c - '0'); | |
| 8452 } | |
| 8453 } | |
| 8454 } /* prec */ | |
| 8455 if (fmtcnt >= 0) { | |
| 8456 if (c == 'h' || c == 'l' || c == 'L') { | |
| 8457 if (--fmtcnt >= 0) | |
| 8458 c = *fmt++; | |
| 8459 } | |
| 8460 } | |
| 8461 if (fmtcnt < 0) { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8462 PyErr_SetString(PyExc_ValueError, |
| 51216 | 8463 "incomplete format"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8464 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8465 } |
| 51216 | 8466 if (c != '%') { |
| 8467 v = getnextarg(args, arglen, &argidx); | |
| 8468 if (v == NULL) | |
| 8469 goto onError; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8470 } |
| 51216 | 8471 sign = 0; |
| 8472 fill = ' '; | |
| 8473 switch (c) { | |
| 8474 | |
| 8475 case '%': | |
| 8476 pbuf = formatbuf; | |
| 8477 /* presume that buffer length is at least 1 */ | |
| 8478 pbuf[0] = '%'; | |
| 8479 len = 1; | |
| 8480 break; | |
| 8481 | |
| 8482 case 's': | |
| 8483 case 'r': | |
|
59900
6151d09ce039
Merged revisions 79278,79280 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59138
diff
changeset
|
8484 if (PyUnicode_CheckExact(v) && c == 's') { |
| 51216 | 8485 temp = v; |
| 8486 Py_INCREF(temp); | |
| 8487 } | |
| 8488 else { | |
| 8489 PyObject *unicode; | |
| 8490 if (c == 's') | |
| 8491 temp = PyObject_Unicode(v); | |
| 8492 else | |
| 8493 temp = PyObject_Repr(v); | |
| 8494 if (temp == NULL) | |
| 8495 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
|
8496 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
|
8497 /* 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
|
8498 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
|
8499 /* convert to string to Unicode */ |
| 51216 | 8500 unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8501 PyString_GET_SIZE(temp), | |
| 8502 NULL, | |
| 8503 "strict"); | |
| 8504 Py_DECREF(temp); | |
| 8505 temp = unicode; | |
| 8506 if (temp == NULL) | |
| 8507 goto onError; | |
| 8508 } | |
| 8509 else { | |
| 8510 Py_DECREF(temp); | |
| 8511 PyErr_SetString(PyExc_TypeError, | |
| 8512 "%s argument has non-string str()"); | |
| 8513 goto onError; | |
| 8514 } | |
| 8515 } | |
| 8516 pbuf = PyUnicode_AS_UNICODE(temp); | |
| 8517 len = PyUnicode_GET_SIZE(temp); | |
| 8518 if (prec >= 0 && len > prec) | |
| 8519 len = prec; | |
| 8520 break; | |
| 8521 | |
| 8522 case 'i': | |
| 8523 case 'd': | |
| 8524 case 'u': | |
| 8525 case 'o': | |
| 8526 case 'x': | |
| 8527 case 'X': | |
| 8528 if (c == 'i') | |
| 8529 c = 'd'; | |
| 8530 isnumok = 0; | |
| 8531 if (PyNumber_Check(v)) { | |
| 8532 PyObject *iobj=NULL; | |
| 8533 | |
| 8534 if (PyInt_Check(v) || (PyLong_Check(v))) { | |
| 8535 iobj = v; | |
| 8536 Py_INCREF(iobj); | |
| 8537 } | |
| 8538 else { | |
| 8539 iobj = PyNumber_Int(v); | |
| 8540 if (iobj==NULL) iobj = PyNumber_Long(v); | |
| 8541 } | |
| 8542 if (iobj!=NULL) { | |
| 8543 if (PyInt_Check(iobj)) { | |
| 8544 isnumok = 1; | |
| 8545 pbuf = formatbuf; | |
| 8546 len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | |
| 8547 flags, prec, c, iobj); | |
| 8548 Py_DECREF(iobj); | |
| 8549 if (len < 0) | |
| 8550 goto onError; | |
| 8551 sign = 1; | |
| 8552 } | |
| 8553 else if (PyLong_Check(iobj)) { | |
| 8554 isnumok = 1; | |
| 8555 temp = formatlong(iobj, flags, prec, c); | |
| 8556 Py_DECREF(iobj); | |
| 8557 if (!temp) | |
| 8558 goto onError; | |
| 8559 pbuf = PyUnicode_AS_UNICODE(temp); | |
| 8560 len = PyUnicode_GET_SIZE(temp); | |
| 8561 sign = 1; | |
| 8562 } | |
| 8563 else { | |
| 8564 Py_DECREF(iobj); | |
| 8565 } | |
| 8566 } | |
| 8567 } | |
| 8568 if (!isnumok) { | |
| 8569 PyErr_Format(PyExc_TypeError, | |
| 8570 "%%%c format: a number is required, " | |
| 8571 "not %.200s", (char)c, Py_TYPE(v)->tp_name); | |
| 8572 goto onError; | |
| 8573 } | |
| 8574 if (flags & F_ZERO) | |
| 8575 fill = '0'; | |
| 8576 break; | |
| 8577 | |
| 8578 case 'e': | |
| 8579 case 'E': | |
| 8580 case 'f': | |
| 8581 case 'F': | |
| 8582 case 'g': | |
| 8583 case 'G': | |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8584 temp = formatfloat(v, flags, prec, c); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8585 if (temp == NULL) |
| 51216 | 8586 goto onError; |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8587 pbuf = PyUnicode_AS_UNICODE(temp); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8588 len = PyUnicode_GET_SIZE(temp); |
| 51216 | 8589 sign = 1; |
| 8590 if (flags & F_ZERO) | |
| 8591 fill = '0'; | |
| 8592 break; | |
| 8593 | |
| 8594 case 'c': | |
| 8595 pbuf = formatbuf; | |
| 8596 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); | |
| 8597 if (len < 0) | |
| 8598 goto onError; | |
| 8599 break; | |
| 8600 | |
| 8601 default: | |
| 8602 PyErr_Format(PyExc_ValueError, | |
| 8603 "unsupported format character '%c' (0x%x) " | |
| 8604 "at index %zd", | |
| 8605 (31<=c && c<=126) ? (char)c : '?', | |
|
25375
4df0e691f4da
Add cast to avoid compiler warning.
Marc-André Lemburg <mal@egenix.com>
parents:
25248
diff
changeset
|
8606 (int)c, |
| 51216 | 8607 (Py_ssize_t)(fmt - 1 - |
| 8608 PyUnicode_AS_UNICODE(uformat))); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8609 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8610 } |
| 51216 | 8611 if (sign) { |
| 8612 if (*pbuf == '-' || *pbuf == '+') { | |
| 8613 sign = *pbuf++; | |
| 8614 len--; | |
| 8615 } | |
| 8616 else if (flags & F_SIGN) | |
| 8617 sign = '+'; | |
| 8618 else if (flags & F_BLANK) | |
| 8619 sign = ' '; | |
| 8620 else | |
| 8621 sign = 0; | |
| 8622 } | |
| 8623 if (width < len) | |
| 8624 width = len; | |
| 8625 if (rescnt - (sign != 0) < width) { | |
| 8626 reslen -= rescnt; | |
| 8627 rescnt = width + fmtcnt + 100; | |
| 8628 reslen += rescnt; | |
| 8629 if (reslen < 0) { | |
| 8630 Py_XDECREF(temp); | |
| 8631 PyErr_NoMemory(); | |
| 8632 goto onError; | |
| 8633 } | |
| 8634 if (_PyUnicode_Resize(&result, reslen) < 0) { | |
| 8635 Py_XDECREF(temp); | |
| 8636 goto onError; | |
| 8637 } | |
| 8638 res = PyUnicode_AS_UNICODE(result) | |
| 8639 + reslen - rescnt; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8640 } |
| 51216 | 8641 if (sign) { |
| 8642 if (fill != ' ') | |
| 8643 *res++ = sign; | |
| 8644 rescnt--; | |
| 8645 if (width > len) | |
| 8646 width--; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8647 } |
| 51216 | 8648 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8649 assert(pbuf[0] == '0'); | |
| 8650 assert(pbuf[1] == c); | |
| 8651 if (fill != ' ') { | |
| 8652 *res++ = *pbuf++; | |
| 8653 *res++ = *pbuf++; | |
| 8654 } | |
| 8655 rescnt -= 2; | |
| 8656 width -= 2; | |
| 8657 if (width < 0) | |
| 8658 width = 0; | |
| 8659 len -= 2; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8660 } |
| 51216 | 8661 if (width > len && !(flags & F_LJUST)) { |
| 8662 do { | |
| 8663 --rescnt; | |
| 8664 *res++ = fill; | |
| 8665 } while (--width > len); | |
| 8666 } | |
| 8667 if (fill == ' ') { | |
| 8668 if (sign) | |
| 8669 *res++ = sign; | |
| 8670 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | |
| 8671 assert(pbuf[0] == '0'); | |
| 8672 assert(pbuf[1] == c); | |
| 8673 *res++ = *pbuf++; | |
| 8674 *res++ = *pbuf++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8675 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8676 } |
| 51216 | 8677 Py_UNICODE_COPY(res, pbuf, len); |
| 8678 res += len; | |
| 8679 rescnt -= len; | |
| 8680 while (--width >= len) { | |
| 8681 --rescnt; | |
| 8682 *res++ = ' '; | |
| 8683 } | |
| 8684 if (dict && (argidx < arglen) && c != '%') { | |
| 8685 PyErr_SetString(PyExc_TypeError, | |
| 8686 "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
|
8687 Py_XDECREF(temp); |
| 51216 | 8688 goto onError; |
| 8689 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8690 Py_XDECREF(temp); |
| 51216 | 8691 } /* '%' */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8692 } /* until end */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8693 if (argidx < arglen && !dict) { |
| 51216 | 8694 PyErr_SetString(PyExc_TypeError, |
| 8695 "not all arguments converted during string formatting"); | |
| 8696 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8697 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8698 |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8699 if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 51216 | 8700 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8701 if (args_owned) { |
| 51216 | 8702 Py_DECREF(args); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8703 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8704 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8705 return (PyObject *)result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8706 |
| 51216 | 8707 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8708 Py_XDECREF(result); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8709 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8710 if (args_owned) { |
| 51216 | 8711 Py_DECREF(args); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8712 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8713 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8714 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8715 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8716 static PyBufferProcs unicode_as_buffer = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8717 (readbufferproc) unicode_buffer_getreadbuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8718 (writebufferproc) unicode_buffer_getwritebuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8719 (segcountproc) unicode_buffer_getsegcount, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8720 (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
|
8721 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8722 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24071
diff
changeset
|
8723 static PyObject * |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8724 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8725 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8726 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8727 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
|
8728 { |
| 51216 | 8729 PyObject *x = NULL; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8730 static char *kwlist[] = {"string", "encoding", "errors", 0}; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8731 char *encoding = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8732 char *errors = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8733 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8734 if (type != &PyUnicode_Type) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8735 return unicode_subtype_new(type, args, kwds); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8736 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 51216 | 8737 kwlist, &x, &encoding, &errors)) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8738 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8739 if (x == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8740 return (PyObject *)_PyUnicode_New(0); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8741 if (encoding == NULL && errors == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8742 return PyObject_Unicode(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8743 else |
| 51216 | 8744 return PyUnicode_FromEncodedObject(x, encoding, errors); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8745 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8746 |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8747 static PyObject * |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8748 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8749 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8750 PyUnicodeObject *tmp, *pnew; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8751 Py_ssize_t n; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8752 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8753 assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8754 tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8755 if (tmp == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8756 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8757 assert(PyUnicode_Check(tmp)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8758 pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8759 if (pnew == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8760 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8761 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8762 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8763 pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8764 if (pnew->str == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8765 _Py_ForgetReference((PyObject *)pnew); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8766 PyObject_Del(pnew); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8767 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8768 return PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8769 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8770 Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8771 pnew->length = n; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8772 pnew->hash = tmp->hash; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8773 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8774 return (PyObject *)pnew; |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8775 } |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8776 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
8777 PyDoc_STRVAR(unicode_doc, |
|
79590
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
78792
diff
changeset
|
8778 "unicode(object='') -> unicode object\n\ |
|
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
78792
diff
changeset
|
8779 unicode(string[, encoding[, errors]]) -> unicode object\n\ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8780 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8781 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
|
8782 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
|
8783 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
|
8784 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8785 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
|
8786 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8787 "unicode", /* tp_name */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8788 sizeof(PyUnicodeObject), /* tp_size */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8789 0, /* tp_itemsize */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8790 /* Slots */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8791 (destructor)unicode_dealloc, /* tp_dealloc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8792 0, /* tp_print */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8793 0, /* tp_getattr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8794 0, /* tp_setattr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8795 0, /* tp_compare */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8796 unicode_repr, /* tp_repr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8797 &unicode_as_number, /* tp_as_number */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8798 &unicode_as_sequence, /* tp_as_sequence */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8799 &unicode_as_mapping, /* tp_as_mapping */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8800 (hashfunc) unicode_hash, /* tp_hash*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8801 0, /* tp_call*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8802 (reprfunc) unicode_str, /* tp_str */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8803 PyObject_GenericGetAttr, /* tp_getattro */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8804 0, /* tp_setattro */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8805 &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
|
8806 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 51216 | 8807 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8808 unicode_doc, /* tp_doc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8809 0, /* tp_traverse */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8810 0, /* tp_clear */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8811 PyUnicode_RichCompare, /* tp_richcompare */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8812 0, /* tp_weaklistoffset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8813 0, /* tp_iter */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8814 0, /* tp_iternext */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8815 unicode_methods, /* tp_methods */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8816 0, /* tp_members */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8817 0, /* tp_getset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8818 &PyBaseString_Type, /* tp_base */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8819 0, /* tp_dict */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8820 0, /* tp_descr_get */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8821 0, /* tp_descr_set */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8822 0, /* tp_dictoffset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8823 0, /* tp_init */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8824 0, /* tp_alloc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8825 unicode_new, /* tp_new */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8826 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
|
8827 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8828 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8829 /* 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
|
8830 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
8831 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
|
8832 { |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8833 /* 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
|
8834 Py_UNICODE linebreak[] = { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8835 0x000A, /* LINE FEED */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8836 0x000D, /* CARRIAGE RETURN */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8837 0x001C, /* FILE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8838 0x001D, /* GROUP SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8839 0x001E, /* RECORD SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8840 0x0085, /* NEXT LINE */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8841 0x2028, /* LINE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8842 0x2029, /* PARAGRAPH SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8843 }; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8844 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8845 /* Init the implementation */ |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8846 if (!unicode_empty) { |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8847 unicode_empty = _PyUnicode_New(0); |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8848 if (!unicode_empty) |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8849 return; |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8850 } |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8851 |
|
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
|
8852 if (PyType_Ready(&PyUnicode_Type) < 0) |
| 51216 | 8853 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
|
8854 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8855 /* 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
|
8856 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
|
8857 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
|
8858 ); |
|
38786
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
8859 |
|
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
8860 PyType_Ready(&EncodingMapType); |
|
80075
315bd0ceb97a
initialize more global type objects (closes #16369)
Benjamin Peterson <benjamin@python.org>
parents:
80014
diff
changeset
|
8861 |
|
315bd0ceb97a
initialize more global type objects (closes #16369)
Benjamin Peterson <benjamin@python.org>
parents:
80014
diff
changeset
|
8862 if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
|
315bd0ceb97a
initialize more global type objects (closes #16369)
Benjamin Peterson <benjamin@python.org>
parents:
80014
diff
changeset
|
8863 Py_FatalError("Can't initialize field name iterator type"); |
|
315bd0ceb97a
initialize more global type objects (closes #16369)
Benjamin Peterson <benjamin@python.org>
parents:
80014
diff
changeset
|
8864 |
|
315bd0ceb97a
initialize more global type objects (closes #16369)
Benjamin Peterson <benjamin@python.org>
parents:
80014
diff
changeset
|
8865 if (PyType_Ready(&PyFormatterIter_Type) < 0) |
|
315bd0ceb97a
initialize more global type objects (closes #16369)
Benjamin Peterson <benjamin@python.org>
parents:
80014
diff
changeset
|
8866 Py_FatalError("Can't initialize formatter iter type"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8867 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8868 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8869 /* 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
|
8870 |
|
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
|
8871 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
|
8872 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
|
8873 { |
|
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
|
8874 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
|
8875 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
|
8876 |
|
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
|
8877 for (u = free_list; u != NULL;) { |
| 51216 | 8878 PyUnicodeObject *v = u; |
| 8879 u = *(PyUnicodeObject **)u; | |
| 8880 if (v->str) | |
| 8881 PyObject_DEL(v->str); | |
| 8882 Py_XDECREF(v->defenc); | |
| 8883 PyObject_Del(v); | |
| 8884 numfree--; | |
|
45341
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
8885 } |
|
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
|
8886 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
|
8887 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
|
8888 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
|
8889 } |
|
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
|
8890 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8891 void |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
8892 _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
|
8893 { |
|
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
|
8894 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8895 |
|
81748
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8896 Py_CLEAR(unicode_empty); |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8897 |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8898 for (i = 0; i < 256; i++) |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8899 Py_CLEAR(unicode_latin1[i]); |
|
7c8ad0d02664
Issue #10156: In the interpreter's initialization phase, unicode globals
Serhiy Storchaka <storchaka@gmail.com>
parents:
81625
diff
changeset
|
8900 |
|
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
|
8901 (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
|
8902 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8903 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8904 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8905 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8906 #endif |
