blob: ce82717d7d421b3cd62d9b24899419a53ec1f1c2 [file] [log] [blame]
Tim Petersced69f82003-09-16 20:30:58 +00001/*
Guido van Rossumd57fd912000-03-10 22:53:23 +00002
3Unicode implementation based on original code by Fredrik Lundh,
Benjamin Peterson31616ea2011-10-01 00:11:09 -04004modified by Marc-Andre Lemburg <mal@lemburg.com>.
Guido van Rossumd57fd912000-03-10 22:53:23 +00005
Thomas Wouters477c8d52006-05-27 19:21:47 +00006Major speed upgrades to the method implementations at the Reykjavik
7NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.
8
Guido van Rossum16b1ad92000-08-03 16:24:25 +00009Copyright (c) Corporation for National Research Initiatives.
Guido van Rossumd57fd912000-03-10 22:53:23 +000010
Fredrik Lundh0fdb90c2001-01-19 09:45:02 +000011--------------------------------------------------------------------
12The original string type implementation is:
Guido van Rossumd57fd912000-03-10 22:53:23 +000013
Benjamin Peterson29060642009-01-31 22:14:21 +000014 Copyright (c) 1999 by Secret Labs AB
15 Copyright (c) 1999 by Fredrik Lundh
Guido van Rossumd57fd912000-03-10 22:53:23 +000016
Fredrik Lundh0fdb90c2001-01-19 09:45:02 +000017By obtaining, using, and/or copying this software and/or its
18associated documentation, you agree that you have read, understood,
19and will comply with the following terms and conditions:
20
21Permission to use, copy, modify, and distribute this software and its
22associated documentation for any purpose and without fee is hereby
23granted, provided that the above copyright notice appears in all
24copies, and that both that copyright notice and this permission notice
25appear in supporting documentation, and that the name of Secret Labs
26AB or the author not be used in advertising or publicity pertaining to
27distribution of the software without specific, written prior
28permission.
29
30SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
31THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
32FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
33ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
36OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37--------------------------------------------------------------------
38
39*/
Guido van Rossumd57fd912000-03-10 22:53:23 +000040
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041#define PY_SSIZE_T_CLEAN
Guido van Rossumd57fd912000-03-10 22:53:23 +000042#include "Python.h"
Marc-André Lemburgd49e5b42000-06-30 14:58:20 +000043#include "ucnhash.h"
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -050044#include "bytes_methods.h"
Guido van Rossumd57fd912000-03-10 22:53:23 +000045
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000046#ifdef MS_WINDOWS
Guido van Rossumb7a40ba2000-03-28 02:01:52 +000047#include <windows.h>
48#endif
Guido van Rossumfd4b9572000-04-10 13:51:10 +000049
Guido van Rossumd57fd912000-03-10 22:53:23 +000050/* Endianness switches; defaults to little endian */
51
52#ifdef WORDS_BIGENDIAN
53# define BYTEORDER_IS_BIG_ENDIAN
54#else
55# define BYTEORDER_IS_LITTLE_ENDIAN
56#endif
57
Marc-André Lemburgd4ab4a52000-06-08 17:54:00 +000058/* --- Globals ------------------------------------------------------------
59
60 The globals are initialized by the _PyUnicode_Init() API and should
61 not be used before calling that API.
62
63*/
Guido van Rossumd57fd912000-03-10 22:53:23 +000064
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065
66#ifdef __cplusplus
67extern "C" {
68#endif
69
Victor Stinner8faf8212011-12-08 22:14:11 +010070/* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */
71#define MAX_UNICODE 0x10ffff
72
Victor Stinner910337b2011-10-03 03:20:16 +020073#ifdef Py_DEBUG
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020074# define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0)
Victor Stinner910337b2011-10-03 03:20:16 +020075#else
76# define _PyUnicode_CHECK(op) PyUnicode_Check(op)
77#endif
Victor Stinnerfb5f5f22011-09-28 21:39:49 +020078
Victor Stinnere90fe6a2011-10-01 16:48:13 +020079#define _PyUnicode_UTF8(op) \
80 (((PyCompactUnicodeObject*)(op))->utf8)
81#define PyUnicode_UTF8(op) \
Victor Stinner910337b2011-10-03 03:20:16 +020082 (assert(_PyUnicode_CHECK(op)), \
Victor Stinnere90fe6a2011-10-01 16:48:13 +020083 assert(PyUnicode_IS_READY(op)), \
84 PyUnicode_IS_COMPACT_ASCII(op) ? \
85 ((char*)((PyASCIIObject*)(op) + 1)) : \
86 _PyUnicode_UTF8(op))
Victor Stinnerbc8b81b2011-09-29 19:31:34 +020087#define _PyUnicode_UTF8_LENGTH(op) \
Victor Stinnere90fe6a2011-10-01 16:48:13 +020088 (((PyCompactUnicodeObject*)(op))->utf8_length)
89#define PyUnicode_UTF8_LENGTH(op) \
Victor Stinner910337b2011-10-03 03:20:16 +020090 (assert(_PyUnicode_CHECK(op)), \
Victor Stinnere90fe6a2011-10-01 16:48:13 +020091 assert(PyUnicode_IS_READY(op)), \
92 PyUnicode_IS_COMPACT_ASCII(op) ? \
93 ((PyASCIIObject*)(op))->length : \
94 _PyUnicode_UTF8_LENGTH(op))
Victor Stinnera5f91632011-10-04 01:07:11 +020095#define _PyUnicode_WSTR(op) \
96 (((PyASCIIObject*)(op))->wstr)
97#define _PyUnicode_WSTR_LENGTH(op) \
98 (((PyCompactUnicodeObject*)(op))->wstr_length)
99#define _PyUnicode_LENGTH(op) \
100 (((PyASCIIObject *)(op))->length)
101#define _PyUnicode_STATE(op) \
102 (((PyASCIIObject *)(op))->state)
103#define _PyUnicode_HASH(op) \
104 (((PyASCIIObject *)(op))->hash)
Victor Stinner910337b2011-10-03 03:20:16 +0200105#define _PyUnicode_KIND(op) \
106 (assert(_PyUnicode_CHECK(op)), \
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200107 ((PyASCIIObject *)(op))->state.kind)
Victor Stinner910337b2011-10-03 03:20:16 +0200108#define _PyUnicode_GET_LENGTH(op) \
109 (assert(_PyUnicode_CHECK(op)), \
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200110 ((PyASCIIObject *)(op))->length)
Victor Stinnera5f91632011-10-04 01:07:11 +0200111#define _PyUnicode_DATA_ANY(op) \
112 (((PyUnicodeObject*)(op))->data.any)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200113
Victor Stinnere6abb482012-05-02 01:15:40 +0200114/* Optimized version of Py_MAX() to compute the maximum character:
115 use it when your are computing the second argument of PyUnicode_New() */
116#define MAX_MAXCHAR(maxchar1, maxchar2) \
117 ((maxchar1) | (maxchar2))
118
Victor Stinner910337b2011-10-03 03:20:16 +0200119#undef PyUnicode_READY
120#define PyUnicode_READY(op) \
121 (assert(_PyUnicode_CHECK(op)), \
122 (PyUnicode_IS_READY(op) ? \
Victor Stinnera5f91632011-10-04 01:07:11 +0200123 0 : \
Victor Stinner7931d9a2011-11-04 00:22:48 +0100124 _PyUnicode_Ready(op)))
Victor Stinner910337b2011-10-03 03:20:16 +0200125
Victor Stinnerc379ead2011-10-03 12:52:27 +0200126#define _PyUnicode_SHARE_UTF8(op) \
127 (assert(_PyUnicode_CHECK(op)), \
128 assert(!PyUnicode_IS_COMPACT_ASCII(op)), \
129 (_PyUnicode_UTF8(op) == PyUnicode_DATA(op)))
130#define _PyUnicode_SHARE_WSTR(op) \
131 (assert(_PyUnicode_CHECK(op)), \
132 (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op)))
133
Victor Stinner829c0ad2011-10-03 01:08:02 +0200134/* true if the Unicode object has an allocated UTF-8 memory block
135 (not shared with other data) */
Victor Stinner910337b2011-10-03 03:20:16 +0200136#define _PyUnicode_HAS_UTF8_MEMORY(op) \
137 (assert(_PyUnicode_CHECK(op)), \
138 (!PyUnicode_IS_COMPACT_ASCII(op) \
139 && _PyUnicode_UTF8(op) \
Victor Stinner829c0ad2011-10-03 01:08:02 +0200140 && _PyUnicode_UTF8(op) != PyUnicode_DATA(op)))
141
Victor Stinner03490912011-10-03 23:45:12 +0200142/* true if the Unicode object has an allocated wstr memory block
143 (not shared with other data) */
144#define _PyUnicode_HAS_WSTR_MEMORY(op) \
145 (assert(_PyUnicode_CHECK(op)), \
146 (_PyUnicode_WSTR(op) && \
147 (!PyUnicode_IS_READY(op) || \
148 _PyUnicode_WSTR(op) != PyUnicode_DATA(op))))
149
Victor Stinner910337b2011-10-03 03:20:16 +0200150/* Generic helper macro to convert characters of different types.
151 from_type and to_type have to be valid type names, begin and end
152 are pointers to the source characters which should be of type
153 "from_type *". to is a pointer of type "to_type *" and points to the
154 buffer where the result characters are written to. */
155#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \
156 do { \
Antoine Pitroue459a082011-10-11 20:58:41 +0200157 to_type *_to = (to_type *) to; \
158 const from_type *_iter = (begin); \
159 const from_type *_end = (end); \
160 Py_ssize_t n = (_end) - (_iter); \
161 const from_type *_unrolled_end = \
162 _iter + (n & ~ (Py_ssize_t) 3); \
163 while (_iter < (_unrolled_end)) { \
164 _to[0] = (to_type) _iter[0]; \
165 _to[1] = (to_type) _iter[1]; \
166 _to[2] = (to_type) _iter[2]; \
167 _to[3] = (to_type) _iter[3]; \
168 _iter += 4; _to += 4; \
Victor Stinner910337b2011-10-03 03:20:16 +0200169 } \
Antoine Pitroue459a082011-10-11 20:58:41 +0200170 while (_iter < (_end)) \
171 *_to++ = (to_type) *_iter++; \
Victor Stinner910337b2011-10-03 03:20:16 +0200172 } while (0)
Victor Stinner829c0ad2011-10-03 01:08:02 +0200173
Walter Dörwald16807132007-05-25 13:52:07 +0000174/* This dictionary holds all interned unicode strings. Note that references
175 to strings in this dictionary are *not* counted in the string's ob_refcnt.
176 When the interned string reaches a refcnt of 0 the string deallocation
177 function will delete the reference from this dictionary.
178
179 Another way to look at this is that to say that the actual reference
Guido van Rossum98297ee2007-11-06 21:34:58 +0000180 count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
Walter Dörwald16807132007-05-25 13:52:07 +0000181*/
182static PyObject *interned;
183
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +0000184/* The empty Unicode object is shared to improve performance. */
Victor Stinnera464fc12011-10-02 20:39:30 +0200185static PyObject *unicode_empty;
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +0000186
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200187/* List of static strings. */
188static _Py_Identifier *static_strings;
189
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +0000190/* Single character Unicode strings in the Latin-1 range are being
191 shared as well. */
Victor Stinnera464fc12011-10-02 20:39:30 +0200192static PyObject *unicode_latin1[256];
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +0000193
Christian Heimes190d79e2008-01-30 11:58:22 +0000194/* Fast detection of the most frequent whitespace characters */
195const unsigned char _Py_ascii_whitespace[] = {
Benjamin Peterson14339b62009-01-31 16:36:08 +0000196 0, 0, 0, 0, 0, 0, 0, 0,
Florent Xicluna806d8cf2010-03-30 19:34:18 +0000197/* case 0x0009: * CHARACTER TABULATION */
Christian Heimes1a8501c2008-10-02 19:56:01 +0000198/* case 0x000A: * LINE FEED */
Florent Xicluna806d8cf2010-03-30 19:34:18 +0000199/* case 0x000B: * LINE TABULATION */
Christian Heimes1a8501c2008-10-02 19:56:01 +0000200/* case 0x000C: * FORM FEED */
201/* case 0x000D: * CARRIAGE RETURN */
Benjamin Peterson14339b62009-01-31 16:36:08 +0000202 0, 1, 1, 1, 1, 1, 0, 0,
203 0, 0, 0, 0, 0, 0, 0, 0,
Christian Heimes1a8501c2008-10-02 19:56:01 +0000204/* case 0x001C: * FILE SEPARATOR */
205/* case 0x001D: * GROUP SEPARATOR */
206/* case 0x001E: * RECORD SEPARATOR */
207/* case 0x001F: * UNIT SEPARATOR */
Benjamin Peterson14339b62009-01-31 16:36:08 +0000208 0, 0, 0, 0, 1, 1, 1, 1,
Christian Heimes1a8501c2008-10-02 19:56:01 +0000209/* case 0x0020: * SPACE */
Benjamin Peterson14339b62009-01-31 16:36:08 +0000210 1, 0, 0, 0, 0, 0, 0, 0,
211 0, 0, 0, 0, 0, 0, 0, 0,
212 0, 0, 0, 0, 0, 0, 0, 0,
213 0, 0, 0, 0, 0, 0, 0, 0,
Christian Heimes190d79e2008-01-30 11:58:22 +0000214
Benjamin Peterson14339b62009-01-31 16:36:08 +0000215 0, 0, 0, 0, 0, 0, 0, 0,
216 0, 0, 0, 0, 0, 0, 0, 0,
217 0, 0, 0, 0, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0,
219 0, 0, 0, 0, 0, 0, 0, 0,
220 0, 0, 0, 0, 0, 0, 0, 0,
221 0, 0, 0, 0, 0, 0, 0, 0,
222 0, 0, 0, 0, 0, 0, 0, 0
Christian Heimes190d79e2008-01-30 11:58:22 +0000223};
224
Victor Stinner1b4f9ce2011-10-03 13:28:14 +0200225/* forward */
Victor Stinnerfe226c02011-10-03 03:52:20 +0200226static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length);
Victor Stinner1b4f9ce2011-10-03 13:28:14 +0200227static PyObject* get_latin1_char(unsigned char ch);
Victor Stinner488fa492011-12-12 00:01:39 +0100228static int unicode_modifiable(PyObject *unicode);
229
Victor Stinnerfe226c02011-10-03 03:52:20 +0200230
Alexander Belopolsky40018472011-02-26 01:02:56 +0000231static PyObject *
Antoine Pitroudd4e2f02011-10-13 00:02:27 +0200232_PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size);
233static PyObject *
234_PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size);
235static PyObject *
236_PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size);
237
238static PyObject *
Alexander Belopolsky40018472011-02-26 01:02:56 +0000239unicode_encode_call_errorhandler(const char *errors,
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000240 PyObject **errorHandler,const char *encoding, const char *reason,
Martin v. Löwis23e275b2011-11-02 18:02:51 +0100241 PyObject *unicode, PyObject **exceptionObject,
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000242 Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos);
243
Alexander Belopolsky40018472011-02-26 01:02:56 +0000244static void
245raise_encode_exception(PyObject **exceptionObject,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +0300246 const char *encoding,
Martin v. Löwis9e816682011-11-02 12:45:42 +0100247 PyObject *unicode,
248 Py_ssize_t startpos, Py_ssize_t endpos,
249 const char *reason);
Victor Stinner31be90b2010-04-22 19:38:16 +0000250
Christian Heimes190d79e2008-01-30 11:58:22 +0000251/* Same for linebreaks */
252static unsigned char ascii_linebreak[] = {
Benjamin Peterson14339b62009-01-31 16:36:08 +0000253 0, 0, 0, 0, 0, 0, 0, 0,
Christian Heimes1a8501c2008-10-02 19:56:01 +0000254/* 0x000A, * LINE FEED */
Florent Xicluna806d8cf2010-03-30 19:34:18 +0000255/* 0x000B, * LINE TABULATION */
256/* 0x000C, * FORM FEED */
Christian Heimes1a8501c2008-10-02 19:56:01 +0000257/* 0x000D, * CARRIAGE RETURN */
Florent Xicluna806d8cf2010-03-30 19:34:18 +0000258 0, 0, 1, 1, 1, 1, 0, 0,
Benjamin Peterson14339b62009-01-31 16:36:08 +0000259 0, 0, 0, 0, 0, 0, 0, 0,
Christian Heimes1a8501c2008-10-02 19:56:01 +0000260/* 0x001C, * FILE SEPARATOR */
261/* 0x001D, * GROUP SEPARATOR */
262/* 0x001E, * RECORD SEPARATOR */
Benjamin Peterson14339b62009-01-31 16:36:08 +0000263 0, 0, 0, 0, 1, 1, 1, 0,
264 0, 0, 0, 0, 0, 0, 0, 0,
265 0, 0, 0, 0, 0, 0, 0, 0,
266 0, 0, 0, 0, 0, 0, 0, 0,
267 0, 0, 0, 0, 0, 0, 0, 0,
Christian Heimes190d79e2008-01-30 11:58:22 +0000268
Benjamin Peterson14339b62009-01-31 16:36:08 +0000269 0, 0, 0, 0, 0, 0, 0, 0,
270 0, 0, 0, 0, 0, 0, 0, 0,
271 0, 0, 0, 0, 0, 0, 0, 0,
272 0, 0, 0, 0, 0, 0, 0, 0,
273 0, 0, 0, 0, 0, 0, 0, 0,
274 0, 0, 0, 0, 0, 0, 0, 0,
275 0, 0, 0, 0, 0, 0, 0, 0,
276 0, 0, 0, 0, 0, 0, 0, 0
Christian Heimes190d79e2008-01-30 11:58:22 +0000277};
278
Ezio Melotti48a2f8f2011-09-29 00:18:19 +0300279/* The max unicode value is always 0x10FFFF while using the PEP-393 API.
280 This function is kept for backward compatibility with the old API. */
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000281Py_UNICODE
Marc-André Lemburg6c6bfb72001-07-20 17:39:11 +0000282PyUnicode_GetMax(void)
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000283{
Fredrik Lundh8f455852001-06-27 18:59:43 +0000284#ifdef Py_UNICODE_WIDE
Benjamin Peterson14339b62009-01-31 16:36:08 +0000285 return 0x10FFFF;
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000286#else
Benjamin Peterson14339b62009-01-31 16:36:08 +0000287 /* This is actually an illegal character, so it should
288 not be passed to unichr. */
289 return 0xFFFF;
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000290#endif
291}
292
Victor Stinner910337b2011-10-03 03:20:16 +0200293#ifdef Py_DEBUG
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +0200294int
Victor Stinner7931d9a2011-11-04 00:22:48 +0100295_PyUnicode_CheckConsistency(PyObject *op, int check_content)
Victor Stinner910337b2011-10-03 03:20:16 +0200296{
297 PyASCIIObject *ascii;
298 unsigned int kind;
299
300 assert(PyUnicode_Check(op));
301
302 ascii = (PyASCIIObject *)op;
303 kind = ascii->state.kind;
304
Victor Stinnera3b334d2011-10-03 13:53:37 +0200305 if (ascii->state.ascii == 1 && ascii->state.compact == 1) {
Victor Stinner910337b2011-10-03 03:20:16 +0200306 assert(kind == PyUnicode_1BYTE_KIND);
Victor Stinner910337b2011-10-03 03:20:16 +0200307 assert(ascii->state.ready == 1);
308 }
Victor Stinnera41463c2011-10-04 01:05:08 +0200309 else {
Victor Stinner85041a52011-10-03 14:42:39 +0200310 PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op;
Victor Stinner7f11ad42011-10-04 00:00:20 +0200311 void *data;
Victor Stinner910337b2011-10-03 03:20:16 +0200312
Victor Stinnera41463c2011-10-04 01:05:08 +0200313 if (ascii->state.compact == 1) {
314 data = compact + 1;
Victor Stinner910337b2011-10-03 03:20:16 +0200315 assert(kind == PyUnicode_1BYTE_KIND
316 || kind == PyUnicode_2BYTE_KIND
317 || kind == PyUnicode_4BYTE_KIND);
Victor Stinnera41463c2011-10-04 01:05:08 +0200318 assert(ascii->state.ascii == 0);
Victor Stinner910337b2011-10-03 03:20:16 +0200319 assert(ascii->state.ready == 1);
Victor Stinnera41463c2011-10-04 01:05:08 +0200320 assert (compact->utf8 != data);
Victor Stinnere30c0a12011-11-04 20:54:05 +0100321 }
322 else {
Victor Stinnera41463c2011-10-04 01:05:08 +0200323 PyUnicodeObject *unicode = (PyUnicodeObject *)op;
324
325 data = unicode->data.any;
326 if (kind == PyUnicode_WCHAR_KIND) {
Victor Stinnere30c0a12011-11-04 20:54:05 +0100327 assert(ascii->length == 0);
328 assert(ascii->hash == -1);
Victor Stinnera41463c2011-10-04 01:05:08 +0200329 assert(ascii->state.compact == 0);
330 assert(ascii->state.ascii == 0);
331 assert(ascii->state.ready == 0);
Victor Stinnere30c0a12011-11-04 20:54:05 +0100332 assert(ascii->state.interned == SSTATE_NOT_INTERNED);
Victor Stinnera41463c2011-10-04 01:05:08 +0200333 assert(ascii->wstr != NULL);
334 assert(data == NULL);
335 assert(compact->utf8 == NULL);
Victor Stinnera41463c2011-10-04 01:05:08 +0200336 }
337 else {
338 assert(kind == PyUnicode_1BYTE_KIND
339 || kind == PyUnicode_2BYTE_KIND
340 || kind == PyUnicode_4BYTE_KIND);
341 assert(ascii->state.compact == 0);
342 assert(ascii->state.ready == 1);
343 assert(data != NULL);
344 if (ascii->state.ascii) {
345 assert (compact->utf8 == data);
346 assert (compact->utf8_length == ascii->length);
347 }
348 else
349 assert (compact->utf8 != data);
350 }
351 }
352 if (kind != PyUnicode_WCHAR_KIND) {
Victor Stinner7f11ad42011-10-04 00:00:20 +0200353 if (
354#if SIZEOF_WCHAR_T == 2
355 kind == PyUnicode_2BYTE_KIND
356#else
357 kind == PyUnicode_4BYTE_KIND
358#endif
359 )
Victor Stinnera41463c2011-10-04 01:05:08 +0200360 {
361 assert(ascii->wstr == data);
362 assert(compact->wstr_length == ascii->length);
363 } else
364 assert(ascii->wstr != data);
Victor Stinner910337b2011-10-03 03:20:16 +0200365 }
Victor Stinnera41463c2011-10-04 01:05:08 +0200366
367 if (compact->utf8 == NULL)
368 assert(compact->utf8_length == 0);
369 if (ascii->wstr == NULL)
370 assert(compact->wstr_length == 0);
Victor Stinner910337b2011-10-03 03:20:16 +0200371 }
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200372 /* check that the best kind is used */
373 if (check_content && kind != PyUnicode_WCHAR_KIND)
374 {
375 Py_ssize_t i;
376 Py_UCS4 maxchar = 0;
Victor Stinner718fbf02012-04-26 00:39:37 +0200377 void *data;
378 Py_UCS4 ch;
379
380 data = PyUnicode_DATA(ascii);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200381 for (i=0; i < ascii->length; i++)
382 {
Victor Stinner718fbf02012-04-26 00:39:37 +0200383 ch = PyUnicode_READ(kind, data, i);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200384 if (ch > maxchar)
385 maxchar = ch;
386 }
387 if (kind == PyUnicode_1BYTE_KIND) {
Victor Stinner77faf692011-11-20 18:56:05 +0100388 if (ascii->state.ascii == 0) {
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200389 assert(maxchar >= 128);
Victor Stinner77faf692011-11-20 18:56:05 +0100390 assert(maxchar <= 255);
391 }
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200392 else
393 assert(maxchar < 128);
394 }
Victor Stinner77faf692011-11-20 18:56:05 +0100395 else if (kind == PyUnicode_2BYTE_KIND) {
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200396 assert(maxchar >= 0x100);
Victor Stinner77faf692011-11-20 18:56:05 +0100397 assert(maxchar <= 0xFFFF);
398 }
399 else {
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200400 assert(maxchar >= 0x10000);
Victor Stinner8faf8212011-12-08 22:14:11 +0100401 assert(maxchar <= MAX_UNICODE);
Victor Stinner77faf692011-11-20 18:56:05 +0100402 }
Victor Stinner718fbf02012-04-26 00:39:37 +0200403 assert(PyUnicode_READ(kind, data, ascii->length) == 0);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200404 }
Benjamin Petersonccc51c12011-10-03 19:34:12 -0400405 return 1;
406}
Victor Stinner910337b2011-10-03 03:20:16 +0200407#endif
408
Victor Stinnerd3df8ab2011-11-22 01:22:34 +0100409static PyObject*
410unicode_result_wchar(PyObject *unicode)
411{
412#ifndef Py_DEBUG
413 Py_ssize_t len;
414
415 assert(Py_REFCNT(unicode) == 1);
416
417 len = _PyUnicode_WSTR_LENGTH(unicode);
418 if (len == 0) {
419 Py_INCREF(unicode_empty);
420 Py_DECREF(unicode);
421 return unicode_empty;
422 }
423
424 if (len == 1) {
425 wchar_t ch = _PyUnicode_WSTR(unicode)[0];
426 if (ch < 256) {
427 PyObject *latin1_char = get_latin1_char((unsigned char)ch);
428 Py_DECREF(unicode);
429 return latin1_char;
430 }
431 }
432
433 if (_PyUnicode_Ready(unicode) < 0) {
434 Py_XDECREF(unicode);
435 return NULL;
436 }
437#else
438 /* don't make the result ready in debug mode to ensure that the caller
439 makes the string ready before using it */
440 assert(_PyUnicode_CheckConsistency(unicode, 1));
441#endif
442 return unicode;
443}
444
445static PyObject*
446unicode_result_ready(PyObject *unicode)
447{
448 Py_ssize_t length;
449
450 length = PyUnicode_GET_LENGTH(unicode);
451 if (length == 0) {
452 if (unicode != unicode_empty) {
453 Py_INCREF(unicode_empty);
454 Py_DECREF(unicode);
455 }
456 return unicode_empty;
457 }
458
459 if (length == 1) {
460 Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0);
461 if (ch < 256) {
462 PyObject *latin1_char = unicode_latin1[ch];
463 if (latin1_char != NULL) {
464 if (unicode != latin1_char) {
465 Py_INCREF(latin1_char);
466 Py_DECREF(unicode);
467 }
468 return latin1_char;
469 }
470 else {
471 assert(_PyUnicode_CheckConsistency(unicode, 1));
472 Py_INCREF(unicode);
473 unicode_latin1[ch] = unicode;
474 return unicode;
475 }
476 }
477 }
478
479 assert(_PyUnicode_CheckConsistency(unicode, 1));
480 return unicode;
481}
482
483static PyObject*
484unicode_result(PyObject *unicode)
485{
486 assert(_PyUnicode_CHECK(unicode));
487 if (PyUnicode_IS_READY(unicode))
488 return unicode_result_ready(unicode);
489 else
490 return unicode_result_wchar(unicode);
491}
492
Victor Stinnerc4b49542011-12-11 22:44:26 +0100493static PyObject*
494unicode_result_unchanged(PyObject *unicode)
495{
496 if (PyUnicode_CheckExact(unicode)) {
Benjamin Petersonbac79492012-01-14 13:34:47 -0500497 if (PyUnicode_READY(unicode) == -1)
Victor Stinnerc4b49542011-12-11 22:44:26 +0100498 return NULL;
499 Py_INCREF(unicode);
500 return unicode;
501 }
502 else
503 /* Subtype -- return genuine unicode string with the same value. */
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100504 return _PyUnicode_Copy(unicode);
Victor Stinnerc4b49542011-12-11 22:44:26 +0100505}
506
Victor Stinner3a50e702011-10-18 21:21:00 +0200507#ifdef HAVE_MBCS
508static OSVERSIONINFOEX winver;
509#endif
510
Thomas Wouters477c8d52006-05-27 19:21:47 +0000511/* --- Bloom Filters ----------------------------------------------------- */
512
513/* stuff to implement simple "bloom filters" for Unicode characters.
514 to keep things simple, we use a single bitmask, using the least 5
515 bits from each unicode characters as the bit index. */
516
517/* the linebreak mask is set up by Unicode_Init below */
518
Antoine Pitrouf068f942010-01-13 14:19:12 +0000519#if LONG_BIT >= 128
520#define BLOOM_WIDTH 128
521#elif LONG_BIT >= 64
522#define BLOOM_WIDTH 64
523#elif LONG_BIT >= 32
524#define BLOOM_WIDTH 32
525#else
526#error "LONG_BIT is smaller than 32"
527#endif
528
Thomas Wouters477c8d52006-05-27 19:21:47 +0000529#define BLOOM_MASK unsigned long
530
531static BLOOM_MASK bloom_linebreak;
532
Antoine Pitrouf068f942010-01-13 14:19:12 +0000533#define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1)))))
534#define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1)))))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000535
Benjamin Peterson29060642009-01-31 22:14:21 +0000536#define BLOOM_LINEBREAK(ch) \
537 ((ch) < 128U ? ascii_linebreak[(ch)] : \
538 (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch)))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000539
Alexander Belopolsky40018472011-02-26 01:02:56 +0000540Py_LOCAL_INLINE(BLOOM_MASK)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200541make_bloom_mask(int kind, void* ptr, Py_ssize_t len)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000542{
543 /* calculate simple bloom-style bitmask for a given unicode string */
544
Antoine Pitrouf068f942010-01-13 14:19:12 +0000545 BLOOM_MASK mask;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000546 Py_ssize_t i;
547
548 mask = 0;
549 for (i = 0; i < len; i++)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200550 BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551
552 return mask;
553}
554
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200555#define BLOOM_MEMBER(mask, chr, str) \
556 (BLOOM(mask, chr) \
557 && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000558
Antoine Pitroudd4e2f02011-10-13 00:02:27 +0200559/* Compilation of templated routines */
560
561#include "stringlib/asciilib.h"
562#include "stringlib/fastsearch.h"
563#include "stringlib/partition.h"
564#include "stringlib/split.h"
565#include "stringlib/count.h"
566#include "stringlib/find.h"
567#include "stringlib/find_max_char.h"
568#include "stringlib/localeutil.h"
569#include "stringlib/undef.h"
570
571#include "stringlib/ucs1lib.h"
572#include "stringlib/fastsearch.h"
573#include "stringlib/partition.h"
574#include "stringlib/split.h"
575#include "stringlib/count.h"
576#include "stringlib/find.h"
577#include "stringlib/find_max_char.h"
578#include "stringlib/localeutil.h"
579#include "stringlib/undef.h"
580
581#include "stringlib/ucs2lib.h"
582#include "stringlib/fastsearch.h"
583#include "stringlib/partition.h"
584#include "stringlib/split.h"
585#include "stringlib/count.h"
586#include "stringlib/find.h"
587#include "stringlib/find_max_char.h"
588#include "stringlib/localeutil.h"
589#include "stringlib/undef.h"
590
591#include "stringlib/ucs4lib.h"
592#include "stringlib/fastsearch.h"
593#include "stringlib/partition.h"
594#include "stringlib/split.h"
595#include "stringlib/count.h"
596#include "stringlib/find.h"
597#include "stringlib/find_max_char.h"
598#include "stringlib/localeutil.h"
599#include "stringlib/undef.h"
600
Antoine Pitrouf0b934b2011-10-13 18:55:09 +0200601#include "stringlib/unicodedefs.h"
602#include "stringlib/fastsearch.h"
603#include "stringlib/count.h"
604#include "stringlib/find.h"
Antoine Pitrou0a3229d2011-11-21 20:39:13 +0100605#include "stringlib/undef.h"
Antoine Pitrouf0b934b2011-10-13 18:55:09 +0200606
Guido van Rossumd57fd912000-03-10 22:53:23 +0000607/* --- Unicode Object ----------------------------------------------------- */
608
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200609static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +0200610fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611
Antoine Pitrouf0b934b2011-10-13 18:55:09 +0200612Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind,
613 Py_ssize_t size, Py_UCS4 ch,
614 int direction)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200615{
Antoine Pitrouf0b934b2011-10-13 18:55:09 +0200616 int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH;
617
618 switch (kind) {
619 case PyUnicode_1BYTE_KIND:
620 {
621 Py_UCS1 ch1 = (Py_UCS1) ch;
622 if (ch1 == ch)
623 return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode);
624 else
625 return -1;
626 }
627 case PyUnicode_2BYTE_KIND:
628 {
629 Py_UCS2 ch2 = (Py_UCS2) ch;
630 if (ch2 == ch)
631 return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode);
632 else
633 return -1;
634 }
635 case PyUnicode_4BYTE_KIND:
636 return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode);
637 default:
638 assert(0);
639 return -1;
Victor Stinner9e7a1bc2011-10-13 00:18:12 +0200640 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200641}
642
Victor Stinnerfe226c02011-10-03 03:52:20 +0200643static PyObject*
644resize_compact(PyObject *unicode, Py_ssize_t length)
645{
646 Py_ssize_t char_size;
647 Py_ssize_t struct_size;
648 Py_ssize_t new_size;
649 int share_wstr;
Victor Stinner84def372011-12-11 20:04:56 +0100650 PyObject *new_unicode;
Victor Stinner79891572012-05-03 13:43:07 +0200651 assert(unicode_modifiable(unicode));
Victor Stinnerfe226c02011-10-03 03:52:20 +0200652 assert(PyUnicode_IS_READY(unicode));
Victor Stinner488fa492011-12-12 00:01:39 +0100653 assert(PyUnicode_IS_COMPACT(unicode));
654
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200655 char_size = PyUnicode_KIND(unicode);
Victor Stinner488fa492011-12-12 00:01:39 +0100656 if (PyUnicode_IS_ASCII(unicode))
Victor Stinnerfe226c02011-10-03 03:52:20 +0200657 struct_size = sizeof(PyASCIIObject);
658 else
659 struct_size = sizeof(PyCompactUnicodeObject);
Victor Stinnerc379ead2011-10-03 12:52:27 +0200660 share_wstr = _PyUnicode_SHARE_WSTR(unicode);
Victor Stinnerfe226c02011-10-03 03:52:20 +0200661
Victor Stinnerfe226c02011-10-03 03:52:20 +0200662 if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) {
663 PyErr_NoMemory();
664 return NULL;
665 }
666 new_size = (struct_size + (length + 1) * char_size);
667
Victor Stinner84def372011-12-11 20:04:56 +0100668 _Py_DEC_REFTOTAL;
669 _Py_ForgetReference(unicode);
670
671 new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size);
672 if (new_unicode == NULL) {
Victor Stinnerb0a82a62011-12-12 13:08:33 +0100673 _Py_NewReference(unicode);
Victor Stinnerfe226c02011-10-03 03:52:20 +0200674 PyErr_NoMemory();
675 return NULL;
676 }
Victor Stinner84def372011-12-11 20:04:56 +0100677 unicode = new_unicode;
Victor Stinnerfe226c02011-10-03 03:52:20 +0200678 _Py_NewReference(unicode);
Victor Stinner84def372011-12-11 20:04:56 +0100679
Victor Stinnerfe226c02011-10-03 03:52:20 +0200680 _PyUnicode_LENGTH(unicode) = length;
Victor Stinnerc379ead2011-10-03 12:52:27 +0200681 if (share_wstr) {
Victor Stinnerfe226c02011-10-03 03:52:20 +0200682 _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode);
Victor Stinner488fa492011-12-12 00:01:39 +0100683 if (!PyUnicode_IS_ASCII(unicode))
Victor Stinnerc379ead2011-10-03 12:52:27 +0200684 _PyUnicode_WSTR_LENGTH(unicode) = length;
685 }
Victor Stinnerfe226c02011-10-03 03:52:20 +0200686 PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
687 length, 0);
Victor Stinner79891572012-05-03 13:43:07 +0200688 assert(_PyUnicode_CheckConsistency(unicode, 0));
Victor Stinnerfe226c02011-10-03 03:52:20 +0200689 return unicode;
690}
691
Alexander Belopolsky40018472011-02-26 01:02:56 +0000692static int
Victor Stinner9db1a8b2011-10-23 20:04:37 +0200693resize_inplace(PyObject *unicode, Py_ssize_t length)
Guido van Rossumd57fd912000-03-10 22:53:23 +0000694{
Victor Stinner95663112011-10-04 01:03:50 +0200695 wchar_t *wstr;
Victor Stinner7a9105a2011-12-12 00:13:42 +0100696 Py_ssize_t new_size;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200697 assert(!PyUnicode_IS_COMPACT(unicode));
Victor Stinnerfe226c02011-10-03 03:52:20 +0200698 assert(Py_REFCNT(unicode) == 1);
Tim Petersced69f82003-09-16 20:30:58 +0000699
Victor Stinnerfe226c02011-10-03 03:52:20 +0200700 if (PyUnicode_IS_READY(unicode)) {
701 Py_ssize_t char_size;
Victor Stinner1c8d0c72011-10-03 12:11:00 +0200702 int share_wstr, share_utf8;
Victor Stinnerfe226c02011-10-03 03:52:20 +0200703 void *data;
704
705 data = _PyUnicode_DATA_ANY(unicode);
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200706 char_size = PyUnicode_KIND(unicode);
Victor Stinnerc379ead2011-10-03 12:52:27 +0200707 share_wstr = _PyUnicode_SHARE_WSTR(unicode);
708 share_utf8 = _PyUnicode_SHARE_UTF8(unicode);
Victor Stinnerfe226c02011-10-03 03:52:20 +0200709
710 if (length > (PY_SSIZE_T_MAX / char_size - 1)) {
711 PyErr_NoMemory();
712 return -1;
713 }
714 new_size = (length + 1) * char_size;
715
Victor Stinner7a9105a2011-12-12 00:13:42 +0100716 if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode))
717 {
718 PyObject_DEL(_PyUnicode_UTF8(unicode));
719 _PyUnicode_UTF8(unicode) = NULL;
720 _PyUnicode_UTF8_LENGTH(unicode) = 0;
721 }
722
Victor Stinnerfe226c02011-10-03 03:52:20 +0200723 data = (PyObject *)PyObject_REALLOC(data, new_size);
724 if (data == NULL) {
725 PyErr_NoMemory();
726 return -1;
727 }
728 _PyUnicode_DATA_ANY(unicode) = data;
Victor Stinnerc379ead2011-10-03 12:52:27 +0200729 if (share_wstr) {
Victor Stinnerfe226c02011-10-03 03:52:20 +0200730 _PyUnicode_WSTR(unicode) = data;
Victor Stinnerc379ead2011-10-03 12:52:27 +0200731 _PyUnicode_WSTR_LENGTH(unicode) = length;
732 }
733 if (share_utf8) {
Victor Stinner1c8d0c72011-10-03 12:11:00 +0200734 _PyUnicode_UTF8(unicode) = data;
Victor Stinnerc379ead2011-10-03 12:52:27 +0200735 _PyUnicode_UTF8_LENGTH(unicode) = length;
736 }
Victor Stinnerfe226c02011-10-03 03:52:20 +0200737 _PyUnicode_LENGTH(unicode) = length;
738 PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
Victor Stinner95663112011-10-04 01:03:50 +0200739 if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) {
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200740 assert(_PyUnicode_CheckConsistency(unicode, 0));
Victor Stinnerfe226c02011-10-03 03:52:20 +0200741 return 0;
Victor Stinnerfe226c02011-10-03 03:52:20 +0200742 }
Victor Stinnerfe226c02011-10-03 03:52:20 +0200743 }
Victor Stinner95663112011-10-04 01:03:50 +0200744 assert(_PyUnicode_WSTR(unicode) != NULL);
745
746 /* check for integer overflow */
747 if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
748 PyErr_NoMemory();
749 return -1;
750 }
Victor Stinner7a9105a2011-12-12 00:13:42 +0100751 new_size = sizeof(wchar_t) * (length + 1);
Victor Stinner95663112011-10-04 01:03:50 +0200752 wstr = _PyUnicode_WSTR(unicode);
Victor Stinner7a9105a2011-12-12 00:13:42 +0100753 wstr = PyObject_REALLOC(wstr, new_size);
Victor Stinner95663112011-10-04 01:03:50 +0200754 if (!wstr) {
755 PyErr_NoMemory();
756 return -1;
757 }
758 _PyUnicode_WSTR(unicode) = wstr;
759 _PyUnicode_WSTR(unicode)[length] = 0;
760 _PyUnicode_WSTR_LENGTH(unicode) = length;
Victor Stinnerbb10a1f2011-10-05 01:34:17 +0200761 assert(_PyUnicode_CheckConsistency(unicode, 0));
Guido van Rossumd57fd912000-03-10 22:53:23 +0000762 return 0;
763}
764
Victor Stinnerfe226c02011-10-03 03:52:20 +0200765static PyObject*
766resize_copy(PyObject *unicode, Py_ssize_t length)
767{
768 Py_ssize_t copy_length;
Victor Stinner7a9105a2011-12-12 00:13:42 +0100769 if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) {
Victor Stinnerfe226c02011-10-03 03:52:20 +0200770 PyObject *copy;
Victor Stinner7a9105a2011-12-12 00:13:42 +0100771
Benjamin Petersonbac79492012-01-14 13:34:47 -0500772 if (PyUnicode_READY(unicode) == -1)
Victor Stinner7a9105a2011-12-12 00:13:42 +0100773 return NULL;
Victor Stinnerfe226c02011-10-03 03:52:20 +0200774
775 copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
776 if (copy == NULL)
777 return NULL;
778
779 copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode));
Victor Stinnerd3f08822012-05-29 12:57:52 +0200780 _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length);
Victor Stinnerfe226c02011-10-03 03:52:20 +0200781 return copy;
Victor Stinner8cfcbed2011-10-03 23:19:21 +0200782 }
783 else {
Victor Stinner9db1a8b2011-10-23 20:04:37 +0200784 PyObject *w;
Victor Stinner7a9105a2011-12-12 00:13:42 +0100785
Victor Stinner9db1a8b2011-10-23 20:04:37 +0200786 w = (PyObject*)_PyUnicode_New(length);
Victor Stinnerfe226c02011-10-03 03:52:20 +0200787 if (w == NULL)
788 return NULL;
789 copy_length = _PyUnicode_WSTR_LENGTH(unicode);
790 copy_length = Py_MIN(copy_length, length);
791 Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode),
792 copy_length);
Victor Stinner9db1a8b2011-10-23 20:04:37 +0200793 return w;
Victor Stinnerfe226c02011-10-03 03:52:20 +0200794 }
795}
796
Guido van Rossumd57fd912000-03-10 22:53:23 +0000797/* We allocate one more byte to make sure the string is
Martin v. Löwis47383402007-08-15 07:32:56 +0000798 Ux0000 terminated; some code (e.g. new_identifier)
799 relies on that.
Guido van Rossumd57fd912000-03-10 22:53:23 +0000800
801 XXX This allocator could further be enhanced by assuring that the
Benjamin Peterson29060642009-01-31 22:14:21 +0000802 free list never reduces its size below 1.
Guido van Rossumd57fd912000-03-10 22:53:23 +0000803
804*/
805
Alexander Belopolsky40018472011-02-26 01:02:56 +0000806static PyUnicodeObject *
807_PyUnicode_New(Py_ssize_t length)
Guido van Rossumd57fd912000-03-10 22:53:23 +0000808{
809 register PyUnicodeObject *unicode;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200810 size_t new_size;
Guido van Rossumd57fd912000-03-10 22:53:23 +0000811
Thomas Wouters477c8d52006-05-27 19:21:47 +0000812 /* Optimization for empty strings */
Guido van Rossumd57fd912000-03-10 22:53:23 +0000813 if (length == 0 && unicode_empty != NULL) {
814 Py_INCREF(unicode_empty);
Victor Stinnera464fc12011-10-02 20:39:30 +0200815 return (PyUnicodeObject*)unicode_empty;
Guido van Rossumd57fd912000-03-10 22:53:23 +0000816 }
817
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000818 /* Ensure we won't overflow the size. */
819 if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
820 return (PyUnicodeObject *)PyErr_NoMemory();
821 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200822 if (length < 0) {
823 PyErr_SetString(PyExc_SystemError,
824 "Negative size passed to _PyUnicode_New");
825 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +0000826 }
827
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200828 unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
829 if (unicode == NULL)
830 return NULL;
831 new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
832 _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size);
833 if (!_PyUnicode_WSTR(unicode)) {
Victor Stinnerb0a82a62011-12-12 13:08:33 +0100834 Py_DECREF(unicode);
Benjamin Peterson29060642009-01-31 22:14:21 +0000835 PyErr_NoMemory();
Victor Stinnerb0a82a62011-12-12 13:08:33 +0100836 return NULL;
Guido van Rossum3c1bb802000-04-27 20:13:50 +0000837 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200838
Jeremy Hyltond8082792003-09-16 19:41:39 +0000839 /* Initialize the first element to guard against cases where
Tim Petersced69f82003-09-16 20:30:58 +0000840 * the caller fails before initializing str -- unicode_resize()
841 * reads str[0], and the Keep-Alive optimization can keep memory
842 * allocated for str alive across a call to unicode_dealloc(unicode).
843 * We don't want unicode_resize to read uninitialized memory in
844 * that case.
845 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200846 _PyUnicode_WSTR(unicode)[0] = 0;
847 _PyUnicode_WSTR(unicode)[length] = 0;
848 _PyUnicode_WSTR_LENGTH(unicode) = length;
849 _PyUnicode_HASH(unicode) = -1;
850 _PyUnicode_STATE(unicode).interned = 0;
851 _PyUnicode_STATE(unicode).kind = 0;
852 _PyUnicode_STATE(unicode).compact = 0;
853 _PyUnicode_STATE(unicode).ready = 0;
854 _PyUnicode_STATE(unicode).ascii = 0;
Victor Stinnerc3c74152011-10-02 20:39:55 +0200855 _PyUnicode_DATA_ANY(unicode) = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200856 _PyUnicode_LENGTH(unicode) = 0;
Victor Stinnere90fe6a2011-10-01 16:48:13 +0200857 _PyUnicode_UTF8(unicode) = NULL;
858 _PyUnicode_UTF8_LENGTH(unicode) = 0;
Victor Stinner7931d9a2011-11-04 00:22:48 +0100859 assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0));
Guido van Rossumd57fd912000-03-10 22:53:23 +0000860 return unicode;
861}
862
Victor Stinnerf42dc442011-10-02 23:33:16 +0200863static const char*
864unicode_kind_name(PyObject *unicode)
865{
Victor Stinner42dfd712011-10-03 14:41:45 +0200866 /* don't check consistency: unicode_kind_name() is called from
867 _PyUnicode_Dump() */
Victor Stinnerf42dc442011-10-02 23:33:16 +0200868 if (!PyUnicode_IS_COMPACT(unicode))
869 {
870 if (!PyUnicode_IS_READY(unicode))
871 return "wstr";
Benjamin Petersonead6b532011-12-20 17:23:42 -0600872 switch (PyUnicode_KIND(unicode))
Victor Stinnerf42dc442011-10-02 23:33:16 +0200873 {
874 case PyUnicode_1BYTE_KIND:
Victor Stinnera3b334d2011-10-03 13:53:37 +0200875 if (PyUnicode_IS_ASCII(unicode))
Victor Stinnerf42dc442011-10-02 23:33:16 +0200876 return "legacy ascii";
877 else
878 return "legacy latin1";
879 case PyUnicode_2BYTE_KIND:
880 return "legacy UCS2";
881 case PyUnicode_4BYTE_KIND:
882 return "legacy UCS4";
883 default:
884 return "<legacy invalid kind>";
885 }
886 }
887 assert(PyUnicode_IS_READY(unicode));
Benjamin Petersonead6b532011-12-20 17:23:42 -0600888 switch (PyUnicode_KIND(unicode)) {
Victor Stinnerf42dc442011-10-02 23:33:16 +0200889 case PyUnicode_1BYTE_KIND:
Victor Stinnera3b334d2011-10-03 13:53:37 +0200890 if (PyUnicode_IS_ASCII(unicode))
Victor Stinnerf42dc442011-10-02 23:33:16 +0200891 return "ascii";
892 else
Victor Stinnera3b334d2011-10-03 13:53:37 +0200893 return "latin1";
Victor Stinnerf42dc442011-10-02 23:33:16 +0200894 case PyUnicode_2BYTE_KIND:
Victor Stinnera3b334d2011-10-03 13:53:37 +0200895 return "UCS2";
Victor Stinnerf42dc442011-10-02 23:33:16 +0200896 case PyUnicode_4BYTE_KIND:
Victor Stinnera3b334d2011-10-03 13:53:37 +0200897 return "UCS4";
Victor Stinnerf42dc442011-10-02 23:33:16 +0200898 default:
899 return "<invalid compact kind>";
900 }
901}
902
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200903#ifdef Py_DEBUG
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904/* Functions wrapping macros for use in debugger */
905char *_PyUnicode_utf8(void *unicode){
Victor Stinnere90fe6a2011-10-01 16:48:13 +0200906 return PyUnicode_UTF8(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200907}
908
909void *_PyUnicode_compact_data(void *unicode) {
910 return _PyUnicode_COMPACT_DATA(unicode);
911}
912void *_PyUnicode_data(void *unicode){
913 printf("obj %p\n", unicode);
914 printf("compact %d\n", PyUnicode_IS_COMPACT(unicode));
915 printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode));
916 printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1)));
917 printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1)));
918 printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode));
919 return PyUnicode_DATA(unicode);
920}
Victor Stinnerfe0c1552011-10-03 02:59:31 +0200921
922void
923_PyUnicode_Dump(PyObject *op)
924{
925 PyASCIIObject *ascii = (PyASCIIObject *)op;
Victor Stinnera849a4b2011-10-03 12:12:11 +0200926 PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op;
927 PyUnicodeObject *unicode = (PyUnicodeObject *)op;
928 void *data;
Victor Stinner0d60e872011-10-23 19:47:19 +0200929
Victor Stinnera849a4b2011-10-03 12:12:11 +0200930 if (ascii->state.compact)
Victor Stinner0d60e872011-10-23 19:47:19 +0200931 {
932 if (ascii->state.ascii)
933 data = (ascii + 1);
934 else
935 data = (compact + 1);
936 }
Victor Stinnera849a4b2011-10-03 12:12:11 +0200937 else
938 data = unicode->data.any;
Victor Stinner0d60e872011-10-23 19:47:19 +0200939 printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length);
940
Victor Stinnera849a4b2011-10-03 12:12:11 +0200941 if (ascii->wstr == data)
942 printf("shared ");
943 printf("wstr=%p", ascii->wstr);
Victor Stinner0d60e872011-10-23 19:47:19 +0200944
Victor Stinnera3b334d2011-10-03 13:53:37 +0200945 if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) {
Victor Stinnera849a4b2011-10-03 12:12:11 +0200946 printf(" (%zu), ", compact->wstr_length);
947 if (!ascii->state.compact && compact->utf8 == unicode->data.any)
948 printf("shared ");
949 printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length);
Victor Stinnerfe0c1552011-10-03 02:59:31 +0200950 }
Victor Stinnera849a4b2011-10-03 12:12:11 +0200951 printf(", data=%p\n", data);
Victor Stinnerfe0c1552011-10-03 02:59:31 +0200952}
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953#endif
954
955PyObject *
956PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
957{
958 PyObject *obj;
959 PyCompactUnicodeObject *unicode;
960 void *data;
Victor Stinner8f825062012-04-27 13:55:39 +0200961 enum PyUnicode_Kind kind;
Victor Stinner9e9d6892011-10-04 01:02:02 +0200962 int is_sharing, is_ascii;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200963 Py_ssize_t char_size;
964 Py_ssize_t struct_size;
965
966 /* Optimization for empty strings */
967 if (size == 0 && unicode_empty != NULL) {
968 Py_INCREF(unicode_empty);
Victor Stinnera464fc12011-10-02 20:39:30 +0200969 return unicode_empty;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200970 }
971
Victor Stinner9e9d6892011-10-04 01:02:02 +0200972 is_ascii = 0;
973 is_sharing = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200974 struct_size = sizeof(PyCompactUnicodeObject);
975 if (maxchar < 128) {
Victor Stinner8f825062012-04-27 13:55:39 +0200976 kind = PyUnicode_1BYTE_KIND;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200977 char_size = 1;
978 is_ascii = 1;
979 struct_size = sizeof(PyASCIIObject);
980 }
981 else if (maxchar < 256) {
Victor Stinner8f825062012-04-27 13:55:39 +0200982 kind = PyUnicode_1BYTE_KIND;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200983 char_size = 1;
984 }
985 else if (maxchar < 65536) {
Victor Stinner8f825062012-04-27 13:55:39 +0200986 kind = PyUnicode_2BYTE_KIND;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200987 char_size = 2;
988 if (sizeof(wchar_t) == 2)
989 is_sharing = 1;
990 }
991 else {
Victor Stinnerc9590ad2012-03-04 01:34:37 +0100992 if (maxchar > MAX_UNICODE) {
993 PyErr_SetString(PyExc_SystemError,
994 "invalid maximum character passed to PyUnicode_New");
995 return NULL;
996 }
Victor Stinner8f825062012-04-27 13:55:39 +0200997 kind = PyUnicode_4BYTE_KIND;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200998 char_size = 4;
999 if (sizeof(wchar_t) == 4)
1000 is_sharing = 1;
1001 }
1002
1003 /* Ensure we won't overflow the size. */
1004 if (size < 0) {
1005 PyErr_SetString(PyExc_SystemError,
1006 "Negative size passed to PyUnicode_New");
1007 return NULL;
1008 }
1009 if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1))
1010 return PyErr_NoMemory();
1011
1012 /* Duplicated allocation code from _PyObject_New() instead of a call to
1013 * PyObject_New() so we are able to allocate space for the object and
1014 * it's data buffer.
1015 */
1016 obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size);
1017 if (obj == NULL)
1018 return PyErr_NoMemory();
1019 obj = PyObject_INIT(obj, &PyUnicode_Type);
1020 if (obj == NULL)
1021 return NULL;
1022
1023 unicode = (PyCompactUnicodeObject *)obj;
1024 if (is_ascii)
1025 data = ((PyASCIIObject*)obj) + 1;
1026 else
1027 data = unicode + 1;
1028 _PyUnicode_LENGTH(unicode) = size;
1029 _PyUnicode_HASH(unicode) = -1;
1030 _PyUnicode_STATE(unicode).interned = 0;
Victor Stinner8f825062012-04-27 13:55:39 +02001031 _PyUnicode_STATE(unicode).kind = kind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001032 _PyUnicode_STATE(unicode).compact = 1;
1033 _PyUnicode_STATE(unicode).ready = 1;
1034 _PyUnicode_STATE(unicode).ascii = is_ascii;
1035 if (is_ascii) {
1036 ((char*)data)[size] = 0;
1037 _PyUnicode_WSTR(unicode) = NULL;
1038 }
Victor Stinner8f825062012-04-27 13:55:39 +02001039 else if (kind == PyUnicode_1BYTE_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001040 ((char*)data)[size] = 0;
1041 _PyUnicode_WSTR(unicode) = NULL;
1042 _PyUnicode_WSTR_LENGTH(unicode) = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001043 unicode->utf8 = NULL;
Victor Stinner9e9d6892011-10-04 01:02:02 +02001044 unicode->utf8_length = 0;
Victor Stinner8f825062012-04-27 13:55:39 +02001045 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001046 else {
1047 unicode->utf8 = NULL;
Victor Stinner9e9d6892011-10-04 01:02:02 +02001048 unicode->utf8_length = 0;
Victor Stinner8f825062012-04-27 13:55:39 +02001049 if (kind == PyUnicode_2BYTE_KIND)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001050 ((Py_UCS2*)data)[size] = 0;
Victor Stinner8f825062012-04-27 13:55:39 +02001051 else /* kind == PyUnicode_4BYTE_KIND */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001052 ((Py_UCS4*)data)[size] = 0;
1053 if (is_sharing) {
1054 _PyUnicode_WSTR_LENGTH(unicode) = size;
1055 _PyUnicode_WSTR(unicode) = (wchar_t *)data;
1056 }
1057 else {
1058 _PyUnicode_WSTR_LENGTH(unicode) = 0;
1059 _PyUnicode_WSTR(unicode) = NULL;
1060 }
1061 }
Victor Stinner8f825062012-04-27 13:55:39 +02001062#ifdef Py_DEBUG
1063 /* Fill the data with invalid characters to detect bugs earlier.
1064 _PyUnicode_CheckConsistency(str, 1) detects invalid characters,
1065 at least for ASCII and UCS-4 strings. U+00FF is invalid in ASCII
1066 and U+FFFFFFFF is an invalid character in Unicode 6.0. */
1067 memset(data, 0xff, size * kind);
1068#endif
Victor Stinner7931d9a2011-11-04 00:22:48 +01001069 assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001070 return obj;
1071}
1072
1073#if SIZEOF_WCHAR_T == 2
1074/* Helper function to convert a 16-bits wchar_t representation to UCS4, this
1075 will decode surrogate pairs, the other conversions are implemented as macros
Georg Brandl7597add2011-10-05 16:36:47 +02001076 for efficiency.
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001077
1078 This function assumes that unicode can hold one more code point than wstr
1079 characters for a terminating null character. */
Victor Stinnerc53be962011-10-02 21:33:54 +02001080static void
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001081unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end,
Victor Stinner9db1a8b2011-10-23 20:04:37 +02001082 PyObject *unicode)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001083{
1084 const wchar_t *iter;
1085 Py_UCS4 *ucs4_out;
1086
Victor Stinner910337b2011-10-03 03:20:16 +02001087 assert(unicode != NULL);
1088 assert(_PyUnicode_CHECK(unicode));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001089 assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND);
1090 ucs4_out = PyUnicode_4BYTE_DATA(unicode);
1091
1092 for (iter = begin; iter < end; ) {
1093 assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) +
1094 _PyUnicode_GET_LENGTH(unicode)));
Victor Stinner551ac952011-11-29 22:58:13 +01001095 if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])
1096 && (iter+1) < end
1097 && Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001098 {
Victor Stinner551ac952011-11-29 22:58:13 +01001099 *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001100 iter += 2;
1101 }
1102 else {
1103 *ucs4_out++ = *iter;
1104 iter++;
1105 }
1106 }
1107 assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) +
1108 _PyUnicode_GET_LENGTH(unicode)));
1109
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001110}
1111#endif
1112
Victor Stinnercd9950f2011-10-02 00:34:53 +02001113static int
Victor Stinner488fa492011-12-12 00:01:39 +01001114unicode_check_modifiable(PyObject *unicode)
Victor Stinnercd9950f2011-10-02 00:34:53 +02001115{
Victor Stinner488fa492011-12-12 00:01:39 +01001116 if (!unicode_modifiable(unicode)) {
Victor Stinner01698042011-10-04 00:04:26 +02001117 PyErr_SetString(PyExc_SystemError,
Victor Stinner488fa492011-12-12 00:01:39 +01001118 "Cannot modify a string currently used");
Victor Stinnercd9950f2011-10-02 00:34:53 +02001119 return -1;
1120 }
Victor Stinnercd9950f2011-10-02 00:34:53 +02001121 return 0;
1122}
1123
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001124static int
1125_copy_characters(PyObject *to, Py_ssize_t to_start,
1126 PyObject *from, Py_ssize_t from_start,
1127 Py_ssize_t how_many, int check_maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001128{
Victor Stinnera0702ab2011-09-29 14:14:38 +02001129 unsigned int from_kind, to_kind;
1130 void *from_data, *to_data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001131
Victor Stinneree4544c2012-05-09 22:24:08 +02001132 assert(0 <= how_many);
1133 assert(0 <= from_start);
1134 assert(0 <= to_start);
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001135 assert(PyUnicode_Check(from));
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001136 assert(PyUnicode_IS_READY(from));
Victor Stinneree4544c2012-05-09 22:24:08 +02001137 assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001138
Victor Stinnerd3f08822012-05-29 12:57:52 +02001139 assert(PyUnicode_Check(to));
1140 assert(PyUnicode_IS_READY(to));
1141 assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
1142
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001143 if (how_many == 0)
1144 return 0;
1145
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001146 from_kind = PyUnicode_KIND(from);
Victor Stinnera0702ab2011-09-29 14:14:38 +02001147 from_data = PyUnicode_DATA(from);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001148 to_kind = PyUnicode_KIND(to);
Victor Stinnera0702ab2011-09-29 14:14:38 +02001149 to_data = PyUnicode_DATA(to);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001150
Victor Stinnerf1852262012-06-16 16:38:26 +02001151#ifdef Py_DEBUG
1152 if (!check_maxchar
1153 && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to))
1154 {
1155 const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
1156 Py_UCS4 ch;
1157 Py_ssize_t i;
1158 for (i=0; i < how_many; i++) {
1159 ch = PyUnicode_READ(from_kind, from_data, from_start + i);
1160 assert(ch <= to_maxchar);
1161 }
1162 }
1163#endif
1164
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001165 if (from_kind == to_kind) {
Victor Stinnerf1852262012-06-16 16:38:26 +02001166 if (check_maxchar
1167 && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))
1168 {
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001169 /* Writing Latin-1 characters into an ASCII string requires to
1170 check that all written characters are pure ASCII */
Victor Stinnerf1852262012-06-16 16:38:26 +02001171 Py_UCS4 max_char;
1172 max_char = ucs1lib_find_max_char(from_data,
1173 (Py_UCS1*)from_data + how_many);
1174 if (max_char >= 128)
1175 return -1;
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001176 }
Martin v. Löwisc47adb02011-10-07 20:55:35 +02001177 Py_MEMCPY((char*)to_data + to_kind * to_start,
1178 (char*)from_data + from_kind * from_start,
1179 to_kind * how_many);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001180 }
Victor Stinnera0702ab2011-09-29 14:14:38 +02001181 else if (from_kind == PyUnicode_1BYTE_KIND
1182 && to_kind == PyUnicode_2BYTE_KIND)
Victor Stinnerbe78eaf2011-09-28 21:37:03 +02001183 {
1184 _PyUnicode_CONVERT_BYTES(
1185 Py_UCS1, Py_UCS2,
1186 PyUnicode_1BYTE_DATA(from) + from_start,
1187 PyUnicode_1BYTE_DATA(from) + from_start + how_many,
1188 PyUnicode_2BYTE_DATA(to) + to_start
1189 );
Victor Stinnerbe78eaf2011-09-28 21:37:03 +02001190 }
Victor Stinner157f83f2011-09-28 21:41:31 +02001191 else if (from_kind == PyUnicode_1BYTE_KIND
Victor Stinnerbe78eaf2011-09-28 21:37:03 +02001192 && to_kind == PyUnicode_4BYTE_KIND)
1193 {
1194 _PyUnicode_CONVERT_BYTES(
1195 Py_UCS1, Py_UCS4,
1196 PyUnicode_1BYTE_DATA(from) + from_start,
1197 PyUnicode_1BYTE_DATA(from) + from_start + how_many,
1198 PyUnicode_4BYTE_DATA(to) + to_start
1199 );
Victor Stinnerbe78eaf2011-09-28 21:37:03 +02001200 }
1201 else if (from_kind == PyUnicode_2BYTE_KIND
1202 && to_kind == PyUnicode_4BYTE_KIND)
1203 {
1204 _PyUnicode_CONVERT_BYTES(
1205 Py_UCS2, Py_UCS4,
1206 PyUnicode_2BYTE_DATA(from) + from_start,
1207 PyUnicode_2BYTE_DATA(from) + from_start + how_many,
1208 PyUnicode_4BYTE_DATA(to) + to_start
1209 );
Victor Stinnerbe78eaf2011-09-28 21:37:03 +02001210 }
Victor Stinnera0702ab2011-09-29 14:14:38 +02001211 else {
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001212 assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to));
1213
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001214 if (!check_maxchar) {
1215 if (from_kind == PyUnicode_2BYTE_KIND
1216 && to_kind == PyUnicode_1BYTE_KIND)
1217 {
1218 _PyUnicode_CONVERT_BYTES(
1219 Py_UCS2, Py_UCS1,
1220 PyUnicode_2BYTE_DATA(from) + from_start,
1221 PyUnicode_2BYTE_DATA(from) + from_start + how_many,
1222 PyUnicode_1BYTE_DATA(to) + to_start
1223 );
1224 }
1225 else if (from_kind == PyUnicode_4BYTE_KIND
1226 && to_kind == PyUnicode_1BYTE_KIND)
1227 {
1228 _PyUnicode_CONVERT_BYTES(
1229 Py_UCS4, Py_UCS1,
1230 PyUnicode_4BYTE_DATA(from) + from_start,
1231 PyUnicode_4BYTE_DATA(from) + from_start + how_many,
1232 PyUnicode_1BYTE_DATA(to) + to_start
1233 );
1234 }
1235 else if (from_kind == PyUnicode_4BYTE_KIND
1236 && to_kind == PyUnicode_2BYTE_KIND)
1237 {
1238 _PyUnicode_CONVERT_BYTES(
1239 Py_UCS4, Py_UCS2,
1240 PyUnicode_4BYTE_DATA(from) + from_start,
1241 PyUnicode_4BYTE_DATA(from) + from_start + how_many,
1242 PyUnicode_2BYTE_DATA(to) + to_start
1243 );
1244 }
1245 else {
1246 assert(0);
1247 return -1;
1248 }
1249 }
Victor Stinnerf1852262012-06-16 16:38:26 +02001250 else {
Victor Stinnera0702ab2011-09-29 14:14:38 +02001251 const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001252 Py_UCS4 ch;
Victor Stinnera0702ab2011-09-29 14:14:38 +02001253 Py_ssize_t i;
1254
Victor Stinnera0702ab2011-09-29 14:14:38 +02001255 for (i=0; i < how_many; i++) {
1256 ch = PyUnicode_READ(from_kind, from_data, from_start + i);
Victor Stinnerc9d369f2012-06-16 02:22:37 +02001257 if (ch > to_maxchar)
1258 return -1;
Victor Stinnera0702ab2011-09-29 14:14:38 +02001259 PyUnicode_WRITE(to_kind, to_data, to_start + i, ch);
1260 }
Victor Stinnera0702ab2011-09-29 14:14:38 +02001261 }
1262 }
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001263 return 0;
1264}
1265
Victor Stinnerd3f08822012-05-29 12:57:52 +02001266void
1267_PyUnicode_FastCopyCharacters(
1268 PyObject *to, Py_ssize_t to_start,
1269 PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many)
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001270{
1271 (void)_copy_characters(to, to_start, from, from_start, how_many, 0);
1272}
1273
1274Py_ssize_t
1275PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start,
1276 PyObject *from, Py_ssize_t from_start,
1277 Py_ssize_t how_many)
1278{
1279 int err;
1280
1281 if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) {
1282 PyErr_BadInternalCall();
1283 return -1;
1284 }
1285
Benjamin Petersonbac79492012-01-14 13:34:47 -05001286 if (PyUnicode_READY(from) == -1)
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001287 return -1;
Benjamin Petersonbac79492012-01-14 13:34:47 -05001288 if (PyUnicode_READY(to) == -1)
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001289 return -1;
1290
Victor Stinnerd3f08822012-05-29 12:57:52 +02001291 if (from_start < 0) {
1292 PyErr_SetString(PyExc_IndexError, "string index out of range");
1293 return -1;
1294 }
1295 if (to_start < 0) {
1296 PyErr_SetString(PyExc_IndexError, "string index out of range");
1297 return -1;
1298 }
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001299 how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many);
1300 if (to_start + how_many > PyUnicode_GET_LENGTH(to)) {
1301 PyErr_Format(PyExc_SystemError,
1302 "Cannot write %zi characters at %zi "
1303 "in a string of %zi characters",
1304 how_many, to_start, PyUnicode_GET_LENGTH(to));
1305 return -1;
1306 }
1307
1308 if (how_many == 0)
1309 return 0;
1310
Victor Stinner488fa492011-12-12 00:01:39 +01001311 if (unicode_check_modifiable(to))
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001312 return -1;
1313
1314 err = _copy_characters(to, to_start, from, from_start, how_many, 1);
1315 if (err) {
1316 PyErr_Format(PyExc_SystemError,
1317 "Cannot copy %s characters "
1318 "into a string of %s characters",
1319 unicode_kind_name(from),
1320 unicode_kind_name(to));
1321 return -1;
1322 }
Victor Stinnera0702ab2011-09-29 14:14:38 +02001323 return how_many;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001324}
1325
Victor Stinner17222162011-09-28 22:15:37 +02001326/* Find the maximum code point and count the number of surrogate pairs so a
1327 correct string length can be computed before converting a string to UCS4.
1328 This function counts single surrogates as a character and not as a pair.
1329
1330 Return 0 on success, or -1 on error. */
1331static int
1332find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end,
1333 Py_UCS4 *maxchar, Py_ssize_t *num_surrogates)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001334{
1335 const wchar_t *iter;
Victor Stinner8faf8212011-12-08 22:14:11 +01001336 Py_UCS4 ch;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001337
Victor Stinnerc53be962011-10-02 21:33:54 +02001338 assert(num_surrogates != NULL && maxchar != NULL);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001339 *num_surrogates = 0;
1340 *maxchar = 0;
1341
1342 for (iter = begin; iter < end; ) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001343#if SIZEOF_WCHAR_T == 2
Victor Stinnerca4f2072011-11-22 03:38:40 +01001344 if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])
1345 && (iter+1) < end
1346 && Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001347 {
Victor Stinner8faf8212011-12-08 22:14:11 +01001348 ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001349 ++(*num_surrogates);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001350 iter += 2;
1351 }
1352 else
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001353#endif
Victor Stinner8faf8212011-12-08 22:14:11 +01001354 {
1355 ch = *iter;
1356 iter++;
1357 }
1358 if (ch > *maxchar) {
1359 *maxchar = ch;
1360 if (*maxchar > MAX_UNICODE) {
1361 PyErr_Format(PyExc_ValueError,
1362 "character U+%x is not in range [U+0000; U+10ffff]",
1363 ch);
1364 return -1;
1365 }
1366 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001367 }
1368 return 0;
1369}
1370
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001371int
1372_PyUnicode_Ready(PyObject *unicode)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001373{
1374 wchar_t *end;
1375 Py_UCS4 maxchar = 0;
1376 Py_ssize_t num_surrogates;
1377#if SIZEOF_WCHAR_T == 2
1378 Py_ssize_t length_wo_surrogates;
1379#endif
1380
Georg Brandl7597add2011-10-05 16:36:47 +02001381 /* _PyUnicode_Ready() is only intended for old-style API usage where
Victor Stinnerd8f65102011-09-29 19:43:17 +02001382 strings were created using _PyObject_New() and where no canonical
1383 representation (the str field) has been set yet aka strings
1384 which are not yet ready. */
Victor Stinner910337b2011-10-03 03:20:16 +02001385 assert(_PyUnicode_CHECK(unicode));
1386 assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001387 assert(_PyUnicode_WSTR(unicode) != NULL);
Victor Stinnerc3c74152011-10-02 20:39:55 +02001388 assert(_PyUnicode_DATA_ANY(unicode) == NULL);
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001389 assert(_PyUnicode_UTF8(unicode) == NULL);
Victor Stinnerd8f65102011-09-29 19:43:17 +02001390 /* Actually, it should neither be interned nor be anything else: */
1391 assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001392
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001393 end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode);
Victor Stinner17222162011-09-28 22:15:37 +02001394 if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end,
Victor Stinnerd8f65102011-09-29 19:43:17 +02001395 &maxchar, &num_surrogates) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001396 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001397
1398 if (maxchar < 256) {
Victor Stinnerc3c74152011-10-02 20:39:55 +02001399 _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1);
1400 if (!_PyUnicode_DATA_ANY(unicode)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001401 PyErr_NoMemory();
1402 return -1;
1403 }
Victor Stinnerfb5f5f22011-09-28 21:39:49 +02001404 _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001405 _PyUnicode_WSTR(unicode), end,
1406 PyUnicode_1BYTE_DATA(unicode));
1407 PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
1408 _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
1409 _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND;
1410 if (maxchar < 128) {
Victor Stinnera3b334d2011-10-03 13:53:37 +02001411 _PyUnicode_STATE(unicode).ascii = 1;
Victor Stinnerc3c74152011-10-02 20:39:55 +02001412 _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode);
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001413 _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001414 }
1415 else {
Victor Stinnera3b334d2011-10-03 13:53:37 +02001416 _PyUnicode_STATE(unicode).ascii = 0;
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001417 _PyUnicode_UTF8(unicode) = NULL;
1418 _PyUnicode_UTF8_LENGTH(unicode) = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001419 }
1420 PyObject_FREE(_PyUnicode_WSTR(unicode));
1421 _PyUnicode_WSTR(unicode) = NULL;
1422 _PyUnicode_WSTR_LENGTH(unicode) = 0;
1423 }
1424 /* In this case we might have to convert down from 4-byte native
1425 wchar_t to 2-byte unicode. */
1426 else if (maxchar < 65536) {
1427 assert(num_surrogates == 0 &&
1428 "FindMaxCharAndNumSurrogatePairs() messed up");
1429
Victor Stinner506f5922011-09-28 22:34:18 +02001430#if SIZEOF_WCHAR_T == 2
1431 /* We can share representations and are done. */
Victor Stinnerc3c74152011-10-02 20:39:55 +02001432 _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode);
Victor Stinner506f5922011-09-28 22:34:18 +02001433 PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
1434 _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
1435 _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND;
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001436 _PyUnicode_UTF8(unicode) = NULL;
1437 _PyUnicode_UTF8_LENGTH(unicode) = 0;
Victor Stinner506f5922011-09-28 22:34:18 +02001438#else
1439 /* sizeof(wchar_t) == 4 */
Victor Stinnerc3c74152011-10-02 20:39:55 +02001440 _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(
Victor Stinner506f5922011-09-28 22:34:18 +02001441 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1));
Victor Stinnerc3c74152011-10-02 20:39:55 +02001442 if (!_PyUnicode_DATA_ANY(unicode)) {
Victor Stinner506f5922011-09-28 22:34:18 +02001443 PyErr_NoMemory();
1444 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001445 }
Victor Stinner506f5922011-09-28 22:34:18 +02001446 _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2,
1447 _PyUnicode_WSTR(unicode), end,
1448 PyUnicode_2BYTE_DATA(unicode));
1449 PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
1450 _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
1451 _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND;
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001452 _PyUnicode_UTF8(unicode) = NULL;
1453 _PyUnicode_UTF8_LENGTH(unicode) = 0;
Victor Stinner506f5922011-09-28 22:34:18 +02001454 PyObject_FREE(_PyUnicode_WSTR(unicode));
1455 _PyUnicode_WSTR(unicode) = NULL;
1456 _PyUnicode_WSTR_LENGTH(unicode) = 0;
1457#endif
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001458 }
1459 /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */
1460 else {
1461#if SIZEOF_WCHAR_T == 2
1462 /* in case the native representation is 2-bytes, we need to allocate a
1463 new normalized 4-byte version. */
1464 length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates;
Victor Stinnerc3c74152011-10-02 20:39:55 +02001465 _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1));
1466 if (!_PyUnicode_DATA_ANY(unicode)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001467 PyErr_NoMemory();
1468 return -1;
1469 }
1470 _PyUnicode_LENGTH(unicode) = length_wo_surrogates;
1471 _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND;
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001472 _PyUnicode_UTF8(unicode) = NULL;
1473 _PyUnicode_UTF8_LENGTH(unicode) = 0;
Victor Stinner126c5592011-10-03 04:17:10 +02001474 /* unicode_convert_wchar_to_ucs4() requires a ready string */
1475 _PyUnicode_STATE(unicode).ready = 1;
Victor Stinnerc53be962011-10-02 21:33:54 +02001476 unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001477 PyObject_FREE(_PyUnicode_WSTR(unicode));
1478 _PyUnicode_WSTR(unicode) = NULL;
1479 _PyUnicode_WSTR_LENGTH(unicode) = 0;
1480#else
1481 assert(num_surrogates == 0);
1482
Victor Stinnerc3c74152011-10-02 20:39:55 +02001483 _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001484 _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001485 _PyUnicode_UTF8(unicode) = NULL;
1486 _PyUnicode_UTF8_LENGTH(unicode) = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001487 _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND;
1488#endif
1489 PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0';
1490 }
1491 _PyUnicode_STATE(unicode).ready = 1;
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02001492 assert(_PyUnicode_CheckConsistency(unicode, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001493 return 0;
1494}
1495
Alexander Belopolsky40018472011-02-26 01:02:56 +00001496static void
Victor Stinner9db1a8b2011-10-23 20:04:37 +02001497unicode_dealloc(register PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00001498{
Walter Dörwald16807132007-05-25 13:52:07 +00001499 switch (PyUnicode_CHECK_INTERNED(unicode)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00001500 case SSTATE_NOT_INTERNED:
1501 break;
Walter Dörwald16807132007-05-25 13:52:07 +00001502
Benjamin Peterson29060642009-01-31 22:14:21 +00001503 case SSTATE_INTERNED_MORTAL:
1504 /* revive dead object temporarily for DelItem */
1505 Py_REFCNT(unicode) = 3;
Victor Stinner7931d9a2011-11-04 00:22:48 +01001506 if (PyDict_DelItem(interned, unicode) != 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00001507 Py_FatalError(
1508 "deletion of interned string failed");
1509 break;
Walter Dörwald16807132007-05-25 13:52:07 +00001510
Benjamin Peterson29060642009-01-31 22:14:21 +00001511 case SSTATE_INTERNED_IMMORTAL:
1512 Py_FatalError("Immortal interned string died.");
Walter Dörwald16807132007-05-25 13:52:07 +00001513
Benjamin Peterson29060642009-01-31 22:14:21 +00001514 default:
1515 Py_FatalError("Inconsistent interned string state.");
Walter Dörwald16807132007-05-25 13:52:07 +00001516 }
1517
Victor Stinner03490912011-10-03 23:45:12 +02001518 if (_PyUnicode_HAS_WSTR_MEMORY(unicode))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001519 PyObject_DEL(_PyUnicode_WSTR(unicode));
Victor Stinner829c0ad2011-10-03 01:08:02 +02001520 if (_PyUnicode_HAS_UTF8_MEMORY(unicode))
Victor Stinnere90fe6a2011-10-01 16:48:13 +02001521 PyObject_DEL(_PyUnicode_UTF8(unicode));
Victor Stinnerb0a82a62011-12-12 13:08:33 +01001522 if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode))
1523 PyObject_DEL(_PyUnicode_DATA_ANY(unicode));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001524
Victor Stinnerb0a82a62011-12-12 13:08:33 +01001525 Py_TYPE(unicode)->tp_free(unicode);
Guido van Rossumd57fd912000-03-10 22:53:23 +00001526}
1527
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001528#ifdef Py_DEBUG
1529static int
1530unicode_is_singleton(PyObject *unicode)
1531{
1532 PyASCIIObject *ascii = (PyASCIIObject *)unicode;
1533 if (unicode == unicode_empty)
1534 return 1;
1535 if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1)
1536 {
1537 Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0);
1538 if (ch < 256 && unicode_latin1[ch] == unicode)
1539 return 1;
1540 }
1541 return 0;
1542}
1543#endif
1544
Alexander Belopolsky40018472011-02-26 01:02:56 +00001545static int
Victor Stinner488fa492011-12-12 00:01:39 +01001546unicode_modifiable(PyObject *unicode)
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001547{
Victor Stinner488fa492011-12-12 00:01:39 +01001548 assert(_PyUnicode_CHECK(unicode));
Victor Stinnerfe226c02011-10-03 03:52:20 +02001549 if (Py_REFCNT(unicode) != 1)
1550 return 0;
Victor Stinner488fa492011-12-12 00:01:39 +01001551 if (_PyUnicode_HASH(unicode) != -1)
1552 return 0;
Victor Stinnerfe226c02011-10-03 03:52:20 +02001553 if (PyUnicode_CHECK_INTERNED(unicode))
1554 return 0;
Victor Stinner488fa492011-12-12 00:01:39 +01001555 if (!PyUnicode_CheckExact(unicode))
1556 return 0;
Victor Stinner77bb47b2011-10-03 20:06:05 +02001557#ifdef Py_DEBUG
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02001558 /* singleton refcount is greater than 1 */
1559 assert(!unicode_is_singleton(unicode));
Victor Stinner77bb47b2011-10-03 20:06:05 +02001560#endif
Victor Stinnerfe226c02011-10-03 03:52:20 +02001561 return 1;
1562}
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001563
Victor Stinnerfe226c02011-10-03 03:52:20 +02001564static int
1565unicode_resize(PyObject **p_unicode, Py_ssize_t length)
1566{
1567 PyObject *unicode;
1568 Py_ssize_t old_length;
1569
1570 assert(p_unicode != NULL);
1571 unicode = *p_unicode;
1572
1573 assert(unicode != NULL);
1574 assert(PyUnicode_Check(unicode));
1575 assert(0 <= length);
1576
Victor Stinner910337b2011-10-03 03:20:16 +02001577 if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND)
Victor Stinnerfe226c02011-10-03 03:52:20 +02001578 old_length = PyUnicode_WSTR_LENGTH(unicode);
1579 else
1580 old_length = PyUnicode_GET_LENGTH(unicode);
1581 if (old_length == length)
1582 return 0;
1583
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001584 if (length == 0) {
1585 Py_DECREF(*p_unicode);
1586 *p_unicode = unicode_empty;
1587 Py_INCREF(*p_unicode);
1588 return 0;
1589 }
1590
Victor Stinner488fa492011-12-12 00:01:39 +01001591 if (!unicode_modifiable(unicode)) {
Victor Stinnerfe226c02011-10-03 03:52:20 +02001592 PyObject *copy = resize_copy(unicode, length);
1593 if (copy == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00001594 return -1;
Victor Stinnerfe226c02011-10-03 03:52:20 +02001595 Py_DECREF(*p_unicode);
1596 *p_unicode = copy;
Benjamin Peterson29060642009-01-31 22:14:21 +00001597 return 0;
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001598 }
1599
Victor Stinnerfe226c02011-10-03 03:52:20 +02001600 if (PyUnicode_IS_COMPACT(unicode)) {
Victor Stinnerb0a82a62011-12-12 13:08:33 +01001601 PyObject *new_unicode = resize_compact(unicode, length);
1602 if (new_unicode == NULL)
Victor Stinnerfe226c02011-10-03 03:52:20 +02001603 return -1;
Victor Stinnerb0a82a62011-12-12 13:08:33 +01001604 *p_unicode = new_unicode;
Victor Stinnerfe226c02011-10-03 03:52:20 +02001605 return 0;
Benjamin Peterson4bfce8f2011-10-03 19:35:07 -04001606 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02001607 return resize_inplace(unicode, length);
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001608}
1609
Alexander Belopolsky40018472011-02-26 01:02:56 +00001610int
Victor Stinnerfe226c02011-10-03 03:52:20 +02001611PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length)
Alexandre Vassalottiaa0e5312008-12-27 06:43:58 +00001612{
Victor Stinnerfe226c02011-10-03 03:52:20 +02001613 PyObject *unicode;
1614 if (p_unicode == NULL) {
1615 PyErr_BadInternalCall();
1616 return -1;
1617 }
1618 unicode = *p_unicode;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001619 if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0)
Victor Stinnerfe226c02011-10-03 03:52:20 +02001620 {
1621 PyErr_BadInternalCall();
1622 return -1;
1623 }
1624 return unicode_resize(p_unicode, length);
Alexandre Vassalottiaa0e5312008-12-27 06:43:58 +00001625}
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001626
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001627static int
Victor Stinner1b487b42012-05-03 12:29:04 +02001628unicode_widen(PyObject **p_unicode, Py_ssize_t length,
1629 unsigned int maxchar)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001630{
1631 PyObject *result;
1632 assert(PyUnicode_IS_READY(*p_unicode));
Victor Stinner1b487b42012-05-03 12:29:04 +02001633 assert(length <= PyUnicode_GET_LENGTH(*p_unicode));
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001634 if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode))
1635 return 0;
1636 result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode),
1637 maxchar);
1638 if (result == NULL)
1639 return -1;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001640 _PyUnicode_FastCopyCharacters(result, 0, *p_unicode, 0, length);
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001641 Py_DECREF(*p_unicode);
1642 *p_unicode = result;
1643 return 0;
1644}
1645
1646static int
1647unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos,
1648 Py_UCS4 ch)
1649{
Victor Stinner15e9ed22012-02-22 13:36:20 +01001650 assert(ch <= MAX_UNICODE);
Victor Stinner1b487b42012-05-03 12:29:04 +02001651 if (unicode_widen(p_unicode, *pos, ch) < 0)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01001652 return -1;
1653 PyUnicode_WRITE(PyUnicode_KIND(*p_unicode),
1654 PyUnicode_DATA(*p_unicode),
1655 (*pos)++, ch);
1656 return 0;
1657}
1658
Victor Stinnerc5166102012-02-22 13:55:02 +01001659/* Copy a ASCII or latin1 char* string into a Python Unicode string.
Victor Stinnerc5166102012-02-22 13:55:02 +01001660
Victor Stinnerb429d3b2012-02-22 21:22:20 +01001661 WARNING: The function doesn't copy the terminating null character and
1662 doesn't check the maximum character (may write a latin1 character in an
1663 ASCII string). */
Victor Stinner184252a2012-06-16 02:57:41 +02001664static void
1665unicode_write_cstr(PyObject *unicode, Py_ssize_t index,
1666 const char *str, Py_ssize_t len)
Victor Stinnerc5166102012-02-22 13:55:02 +01001667{
1668 enum PyUnicode_Kind kind = PyUnicode_KIND(unicode);
1669 void *data = PyUnicode_DATA(unicode);
Victor Stinner184252a2012-06-16 02:57:41 +02001670 const char *end = str + len;
Victor Stinnerc5166102012-02-22 13:55:02 +01001671
1672 switch (kind) {
1673 case PyUnicode_1BYTE_KIND: {
Victor Stinnerc5166102012-02-22 13:55:02 +01001674 assert(index + len <= PyUnicode_GET_LENGTH(unicode));
Antoine Pitrouba6bafc2012-02-22 16:41:50 +01001675 memcpy((char *) data + index, str, len);
Victor Stinner184252a2012-06-16 02:57:41 +02001676 break;
Victor Stinnerc5166102012-02-22 13:55:02 +01001677 }
1678 case PyUnicode_2BYTE_KIND: {
1679 Py_UCS2 *start = (Py_UCS2 *)data + index;
1680 Py_UCS2 *ucs2 = start;
1681 assert(index <= PyUnicode_GET_LENGTH(unicode));
1682
Victor Stinner184252a2012-06-16 02:57:41 +02001683 for (; str < end; ++ucs2, ++str)
Victor Stinnerc5166102012-02-22 13:55:02 +01001684 *ucs2 = (Py_UCS2)*str;
1685
1686 assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode));
Victor Stinner184252a2012-06-16 02:57:41 +02001687 break;
Victor Stinnerc5166102012-02-22 13:55:02 +01001688 }
1689 default: {
1690 Py_UCS4 *start = (Py_UCS4 *)data + index;
1691 Py_UCS4 *ucs4 = start;
1692 assert(kind == PyUnicode_4BYTE_KIND);
1693 assert(index <= PyUnicode_GET_LENGTH(unicode));
1694
Victor Stinner184252a2012-06-16 02:57:41 +02001695 for (; str < end; ++ucs4, ++str)
Victor Stinnerc5166102012-02-22 13:55:02 +01001696 *ucs4 = (Py_UCS4)*str;
1697
1698 assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode));
Victor Stinnerc5166102012-02-22 13:55:02 +01001699 }
1700 }
1701}
1702
1703
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001704static PyObject*
1705get_latin1_char(unsigned char ch)
1706{
Victor Stinnera464fc12011-10-02 20:39:30 +02001707 PyObject *unicode = unicode_latin1[ch];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001708 if (!unicode) {
Victor Stinnera464fc12011-10-02 20:39:30 +02001709 unicode = PyUnicode_New(1, ch);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001710 if (!unicode)
1711 return NULL;
1712 PyUnicode_1BYTE_DATA(unicode)[0] = ch;
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02001713 assert(_PyUnicode_CheckConsistency(unicode, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001714 unicode_latin1[ch] = unicode;
1715 }
1716 Py_INCREF(unicode);
Victor Stinnera464fc12011-10-02 20:39:30 +02001717 return unicode;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001718}
1719
Alexander Belopolsky40018472011-02-26 01:02:56 +00001720PyObject *
1721PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
Guido van Rossumd57fd912000-03-10 22:53:23 +00001722{
Victor Stinner9db1a8b2011-10-23 20:04:37 +02001723 PyObject *unicode;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001724 Py_UCS4 maxchar = 0;
1725 Py_ssize_t num_surrogates;
1726
1727 if (u == NULL)
1728 return (PyObject*)_PyUnicode_New(size);
Guido van Rossumd57fd912000-03-10 22:53:23 +00001729
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001730 /* If the Unicode data is known at construction time, we can apply
1731 some optimizations which share commonly used objects. */
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001732
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001733 /* Optimization for empty strings */
1734 if (size == 0 && unicode_empty != NULL) {
1735 Py_INCREF(unicode_empty);
Victor Stinnera464fc12011-10-02 20:39:30 +02001736 return unicode_empty;
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00001737 }
Tim Petersced69f82003-09-16 20:30:58 +00001738
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001739 /* Single character Unicode objects in the Latin-1 range are
1740 shared when using this constructor */
1741 if (size == 1 && *u < 256)
1742 return get_latin1_char((unsigned char)*u);
1743
1744 /* If not empty and not single character, copy the Unicode data
1745 into the new object */
Victor Stinnerd8f65102011-09-29 19:43:17 +02001746 if (find_maxchar_surrogates(u, u + size,
1747 &maxchar, &num_surrogates) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001748 return NULL;
1749
Victor Stinner8faf8212011-12-08 22:14:11 +01001750 unicode = PyUnicode_New(size - num_surrogates, maxchar);
Guido van Rossumd57fd912000-03-10 22:53:23 +00001751 if (!unicode)
1752 return NULL;
1753
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001754 switch (PyUnicode_KIND(unicode)) {
1755 case PyUnicode_1BYTE_KIND:
Victor Stinnerfb5f5f22011-09-28 21:39:49 +02001756 _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001757 u, u + size, PyUnicode_1BYTE_DATA(unicode));
1758 break;
1759 case PyUnicode_2BYTE_KIND:
1760#if Py_UNICODE_SIZE == 2
1761 Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2);
1762#else
Victor Stinnerfb5f5f22011-09-28 21:39:49 +02001763 _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001764 u, u + size, PyUnicode_2BYTE_DATA(unicode));
1765#endif
1766 break;
1767 case PyUnicode_4BYTE_KIND:
1768#if SIZEOF_WCHAR_T == 2
1769 /* This is the only case which has to process surrogates, thus
1770 a simple copy loop is not enough and we need a function. */
Victor Stinnerc53be962011-10-02 21:33:54 +02001771 unicode_convert_wchar_to_ucs4(u, u + size, unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001772#else
1773 assert(num_surrogates == 0);
1774 Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4);
1775#endif
1776 break;
1777 default:
1778 assert(0 && "Impossible state");
1779 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00001780
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001781 return unicode_result(unicode);
Guido van Rossumd57fd912000-03-10 22:53:23 +00001782}
1783
Alexander Belopolsky40018472011-02-26 01:02:56 +00001784PyObject *
1785PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
Walter Dörwaldacaa5a12007-05-05 12:00:46 +00001786{
Benjamin Peterson14339b62009-01-31 16:36:08 +00001787 if (size < 0) {
1788 PyErr_SetString(PyExc_SystemError,
Benjamin Peterson29060642009-01-31 22:14:21 +00001789 "Negative size passed to PyUnicode_FromStringAndSize");
Benjamin Peterson14339b62009-01-31 16:36:08 +00001790 return NULL;
1791 }
Victor Stinnera1d12bb2011-12-11 21:53:09 +01001792 if (u != NULL)
1793 return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL);
1794 else
1795 return (PyObject *)_PyUnicode_New(size);
Walter Dörwaldacaa5a12007-05-05 12:00:46 +00001796}
1797
Alexander Belopolsky40018472011-02-26 01:02:56 +00001798PyObject *
1799PyUnicode_FromString(const char *u)
Walter Dörwaldd2034312007-05-18 16:29:38 +00001800{
1801 size_t size = strlen(u);
1802 if (size > PY_SSIZE_T_MAX) {
1803 PyErr_SetString(PyExc_OverflowError, "input too long");
1804 return NULL;
1805 }
Victor Stinnera1d12bb2011-12-11 21:53:09 +01001806 return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001807}
1808
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001809PyObject *
1810_PyUnicode_FromId(_Py_Identifier *id)
1811{
1812 if (!id->object) {
Victor Stinnerd1cd99b2012-02-07 23:05:55 +01001813 id->object = PyUnicode_DecodeUTF8Stateful(id->string,
1814 strlen(id->string),
1815 NULL, NULL);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001816 if (!id->object)
1817 return NULL;
1818 PyUnicode_InternInPlace(&id->object);
1819 assert(!id->next);
1820 id->next = static_strings;
1821 static_strings = id;
1822 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001823 return id->object;
1824}
1825
1826void
1827_PyUnicode_ClearStaticStrings()
1828{
1829 _Py_Identifier *i;
1830 for (i = static_strings; i; i = i->next) {
1831 Py_DECREF(i->object);
1832 i->object = NULL;
1833 i->next = NULL;
1834 }
1835}
1836
Benjamin Peterson0df54292012-03-26 14:50:32 -04001837/* Internal function, doesn't check maximum character */
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001838
Victor Stinnerd3f08822012-05-29 12:57:52 +02001839PyObject*
1840_PyUnicode_FromASCII(const char *buffer, Py_ssize_t size)
Victor Stinner702c7342011-10-05 13:50:52 +02001841{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001842 const unsigned char *s = (const unsigned char *)buffer;
Victor Stinner785938e2011-12-11 20:09:03 +01001843 PyObject *unicode;
Victor Stinnere6b2d442011-12-11 21:54:30 +01001844 if (size == 1) {
Victor Stinner0617b6e2011-10-05 23:26:01 +02001845#ifdef Py_DEBUG
Victor Stinnere6b2d442011-12-11 21:54:30 +01001846 assert(s[0] < 128);
Victor Stinner0617b6e2011-10-05 23:26:01 +02001847#endif
Antoine Pitrou7c46da72011-10-06 22:07:51 +02001848 return get_latin1_char(s[0]);
Victor Stinnere6b2d442011-12-11 21:54:30 +01001849 }
Victor Stinner785938e2011-12-11 20:09:03 +01001850 unicode = PyUnicode_New(size, 127);
1851 if (!unicode)
Victor Stinner702c7342011-10-05 13:50:52 +02001852 return NULL;
Victor Stinner785938e2011-12-11 20:09:03 +01001853 memcpy(PyUnicode_1BYTE_DATA(unicode), s, size);
1854 assert(_PyUnicode_CheckConsistency(unicode, 1));
1855 return unicode;
Victor Stinner702c7342011-10-05 13:50:52 +02001856}
1857
Victor Stinnerc80d6d22011-10-05 14:13:28 +02001858static Py_UCS4
1859kind_maxchar_limit(unsigned int kind)
1860{
Benjamin Petersonead6b532011-12-20 17:23:42 -06001861 switch (kind) {
Victor Stinnerc80d6d22011-10-05 14:13:28 +02001862 case PyUnicode_1BYTE_KIND:
1863 return 0x80;
1864 case PyUnicode_2BYTE_KIND:
1865 return 0x100;
1866 case PyUnicode_4BYTE_KIND:
1867 return 0x10000;
1868 default:
1869 assert(0 && "invalid kind");
Victor Stinner8faf8212011-12-08 22:14:11 +01001870 return MAX_UNICODE;
Victor Stinnerc80d6d22011-10-05 14:13:28 +02001871 }
1872}
1873
Victor Stinnere6abb482012-05-02 01:15:40 +02001874Py_LOCAL_INLINE(Py_UCS4)
1875align_maxchar(Py_UCS4 maxchar)
1876{
1877 if (maxchar <= 127)
1878 return 127;
1879 else if (maxchar <= 255)
1880 return 255;
1881 else if (maxchar <= 65535)
1882 return 65535;
1883 else
1884 return MAX_UNICODE;
1885}
1886
Victor Stinner702c7342011-10-05 13:50:52 +02001887static PyObject*
Victor Stinnere57b1c02011-09-28 22:20:48 +02001888_PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size)
Mark Dickinson081dfee2009-03-18 14:47:41 +00001889{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001890 PyObject *res;
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001891 unsigned char max_char;
Victor Stinnerb9275c12011-10-05 14:01:42 +02001892
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001893 if (size == 0) {
1894 Py_INCREF(unicode_empty);
1895 return unicode_empty;
1896 }
1897 assert(size > 0);
Antoine Pitrou7c46da72011-10-06 22:07:51 +02001898 if (size == 1)
1899 return get_latin1_char(u[0]);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001900
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02001901 max_char = ucs1lib_find_max_char(u, u + size);
Victor Stinnerb9275c12011-10-05 14:01:42 +02001902 res = PyUnicode_New(size, max_char);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001903 if (!res)
1904 return NULL;
1905 memcpy(PyUnicode_1BYTE_DATA(res), u, size);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02001906 assert(_PyUnicode_CheckConsistency(res, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001907 return res;
Mark Dickinson081dfee2009-03-18 14:47:41 +00001908}
1909
Victor Stinnere57b1c02011-09-28 22:20:48 +02001910static PyObject*
1911_PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001912{
1913 PyObject *res;
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001914 Py_UCS2 max_char;
Victor Stinnerb9275c12011-10-05 14:01:42 +02001915
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001916 if (size == 0) {
1917 Py_INCREF(unicode_empty);
1918 return unicode_empty;
1919 }
1920 assert(size > 0);
Victor Stinnerb6cd0142012-05-03 02:17:04 +02001921 if (size == 1) {
1922 Py_UCS4 ch = u[0];
1923 if (ch < 256)
1924 return get_latin1_char((unsigned char)ch);
1925
1926 res = PyUnicode_New(1, ch);
1927 if (res == NULL)
1928 return NULL;
1929 PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch);
1930 assert(_PyUnicode_CheckConsistency(res, 1));
1931 return res;
1932 }
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001933
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02001934 max_char = ucs2lib_find_max_char(u, u + size);
Victor Stinnerb9275c12011-10-05 14:01:42 +02001935 res = PyUnicode_New(size, max_char);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001936 if (!res)
1937 return NULL;
Victor Stinnerb9275c12011-10-05 14:01:42 +02001938 if (max_char >= 256)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001939 memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size);
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02001940 else {
1941 _PyUnicode_CONVERT_BYTES(
1942 Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res));
1943 }
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02001944 assert(_PyUnicode_CheckConsistency(res, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001945 return res;
1946}
1947
Victor Stinnere57b1c02011-09-28 22:20:48 +02001948static PyObject*
1949_PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001950{
1951 PyObject *res;
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001952 Py_UCS4 max_char;
Victor Stinnerb9275c12011-10-05 14:01:42 +02001953
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001954 if (size == 0) {
1955 Py_INCREF(unicode_empty);
1956 return unicode_empty;
1957 }
1958 assert(size > 0);
Victor Stinnerb6cd0142012-05-03 02:17:04 +02001959 if (size == 1) {
1960 Py_UCS4 ch = u[0];
1961 if (ch < 256)
1962 return get_latin1_char((unsigned char)ch);
1963
1964 res = PyUnicode_New(1, ch);
1965 if (res == NULL)
1966 return NULL;
1967 PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch);
1968 assert(_PyUnicode_CheckConsistency(res, 1));
1969 return res;
1970 }
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01001971
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02001972 max_char = ucs4lib_find_max_char(u, u + size);
Victor Stinnerb9275c12011-10-05 14:01:42 +02001973 res = PyUnicode_New(size, max_char);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001974 if (!res)
1975 return NULL;
Antoine Pitrou950468e2011-10-11 22:45:48 +02001976 if (max_char < 256)
1977 _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size,
1978 PyUnicode_1BYTE_DATA(res));
1979 else if (max_char < 0x10000)
1980 _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size,
1981 PyUnicode_2BYTE_DATA(res));
1982 else
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001983 memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02001984 assert(_PyUnicode_CheckConsistency(res, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001985 return res;
1986}
1987
1988PyObject*
1989PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
1990{
Victor Stinnercfed46e2011-11-22 01:29:14 +01001991 if (size < 0) {
1992 PyErr_SetString(PyExc_ValueError, "size must be positive");
1993 return NULL;
1994 }
Benjamin Petersonead6b532011-12-20 17:23:42 -06001995 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001996 case PyUnicode_1BYTE_KIND:
Victor Stinnere57b1c02011-09-28 22:20:48 +02001997 return _PyUnicode_FromUCS1(buffer, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001998 case PyUnicode_2BYTE_KIND:
Victor Stinnere57b1c02011-09-28 22:20:48 +02001999 return _PyUnicode_FromUCS2(buffer, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002000 case PyUnicode_4BYTE_KIND:
Victor Stinnere57b1c02011-09-28 22:20:48 +02002001 return _PyUnicode_FromUCS4(buffer, size);
Victor Stinnerb9275c12011-10-05 14:01:42 +02002002 default:
Victor Stinnerb9275c12011-10-05 14:01:42 +02002003 PyErr_SetString(PyExc_SystemError, "invalid kind");
2004 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002005 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002006}
2007
Victor Stinnerece58de2012-04-23 23:36:38 +02002008Py_UCS4
2009_PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end)
2010{
2011 enum PyUnicode_Kind kind;
2012 void *startptr, *endptr;
2013
2014 assert(PyUnicode_IS_READY(unicode));
2015 assert(0 <= start);
2016 assert(end <= PyUnicode_GET_LENGTH(unicode));
2017 assert(start <= end);
2018
2019 if (start == 0 && end == PyUnicode_GET_LENGTH(unicode))
2020 return PyUnicode_MAX_CHAR_VALUE(unicode);
2021
2022 if (start == end)
2023 return 127;
2024
Victor Stinner94d558b2012-04-27 22:26:58 +02002025 if (PyUnicode_IS_ASCII(unicode))
2026 return 127;
2027
Victor Stinnerece58de2012-04-23 23:36:38 +02002028 kind = PyUnicode_KIND(unicode);
Benjamin Petersonf3b7d862012-04-23 18:07:01 -04002029 startptr = PyUnicode_DATA(unicode);
Benjamin Petersonb9f4c9d2012-04-23 21:45:40 -04002030 endptr = (char *)startptr + end * kind;
2031 startptr = (char *)startptr + start * kind;
Benjamin Peterson2844a7a2012-04-23 18:00:25 -04002032 switch(kind) {
2033 case PyUnicode_1BYTE_KIND:
2034 return ucs1lib_find_max_char(startptr, endptr);
2035 case PyUnicode_2BYTE_KIND:
2036 return ucs2lib_find_max_char(startptr, endptr);
2037 case PyUnicode_4BYTE_KIND:
2038 return ucs4lib_find_max_char(startptr, endptr);
Victor Stinnerece58de2012-04-23 23:36:38 +02002039 default:
Benjamin Peterson2844a7a2012-04-23 18:00:25 -04002040 assert(0);
2041 return 0;
Victor Stinnerece58de2012-04-23 23:36:38 +02002042 }
2043}
2044
Victor Stinner25a4b292011-10-06 12:31:55 +02002045/* Ensure that a string uses the most efficient storage, if it is not the
2046 case: create a new string with of the right kind. Write NULL into *p_unicode
2047 on error. */
Antoine Pitrou53bb5482011-10-10 23:49:24 +02002048static void
Victor Stinner25a4b292011-10-06 12:31:55 +02002049unicode_adjust_maxchar(PyObject **p_unicode)
2050{
2051 PyObject *unicode, *copy;
2052 Py_UCS4 max_char;
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02002053 Py_ssize_t len;
Victor Stinner25a4b292011-10-06 12:31:55 +02002054 unsigned int kind;
2055
2056 assert(p_unicode != NULL);
2057 unicode = *p_unicode;
2058 assert(PyUnicode_IS_READY(unicode));
2059 if (PyUnicode_IS_ASCII(unicode))
2060 return;
2061
2062 len = PyUnicode_GET_LENGTH(unicode);
2063 kind = PyUnicode_KIND(unicode);
2064 if (kind == PyUnicode_1BYTE_KIND) {
2065 const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode);
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02002066 max_char = ucs1lib_find_max_char(u, u + len);
2067 if (max_char >= 128)
2068 return;
Victor Stinner25a4b292011-10-06 12:31:55 +02002069 }
2070 else if (kind == PyUnicode_2BYTE_KIND) {
2071 const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode);
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02002072 max_char = ucs2lib_find_max_char(u, u + len);
2073 if (max_char >= 256)
2074 return;
Victor Stinner25a4b292011-10-06 12:31:55 +02002075 }
2076 else {
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02002077 const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode);
Victor Stinner25a4b292011-10-06 12:31:55 +02002078 assert(kind == PyUnicode_4BYTE_KIND);
Antoine Pitroudd4e2f02011-10-13 00:02:27 +02002079 max_char = ucs4lib_find_max_char(u, u + len);
2080 if (max_char >= 0x10000)
2081 return;
Victor Stinner25a4b292011-10-06 12:31:55 +02002082 }
Victor Stinner25a4b292011-10-06 12:31:55 +02002083 copy = PyUnicode_New(len, max_char);
Victor Stinnerca439ee2012-06-16 03:17:34 +02002084 if (copy != NULL)
2085 _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len);
Victor Stinner25a4b292011-10-06 12:31:55 +02002086 Py_DECREF(unicode);
2087 *p_unicode = copy;
2088}
2089
Victor Stinner034f6cf2011-09-30 02:26:44 +02002090PyObject*
Victor Stinnerbf6e5602011-12-12 01:53:47 +01002091_PyUnicode_Copy(PyObject *unicode)
Victor Stinner034f6cf2011-09-30 02:26:44 +02002092{
Victor Stinner87af4f22011-11-21 23:03:47 +01002093 Py_ssize_t length;
Victor Stinnerc841e7d2011-10-01 01:34:32 +02002094 PyObject *copy;
Victor Stinnerc841e7d2011-10-01 01:34:32 +02002095
Victor Stinner034f6cf2011-09-30 02:26:44 +02002096 if (!PyUnicode_Check(unicode)) {
2097 PyErr_BadInternalCall();
2098 return NULL;
2099 }
Benjamin Petersonbac79492012-01-14 13:34:47 -05002100 if (PyUnicode_READY(unicode) == -1)
Victor Stinner034f6cf2011-09-30 02:26:44 +02002101 return NULL;
Victor Stinnerc841e7d2011-10-01 01:34:32 +02002102
Victor Stinner87af4f22011-11-21 23:03:47 +01002103 length = PyUnicode_GET_LENGTH(unicode);
2104 copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
Victor Stinnerc841e7d2011-10-01 01:34:32 +02002105 if (!copy)
2106 return NULL;
2107 assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode));
2108
Victor Stinner87af4f22011-11-21 23:03:47 +01002109 Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode),
2110 length * PyUnicode_KIND(unicode));
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02002111 assert(_PyUnicode_CheckConsistency(copy, 1));
Victor Stinnerc841e7d2011-10-01 01:34:32 +02002112 return copy;
Victor Stinner034f6cf2011-09-30 02:26:44 +02002113}
2114
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002115
Victor Stinnerbc603d12011-10-02 01:00:40 +02002116/* Widen Unicode objects to larger buffers. Don't write terminating null
2117 character. Return NULL on error. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002118
2119void*
2120_PyUnicode_AsKind(PyObject *s, unsigned int kind)
2121{
Victor Stinnerbc603d12011-10-02 01:00:40 +02002122 Py_ssize_t len;
2123 void *result;
2124 unsigned int skind;
2125
Benjamin Petersonbac79492012-01-14 13:34:47 -05002126 if (PyUnicode_READY(s) == -1)
Victor Stinnerbc603d12011-10-02 01:00:40 +02002127 return NULL;
2128
2129 len = PyUnicode_GET_LENGTH(s);
2130 skind = PyUnicode_KIND(s);
2131 if (skind >= kind) {
Victor Stinner01698042011-10-04 00:04:26 +02002132 PyErr_SetString(PyExc_SystemError, "invalid widening attempt");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002133 return NULL;
2134 }
Benjamin Petersonead6b532011-12-20 17:23:42 -06002135 switch (kind) {
Victor Stinnerbc603d12011-10-02 01:00:40 +02002136 case PyUnicode_2BYTE_KIND:
2137 result = PyMem_Malloc(len * sizeof(Py_UCS2));
2138 if (!result)
2139 return PyErr_NoMemory();
2140 assert(skind == PyUnicode_1BYTE_KIND);
2141 _PyUnicode_CONVERT_BYTES(
2142 Py_UCS1, Py_UCS2,
2143 PyUnicode_1BYTE_DATA(s),
2144 PyUnicode_1BYTE_DATA(s) + len,
2145 result);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002146 return result;
Victor Stinnerbc603d12011-10-02 01:00:40 +02002147 case PyUnicode_4BYTE_KIND:
2148 result = PyMem_Malloc(len * sizeof(Py_UCS4));
2149 if (!result)
2150 return PyErr_NoMemory();
2151 if (skind == PyUnicode_2BYTE_KIND) {
2152 _PyUnicode_CONVERT_BYTES(
2153 Py_UCS2, Py_UCS4,
2154 PyUnicode_2BYTE_DATA(s),
2155 PyUnicode_2BYTE_DATA(s) + len,
2156 result);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002157 }
Victor Stinnerbc603d12011-10-02 01:00:40 +02002158 else {
2159 assert(skind == PyUnicode_1BYTE_KIND);
2160 _PyUnicode_CONVERT_BYTES(
2161 Py_UCS1, Py_UCS4,
2162 PyUnicode_1BYTE_DATA(s),
2163 PyUnicode_1BYTE_DATA(s) + len,
2164 result);
2165 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002166 return result;
Victor Stinnerbc603d12011-10-02 01:00:40 +02002167 default:
2168 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002169 }
Victor Stinner01698042011-10-04 00:04:26 +02002170 PyErr_SetString(PyExc_SystemError, "invalid kind");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002171 return NULL;
2172}
2173
2174static Py_UCS4*
2175as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
2176 int copy_null)
2177{
2178 int kind;
2179 void *data;
2180 Py_ssize_t len, targetlen;
2181 if (PyUnicode_READY(string) == -1)
2182 return NULL;
2183 kind = PyUnicode_KIND(string);
2184 data = PyUnicode_DATA(string);
2185 len = PyUnicode_GET_LENGTH(string);
2186 targetlen = len;
2187 if (copy_null)
2188 targetlen++;
2189 if (!target) {
2190 if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) {
2191 PyErr_NoMemory();
2192 return NULL;
2193 }
2194 target = PyMem_Malloc(targetlen * sizeof(Py_UCS4));
2195 if (!target) {
2196 PyErr_NoMemory();
2197 return NULL;
2198 }
2199 }
2200 else {
2201 if (targetsize < targetlen) {
2202 PyErr_Format(PyExc_SystemError,
2203 "string is longer than the buffer");
2204 if (copy_null && 0 < targetsize)
2205 target[0] = 0;
2206 return NULL;
2207 }
2208 }
Antoine Pitrou950468e2011-10-11 22:45:48 +02002209 if (kind == PyUnicode_1BYTE_KIND) {
2210 Py_UCS1 *start = (Py_UCS1 *) data;
2211 _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002212 }
Antoine Pitrou950468e2011-10-11 22:45:48 +02002213 else if (kind == PyUnicode_2BYTE_KIND) {
2214 Py_UCS2 *start = (Py_UCS2 *) data;
2215 _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target);
2216 }
2217 else {
2218 assert(kind == PyUnicode_4BYTE_KIND);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002219 Py_MEMCPY(target, data, len * sizeof(Py_UCS4));
Antoine Pitrou950468e2011-10-11 22:45:48 +02002220 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002221 if (copy_null)
2222 target[len] = 0;
2223 return target;
2224}
2225
2226Py_UCS4*
2227PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
2228 int copy_null)
2229{
Antoine Pitroude20b0b2011-11-10 21:47:38 +01002230 if (target == NULL || targetsize < 0) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002231 PyErr_BadInternalCall();
2232 return NULL;
2233 }
2234 return as_ucs4(string, target, targetsize, copy_null);
2235}
2236
2237Py_UCS4*
2238PyUnicode_AsUCS4Copy(PyObject *string)
2239{
2240 return as_ucs4(string, NULL, 0, 1);
2241}
2242
2243#ifdef HAVE_WCHAR_H
Mark Dickinson081dfee2009-03-18 14:47:41 +00002244
Alexander Belopolsky40018472011-02-26 01:02:56 +00002245PyObject *
2246PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size)
Guido van Rossumd57fd912000-03-10 22:53:23 +00002247{
Guido van Rossumd57fd912000-03-10 22:53:23 +00002248 if (w == NULL) {
Victor Stinner382955f2011-12-11 21:44:00 +01002249 if (size == 0) {
2250 Py_INCREF(unicode_empty);
2251 return unicode_empty;
2252 }
Benjamin Peterson29060642009-01-31 22:14:21 +00002253 PyErr_BadInternalCall();
2254 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00002255 }
2256
Martin v. Löwis790465f2008-04-05 20:41:37 +00002257 if (size == -1) {
2258 size = wcslen(w);
2259 }
2260
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002261 return PyUnicode_FromUnicode(w, size);
Guido van Rossumd57fd912000-03-10 22:53:23 +00002262}
2263
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002264#endif /* HAVE_WCHAR_H */
Mark Dickinson081dfee2009-03-18 14:47:41 +00002265
Walter Dörwald346737f2007-05-31 10:44:43 +00002266static void
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002267makefmt(char *fmt, int longflag, int longlongflag, int size_tflag,
2268 int zeropad, int width, int precision, char c)
Walter Dörwald346737f2007-05-31 10:44:43 +00002269{
Benjamin Peterson14339b62009-01-31 16:36:08 +00002270 *fmt++ = '%';
2271 if (width) {
2272 if (zeropad)
2273 *fmt++ = '0';
2274 fmt += sprintf(fmt, "%d", width);
2275 }
2276 if (precision)
2277 fmt += sprintf(fmt, ".%d", precision);
2278 if (longflag)
2279 *fmt++ = 'l';
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002280 else if (longlongflag) {
2281 /* longlongflag should only ever be nonzero on machines with
2282 HAVE_LONG_LONG defined */
2283#ifdef HAVE_LONG_LONG
2284 char *f = PY_FORMAT_LONG_LONG;
2285 while (*f)
2286 *fmt++ = *f++;
2287#else
2288 /* we shouldn't ever get here */
2289 assert(0);
2290 *fmt++ = 'l';
2291#endif
2292 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00002293 else if (size_tflag) {
2294 char *f = PY_FORMAT_SIZE_T;
2295 while (*f)
2296 *fmt++ = *f++;
2297 }
2298 *fmt++ = c;
2299 *fmt = '\0';
Walter Dörwald346737f2007-05-31 10:44:43 +00002300}
2301
Victor Stinner96865452011-03-01 23:44:09 +00002302/* helper for PyUnicode_FromFormatV() */
2303
2304static const char*
2305parse_format_flags(const char *f,
2306 int *p_width, int *p_precision,
2307 int *p_longflag, int *p_longlongflag, int *p_size_tflag)
2308{
2309 int width, precision, longflag, longlongflag, size_tflag;
2310
2311 /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */
2312 f++;
2313 width = 0;
2314 while (Py_ISDIGIT((unsigned)*f))
2315 width = (width*10) + *f++ - '0';
2316 precision = 0;
2317 if (*f == '.') {
2318 f++;
2319 while (Py_ISDIGIT((unsigned)*f))
2320 precision = (precision*10) + *f++ - '0';
2321 if (*f == '%') {
2322 /* "%.3%s" => f points to "3" */
2323 f--;
2324 }
2325 }
2326 if (*f == '\0') {
2327 /* bogus format "%.1" => go backward, f points to "1" */
2328 f--;
2329 }
2330 if (p_width != NULL)
2331 *p_width = width;
2332 if (p_precision != NULL)
2333 *p_precision = precision;
2334
2335 /* Handle %ld, %lu, %lld and %llu. */
2336 longflag = 0;
2337 longlongflag = 0;
Victor Stinnere7faec12011-03-02 00:01:53 +00002338 size_tflag = 0;
Victor Stinner96865452011-03-01 23:44:09 +00002339
2340 if (*f == 'l') {
Victor Stinner6d970f42011-03-02 00:04:25 +00002341 if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') {
Victor Stinner96865452011-03-01 23:44:09 +00002342 longflag = 1;
2343 ++f;
2344 }
2345#ifdef HAVE_LONG_LONG
2346 else if (f[1] == 'l' &&
Victor Stinner6d970f42011-03-02 00:04:25 +00002347 (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) {
Victor Stinner96865452011-03-01 23:44:09 +00002348 longlongflag = 1;
2349 f += 2;
2350 }
2351#endif
2352 }
2353 /* handle the size_t flag. */
Victor Stinner6d970f42011-03-02 00:04:25 +00002354 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) {
Victor Stinner96865452011-03-01 23:44:09 +00002355 size_tflag = 1;
2356 ++f;
2357 }
2358 if (p_longflag != NULL)
2359 *p_longflag = longflag;
2360 if (p_longlongflag != NULL)
2361 *p_longlongflag = longlongflag;
2362 if (p_size_tflag != NULL)
2363 *p_size_tflag = size_tflag;
2364 return f;
2365}
2366
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002367/* maximum number of characters required for output of %ld. 21 characters
2368 allows for 64-bit integers (in decimal) and an optional sign. */
2369#define MAX_LONG_CHARS 21
2370/* maximum number of characters required for output of %lld.
2371 We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits,
2372 plus 1 for the sign. 53/22 is an upper bound for log10(256). */
2373#define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22)
2374
Walter Dörwaldd2034312007-05-18 16:29:38 +00002375PyObject *
2376PyUnicode_FromFormatV(const char *format, va_list vargs)
2377{
Benjamin Peterson14339b62009-01-31 16:36:08 +00002378 va_list count;
2379 Py_ssize_t callcount = 0;
2380 PyObject **callresults = NULL;
2381 PyObject **callresult = NULL;
2382 Py_ssize_t n = 0;
2383 int width = 0;
2384 int precision = 0;
2385 int zeropad;
2386 const char* f;
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02002387 PyObject *string;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002388 /* used by sprintf */
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002389 char fmt[61]; /* should be enough for %0width.precisionlld */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002390 Py_UCS4 maxchar = 127; /* result is ASCII by default */
2391 Py_UCS4 argmaxchar;
2392 Py_ssize_t numbersize = 0;
2393 char *numberresults = NULL;
2394 char *numberresult = NULL;
2395 Py_ssize_t i;
2396 int kind;
2397 void *data;
Walter Dörwaldd2034312007-05-18 16:29:38 +00002398
Victor Stinner4a2b7a12010-08-13 14:03:48 +00002399 Py_VA_COPY(count, vargs);
Walter Dörwaldc1651a02009-05-03 22:55:55 +00002400 /* step 1: count the number of %S/%R/%A/%s format specifications
2401 * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/
2402 * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002403 * result in an array)
Georg Brandl7597add2011-10-05 16:36:47 +02002404 * also estimate a upper bound for all the number formats in the string,
2405 * numbers will be formatted in step 3 and be kept in a '\0'-separated
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002406 * buffer before putting everything together. */
Benjamin Peterson14339b62009-01-31 16:36:08 +00002407 for (f = format; *f; f++) {
2408 if (*f == '%') {
Victor Stinner96865452011-03-01 23:44:09 +00002409 int longlongflag;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002410 /* skip width or width.precision (eg. "1.2" of "%1.2f") */
2411 f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL);
2412 if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V')
2413 ++callcount;
Walter Dörwaldd2034312007-05-18 16:29:38 +00002414
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002415 else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') {
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +00002416#ifdef HAVE_LONG_LONG
2417 if (longlongflag) {
2418 if (width < MAX_LONG_LONG_CHARS)
2419 width = MAX_LONG_LONG_CHARS;
2420 }
2421 else
2422#endif
2423 /* MAX_LONG_CHARS is enough to hold a 64-bit integer,
2424 including sign. Decimal takes the most space. This
2425 isn't enough for octal. If a width is specified we
2426 need more (which we allocate later). */
2427 if (width < MAX_LONG_CHARS)
2428 width = MAX_LONG_CHARS;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002429
2430 /* account for the size + '\0' to separate numbers
2431 inside of the numberresults buffer */
2432 numbersize += (width + 1);
2433 }
2434 }
2435 else if ((unsigned char)*f > 127) {
2436 PyErr_Format(PyExc_ValueError,
2437 "PyUnicode_FromFormatV() expects an ASCII-encoded format "
2438 "string, got a non-ASCII byte: 0x%02x",
2439 (unsigned char)*f);
2440 return NULL;
2441 }
2442 }
2443 /* step 2: allocate memory for the results of
2444 * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */
2445 if (callcount) {
2446 callresults = PyObject_Malloc(sizeof(PyObject *) * callcount);
2447 if (!callresults) {
2448 PyErr_NoMemory();
2449 return NULL;
2450 }
2451 callresult = callresults;
2452 }
2453 /* step 2.5: allocate memory for the results of formating numbers */
2454 if (numbersize) {
2455 numberresults = PyObject_Malloc(numbersize);
2456 if (!numberresults) {
2457 PyErr_NoMemory();
2458 goto fail;
2459 }
2460 numberresult = numberresults;
2461 }
2462
2463 /* step 3: format numbers and figure out how large a buffer we need */
2464 for (f = format; *f; f++) {
2465 if (*f == '%') {
2466 const char* p;
2467 int longflag;
2468 int longlongflag;
2469 int size_tflag;
2470 int numprinted;
2471
2472 p = f;
2473 zeropad = (f[1] == '0');
2474 f = parse_format_flags(f, &width, &precision,
2475 &longflag, &longlongflag, &size_tflag);
2476 switch (*f) {
2477 case 'c':
2478 {
2479 Py_UCS4 ordinal = va_arg(count, int);
Victor Stinnere6abb482012-05-02 01:15:40 +02002480 maxchar = MAX_MAXCHAR(maxchar, ordinal);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002481 n++;
2482 break;
2483 }
2484 case '%':
2485 n++;
2486 break;
2487 case 'i':
2488 case 'd':
2489 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
2490 width, precision, *f);
2491 if (longflag)
2492 numprinted = sprintf(numberresult, fmt,
2493 va_arg(count, long));
2494#ifdef HAVE_LONG_LONG
2495 else if (longlongflag)
2496 numprinted = sprintf(numberresult, fmt,
2497 va_arg(count, PY_LONG_LONG));
2498#endif
2499 else if (size_tflag)
2500 numprinted = sprintf(numberresult, fmt,
2501 va_arg(count, Py_ssize_t));
2502 else
2503 numprinted = sprintf(numberresult, fmt,
2504 va_arg(count, int));
2505 n += numprinted;
2506 /* advance by +1 to skip over the '\0' */
2507 numberresult += (numprinted + 1);
2508 assert(*(numberresult - 1) == '\0');
2509 assert(*(numberresult - 2) != '\0');
2510 assert(numprinted >= 0);
2511 assert(numberresult <= numberresults + numbersize);
2512 break;
2513 case 'u':
2514 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
2515 width, precision, 'u');
2516 if (longflag)
2517 numprinted = sprintf(numberresult, fmt,
2518 va_arg(count, unsigned long));
2519#ifdef HAVE_LONG_LONG
2520 else if (longlongflag)
2521 numprinted = sprintf(numberresult, fmt,
2522 va_arg(count, unsigned PY_LONG_LONG));
2523#endif
2524 else if (size_tflag)
2525 numprinted = sprintf(numberresult, fmt,
2526 va_arg(count, size_t));
2527 else
2528 numprinted = sprintf(numberresult, fmt,
2529 va_arg(count, unsigned int));
2530 n += numprinted;
2531 numberresult += (numprinted + 1);
2532 assert(*(numberresult - 1) == '\0');
2533 assert(*(numberresult - 2) != '\0');
2534 assert(numprinted >= 0);
2535 assert(numberresult <= numberresults + numbersize);
2536 break;
2537 case 'x':
2538 makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
2539 numprinted = sprintf(numberresult, fmt, va_arg(count, int));
2540 n += numprinted;
2541 numberresult += (numprinted + 1);
2542 assert(*(numberresult - 1) == '\0');
2543 assert(*(numberresult - 2) != '\0');
2544 assert(numprinted >= 0);
2545 assert(numberresult <= numberresults + numbersize);
2546 break;
2547 case 'p':
2548 numprinted = sprintf(numberresult, "%p", va_arg(count, void*));
2549 /* %p is ill-defined: ensure leading 0x. */
2550 if (numberresult[1] == 'X')
2551 numberresult[1] = 'x';
2552 else if (numberresult[1] != 'x') {
2553 memmove(numberresult + 2, numberresult,
2554 strlen(numberresult) + 1);
2555 numberresult[0] = '0';
2556 numberresult[1] = 'x';
2557 numprinted += 2;
2558 }
2559 n += numprinted;
2560 numberresult += (numprinted + 1);
2561 assert(*(numberresult - 1) == '\0');
2562 assert(*(numberresult - 2) != '\0');
2563 assert(numprinted >= 0);
2564 assert(numberresult <= numberresults + numbersize);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002565 break;
2566 case 's':
2567 {
2568 /* UTF-8 */
Georg Brandl780b2a62009-05-05 09:19:59 +00002569 const char *s = va_arg(count, const char*);
Victor Stinnera1d12bb2011-12-11 21:53:09 +01002570 PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL);
Walter Dörwaldc1651a02009-05-03 22:55:55 +00002571 if (!str)
2572 goto fail;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002573 /* since PyUnicode_DecodeUTF8 returns already flexible
2574 unicode objects, there is no need to call ready on them */
2575 argmaxchar = PyUnicode_MAX_CHAR_VALUE(str);
Victor Stinnere6abb482012-05-02 01:15:40 +02002576 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002577 n += PyUnicode_GET_LENGTH(str);
Walter Dörwaldc1651a02009-05-03 22:55:55 +00002578 /* Remember the str and switch to the next slot */
2579 *callresult++ = str;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002580 break;
2581 }
2582 case 'U':
2583 {
2584 PyObject *obj = va_arg(count, PyObject *);
Victor Stinner910337b2011-10-03 03:20:16 +02002585 assert(obj && _PyUnicode_CHECK(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002586 if (PyUnicode_READY(obj) == -1)
2587 goto fail;
2588 argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj);
Victor Stinnere6abb482012-05-02 01:15:40 +02002589 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002590 n += PyUnicode_GET_LENGTH(obj);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002591 break;
2592 }
2593 case 'V':
2594 {
2595 PyObject *obj = va_arg(count, PyObject *);
2596 const char *str = va_arg(count, const char *);
Victor Stinner2512a8b2011-03-01 22:46:52 +00002597 PyObject *str_obj;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002598 assert(obj || str);
Victor Stinner910337b2011-10-03 03:20:16 +02002599 assert(!obj || _PyUnicode_CHECK(obj));
Victor Stinner2512a8b2011-03-01 22:46:52 +00002600 if (obj) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002601 if (PyUnicode_READY(obj) == -1)
2602 goto fail;
2603 argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj);
Victor Stinnere6abb482012-05-02 01:15:40 +02002604 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002605 n += PyUnicode_GET_LENGTH(obj);
Victor Stinner2512a8b2011-03-01 22:46:52 +00002606 *callresult++ = NULL;
2607 }
2608 else {
Victor Stinnera1d12bb2011-12-11 21:53:09 +01002609 str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL);
Victor Stinner2512a8b2011-03-01 22:46:52 +00002610 if (!str_obj)
2611 goto fail;
Benjamin Petersonbac79492012-01-14 13:34:47 -05002612 if (PyUnicode_READY(str_obj) == -1) {
Victor Stinnere1335c72011-10-04 20:53:03 +02002613 Py_DECREF(str_obj);
2614 goto fail;
2615 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002616 argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj);
Victor Stinnere6abb482012-05-02 01:15:40 +02002617 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002618 n += PyUnicode_GET_LENGTH(str_obj);
Victor Stinner2512a8b2011-03-01 22:46:52 +00002619 *callresult++ = str_obj;
2620 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00002621 break;
2622 }
2623 case 'S':
2624 {
2625 PyObject *obj = va_arg(count, PyObject *);
2626 PyObject *str;
2627 assert(obj);
2628 str = PyObject_Str(obj);
Benjamin Petersonc8d8b882012-01-14 13:37:31 -05002629 if (!str)
Benjamin Peterson14339b62009-01-31 16:36:08 +00002630 goto fail;
Benjamin Petersonc8d8b882012-01-14 13:37:31 -05002631 if (PyUnicode_READY(str) == -1) {
2632 Py_DECREF(str);
2633 goto fail;
2634 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002635 argmaxchar = PyUnicode_MAX_CHAR_VALUE(str);
Victor Stinnere6abb482012-05-02 01:15:40 +02002636 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002637 n += PyUnicode_GET_LENGTH(str);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002638 /* Remember the str and switch to the next slot */
2639 *callresult++ = str;
2640 break;
2641 }
2642 case 'R':
2643 {
2644 PyObject *obj = va_arg(count, PyObject *);
2645 PyObject *repr;
2646 assert(obj);
2647 repr = PyObject_Repr(obj);
Benjamin Petersonc8d8b882012-01-14 13:37:31 -05002648 if (!repr)
Benjamin Peterson14339b62009-01-31 16:36:08 +00002649 goto fail;
Benjamin Petersonc8d8b882012-01-14 13:37:31 -05002650 if (PyUnicode_READY(repr) == -1) {
2651 Py_DECREF(repr);
2652 goto fail;
2653 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002654 argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr);
Victor Stinnere6abb482012-05-02 01:15:40 +02002655 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002656 n += PyUnicode_GET_LENGTH(repr);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002657 /* Remember the repr and switch to the next slot */
2658 *callresult++ = repr;
2659 break;
2660 }
2661 case 'A':
2662 {
2663 PyObject *obj = va_arg(count, PyObject *);
2664 PyObject *ascii;
2665 assert(obj);
2666 ascii = PyObject_ASCII(obj);
Benjamin Petersonc8d8b882012-01-14 13:37:31 -05002667 if (!ascii)
Benjamin Peterson14339b62009-01-31 16:36:08 +00002668 goto fail;
Benjamin Petersonc8d8b882012-01-14 13:37:31 -05002669 if (PyUnicode_READY(ascii) == -1) {
2670 Py_DECREF(ascii);
2671 goto fail;
2672 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002673 argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii);
Victor Stinnere6abb482012-05-02 01:15:40 +02002674 maxchar = MAX_MAXCHAR(maxchar, argmaxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002675 n += PyUnicode_GET_LENGTH(ascii);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002676 /* Remember the repr and switch to the next slot */
2677 *callresult++ = ascii;
2678 break;
2679 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00002680 default:
2681 /* if we stumble upon an unknown
2682 formatting code, copy the rest of
2683 the format string to the output
2684 string. (we cannot just skip the
2685 code, since there's no way to know
2686 what's in the argument list) */
2687 n += strlen(p);
2688 goto expand;
2689 }
2690 } else
2691 n++;
2692 }
Benjamin Peterson29060642009-01-31 22:14:21 +00002693 expand:
Benjamin Peterson14339b62009-01-31 16:36:08 +00002694 /* step 4: fill the buffer */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002695 /* Since we've analyzed how much space we need,
Benjamin Peterson14339b62009-01-31 16:36:08 +00002696 we don't have to resize the string.
2697 There can be no errors beyond this point. */
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02002698 string = PyUnicode_New(n, maxchar);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002699 if (!string)
2700 goto fail;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002701 kind = PyUnicode_KIND(string);
2702 data = PyUnicode_DATA(string);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002703 callresult = callresults;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002704 numberresult = numberresults;
Walter Dörwaldd2034312007-05-18 16:29:38 +00002705
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002706 for (i = 0, f = format; *f; f++) {
Benjamin Peterson14339b62009-01-31 16:36:08 +00002707 if (*f == '%') {
Victor Stinner96865452011-03-01 23:44:09 +00002708 const char* p;
Victor Stinner96865452011-03-01 23:44:09 +00002709
2710 p = f;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002711 f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL);
2712 /* checking for == because the last argument could be a empty
2713 string, which causes i to point to end, the assert at the end of
2714 the loop */
2715 assert(i <= PyUnicode_GET_LENGTH(string));
Walter Dörwaldd2034312007-05-18 16:29:38 +00002716
Benjamin Peterson14339b62009-01-31 16:36:08 +00002717 switch (*f) {
2718 case 'c':
Victor Stinner5ed8b2c2011-02-21 21:13:44 +00002719 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002720 const int ordinal = va_arg(vargs, int);
2721 PyUnicode_WRITE(kind, data, i++, ordinal);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002722 break;
Victor Stinner5ed8b2c2011-02-21 21:13:44 +00002723 }
Victor Stinner6d970f42011-03-02 00:04:25 +00002724 case 'i':
Benjamin Peterson14339b62009-01-31 16:36:08 +00002725 case 'd':
Benjamin Peterson14339b62009-01-31 16:36:08 +00002726 case 'u':
Benjamin Peterson14339b62009-01-31 16:36:08 +00002727 case 'x':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002728 case 'p':
Victor Stinnerc5166102012-02-22 13:55:02 +01002729 {
Victor Stinner184252a2012-06-16 02:57:41 +02002730 Py_ssize_t len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002731 /* unused, since we already have the result */
2732 if (*f == 'p')
2733 (void) va_arg(vargs, void *);
2734 else
2735 (void) va_arg(vargs, int);
2736 /* extract the result from numberresults and append. */
Victor Stinner184252a2012-06-16 02:57:41 +02002737 len = strlen(numberresult);
2738 unicode_write_cstr(string, i, numberresult, len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002739 /* skip over the separating '\0' */
Victor Stinner184252a2012-06-16 02:57:41 +02002740 i += len;
2741 numberresult += len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002742 assert(*numberresult == '\0');
2743 numberresult++;
2744 assert(numberresult <= numberresults + numbersize);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002745 break;
Victor Stinnerc5166102012-02-22 13:55:02 +01002746 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00002747 case 's':
2748 {
Walter Dörwaldc1651a02009-05-03 22:55:55 +00002749 /* unused, since we already have the result */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002750 Py_ssize_t size;
Walter Dörwaldc1651a02009-05-03 22:55:55 +00002751 (void) va_arg(vargs, char *);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002752 size = PyUnicode_GET_LENGTH(*callresult);
2753 assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
Victor Stinnerd3f08822012-05-29 12:57:52 +02002754 _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002755 i += size;
Walter Dörwaldc1651a02009-05-03 22:55:55 +00002756 /* We're done with the unicode()/repr() => forget it */
2757 Py_DECREF(*callresult);
2758 /* switch to next unicode()/repr() result */
2759 ++callresult;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002760 break;
2761 }
2762 case 'U':
2763 {
2764 PyObject *obj = va_arg(vargs, PyObject *);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002765 Py_ssize_t size;
2766 assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
2767 size = PyUnicode_GET_LENGTH(obj);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002768 _PyUnicode_FastCopyCharacters(string, i, obj, 0, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002769 i += size;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002770 break;
2771 }
2772 case 'V':
2773 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002774 Py_ssize_t size;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002775 PyObject *obj = va_arg(vargs, PyObject *);
Victor Stinner2512a8b2011-03-01 22:46:52 +00002776 va_arg(vargs, const char *);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002777 if (obj) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002778 size = PyUnicode_GET_LENGTH(obj);
2779 assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
Victor Stinnerd3f08822012-05-29 12:57:52 +02002780 _PyUnicode_FastCopyCharacters(string, i, obj, 0, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002781 i += size;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002782 } else {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002783 size = PyUnicode_GET_LENGTH(*callresult);
2784 assert(PyUnicode_KIND(*callresult) <=
2785 PyUnicode_KIND(string));
Victor Stinnerd3f08822012-05-29 12:57:52 +02002786 _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002787 i += size;
Victor Stinner2512a8b2011-03-01 22:46:52 +00002788 Py_DECREF(*callresult);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002789 }
Victor Stinner2512a8b2011-03-01 22:46:52 +00002790 ++callresult;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002791 break;
2792 }
2793 case 'S':
2794 case 'R':
Victor Stinner9a909002010-10-18 20:59:24 +00002795 case 'A':
Benjamin Peterson14339b62009-01-31 16:36:08 +00002796 {
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02002797 Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002798 /* unused, since we already have the result */
2799 (void) va_arg(vargs, PyObject *);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002800 assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
Victor Stinnerd3f08822012-05-29 12:57:52 +02002801 _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size);
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02002802 i += size;
Benjamin Peterson14339b62009-01-31 16:36:08 +00002803 /* We're done with the unicode()/repr() => forget it */
2804 Py_DECREF(*callresult);
2805 /* switch to next unicode()/repr() result */
2806 ++callresult;
2807 break;
2808 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00002809 case '%':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002810 PyUnicode_WRITE(kind, data, i++, '%');
Benjamin Peterson14339b62009-01-31 16:36:08 +00002811 break;
2812 default:
Victor Stinner184252a2012-06-16 02:57:41 +02002813 {
2814 Py_ssize_t len = strlen(p);
2815 unicode_write_cstr(string, i, p, len);
2816 i += len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002817 assert(i == PyUnicode_GET_LENGTH(string));
Benjamin Peterson14339b62009-01-31 16:36:08 +00002818 goto end;
2819 }
Victor Stinner184252a2012-06-16 02:57:41 +02002820 }
Victor Stinner1205f272010-09-11 00:54:47 +00002821 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002822 else {
2823 assert(i < PyUnicode_GET_LENGTH(string));
2824 PyUnicode_WRITE(kind, data, i++, *f);
2825 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00002826 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002827 assert(i == PyUnicode_GET_LENGTH(string));
Walter Dörwaldd2034312007-05-18 16:29:38 +00002828
Benjamin Peterson29060642009-01-31 22:14:21 +00002829 end:
Benjamin Peterson14339b62009-01-31 16:36:08 +00002830 if (callresults)
2831 PyObject_Free(callresults);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002832 if (numberresults)
2833 PyObject_Free(numberresults);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01002834 return unicode_result(string);
Benjamin Peterson29060642009-01-31 22:14:21 +00002835 fail:
Benjamin Peterson14339b62009-01-31 16:36:08 +00002836 if (callresults) {
2837 PyObject **callresult2 = callresults;
2838 while (callresult2 < callresult) {
Victor Stinner2512a8b2011-03-01 22:46:52 +00002839 Py_XDECREF(*callresult2);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002840 ++callresult2;
2841 }
2842 PyObject_Free(callresults);
2843 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002844 if (numberresults)
2845 PyObject_Free(numberresults);
Benjamin Peterson14339b62009-01-31 16:36:08 +00002846 return NULL;
Walter Dörwaldd2034312007-05-18 16:29:38 +00002847}
2848
Walter Dörwaldd2034312007-05-18 16:29:38 +00002849PyObject *
2850PyUnicode_FromFormat(const char *format, ...)
2851{
Benjamin Peterson14339b62009-01-31 16:36:08 +00002852 PyObject* ret;
2853 va_list vargs;
Walter Dörwaldd2034312007-05-18 16:29:38 +00002854
2855#ifdef HAVE_STDARG_PROTOTYPES
Benjamin Peterson14339b62009-01-31 16:36:08 +00002856 va_start(vargs, format);
Walter Dörwaldd2034312007-05-18 16:29:38 +00002857#else
Benjamin Peterson14339b62009-01-31 16:36:08 +00002858 va_start(vargs);
Walter Dörwaldd2034312007-05-18 16:29:38 +00002859#endif
Benjamin Peterson14339b62009-01-31 16:36:08 +00002860 ret = PyUnicode_FromFormatV(format, vargs);
2861 va_end(vargs);
2862 return ret;
Walter Dörwaldd2034312007-05-18 16:29:38 +00002863}
2864
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002865#ifdef HAVE_WCHAR_H
2866
Victor Stinner5593d8a2010-10-02 11:11:27 +00002867/* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString():
2868 convert a Unicode object to a wide character string.
2869
Victor Stinnerd88d9832011-09-06 02:00:05 +02002870 - If w is NULL: return the number of wide characters (including the null
Victor Stinner5593d8a2010-10-02 11:11:27 +00002871 character) required to convert the unicode object. Ignore size argument.
2872
Victor Stinnerd88d9832011-09-06 02:00:05 +02002873 - Otherwise: return the number of wide characters (excluding the null
Victor Stinner5593d8a2010-10-02 11:11:27 +00002874 character) written into w. Write at most size wide characters (including
Victor Stinnerd88d9832011-09-06 02:00:05 +02002875 the null character). */
Victor Stinner5593d8a2010-10-02 11:11:27 +00002876static Py_ssize_t
Victor Stinner9db1a8b2011-10-23 20:04:37 +02002877unicode_aswidechar(PyObject *unicode,
Victor Stinner137c34c2010-09-29 10:25:54 +00002878 wchar_t *w,
2879 Py_ssize_t size)
2880{
Victor Stinner5593d8a2010-10-02 11:11:27 +00002881 Py_ssize_t res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002882 const wchar_t *wstr;
2883
Victor Stinner9db1a8b2011-10-23 20:04:37 +02002884 wstr = PyUnicode_AsUnicodeAndSize(unicode, &res);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002885 if (wstr == NULL)
2886 return -1;
2887
Victor Stinner5593d8a2010-10-02 11:11:27 +00002888 if (w != NULL) {
Victor Stinner5593d8a2010-10-02 11:11:27 +00002889 if (size > res)
2890 size = res + 1;
2891 else
2892 res = size;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002893 Py_MEMCPY(w, wstr, size * sizeof(wchar_t));
Victor Stinner5593d8a2010-10-02 11:11:27 +00002894 return res;
2895 }
2896 else
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002897 return res + 1;
Victor Stinner137c34c2010-09-29 10:25:54 +00002898}
2899
2900Py_ssize_t
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002901PyUnicode_AsWideChar(PyObject *unicode,
Victor Stinner137c34c2010-09-29 10:25:54 +00002902 wchar_t *w,
2903 Py_ssize_t size)
Guido van Rossumd57fd912000-03-10 22:53:23 +00002904{
2905 if (unicode == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00002906 PyErr_BadInternalCall();
2907 return -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00002908 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02002909 return unicode_aswidechar(unicode, w, size);
Guido van Rossumd57fd912000-03-10 22:53:23 +00002910}
2911
Victor Stinner137c34c2010-09-29 10:25:54 +00002912wchar_t*
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00002913PyUnicode_AsWideCharString(PyObject *unicode,
Victor Stinner137c34c2010-09-29 10:25:54 +00002914 Py_ssize_t *size)
2915{
2916 wchar_t* buffer;
2917 Py_ssize_t buflen;
2918
2919 if (unicode == NULL) {
2920 PyErr_BadInternalCall();
2921 return NULL;
2922 }
2923
Victor Stinner9db1a8b2011-10-23 20:04:37 +02002924 buflen = unicode_aswidechar(unicode, NULL, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002925 if (buflen == -1)
2926 return NULL;
Victor Stinner5593d8a2010-10-02 11:11:27 +00002927 if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) {
Victor Stinner137c34c2010-09-29 10:25:54 +00002928 PyErr_NoMemory();
2929 return NULL;
2930 }
2931
Victor Stinner137c34c2010-09-29 10:25:54 +00002932 buffer = PyMem_MALLOC(buflen * sizeof(wchar_t));
2933 if (buffer == NULL) {
2934 PyErr_NoMemory();
2935 return NULL;
2936 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02002937 buflen = unicode_aswidechar(unicode, buffer, buflen);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002938 if (buflen == -1)
2939 return NULL;
Victor Stinner5593d8a2010-10-02 11:11:27 +00002940 if (size != NULL)
2941 *size = buflen;
Victor Stinner137c34c2010-09-29 10:25:54 +00002942 return buffer;
2943}
2944
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002945#endif /* HAVE_WCHAR_H */
Guido van Rossumd57fd912000-03-10 22:53:23 +00002946
Alexander Belopolsky40018472011-02-26 01:02:56 +00002947PyObject *
2948PyUnicode_FromOrdinal(int ordinal)
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +00002949{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002950 PyObject *v;
Victor Stinner8faf8212011-12-08 22:14:11 +01002951 if (ordinal < 0 || ordinal > MAX_UNICODE) {
Benjamin Peterson29060642009-01-31 22:14:21 +00002952 PyErr_SetString(PyExc_ValueError,
2953 "chr() arg not in range(0x110000)");
2954 return NULL;
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +00002955 }
Guido van Rossum8ac004e2007-07-15 13:00:05 +00002956
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002957 if (ordinal < 256)
2958 return get_latin1_char(ordinal);
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +00002959
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002960 v = PyUnicode_New(1, ordinal);
2961 if (v == NULL)
2962 return NULL;
2963 PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02002964 assert(_PyUnicode_CheckConsistency(v, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002965 return v;
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +00002966}
2967
Alexander Belopolsky40018472011-02-26 01:02:56 +00002968PyObject *
2969PyUnicode_FromObject(register PyObject *obj)
Guido van Rossumd57fd912000-03-10 22:53:23 +00002970{
Guido van Rossumb8c65bc2001-10-19 02:01:31 +00002971 /* XXX Perhaps we should make this API an alias of
Benjamin Peterson29060642009-01-31 22:14:21 +00002972 PyObject_Str() instead ?! */
Guido van Rossumb8c65bc2001-10-19 02:01:31 +00002973 if (PyUnicode_CheckExact(obj)) {
Benjamin Petersonbac79492012-01-14 13:34:47 -05002974 if (PyUnicode_READY(obj) == -1)
Victor Stinnerd3a83d52011-10-01 03:09:33 +02002975 return NULL;
Benjamin Peterson29060642009-01-31 22:14:21 +00002976 Py_INCREF(obj);
2977 return obj;
Guido van Rossumb8c65bc2001-10-19 02:01:31 +00002978 }
2979 if (PyUnicode_Check(obj)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00002980 /* For a Unicode subtype that's not a Unicode object,
2981 return a true Unicode object with the same data. */
Victor Stinnerbf6e5602011-12-12 01:53:47 +01002982 return _PyUnicode_Copy(obj);
Guido van Rossumb8c65bc2001-10-19 02:01:31 +00002983 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002984 PyErr_Format(PyExc_TypeError,
2985 "Can't convert '%.100s' object to str implicitly",
Christian Heimes90aa7642007-12-19 02:45:37 +00002986 Py_TYPE(obj)->tp_name);
Guido van Rossum98297ee2007-11-06 21:34:58 +00002987 return NULL;
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +00002988}
2989
Alexander Belopolsky40018472011-02-26 01:02:56 +00002990PyObject *
2991PyUnicode_FromEncodedObject(register PyObject *obj,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03002992 const char *encoding,
2993 const char *errors)
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +00002994{
Antoine Pitroub0fa8312010-09-01 15:10:12 +00002995 Py_buffer buffer;
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +00002996 PyObject *v;
Tim Petersced69f82003-09-16 20:30:58 +00002997
Guido van Rossumd57fd912000-03-10 22:53:23 +00002998 if (obj == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00002999 PyErr_BadInternalCall();
3000 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00003001 }
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +00003002
Antoine Pitroub0fa8312010-09-01 15:10:12 +00003003 /* Decoding bytes objects is the most common case and should be fast */
3004 if (PyBytes_Check(obj)) {
3005 if (PyBytes_GET_SIZE(obj) == 0) {
3006 Py_INCREF(unicode_empty);
Victor Stinnera464fc12011-10-02 20:39:30 +02003007 v = unicode_empty;
Antoine Pitroub0fa8312010-09-01 15:10:12 +00003008 }
3009 else {
3010 v = PyUnicode_Decode(
3011 PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj),
3012 encoding, errors);
3013 }
3014 return v;
3015 }
3016
Guido van Rossumb8c65bc2001-10-19 02:01:31 +00003017 if (PyUnicode_Check(obj)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00003018 PyErr_SetString(PyExc_TypeError,
3019 "decoding str is not supported");
3020 return NULL;
Benjamin Peterson14339b62009-01-31 16:36:08 +00003021 }
Guido van Rossumb8c65bc2001-10-19 02:01:31 +00003022
Antoine Pitroub0fa8312010-09-01 15:10:12 +00003023 /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
3024 if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
3025 PyErr_Format(PyExc_TypeError,
3026 "coercing to str: need bytes, bytearray "
3027 "or buffer-like object, %.80s found",
3028 Py_TYPE(obj)->tp_name);
3029 return NULL;
Marc-André Lemburg6871f6a2001-09-20 12:53:16 +00003030 }
Tim Petersced69f82003-09-16 20:30:58 +00003031
Antoine Pitroub0fa8312010-09-01 15:10:12 +00003032 if (buffer.len == 0) {
Benjamin Peterson29060642009-01-31 22:14:21 +00003033 Py_INCREF(unicode_empty);
Victor Stinnera464fc12011-10-02 20:39:30 +02003034 v = unicode_empty;
Guido van Rossumd57fd912000-03-10 22:53:23 +00003035 }
Tim Petersced69f82003-09-16 20:30:58 +00003036 else
Antoine Pitroub0fa8312010-09-01 15:10:12 +00003037 v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors);
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +00003038
Antoine Pitroub0fa8312010-09-01 15:10:12 +00003039 PyBuffer_Release(&buffer);
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +00003040 return v;
Guido van Rossumd57fd912000-03-10 22:53:23 +00003041}
3042
Victor Stinner600d3be2010-06-10 12:00:55 +00003043/* Convert encoding to lower case and replace '_' with '-' in order to
Victor Stinner37296e82010-06-10 13:36:23 +00003044 catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1),
3045 1 on success. */
3046static int
3047normalize_encoding(const char *encoding,
3048 char *lower,
3049 size_t lower_len)
Guido van Rossumd57fd912000-03-10 22:53:23 +00003050{
Guido van Rossumdaa251c2007-10-25 23:47:33 +00003051 const char *e;
Victor Stinner600d3be2010-06-10 12:00:55 +00003052 char *l;
3053 char *l_end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00003054
Benjamin Peterson7a6debe2011-10-15 09:25:28 -04003055 if (encoding == NULL) {
3056 strcpy(lower, "utf-8");
3057 return 1;
3058 }
Guido van Rossumdaa251c2007-10-25 23:47:33 +00003059 e = encoding;
3060 l = lower;
Victor Stinner600d3be2010-06-10 12:00:55 +00003061 l_end = &lower[lower_len - 1];
Victor Stinner37296e82010-06-10 13:36:23 +00003062 while (*e) {
3063 if (l == l_end)
3064 return 0;
David Malcolm96960882010-11-05 17:23:41 +00003065 if (Py_ISUPPER(*e)) {
3066 *l++ = Py_TOLOWER(*e++);
Guido van Rossumdaa251c2007-10-25 23:47:33 +00003067 }
3068 else if (*e == '_') {
3069 *l++ = '-';
3070 e++;
3071 }
3072 else {
3073 *l++ = *e++;
3074 }
3075 }
3076 *l = '\0';
Victor Stinner37296e82010-06-10 13:36:23 +00003077 return 1;
Victor Stinner600d3be2010-06-10 12:00:55 +00003078}
3079
Alexander Belopolsky40018472011-02-26 01:02:56 +00003080PyObject *
3081PyUnicode_Decode(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003082 Py_ssize_t size,
3083 const char *encoding,
3084 const char *errors)
Victor Stinner600d3be2010-06-10 12:00:55 +00003085{
3086 PyObject *buffer = NULL, *unicode;
3087 Py_buffer info;
3088 char lower[11]; /* Enough for any encoding shortcut */
3089
Fred Drakee4315f52000-05-09 19:53:39 +00003090 /* Shortcuts for common default encodings */
Victor Stinner37296e82010-06-10 13:36:23 +00003091 if (normalize_encoding(encoding, lower, sizeof(lower))) {
Alexander Belopolsky1d521462011-02-25 19:19:57 +00003092 if ((strcmp(lower, "utf-8") == 0) ||
3093 (strcmp(lower, "utf8") == 0))
Victor Stinnera1d12bb2011-12-11 21:53:09 +01003094 return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);
Victor Stinner37296e82010-06-10 13:36:23 +00003095 else if ((strcmp(lower, "latin-1") == 0) ||
Alexander Belopolsky1d521462011-02-25 19:19:57 +00003096 (strcmp(lower, "latin1") == 0) ||
Victor Stinner37296e82010-06-10 13:36:23 +00003097 (strcmp(lower, "iso-8859-1") == 0))
3098 return PyUnicode_DecodeLatin1(s, size, errors);
Victor Stinner99b95382011-07-04 14:23:54 +02003099#ifdef HAVE_MBCS
Victor Stinner37296e82010-06-10 13:36:23 +00003100 else if (strcmp(lower, "mbcs") == 0)
3101 return PyUnicode_DecodeMBCS(s, size, errors);
Mark Hammond0ccda1e2003-07-01 00:13:27 +00003102#endif
Victor Stinner37296e82010-06-10 13:36:23 +00003103 else if (strcmp(lower, "ascii") == 0)
3104 return PyUnicode_DecodeASCII(s, size, errors);
3105 else if (strcmp(lower, "utf-16") == 0)
3106 return PyUnicode_DecodeUTF16(s, size, errors, 0);
3107 else if (strcmp(lower, "utf-32") == 0)
3108 return PyUnicode_DecodeUTF32(s, size, errors, 0);
3109 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00003110
3111 /* Decode via the codec registry */
Guido van Rossumbe801ac2007-10-08 03:32:34 +00003112 buffer = NULL;
Antoine Pitrouc3b39242009-01-03 16:59:18 +00003113 if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0)
Guido van Rossumbe801ac2007-10-08 03:32:34 +00003114 goto onError;
Antoine Pitrouee58fa42008-08-19 18:22:14 +00003115 buffer = PyMemoryView_FromBuffer(&info);
Guido van Rossumd57fd912000-03-10 22:53:23 +00003116 if (buffer == NULL)
3117 goto onError;
3118 unicode = PyCodec_Decode(buffer, encoding, errors);
3119 if (unicode == NULL)
3120 goto onError;
3121 if (!PyUnicode_Check(unicode)) {
3122 PyErr_Format(PyExc_TypeError,
Benjamin Peterson142957c2008-07-04 19:55:29 +00003123 "decoder did not return a str object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +00003124 Py_TYPE(unicode)->tp_name);
Guido van Rossumd57fd912000-03-10 22:53:23 +00003125 Py_DECREF(unicode);
3126 goto onError;
3127 }
3128 Py_DECREF(buffer);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01003129 return unicode_result(unicode);
Tim Petersced69f82003-09-16 20:30:58 +00003130
Benjamin Peterson29060642009-01-31 22:14:21 +00003131 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00003132 Py_XDECREF(buffer);
3133 return NULL;
3134}
3135
Alexander Belopolsky40018472011-02-26 01:02:56 +00003136PyObject *
3137PyUnicode_AsDecodedObject(PyObject *unicode,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003138 const char *encoding,
3139 const char *errors)
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003140{
3141 PyObject *v;
3142
3143 if (!PyUnicode_Check(unicode)) {
3144 PyErr_BadArgument();
3145 goto onError;
3146 }
3147
3148 if (encoding == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00003149 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003150
3151 /* Decode via the codec registry */
3152 v = PyCodec_Decode(unicode, encoding, errors);
3153 if (v == NULL)
3154 goto onError;
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01003155 return unicode_result(v);
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003156
Benjamin Peterson29060642009-01-31 22:14:21 +00003157 onError:
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003158 return NULL;
3159}
3160
Alexander Belopolsky40018472011-02-26 01:02:56 +00003161PyObject *
3162PyUnicode_AsDecodedUnicode(PyObject *unicode,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003163 const char *encoding,
3164 const char *errors)
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003165{
3166 PyObject *v;
3167
3168 if (!PyUnicode_Check(unicode)) {
3169 PyErr_BadArgument();
3170 goto onError;
3171 }
3172
3173 if (encoding == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00003174 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003175
3176 /* Decode via the codec registry */
3177 v = PyCodec_Decode(unicode, encoding, errors);
3178 if (v == NULL)
3179 goto onError;
3180 if (!PyUnicode_Check(v)) {
3181 PyErr_Format(PyExc_TypeError,
Benjamin Peterson142957c2008-07-04 19:55:29 +00003182 "decoder did not return a str object (type=%.400s)",
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003183 Py_TYPE(v)->tp_name);
3184 Py_DECREF(v);
3185 goto onError;
3186 }
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01003187 return unicode_result(v);
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003188
Benjamin Peterson29060642009-01-31 22:14:21 +00003189 onError:
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003190 return NULL;
3191}
3192
Alexander Belopolsky40018472011-02-26 01:02:56 +00003193PyObject *
3194PyUnicode_Encode(const Py_UNICODE *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003195 Py_ssize_t size,
3196 const char *encoding,
3197 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00003198{
3199 PyObject *v, *unicode;
Tim Petersced69f82003-09-16 20:30:58 +00003200
Guido van Rossumd57fd912000-03-10 22:53:23 +00003201 unicode = PyUnicode_FromUnicode(s, size);
3202 if (unicode == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00003203 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00003204 v = PyUnicode_AsEncodedString(unicode, encoding, errors);
3205 Py_DECREF(unicode);
3206 return v;
3207}
3208
Alexander Belopolsky40018472011-02-26 01:02:56 +00003209PyObject *
3210PyUnicode_AsEncodedObject(PyObject *unicode,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003211 const char *encoding,
3212 const char *errors)
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003213{
3214 PyObject *v;
3215
3216 if (!PyUnicode_Check(unicode)) {
3217 PyErr_BadArgument();
3218 goto onError;
3219 }
3220
3221 if (encoding == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00003222 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003223
3224 /* Encode via the codec registry */
3225 v = PyCodec_Encode(unicode, encoding, errors);
3226 if (v == NULL)
3227 goto onError;
3228 return v;
3229
Benjamin Peterson29060642009-01-31 22:14:21 +00003230 onError:
Marc-André Lemburgd2d45982004-07-08 17:57:32 +00003231 return NULL;
3232}
3233
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003234static size_t
3235wcstombs_errorpos(const wchar_t *wstr)
3236{
3237 size_t len;
3238#if SIZEOF_WCHAR_T == 2
3239 wchar_t buf[3];
3240#else
3241 wchar_t buf[2];
3242#endif
3243 char outbuf[MB_LEN_MAX];
3244 const wchar_t *start, *previous;
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003245
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003246#if SIZEOF_WCHAR_T == 2
3247 buf[2] = 0;
3248#else
3249 buf[1] = 0;
3250#endif
3251 start = wstr;
3252 while (*wstr != L'\0')
3253 {
3254 previous = wstr;
3255#if SIZEOF_WCHAR_T == 2
3256 if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0])
3257 && Py_UNICODE_IS_LOW_SURROGATE(wstr[1]))
3258 {
3259 buf[0] = wstr[0];
3260 buf[1] = wstr[1];
3261 wstr += 2;
3262 }
3263 else {
3264 buf[0] = *wstr;
3265 buf[1] = 0;
3266 wstr++;
3267 }
3268#else
3269 buf[0] = *wstr;
3270 wstr++;
3271#endif
3272 len = wcstombs(outbuf, buf, sizeof(outbuf));
Victor Stinner2f197072011-12-17 07:08:30 +01003273 if (len == (size_t)-1)
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003274 return previous - start;
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003275 }
3276
3277 /* failed to find the unencodable character */
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003278 return 0;
3279}
3280
Victor Stinner1b579672011-12-17 05:47:23 +01003281static int
3282locale_error_handler(const char *errors, int *surrogateescape)
3283{
3284 if (errors == NULL) {
3285 *surrogateescape = 0;
3286 return 0;
3287 }
3288
3289 if (strcmp(errors, "strict") == 0) {
3290 *surrogateescape = 0;
3291 return 0;
3292 }
3293 if (strcmp(errors, "surrogateescape") == 0) {
3294 *surrogateescape = 1;
3295 return 0;
3296 }
3297 PyErr_Format(PyExc_ValueError,
3298 "only 'strict' and 'surrogateescape' error handlers "
3299 "are supported, not '%s'",
3300 errors);
3301 return -1;
3302}
3303
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003304PyObject *
Victor Stinner1b579672011-12-17 05:47:23 +01003305PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003306{
3307 Py_ssize_t wlen, wlen2;
3308 wchar_t *wstr;
3309 PyObject *bytes = NULL;
3310 char *errmsg;
Victor Stinner1f33f2b2011-12-17 04:45:09 +01003311 PyObject *reason;
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003312 PyObject *exc;
3313 size_t error_pos;
Victor Stinner1b579672011-12-17 05:47:23 +01003314 int surrogateescape;
3315
3316 if (locale_error_handler(errors, &surrogateescape) < 0)
3317 return NULL;
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003318
3319 wstr = PyUnicode_AsWideCharString(unicode, &wlen);
3320 if (wstr == NULL)
3321 return NULL;
3322
3323 wlen2 = wcslen(wstr);
3324 if (wlen2 != wlen) {
3325 PyMem_Free(wstr);
3326 PyErr_SetString(PyExc_TypeError, "embedded null character");
3327 return NULL;
3328 }
3329
3330 if (surrogateescape) {
3331 /* locale encoding with surrogateescape */
3332 char *str;
3333
3334 str = _Py_wchar2char(wstr, &error_pos);
3335 if (str == NULL) {
3336 if (error_pos == (size_t)-1) {
3337 PyErr_NoMemory();
3338 PyMem_Free(wstr);
3339 return NULL;
3340 }
3341 else {
3342 goto encode_error;
3343 }
3344 }
3345 PyMem_Free(wstr);
3346
3347 bytes = PyBytes_FromString(str);
3348 PyMem_Free(str);
3349 }
3350 else {
3351 size_t len, len2;
3352
3353 len = wcstombs(NULL, wstr, 0);
3354 if (len == (size_t)-1) {
Victor Stinner2f197072011-12-17 07:08:30 +01003355 error_pos = (size_t)-1;
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003356 goto encode_error;
3357 }
3358
3359 bytes = PyBytes_FromStringAndSize(NULL, len);
3360 if (bytes == NULL) {
3361 PyMem_Free(wstr);
3362 return NULL;
3363 }
3364
3365 len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1);
3366 if (len2 == (size_t)-1 || len2 > len) {
Victor Stinner2f197072011-12-17 07:08:30 +01003367 error_pos = (size_t)-1;
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003368 goto encode_error;
3369 }
3370 PyMem_Free(wstr);
3371 }
3372 return bytes;
3373
3374encode_error:
3375 errmsg = strerror(errno);
3376 assert(errmsg != NULL);
Victor Stinner2f197072011-12-17 07:08:30 +01003377
3378 if (error_pos == (size_t)-1)
3379 error_pos = wcstombs_errorpos(wstr);
3380
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003381 PyMem_Free(wstr);
3382 Py_XDECREF(bytes);
3383
Victor Stinner2f197072011-12-17 07:08:30 +01003384 if (errmsg != NULL) {
3385 size_t errlen;
3386 wstr = _Py_char2wchar(errmsg, &errlen);
3387 if (wstr != NULL) {
3388 reason = PyUnicode_FromWideChar(wstr, errlen);
3389 PyMem_Free(wstr);
3390 } else
3391 errmsg = NULL;
3392 }
3393 if (errmsg == NULL)
Victor Stinner1f33f2b2011-12-17 04:45:09 +01003394 reason = PyUnicode_FromString(
3395 "wcstombs() encountered an unencodable "
3396 "wide character");
3397 if (reason == NULL)
3398 return NULL;
3399
3400 exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO",
3401 "locale", unicode,
3402 (Py_ssize_t)error_pos,
3403 (Py_ssize_t)(error_pos+1),
3404 reason);
3405 Py_DECREF(reason);
3406 if (exc != NULL) {
3407 PyCodec_StrictErrors(exc);
3408 Py_XDECREF(exc);
3409 }
Victor Stinnerf2ea71f2011-12-17 04:13:41 +01003410 return NULL;
3411}
3412
Victor Stinnerad158722010-10-27 00:25:46 +00003413PyObject *
3414PyUnicode_EncodeFSDefault(PyObject *unicode)
Victor Stinnerae6265f2010-05-15 16:27:27 +00003415{
Victor Stinner99b95382011-07-04 14:23:54 +02003416#ifdef HAVE_MBCS
Victor Stinnerac931b12011-11-20 18:27:03 +01003417 return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL);
Victor Stinnerad158722010-10-27 00:25:46 +00003418#elif defined(__APPLE__)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003419 return _PyUnicode_AsUTF8String(unicode, "surrogateescape");
Victor Stinnerad158722010-10-27 00:25:46 +00003420#else
Victor Stinner793b5312011-04-27 00:24:21 +02003421 PyInterpreterState *interp = PyThreadState_GET()->interp;
3422 /* Bootstrap check: if the filesystem codec is implemented in Python, we
3423 cannot use it to encode and decode filenames before it is loaded. Load
3424 the Python codec requires to encode at least its own filename. Use the C
3425 version of the locale codec until the codec registry is initialized and
3426 the Python codec is loaded.
3427
3428 Py_FileSystemDefaultEncoding is shared between all interpreters, we
3429 cannot only rely on it: check also interp->fscodec_initialized for
3430 subinterpreters. */
3431 if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) {
Victor Stinnerae6265f2010-05-15 16:27:27 +00003432 return PyUnicode_AsEncodedString(unicode,
3433 Py_FileSystemDefaultEncoding,
3434 "surrogateescape");
Victor Stinnerc39211f2010-09-29 16:35:47 +00003435 }
3436 else {
Victor Stinner1b579672011-12-17 05:47:23 +01003437 return PyUnicode_EncodeLocale(unicode, "surrogateescape");
Victor Stinnerc39211f2010-09-29 16:35:47 +00003438 }
Victor Stinnerad158722010-10-27 00:25:46 +00003439#endif
Victor Stinnerae6265f2010-05-15 16:27:27 +00003440}
3441
Alexander Belopolsky40018472011-02-26 01:02:56 +00003442PyObject *
3443PyUnicode_AsEncodedString(PyObject *unicode,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003444 const char *encoding,
3445 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00003446{
3447 PyObject *v;
Victor Stinner600d3be2010-06-10 12:00:55 +00003448 char lower[11]; /* Enough for any encoding shortcut */
Tim Petersced69f82003-09-16 20:30:58 +00003449
Guido van Rossumd57fd912000-03-10 22:53:23 +00003450 if (!PyUnicode_Check(unicode)) {
3451 PyErr_BadArgument();
Amaury Forgeot d'Arcf0481112008-09-05 20:48:47 +00003452 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00003453 }
Fred Drakee4315f52000-05-09 19:53:39 +00003454
Fred Drakee4315f52000-05-09 19:53:39 +00003455 /* Shortcuts for common default encodings */
Victor Stinner37296e82010-06-10 13:36:23 +00003456 if (normalize_encoding(encoding, lower, sizeof(lower))) {
Alexander Belopolsky1d521462011-02-25 19:19:57 +00003457 if ((strcmp(lower, "utf-8") == 0) ||
3458 (strcmp(lower, "utf8") == 0))
Victor Stinnera5c68c32011-03-02 01:03:14 +00003459 {
Victor Stinner2f283c22011-03-02 01:21:46 +00003460 if (errors == NULL || strcmp(errors, "strict") == 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003461 return _PyUnicode_AsUTF8String(unicode, NULL);
Victor Stinner2f283c22011-03-02 01:21:46 +00003462 else
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003463 return _PyUnicode_AsUTF8String(unicode, errors);
Victor Stinnera5c68c32011-03-02 01:03:14 +00003464 }
Victor Stinner37296e82010-06-10 13:36:23 +00003465 else if ((strcmp(lower, "latin-1") == 0) ||
Alexander Belopolsky1d521462011-02-25 19:19:57 +00003466 (strcmp(lower, "latin1") == 0) ||
Victor Stinner37296e82010-06-10 13:36:23 +00003467 (strcmp(lower, "iso-8859-1") == 0))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003468 return _PyUnicode_AsLatin1String(unicode, errors);
Victor Stinner99b95382011-07-04 14:23:54 +02003469#ifdef HAVE_MBCS
Victor Stinnerac931b12011-11-20 18:27:03 +01003470 else if (strcmp(lower, "mbcs") == 0)
3471 return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors);
Mark Hammond0ccda1e2003-07-01 00:13:27 +00003472#endif
Victor Stinner37296e82010-06-10 13:36:23 +00003473 else if (strcmp(lower, "ascii") == 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003474 return _PyUnicode_AsASCIIString(unicode, errors);
Victor Stinner37296e82010-06-10 13:36:23 +00003475 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00003476
3477 /* Encode via the codec registry */
3478 v = PyCodec_Encode(unicode, encoding, errors);
3479 if (v == NULL)
Amaury Forgeot d'Arcf0481112008-09-05 20:48:47 +00003480 return NULL;
3481
3482 /* The normal path */
3483 if (PyBytes_Check(v))
3484 return v;
3485
3486 /* If the codec returns a buffer, raise a warning and convert to bytes */
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003487 if (PyByteArray_Check(v)) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +00003488 int error;
Amaury Forgeot d'Arcf0481112008-09-05 20:48:47 +00003489 PyObject *b;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00003490
3491 error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
3492 "encoder %s returned bytearray instead of bytes",
3493 encoding);
3494 if (error) {
Amaury Forgeot d'Arcf0481112008-09-05 20:48:47 +00003495 Py_DECREF(v);
3496 return NULL;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003497 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003498
Amaury Forgeot d'Arcf0481112008-09-05 20:48:47 +00003499 b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v));
3500 Py_DECREF(v);
3501 return b;
3502 }
3503
3504 PyErr_Format(PyExc_TypeError,
3505 "encoder did not return a bytes object (type=%.400s)",
3506 Py_TYPE(v)->tp_name);
3507 Py_DECREF(v);
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003508 return NULL;
3509}
3510
Alexander Belopolsky40018472011-02-26 01:02:56 +00003511PyObject *
3512PyUnicode_AsEncodedUnicode(PyObject *unicode,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03003513 const char *encoding,
3514 const char *errors)
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003515{
3516 PyObject *v;
3517
3518 if (!PyUnicode_Check(unicode)) {
3519 PyErr_BadArgument();
3520 goto onError;
3521 }
3522
3523 if (encoding == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00003524 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003525
3526 /* Encode via the codec registry */
3527 v = PyCodec_Encode(unicode, encoding, errors);
3528 if (v == NULL)
3529 goto onError;
3530 if (!PyUnicode_Check(v)) {
3531 PyErr_Format(PyExc_TypeError,
Benjamin Peterson142957c2008-07-04 19:55:29 +00003532 "encoder did not return an str object (type=%.400s)",
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00003533 Py_TYPE(v)->tp_name);
3534 Py_DECREF(v);
3535 goto onError;
3536 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00003537 return v;
Tim Petersced69f82003-09-16 20:30:58 +00003538
Benjamin Peterson29060642009-01-31 22:14:21 +00003539 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00003540 return NULL;
3541}
3542
Victor Stinner2f197072011-12-17 07:08:30 +01003543static size_t
3544mbstowcs_errorpos(const char *str, size_t len)
3545{
3546#ifdef HAVE_MBRTOWC
3547 const char *start = str;
3548 mbstate_t mbs;
3549 size_t converted;
3550 wchar_t ch;
3551
3552 memset(&mbs, 0, sizeof mbs);
3553 while (len)
3554 {
3555 converted = mbrtowc(&ch, (char*)str, len, &mbs);
3556 if (converted == 0)
3557 /* Reached end of string */
3558 break;
3559 if (converted == (size_t)-1 || converted == (size_t)-2) {
3560 /* Conversion error or incomplete character */
3561 return str - start;
3562 }
3563 else {
3564 str += converted;
3565 len -= converted;
3566 }
3567 }
3568 /* failed to find the undecodable byte sequence */
3569 return 0;
3570#endif
3571 return 0;
3572}
3573
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003574PyObject*
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003575PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len,
Victor Stinner1b579672011-12-17 05:47:23 +01003576 const char *errors)
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003577{
3578 wchar_t smallbuf[256];
3579 size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf);
3580 wchar_t *wstr;
3581 size_t wlen, wlen2;
3582 PyObject *unicode;
Victor Stinner1b579672011-12-17 05:47:23 +01003583 int surrogateescape;
Victor Stinner2f197072011-12-17 07:08:30 +01003584 size_t error_pos;
3585 char *errmsg;
3586 PyObject *reason, *exc;
Victor Stinner1b579672011-12-17 05:47:23 +01003587
3588 if (locale_error_handler(errors, &surrogateescape) < 0)
3589 return NULL;
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003590
3591 if (str[len] != '\0' || len != strlen(str)) {
3592 PyErr_SetString(PyExc_TypeError, "embedded null character");
3593 return NULL;
3594 }
3595
3596 if (surrogateescape)
3597 {
3598 wstr = _Py_char2wchar(str, &wlen);
3599 if (wstr == NULL) {
3600 if (wlen == (size_t)-1)
3601 PyErr_NoMemory();
3602 else
3603 PyErr_SetFromErrno(PyExc_OSError);
3604 return NULL;
3605 }
3606
3607 unicode = PyUnicode_FromWideChar(wstr, wlen);
3608 PyMem_Free(wstr);
3609 }
3610 else {
3611#ifndef HAVE_BROKEN_MBSTOWCS
3612 wlen = mbstowcs(NULL, str, 0);
3613#else
3614 wlen = len;
3615#endif
Victor Stinner2f197072011-12-17 07:08:30 +01003616 if (wlen == (size_t)-1)
3617 goto decode_error;
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003618 if (wlen+1 <= smallbuf_len) {
3619 wstr = smallbuf;
3620 }
3621 else {
3622 if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1)
3623 return PyErr_NoMemory();
3624
3625 wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t));
3626 if (!wstr)
3627 return PyErr_NoMemory();
3628 }
3629
3630 /* This shouldn't fail now */
3631 wlen2 = mbstowcs(wstr, str, wlen+1);
3632 if (wlen2 == (size_t)-1) {
3633 if (wstr != smallbuf)
3634 PyMem_Free(wstr);
Victor Stinner2f197072011-12-17 07:08:30 +01003635 goto decode_error;
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003636 }
3637#ifdef HAVE_BROKEN_MBSTOWCS
3638 assert(wlen2 == wlen);
3639#endif
3640 unicode = PyUnicode_FromWideChar(wstr, wlen2);
3641 if (wstr != smallbuf)
3642 PyMem_Free(wstr);
3643 }
3644 return unicode;
Victor Stinner2f197072011-12-17 07:08:30 +01003645
3646decode_error:
3647 errmsg = strerror(errno);
3648 assert(errmsg != NULL);
3649
3650 error_pos = mbstowcs_errorpos(str, len);
3651 if (errmsg != NULL) {
3652 size_t errlen;
3653 wstr = _Py_char2wchar(errmsg, &errlen);
3654 if (wstr != NULL) {
3655 reason = PyUnicode_FromWideChar(wstr, errlen);
3656 PyMem_Free(wstr);
3657 } else
3658 errmsg = NULL;
3659 }
3660 if (errmsg == NULL)
3661 reason = PyUnicode_FromString(
3662 "mbstowcs() encountered an invalid multibyte sequence");
3663 if (reason == NULL)
3664 return NULL;
3665
3666 exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO",
3667 "locale", str, len,
3668 (Py_ssize_t)error_pos,
3669 (Py_ssize_t)(error_pos+1),
3670 reason);
3671 Py_DECREF(reason);
3672 if (exc != NULL) {
3673 PyCodec_StrictErrors(exc);
3674 Py_XDECREF(exc);
3675 }
3676 return NULL;
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003677}
3678
3679PyObject*
Victor Stinner1b579672011-12-17 05:47:23 +01003680PyUnicode_DecodeLocale(const char *str, const char *errors)
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003681{
3682 Py_ssize_t size = (Py_ssize_t)strlen(str);
Victor Stinner1b579672011-12-17 05:47:23 +01003683 return PyUnicode_DecodeLocaleAndSize(str, size, errors);
Victor Stinneraf02e1c2011-12-16 23:56:01 +01003684}
3685
3686
3687PyObject*
Christian Heimes5894ba72007-11-04 11:43:14 +00003688PyUnicode_DecodeFSDefault(const char *s) {
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003689 Py_ssize_t size = (Py_ssize_t)strlen(s);
Christian Heimes5894ba72007-11-04 11:43:14 +00003690 return PyUnicode_DecodeFSDefaultAndSize(s, size);
3691}
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003692
Christian Heimes5894ba72007-11-04 11:43:14 +00003693PyObject*
3694PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
3695{
Victor Stinner99b95382011-07-04 14:23:54 +02003696#ifdef HAVE_MBCS
Victor Stinnerad158722010-10-27 00:25:46 +00003697 return PyUnicode_DecodeMBCS(s, size, NULL);
3698#elif defined(__APPLE__)
Victor Stinnera1d12bb2011-12-11 21:53:09 +01003699 return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL);
Victor Stinnerad158722010-10-27 00:25:46 +00003700#else
Victor Stinner793b5312011-04-27 00:24:21 +02003701 PyInterpreterState *interp = PyThreadState_GET()->interp;
3702 /* Bootstrap check: if the filesystem codec is implemented in Python, we
3703 cannot use it to encode and decode filenames before it is loaded. Load
3704 the Python codec requires to encode at least its own filename. Use the C
3705 version of the locale codec until the codec registry is initialized and
3706 the Python codec is loaded.
3707
3708 Py_FileSystemDefaultEncoding is shared between all interpreters, we
3709 cannot only rely on it: check also interp->fscodec_initialized for
3710 subinterpreters. */
3711 if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) {
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003712 return PyUnicode_Decode(s, size,
3713 Py_FileSystemDefaultEncoding,
Victor Stinnerb9a20ad2010-04-30 16:37:52 +00003714 "surrogateescape");
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003715 }
3716 else {
Victor Stinner1b579672011-12-17 05:47:23 +01003717 return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape");
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003718 }
Victor Stinnerad158722010-10-27 00:25:46 +00003719#endif
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003720}
3721
Martin v. Löwis011e8422009-05-05 04:43:17 +00003722
3723int
Antoine Pitrou13348842012-01-29 18:36:34 +01003724_PyUnicode_HasNULChars(PyObject* s)
3725{
3726 static PyObject *nul = NULL;
3727
3728 if (nul == NULL)
3729 nul = PyUnicode_FromStringAndSize("\0", 1);
3730 if (nul == NULL)
3731 return -1;
3732 return PyUnicode_Contains(s, nul);
3733}
3734
3735
3736int
Martin v. Löwis011e8422009-05-05 04:43:17 +00003737PyUnicode_FSConverter(PyObject* arg, void* addr)
3738{
3739 PyObject *output = NULL;
3740 Py_ssize_t size;
3741 void *data;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00003742 if (arg == NULL) {
3743 Py_DECREF(*(PyObject**)addr);
3744 return 1;
3745 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00003746 if (PyBytes_Check(arg)) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003747 output = arg;
3748 Py_INCREF(output);
3749 }
3750 else {
3751 arg = PyUnicode_FromObject(arg);
3752 if (!arg)
3753 return 0;
Victor Stinnerae6265f2010-05-15 16:27:27 +00003754 output = PyUnicode_EncodeFSDefault(arg);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003755 Py_DECREF(arg);
3756 if (!output)
3757 return 0;
3758 if (!PyBytes_Check(output)) {
3759 Py_DECREF(output);
3760 PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes");
3761 return 0;
3762 }
3763 }
Victor Stinner0ea2a462010-04-30 00:22:08 +00003764 size = PyBytes_GET_SIZE(output);
3765 data = PyBytes_AS_STRING(output);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003766 if (size != strlen(data)) {
Benjamin Peterson7a6b44a2011-08-18 13:51:47 -05003767 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003768 Py_DECREF(output);
3769 return 0;
3770 }
3771 *(PyObject**)addr = output;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00003772 return Py_CLEANUP_SUPPORTED;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003773}
3774
3775
Victor Stinner47fcb5b2010-08-13 23:59:58 +00003776int
3777PyUnicode_FSDecoder(PyObject* arg, void* addr)
3778{
3779 PyObject *output = NULL;
Victor Stinner47fcb5b2010-08-13 23:59:58 +00003780 if (arg == NULL) {
3781 Py_DECREF(*(PyObject**)addr);
3782 return 1;
3783 }
3784 if (PyUnicode_Check(arg)) {
Benjamin Petersonbac79492012-01-14 13:34:47 -05003785 if (PyUnicode_READY(arg) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003786 return 0;
Victor Stinner47fcb5b2010-08-13 23:59:58 +00003787 output = arg;
3788 Py_INCREF(output);
3789 }
3790 else {
3791 arg = PyBytes_FromObject(arg);
3792 if (!arg)
3793 return 0;
3794 output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg),
3795 PyBytes_GET_SIZE(arg));
3796 Py_DECREF(arg);
3797 if (!output)
3798 return 0;
3799 if (!PyUnicode_Check(output)) {
3800 Py_DECREF(output);
3801 PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode");
3802 return 0;
3803 }
3804 }
Benjamin Petersonbac79492012-01-14 13:34:47 -05003805 if (PyUnicode_READY(output) == -1) {
Victor Stinner065836e2011-10-27 01:56:33 +02003806 Py_DECREF(output);
3807 return 0;
3808 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003809 if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
Antoine Pitrouf0b934b2011-10-13 18:55:09 +02003810 PyUnicode_GET_LENGTH(output), 0, 1) >= 0) {
Victor Stinner47fcb5b2010-08-13 23:59:58 +00003811 PyErr_SetString(PyExc_TypeError, "embedded NUL character");
3812 Py_DECREF(output);
3813 return 0;
3814 }
3815 *(PyObject**)addr = output;
3816 return Py_CLEANUP_SUPPORTED;
3817}
3818
3819
Martin v. Löwis5b222132007-06-10 09:51:05 +00003820char*
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003821PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
Martin v. Löwis5b222132007-06-10 09:51:05 +00003822{
Christian Heimesf3863112007-11-22 07:46:41 +00003823 PyObject *bytes;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003824
Neal Norwitze0a0a6e2007-08-25 01:04:21 +00003825 if (!PyUnicode_Check(unicode)) {
3826 PyErr_BadArgument();
3827 return NULL;
3828 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003829 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis5b222132007-06-10 09:51:05 +00003830 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003831
Victor Stinnere90fe6a2011-10-01 16:48:13 +02003832 if (PyUnicode_UTF8(unicode) == NULL) {
3833 assert(!PyUnicode_IS_COMPACT_ASCII(unicode));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003834 bytes = _PyUnicode_AsUTF8String(unicode, "strict");
3835 if (bytes == NULL)
3836 return NULL;
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003837 _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1);
3838 if (_PyUnicode_UTF8(unicode) == NULL) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003839 Py_DECREF(bytes);
3840 return NULL;
3841 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003842 _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes);
3843 Py_MEMCPY(_PyUnicode_UTF8(unicode),
3844 PyBytes_AS_STRING(bytes),
3845 _PyUnicode_UTF8_LENGTH(unicode) + 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003846 Py_DECREF(bytes);
3847 }
3848
3849 if (psize)
Victor Stinnere90fe6a2011-10-01 16:48:13 +02003850 *psize = PyUnicode_UTF8_LENGTH(unicode);
3851 return PyUnicode_UTF8(unicode);
Guido van Rossum7d1df6c2007-08-29 13:53:23 +00003852}
3853
3854char*
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003855PyUnicode_AsUTF8(PyObject *unicode)
Guido van Rossum7d1df6c2007-08-29 13:53:23 +00003856{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003857 return PyUnicode_AsUTF8AndSize(unicode, NULL);
3858}
3859
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003860Py_UNICODE *
3861PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)
3862{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003863 const unsigned char *one_byte;
3864#if SIZEOF_WCHAR_T == 4
3865 const Py_UCS2 *two_bytes;
3866#else
3867 const Py_UCS4 *four_bytes;
3868 const Py_UCS4 *ucs4_end;
3869 Py_ssize_t num_surrogates;
3870#endif
3871 wchar_t *w;
3872 wchar_t *wchar_end;
3873
3874 if (!PyUnicode_Check(unicode)) {
3875 PyErr_BadArgument();
3876 return NULL;
3877 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003878 if (_PyUnicode_WSTR(unicode) == NULL) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003879 /* Non-ASCII compact unicode object */
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003880 assert(_PyUnicode_KIND(unicode) != 0);
3881 assert(PyUnicode_IS_READY(unicode));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003882
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003883 if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003884#if SIZEOF_WCHAR_T == 2
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003885 four_bytes = PyUnicode_4BYTE_DATA(unicode);
3886 ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003887 num_surrogates = 0;
3888
3889 for (; four_bytes < ucs4_end; ++four_bytes) {
3890 if (*four_bytes > 0xFFFF)
3891 ++num_surrogates;
3892 }
3893
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003894 _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(
3895 sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates));
3896 if (!_PyUnicode_WSTR(unicode)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003897 PyErr_NoMemory();
3898 return NULL;
3899 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003900 _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003901
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003902 w = _PyUnicode_WSTR(unicode);
3903 wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode);
3904 four_bytes = PyUnicode_4BYTE_DATA(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003905 for (; four_bytes < ucs4_end; ++four_bytes, ++w) {
3906 if (*four_bytes > 0xFFFF) {
Victor Stinner8faf8212011-12-08 22:14:11 +01003907 assert(*four_bytes <= MAX_UNICODE);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003908 /* encode surrogate pair in this case */
Victor Stinner551ac952011-11-29 22:58:13 +01003909 *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes);
3910 *w = Py_UNICODE_LOW_SURROGATE(*four_bytes);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003911 }
3912 else
3913 *w = *four_bytes;
3914
3915 if (w > wchar_end) {
3916 assert(0 && "Miscalculated string end");
3917 }
3918 }
3919 *w = 0;
3920#else
3921 /* sizeof(wchar_t) == 4 */
3922 Py_FatalError("Impossible unicode object state, wstr and str "
3923 "should share memory already.");
3924 return NULL;
3925#endif
3926 }
3927 else {
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003928 _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) *
3929 (_PyUnicode_LENGTH(unicode) + 1));
3930 if (!_PyUnicode_WSTR(unicode)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003931 PyErr_NoMemory();
3932 return NULL;
3933 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003934 if (!PyUnicode_IS_COMPACT_ASCII(unicode))
3935 _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode);
3936 w = _PyUnicode_WSTR(unicode);
3937 wchar_end = w + _PyUnicode_LENGTH(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003938
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003939 if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) {
3940 one_byte = PyUnicode_1BYTE_DATA(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003941 for (; w < wchar_end; ++one_byte, ++w)
3942 *w = *one_byte;
3943 /* null-terminate the wstr */
3944 *w = 0;
3945 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003946 else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003947#if SIZEOF_WCHAR_T == 4
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003948 two_bytes = PyUnicode_2BYTE_DATA(unicode);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003949 for (; w < wchar_end; ++two_bytes, ++w)
3950 *w = *two_bytes;
3951 /* null-terminate the wstr */
3952 *w = 0;
3953#else
3954 /* sizeof(wchar_t) == 2 */
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003955 PyObject_FREE(_PyUnicode_WSTR(unicode));
3956 _PyUnicode_WSTR(unicode) = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003957 Py_FatalError("Impossible unicode object state, wstr "
3958 "and str should share memory already.");
3959 return NULL;
3960#endif
3961 }
3962 else {
3963 assert(0 && "This should never happen.");
3964 }
3965 }
3966 }
3967 if (size != NULL)
Victor Stinner9db1a8b2011-10-23 20:04:37 +02003968 *size = PyUnicode_WSTR_LENGTH(unicode);
3969 return _PyUnicode_WSTR(unicode);
Martin v. Löwis5b222132007-06-10 09:51:05 +00003970}
3971
Alexander Belopolsky40018472011-02-26 01:02:56 +00003972Py_UNICODE *
3973PyUnicode_AsUnicode(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00003974{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003975 return PyUnicode_AsUnicodeAndSize(unicode, NULL);
Guido van Rossumd57fd912000-03-10 22:53:23 +00003976}
3977
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003978
Alexander Belopolsky40018472011-02-26 01:02:56 +00003979Py_ssize_t
3980PyUnicode_GetSize(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00003981{
3982 if (!PyUnicode_Check(unicode)) {
3983 PyErr_BadArgument();
3984 goto onError;
3985 }
3986 return PyUnicode_GET_SIZE(unicode);
3987
Benjamin Peterson29060642009-01-31 22:14:21 +00003988 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00003989 return -1;
3990}
3991
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003992Py_ssize_t
3993PyUnicode_GetLength(PyObject *unicode)
3994{
Victor Stinner07621332012-06-16 04:53:46 +02003995 if (!PyUnicode_Check(unicode)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003996 PyErr_BadArgument();
3997 return -1;
3998 }
Victor Stinner07621332012-06-16 04:53:46 +02003999 if (PyUnicode_READY(unicode) == -1)
4000 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004001 return PyUnicode_GET_LENGTH(unicode);
4002}
4003
4004Py_UCS4
4005PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
4006{
Victor Stinner2fe5ced2011-10-02 00:25:40 +02004007 if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) {
4008 PyErr_BadArgument();
4009 return (Py_UCS4)-1;
4010 }
Victor Stinnerc4b49542011-12-11 22:44:26 +01004011 if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
Victor Stinner2fe5ced2011-10-02 00:25:40 +02004012 PyErr_SetString(PyExc_IndexError, "string index out of range");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004013 return (Py_UCS4)-1;
4014 }
4015 return PyUnicode_READ_CHAR(unicode, index);
4016}
4017
4018int
4019PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
4020{
4021 if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
Victor Stinnercd9950f2011-10-02 00:34:53 +02004022 PyErr_BadArgument();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004023 return -1;
4024 }
Victor Stinner488fa492011-12-12 00:01:39 +01004025 assert(PyUnicode_IS_READY(unicode));
Victor Stinnerc4b49542011-12-11 22:44:26 +01004026 if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
Victor Stinnercd9950f2011-10-02 00:34:53 +02004027 PyErr_SetString(PyExc_IndexError, "string index out of range");
4028 return -1;
4029 }
Victor Stinner488fa492011-12-12 00:01:39 +01004030 if (unicode_check_modifiable(unicode))
Victor Stinnercd9950f2011-10-02 00:34:53 +02004031 return -1;
Victor Stinnerc9590ad2012-03-04 01:34:37 +01004032 if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) {
4033 PyErr_SetString(PyExc_ValueError, "character out of range");
4034 return -1;
4035 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004036 PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
4037 index, ch);
4038 return 0;
4039}
4040
Alexander Belopolsky40018472011-02-26 01:02:56 +00004041const char *
4042PyUnicode_GetDefaultEncoding(void)
Fred Drakee4315f52000-05-09 19:53:39 +00004043{
Victor Stinner42cb4622010-09-01 19:39:01 +00004044 return "utf-8";
Fred Drakee4315f52000-05-09 19:53:39 +00004045}
4046
Victor Stinner554f3f02010-06-16 23:33:54 +00004047/* create or adjust a UnicodeDecodeError */
4048static void
4049make_decode_exception(PyObject **exceptionObject,
4050 const char *encoding,
4051 const char *input, Py_ssize_t length,
4052 Py_ssize_t startpos, Py_ssize_t endpos,
4053 const char *reason)
4054{
4055 if (*exceptionObject == NULL) {
4056 *exceptionObject = PyUnicodeDecodeError_Create(
4057 encoding, input, length, startpos, endpos, reason);
4058 }
4059 else {
4060 if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos))
4061 goto onError;
4062 if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos))
4063 goto onError;
4064 if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason))
4065 goto onError;
4066 }
4067 return;
4068
4069onError:
4070 Py_DECREF(*exceptionObject);
4071 *exceptionObject = NULL;
4072}
4073
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004074/* error handling callback helper:
4075 build arguments, call the callback and check the arguments,
Fred Drakedb390c12005-10-28 14:39:47 +00004076 if no exception occurred, copy the replacement to the output
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004077 and adjust various state variables.
4078 return 0 on success, -1 on error
4079*/
4080
Alexander Belopolsky40018472011-02-26 01:02:56 +00004081static int
4082unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03004083 const char *encoding, const char *reason,
4084 const char **input, const char **inend, Py_ssize_t *startinpos,
4085 Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004086 PyObject **output, Py_ssize_t *outpos)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004087{
Benjamin Peterson142957c2008-07-04 19:55:29 +00004088 static char *argparse = "O!n;decoding error handler must return (str, int) tuple";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004089
4090 PyObject *restuple = NULL;
4091 PyObject *repunicode = NULL;
Victor Stinner596a6c42011-11-09 00:02:18 +01004092 Py_ssize_t outsize;
Walter Dörwalde78178e2007-07-30 13:31:40 +00004093 Py_ssize_t insize;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004094 Py_ssize_t requiredsize;
4095 Py_ssize_t newpos;
Walter Dörwalde78178e2007-07-30 13:31:40 +00004096 PyObject *inputobj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004097 int res = -1;
4098
Victor Stinner596a6c42011-11-09 00:02:18 +01004099 if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND)
4100 outsize = PyUnicode_GET_LENGTH(*output);
4101 else
4102 outsize = _PyUnicode_WSTR_LENGTH(*output);
4103
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004104 if (*errorHandler == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00004105 *errorHandler = PyCodec_LookupError(errors);
4106 if (*errorHandler == NULL)
4107 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004108 }
4109
Victor Stinner554f3f02010-06-16 23:33:54 +00004110 make_decode_exception(exceptionObject,
4111 encoding,
4112 *input, *inend - *input,
4113 *startinpos, *endinpos,
4114 reason);
4115 if (*exceptionObject == NULL)
4116 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004117
4118 restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL);
4119 if (restuple == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00004120 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004121 if (!PyTuple_Check(restuple)) {
Benjamin Petersond75fcb42009-02-19 04:22:03 +00004122 PyErr_SetString(PyExc_TypeError, &argparse[4]);
Benjamin Peterson29060642009-01-31 22:14:21 +00004123 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004124 }
4125 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00004126 goto onError;
Benjamin Petersonbac79492012-01-14 13:34:47 -05004127 if (PyUnicode_READY(repunicode) == -1)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004128 goto onError;
Walter Dörwalde78178e2007-07-30 13:31:40 +00004129
4130 /* Copy back the bytes variables, which might have been modified by the
4131 callback */
4132 inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject);
4133 if (!inputobj)
4134 goto onError;
Christian Heimes72b710a2008-05-26 13:28:38 +00004135 if (!PyBytes_Check(inputobj)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00004136 PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes");
Walter Dörwalde78178e2007-07-30 13:31:40 +00004137 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004138 *input = PyBytes_AS_STRING(inputobj);
4139 insize = PyBytes_GET_SIZE(inputobj);
Walter Dörwalde78178e2007-07-30 13:31:40 +00004140 *inend = *input + insize;
Walter Dörwald36f938f2007-08-10 10:11:43 +00004141 /* we can DECREF safely, as the exception has another reference,
4142 so the object won't go away. */
4143 Py_DECREF(inputobj);
Walter Dörwalde78178e2007-07-30 13:31:40 +00004144
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004145 if (newpos<0)
Benjamin Peterson29060642009-01-31 22:14:21 +00004146 newpos = insize+newpos;
Walter Dörwald2e0b18a2003-01-31 17:19:08 +00004147 if (newpos<0 || newpos>insize) {
Benjamin Peterson29060642009-01-31 22:14:21 +00004148 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
4149 goto onError;
Walter Dörwald2e0b18a2003-01-31 17:19:08 +00004150 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004151
Victor Stinner596a6c42011-11-09 00:02:18 +01004152 if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) {
4153 /* need more space? (at least enough for what we
4154 have+the replacement+the rest of the string (starting
4155 at the new input position), so we won't have to check space
4156 when there are no errors in the rest of the string) */
4157 Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode);
4158 requiredsize = *outpos + replen + insize-newpos;
4159 if (requiredsize > outsize) {
4160 if (requiredsize<2*outsize)
4161 requiredsize = 2*outsize;
4162 if (unicode_resize(output, requiredsize) < 0)
4163 goto onError;
4164 }
Victor Stinner1b487b42012-05-03 12:29:04 +02004165 if (unicode_widen(output, *outpos,
4166 PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00004167 goto onError;
Victor Stinnerd3f08822012-05-29 12:57:52 +02004168 _PyUnicode_FastCopyCharacters(*output, *outpos, repunicode, 0, replen);
Victor Stinner596a6c42011-11-09 00:02:18 +01004169 *outpos += replen;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004170 }
Victor Stinner596a6c42011-11-09 00:02:18 +01004171 else {
4172 wchar_t *repwstr;
4173 Py_ssize_t repwlen;
4174 repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen);
4175 if (repwstr == NULL)
4176 goto onError;
4177 /* need more space? (at least enough for what we
4178 have+the replacement+the rest of the string (starting
4179 at the new input position), so we won't have to check space
4180 when there are no errors in the rest of the string) */
4181 requiredsize = *outpos + repwlen + insize-newpos;
4182 if (requiredsize > outsize) {
4183 if (requiredsize < 2*outsize)
4184 requiredsize = 2*outsize;
4185 if (unicode_resize(output, requiredsize) < 0)
4186 goto onError;
4187 }
4188 wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen);
4189 *outpos += repwlen;
4190 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004191 *endinpos = newpos;
Walter Dörwalde78178e2007-07-30 13:31:40 +00004192 *inptr = *input + newpos;
Walter Dörwalde78178e2007-07-30 13:31:40 +00004193
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004194 /* we made it! */
4195 res = 0;
4196
Benjamin Peterson29060642009-01-31 22:14:21 +00004197 onError:
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004198 Py_XDECREF(restuple);
4199 return res;
4200}
4201
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004202/* --- UTF-7 Codec -------------------------------------------------------- */
4203
Antoine Pitrou244651a2009-05-04 18:56:13 +00004204/* See RFC2152 for details. We encode conservatively and decode liberally. */
4205
4206/* Three simple macros defining base-64. */
4207
4208/* Is c a base-64 character? */
4209
4210#define IS_BASE64(c) \
4211 (((c) >= 'A' && (c) <= 'Z') || \
4212 ((c) >= 'a' && (c) <= 'z') || \
4213 ((c) >= '0' && (c) <= '9') || \
4214 (c) == '+' || (c) == '/')
4215
4216/* given that c is a base-64 character, what is its base-64 value? */
4217
4218#define FROM_BASE64(c) \
4219 (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \
4220 ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \
4221 ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \
4222 (c) == '+' ? 62 : 63)
4223
4224/* What is the base-64 character of the bottom 6 bits of n? */
4225
4226#define TO_BASE64(n) \
4227 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f])
4228
4229/* DECODE_DIRECT: this byte encountered in a UTF-7 string should be
4230 * decoded as itself. We are permissive on decoding; the only ASCII
4231 * byte not decoding to itself is the + which begins a base64
4232 * string. */
4233
4234#define DECODE_DIRECT(c) \
4235 ((c) <= 127 && (c) != '+')
4236
4237/* The UTF-7 encoder treats ASCII characters differently according to
4238 * whether they are Set D, Set O, Whitespace, or special (i.e. none of
4239 * the above). See RFC2152. This array identifies these different
4240 * sets:
4241 * 0 : "Set D"
4242 * alphanumeric and '(),-./:?
4243 * 1 : "Set O"
4244 * !"#$%&*;<=>@[]^_`{|}
4245 * 2 : "whitespace"
4246 * ht nl cr sp
4247 * 3 : special (must be base64 encoded)
4248 * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127)
4249 */
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004250
Tim Petersced69f82003-09-16 20:30:58 +00004251static
Antoine Pitrou244651a2009-05-04 18:56:13 +00004252char utf7_category[128] = {
4253/* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
4254 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3,
4255/* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
4256 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4257/* sp ! " # $ % & ' ( ) * + , - . / */
4258 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0,
4259/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
4260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
4261/* @ A B C D E F G H I J K L M N O */
4262 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4263/* P Q R S T U V W X Y Z [ \ ] ^ _ */
4264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1,
4265/* ` a b c d e f g h i j k l m n o */
4266 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4267/* p q r s t u v w x y z { | } ~ del */
4268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3,
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004269};
4270
Antoine Pitrou244651a2009-05-04 18:56:13 +00004271/* ENCODE_DIRECT: this character should be encoded as itself. The
4272 * answer depends on whether we are encoding set O as itself, and also
4273 * on whether we are encoding whitespace as itself. RFC2152 makes it
4274 * clear that the answers to these questions vary between
4275 * applications, so this code needs to be flexible. */
Marc-André Lemburge115ec82005-10-19 22:33:31 +00004276
Antoine Pitrou244651a2009-05-04 18:56:13 +00004277#define ENCODE_DIRECT(c, directO, directWS) \
4278 ((c) < 128 && (c) > 0 && \
4279 ((utf7_category[(c)] == 0) || \
4280 (directWS && (utf7_category[(c)] == 2)) || \
4281 (directO && (utf7_category[(c)] == 1))))
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004282
Alexander Belopolsky40018472011-02-26 01:02:56 +00004283PyObject *
4284PyUnicode_DecodeUTF7(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03004285 Py_ssize_t size,
4286 const char *errors)
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004287{
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004288 return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL);
4289}
4290
Antoine Pitrou244651a2009-05-04 18:56:13 +00004291/* The decoder. The only state we preserve is our read position,
4292 * i.e. how many characters we have consumed. So if we end in the
4293 * middle of a shift sequence we have to back off the read position
4294 * and the output to the beginning of the sequence, otherwise we lose
4295 * all the shift state (seen bits, number of bits seen, high
4296 * surrogate). */
4297
Alexander Belopolsky40018472011-02-26 01:02:56 +00004298PyObject *
4299PyUnicode_DecodeUTF7Stateful(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03004300 Py_ssize_t size,
4301 const char *errors,
4302 Py_ssize_t *consumed)
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004303{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004304 const char *starts = s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004305 Py_ssize_t startinpos;
4306 Py_ssize_t endinpos;
4307 Py_ssize_t outpos;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004308 const char *e;
Victor Stinner7931d9a2011-11-04 00:22:48 +01004309 PyObject *unicode;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004310 const char *errmsg = "";
4311 int inShift = 0;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004312 Py_ssize_t shiftOutStart;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004313 unsigned int base64bits = 0;
4314 unsigned long base64buffer = 0;
Victor Stinner24729f32011-11-10 20:31:37 +01004315 Py_UCS4 surrogate = 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004316 PyObject *errorHandler = NULL;
4317 PyObject *exc = NULL;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004318
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004319 /* Start off assuming it's all ASCII. Widen later as necessary. */
4320 unicode = PyUnicode_New(size, 127);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004321 if (!unicode)
4322 return NULL;
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004323 if (size == 0) {
4324 if (consumed)
4325 *consumed = 0;
Victor Stinner7931d9a2011-11-04 00:22:48 +01004326 return unicode;
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004327 }
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004328
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004329 shiftOutStart = outpos = 0;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004330 e = s + size;
4331
4332 while (s < e) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004333 Py_UCS4 ch;
Benjamin Peterson29060642009-01-31 22:14:21 +00004334 restart:
Antoine Pitrou5ffd9e92008-07-25 18:05:24 +00004335 ch = (unsigned char) *s;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004336
Antoine Pitrou244651a2009-05-04 18:56:13 +00004337 if (inShift) { /* in a base-64 section */
4338 if (IS_BASE64(ch)) { /* consume a base-64 character */
4339 base64buffer = (base64buffer << 6) | FROM_BASE64(ch);
4340 base64bits += 6;
4341 s++;
4342 if (base64bits >= 16) {
4343 /* we have enough bits for a UTF-16 value */
Victor Stinner24729f32011-11-10 20:31:37 +01004344 Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16));
Antoine Pitrou244651a2009-05-04 18:56:13 +00004345 base64bits -= 16;
4346 base64buffer &= (1 << base64bits) - 1; /* clear high bits */
4347 if (surrogate) {
4348 /* expecting a second surrogate */
Victor Stinner551ac952011-11-29 22:58:13 +01004349 if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) {
4350 Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh);
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004351 if (unicode_putchar(&unicode, &outpos, ch2) < 0)
4352 goto onError;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004353 surrogate = 0;
Antoine Pitrou5418ee02011-11-15 01:42:21 +01004354 continue;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004355 }
4356 else {
Antoine Pitrou78edf752011-11-15 01:44:16 +01004357 if (unicode_putchar(&unicode, &outpos, surrogate) < 0)
4358 goto onError;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004359 surrogate = 0;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004360 }
4361 }
Victor Stinner551ac952011-11-29 22:58:13 +01004362 if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) {
Antoine Pitrou244651a2009-05-04 18:56:13 +00004363 /* first surrogate */
4364 surrogate = outCh;
4365 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004366 else {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004367 if (unicode_putchar(&unicode, &outpos, outCh) < 0)
4368 goto onError;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004369 }
4370 }
4371 }
4372 else { /* now leaving a base-64 section */
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004373 inShift = 0;
4374 s++;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004375 if (surrogate) {
Antoine Pitrou78edf752011-11-15 01:44:16 +01004376 if (unicode_putchar(&unicode, &outpos, surrogate) < 0)
4377 goto onError;
Antoine Pitrou5418ee02011-11-15 01:42:21 +01004378 surrogate = 0;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004379 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004380 if (base64bits > 0) { /* left-over bits */
4381 if (base64bits >= 6) {
4382 /* We've seen at least one base-64 character */
4383 errmsg = "partial character in shift sequence";
4384 goto utf7Error;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004385 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004386 else {
4387 /* Some bits remain; they should be zero */
4388 if (base64buffer != 0) {
4389 errmsg = "non-zero padding bits in shift sequence";
4390 goto utf7Error;
4391 }
4392 }
4393 }
4394 if (ch != '-') {
4395 /* '-' is absorbed; other terminating
4396 characters are preserved */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004397 if (unicode_putchar(&unicode, &outpos, ch) < 0)
4398 goto onError;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004399 }
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004400 }
4401 }
4402 else if ( ch == '+' ) {
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004403 startinpos = s-starts;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004404 s++; /* consume '+' */
4405 if (s < e && *s == '-') { /* '+-' encodes '+' */
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004406 s++;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004407 if (unicode_putchar(&unicode, &outpos, '+') < 0)
4408 goto onError;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004409 }
4410 else { /* begin base64-encoded section */
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004411 inShift = 1;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004412 shiftOutStart = outpos;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004413 base64bits = 0;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004414 }
4415 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004416 else if (DECODE_DIRECT(ch)) { /* character decodes as itself */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004417 if (unicode_putchar(&unicode, &outpos, ch) < 0)
4418 goto onError;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004419 s++;
4420 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004421 else {
4422 startinpos = s-starts;
4423 s++;
4424 errmsg = "unexpected special character";
4425 goto utf7Error;
4426 }
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004427 continue;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004428utf7Error:
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004429 endinpos = s-starts;
4430 if (unicode_decode_call_errorhandler(
Benjamin Peterson29060642009-01-31 22:14:21 +00004431 errors, &errorHandler,
4432 "utf7", errmsg,
4433 &starts, &e, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004434 &unicode, &outpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00004435 goto onError;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004436 }
4437
Antoine Pitrou244651a2009-05-04 18:56:13 +00004438 /* end of string */
4439
4440 if (inShift && !consumed) { /* in shift sequence, no more to follow */
4441 /* if we're in an inconsistent state, that's an error */
4442 if (surrogate ||
4443 (base64bits >= 6) ||
4444 (base64bits > 0 && base64buffer != 0)) {
Antoine Pitrou244651a2009-05-04 18:56:13 +00004445 endinpos = size;
4446 if (unicode_decode_call_errorhandler(
4447 errors, &errorHandler,
4448 "utf7", "unterminated shift sequence",
4449 &starts, &e, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004450 &unicode, &outpos))
Antoine Pitrou244651a2009-05-04 18:56:13 +00004451 goto onError;
4452 if (s < e)
4453 goto restart;
4454 }
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004455 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004456
4457 /* return state */
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004458 if (consumed) {
Antoine Pitrou244651a2009-05-04 18:56:13 +00004459 if (inShift) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004460 outpos = shiftOutStart; /* back off output */
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004461 *consumed = startinpos;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004462 }
4463 else {
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004464 *consumed = s-starts;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004465 }
Christian Heimes5d14c2b2007-11-20 23:38:09 +00004466 }
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004467
Martin v. Löwise9b11c12011-11-08 17:35:34 +01004468 if (unicode_resize(&unicode, outpos) < 0)
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004469 goto onError;
4470
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004471 Py_XDECREF(errorHandler);
4472 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01004473 return unicode_result(unicode);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004474
Benjamin Peterson29060642009-01-31 22:14:21 +00004475 onError:
Walter Dörwald3aeb6322002-09-02 13:14:32 +00004476 Py_XDECREF(errorHandler);
4477 Py_XDECREF(exc);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004478 Py_DECREF(unicode);
4479 return NULL;
4480}
4481
4482
Alexander Belopolsky40018472011-02-26 01:02:56 +00004483PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004484_PyUnicode_EncodeUTF7(PyObject *str,
4485 int base64SetO,
4486 int base64WhiteSpace,
4487 const char *errors)
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004488{
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004489 int kind;
4490 void *data;
4491 Py_ssize_t len;
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00004492 PyObject *v;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004493 Py_ssize_t allocated;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004494 int inShift = 0;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004495 Py_ssize_t i;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004496 unsigned int base64bits = 0;
4497 unsigned long base64buffer = 0;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004498 char * out;
4499 char * start;
4500
Benjamin Petersonbac79492012-01-14 13:34:47 -05004501 if (PyUnicode_READY(str) == -1)
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004502 return NULL;
4503 kind = PyUnicode_KIND(str);
4504 data = PyUnicode_DATA(str);
4505 len = PyUnicode_GET_LENGTH(str);
4506
4507 if (len == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00004508 return PyBytes_FromStringAndSize(NULL, 0);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004509
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004510 /* It might be possible to tighten this worst case */
4511 allocated = 8 * len;
4512 if (allocated / 8 != len)
Neal Norwitz3ce5d922008-08-24 07:08:55 +00004513 return PyErr_NoMemory();
4514
Antoine Pitrou244651a2009-05-04 18:56:13 +00004515 v = PyBytes_FromStringAndSize(NULL, allocated);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004516 if (v == NULL)
4517 return NULL;
4518
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00004519 start = out = PyBytes_AS_STRING(v);
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004520 for (i = 0; i < len; ++i) {
Victor Stinner0e368262011-11-10 20:12:49 +01004521 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004522
Antoine Pitrou244651a2009-05-04 18:56:13 +00004523 if (inShift) {
4524 if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {
4525 /* shifting out */
4526 if (base64bits) { /* output remaining bits */
4527 *out++ = TO_BASE64(base64buffer << (6-base64bits));
4528 base64buffer = 0;
4529 base64bits = 0;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004530 }
4531 inShift = 0;
Antoine Pitrou244651a2009-05-04 18:56:13 +00004532 /* Characters not in the BASE64 set implicitly unshift the sequence
4533 so no '-' is required, except if the character is itself a '-' */
4534 if (IS_BASE64(ch) || ch == '-') {
4535 *out++ = '-';
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004536 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004537 *out++ = (char) ch;
4538 }
4539 else {
4540 goto encode_char;
Tim Petersced69f82003-09-16 20:30:58 +00004541 }
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004542 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004543 else { /* not in a shift sequence */
4544 if (ch == '+') {
4545 *out++ = '+';
4546 *out++ = '-';
4547 }
4548 else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {
4549 *out++ = (char) ch;
4550 }
4551 else {
4552 *out++ = '+';
4553 inShift = 1;
4554 goto encode_char;
4555 }
4556 }
4557 continue;
4558encode_char:
Antoine Pitrou244651a2009-05-04 18:56:13 +00004559 if (ch >= 0x10000) {
Victor Stinner8faf8212011-12-08 22:14:11 +01004560 assert(ch <= MAX_UNICODE);
Victor Stinner0d3721d2011-11-22 03:27:53 +01004561
Antoine Pitrou244651a2009-05-04 18:56:13 +00004562 /* code first surrogate */
4563 base64bits += 16;
4564 base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10);
4565 while (base64bits >= 6) {
4566 *out++ = TO_BASE64(base64buffer >> (base64bits-6));
4567 base64bits -= 6;
4568 }
4569 /* prepare second surrogate */
Victor Stinner551ac952011-11-29 22:58:13 +01004570 ch = Py_UNICODE_LOW_SURROGATE(ch);
Antoine Pitrou244651a2009-05-04 18:56:13 +00004571 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004572 base64bits += 16;
4573 base64buffer = (base64buffer << 16) | ch;
4574 while (base64bits >= 6) {
4575 *out++ = TO_BASE64(base64buffer >> (base64bits-6));
4576 base64bits -= 6;
4577 }
Hye-Shik Chang1bc09b72004-01-03 19:35:43 +00004578 }
Antoine Pitrou244651a2009-05-04 18:56:13 +00004579 if (base64bits)
4580 *out++= TO_BASE64(base64buffer << (6-base64bits) );
4581 if (inShift)
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004582 *out++ = '-';
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00004583 if (_PyBytes_Resize(&v, out - start) < 0)
4584 return NULL;
4585 return v;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004586}
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004587PyObject *
4588PyUnicode_EncodeUTF7(const Py_UNICODE *s,
4589 Py_ssize_t size,
4590 int base64SetO,
4591 int base64WhiteSpace,
4592 const char *errors)
4593{
4594 PyObject *result;
4595 PyObject *tmp = PyUnicode_FromUnicode(s, size);
4596 if (tmp == NULL)
4597 return NULL;
Victor Stinner0e368262011-11-10 20:12:49 +01004598 result = _PyUnicode_EncodeUTF7(tmp, base64SetO,
Martin v. Löwis1db7c132011-11-10 18:24:32 +01004599 base64WhiteSpace, errors);
4600 Py_DECREF(tmp);
4601 return result;
4602}
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004603
Antoine Pitrou244651a2009-05-04 18:56:13 +00004604#undef IS_BASE64
4605#undef FROM_BASE64
4606#undef TO_BASE64
4607#undef DECODE_DIRECT
4608#undef ENCODE_DIRECT
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +00004609
Guido van Rossumd57fd912000-03-10 22:53:23 +00004610/* --- UTF-8 Codec -------------------------------------------------------- */
4611
Alexander Belopolsky40018472011-02-26 01:02:56 +00004612PyObject *
4613PyUnicode_DecodeUTF8(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03004614 Py_ssize_t size,
4615 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00004616{
Walter Dörwald69652032004-09-07 20:24:22 +00004617 return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);
4618}
4619
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004620#include "stringlib/asciilib.h"
4621#include "stringlib/codecs.h"
4622#include "stringlib/undef.h"
4623
Antoine Pitrou0a3229d2011-11-21 20:39:13 +01004624#include "stringlib/ucs1lib.h"
4625#include "stringlib/codecs.h"
4626#include "stringlib/undef.h"
4627
4628#include "stringlib/ucs2lib.h"
4629#include "stringlib/codecs.h"
4630#include "stringlib/undef.h"
4631
4632#include "stringlib/ucs4lib.h"
4633#include "stringlib/codecs.h"
4634#include "stringlib/undef.h"
4635
Antoine Pitrouab868312009-01-10 15:40:25 +00004636/* Mask to check or force alignment of a pointer to C 'long' boundaries */
4637#define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1)
4638
4639/* Mask to quickly check whether a C 'long' contains a
4640 non-ASCII, UTF8-encoded char. */
4641#if (SIZEOF_LONG == 8)
4642# define ASCII_CHAR_MASK 0x8080808080808080L
4643#elif (SIZEOF_LONG == 4)
4644# define ASCII_CHAR_MASK 0x80808080L
4645#else
4646# error C 'long' size should be either 4 or 8!
4647#endif
4648
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004649static Py_ssize_t
4650ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004651{
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004652 const char *p = start;
4653 const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004654
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004655#if SIZEOF_LONG <= SIZEOF_VOID_P
4656 assert(!((size_t) dest & LONG_PTR_MASK));
4657 if (!((size_t) p & LONG_PTR_MASK)) {
4658 /* Fast path, see in STRINGLIB(utf8_decode) for
4659 an explanation. */
4660 /* Help register allocation */
4661 register const char *_p = p;
4662 register Py_UCS1 * q = dest;
4663 while (_p < aligned_end) {
4664 unsigned long value = *(const unsigned long *) _p;
4665 if (value & ASCII_CHAR_MASK)
Benjamin Peterson29060642009-01-31 22:14:21 +00004666 break;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004667 *((unsigned long *)q) = value;
4668 _p += SIZEOF_LONG;
4669 q += SIZEOF_LONG;
Benjamin Peterson14339b62009-01-31 16:36:08 +00004670 }
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004671 p = _p;
4672 while (p < end) {
4673 if ((unsigned char)*p & 0x80)
4674 break;
4675 *q++ = *p++;
Guido van Rossumd57fd912000-03-10 22:53:23 +00004676 }
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004677 return p - start;
Guido van Rossumd57fd912000-03-10 22:53:23 +00004678 }
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004679#endif
4680 while (p < end) {
4681 /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h
4682 for an explanation. */
4683 if (!((size_t) p & LONG_PTR_MASK)) {
4684 /* Help register allocation */
4685 register const char *_p = p;
4686 while (_p < aligned_end) {
4687 unsigned long value = *(unsigned long *) _p;
4688 if (value & ASCII_CHAR_MASK)
4689 break;
4690 _p += SIZEOF_LONG;
4691 }
4692 p = _p;
4693 if (_p == end)
4694 break;
4695 }
4696 if ((unsigned char)*p & 0x80)
4697 break;
4698 ++p;
4699 }
4700 memcpy(dest, start, p - start);
4701 return p - start;
Guido van Rossumd57fd912000-03-10 22:53:23 +00004702}
Antoine Pitrouab868312009-01-10 15:40:25 +00004703
Victor Stinner785938e2011-12-11 20:09:03 +01004704PyObject *
4705PyUnicode_DecodeUTF8Stateful(const char *s,
4706 Py_ssize_t size,
4707 const char *errors,
4708 Py_ssize_t *consumed)
4709{
Victor Stinner785938e2011-12-11 20:09:03 +01004710 PyObject *unicode;
Victor Stinner785938e2011-12-11 20:09:03 +01004711 const char *starts = s;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004712 const char *end = s + size;
4713 Py_ssize_t outpos;
4714
4715 Py_ssize_t startinpos;
4716 Py_ssize_t endinpos;
4717 const char *errmsg = "";
4718 PyObject *errorHandler = NULL;
4719 PyObject *exc = NULL;
Victor Stinner785938e2011-12-11 20:09:03 +01004720
4721 if (size == 0) {
4722 if (consumed)
4723 *consumed = 0;
Victor Stinner382955f2011-12-11 21:44:00 +01004724 Py_INCREF(unicode_empty);
4725 return unicode_empty;
Victor Stinner785938e2011-12-11 20:09:03 +01004726 }
4727
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004728 /* ASCII is equivalent to the first 128 ordinals in Unicode. */
4729 if (size == 1 && (unsigned char)s[0] < 128) {
Victor Stinner785938e2011-12-11 20:09:03 +01004730 if (consumed)
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004731 *consumed = 1;
4732 return get_latin1_char((unsigned char)s[0]);
Victor Stinner785938e2011-12-11 20:09:03 +01004733 }
4734
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004735 unicode = PyUnicode_New(size, 127);
Victor Stinner785938e2011-12-11 20:09:03 +01004736 if (!unicode)
4737 return NULL;
Victor Stinner785938e2011-12-11 20:09:03 +01004738
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004739 outpos = ascii_decode(s, end, PyUnicode_1BYTE_DATA(unicode));
4740 s += outpos;
4741 while (s < end) {
4742 Py_UCS4 ch;
4743 int kind = PyUnicode_KIND(unicode);
4744 if (kind == PyUnicode_1BYTE_KIND) {
4745 if (PyUnicode_IS_ASCII(unicode))
4746 ch = asciilib_utf8_decode(&s, end,
4747 PyUnicode_1BYTE_DATA(unicode), &outpos);
4748 else
4749 ch = ucs1lib_utf8_decode(&s, end,
4750 PyUnicode_1BYTE_DATA(unicode), &outpos);
4751 } else if (kind == PyUnicode_2BYTE_KIND) {
4752 ch = ucs2lib_utf8_decode(&s, end,
4753 PyUnicode_2BYTE_DATA(unicode), &outpos);
4754 } else {
4755 assert(kind == PyUnicode_4BYTE_KIND);
4756 ch = ucs4lib_utf8_decode(&s, end,
4757 PyUnicode_4BYTE_DATA(unicode), &outpos);
4758 }
4759
4760 switch (ch) {
4761 case 0:
4762 if (s == end || consumed)
4763 goto End;
4764 errmsg = "unexpected end of data";
4765 startinpos = s - starts;
4766 endinpos = startinpos + 1;
4767 while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80)
4768 endinpos++;
4769 break;
4770 case 1:
4771 errmsg = "invalid start byte";
4772 startinpos = s - starts;
4773 endinpos = startinpos + 1;
4774 break;
4775 case 2:
4776 errmsg = "invalid continuation byte";
4777 startinpos = s - starts;
4778 endinpos = startinpos + 1;
4779 while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80)
4780 endinpos++;
4781 break;
4782 default:
4783 if (unicode_putchar(&unicode, &outpos, ch) < 0)
4784 goto onError;
4785 continue;
4786 }
4787
4788 if (unicode_decode_call_errorhandler(
4789 errors, &errorHandler,
4790 "utf-8", errmsg,
4791 &starts, &end, &startinpos, &endinpos, &exc, &s,
4792 &unicode, &outpos))
4793 goto onError;
Victor Stinner785938e2011-12-11 20:09:03 +01004794 }
4795
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004796End:
4797 if (unicode_resize(&unicode, outpos) < 0)
4798 goto onError;
4799
4800 if (consumed)
4801 *consumed = s - starts;
4802
4803 Py_XDECREF(errorHandler);
4804 Py_XDECREF(exc);
4805 assert(_PyUnicode_CheckConsistency(unicode, 1));
4806 return unicode;
4807
4808onError:
4809 Py_XDECREF(errorHandler);
4810 Py_XDECREF(exc);
4811 Py_XDECREF(unicode);
4812 return NULL;
Victor Stinner785938e2011-12-11 20:09:03 +01004813}
4814
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004815#ifdef __APPLE__
4816
4817/* Simplified UTF-8 decoder using surrogateescape error handler,
4818 used to decode the command line arguments on Mac OS X. */
4819
4820wchar_t*
4821_Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size)
4822{
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004823 const char *e;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004824 wchar_t *unicode;
4825 Py_ssize_t outpos;
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004826
4827 /* Note: size will always be longer than the resulting Unicode
4828 character count */
4829 if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) {
4830 PyErr_NoMemory();
4831 return NULL;
4832 }
4833 unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t));
4834 if (!unicode)
4835 return NULL;
4836
4837 /* Unpack UTF-8 encoded data */
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004838 e = s + size;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004839 outpos = 0;
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004840 while (s < e) {
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004841 Py_UCS4 ch;
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004842#if SIZEOF_WCHAR_T == 4
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004843 ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos);
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004844#else
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004845 ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos);
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004846#endif
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004847 if (ch > 0xFF) {
4848#if SIZEOF_WCHAR_T == 4
4849 assert(0);
4850#else
4851 assert(Py_UNICODE_IS_SURROGATE(ch));
4852 /* compute and append the two surrogates: */
4853 unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch);
4854 unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch);
4855#endif
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004856 }
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004857 else {
4858 if (!ch && s == e)
4859 break;
4860 /* surrogateescape */
4861 unicode[outpos++] = 0xDC00 + (unsigned char)*s++;
4862 }
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004863 }
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02004864 unicode[outpos] = L'\0';
Victor Stinnerf933e1a2010-10-20 22:58:25 +00004865 return unicode;
4866}
4867
4868#endif /* __APPLE__ */
Antoine Pitrouab868312009-01-10 15:40:25 +00004869
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004870/* Primary internal function which creates utf8 encoded bytes objects.
4871
4872 Allocation strategy: if the string is short, convert into a stack buffer
Tim Peters602f7402002-04-27 18:03:26 +00004873 and allocate exactly as much space needed at the end. Else allocate the
4874 maximum possible needed (4 result bytes per Unicode character), and return
4875 the excess memory at the end.
Martin v. Löwis2a7ff352002-04-21 09:59:45 +00004876*/
Tim Peters7e3d9612002-04-21 03:26:37 +00004877PyObject *
Victor Stinner7931d9a2011-11-04 00:22:48 +01004878_PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00004879{
Victor Stinner6099a032011-12-18 14:22:26 +01004880 enum PyUnicode_Kind kind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004881 void *data;
4882 Py_ssize_t size;
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +00004883
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004884 if (!PyUnicode_Check(unicode)) {
4885 PyErr_BadArgument();
4886 return NULL;
4887 }
4888
4889 if (PyUnicode_READY(unicode) == -1)
4890 return NULL;
4891
Victor Stinnere90fe6a2011-10-01 16:48:13 +02004892 if (PyUnicode_UTF8(unicode))
4893 return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode),
4894 PyUnicode_UTF8_LENGTH(unicode));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004895
4896 kind = PyUnicode_KIND(unicode);
4897 data = PyUnicode_DATA(unicode);
4898 size = PyUnicode_GET_LENGTH(unicode);
4899
Benjamin Petersonead6b532011-12-20 17:23:42 -06004900 switch (kind) {
Victor Stinner6099a032011-12-18 14:22:26 +01004901 default:
4902 assert(0);
4903 case PyUnicode_1BYTE_KIND:
4904 /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */
4905 assert(!PyUnicode_IS_ASCII(unicode));
4906 return ucs1lib_utf8_encoder(unicode, data, size, errors);
4907 case PyUnicode_2BYTE_KIND:
4908 return ucs2lib_utf8_encoder(unicode, data, size, errors);
4909 case PyUnicode_4BYTE_KIND:
4910 return ucs4lib_utf8_encoder(unicode, data, size, errors);
Tim Peters602f7402002-04-27 18:03:26 +00004911 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00004912}
4913
Alexander Belopolsky40018472011-02-26 01:02:56 +00004914PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004915PyUnicode_EncodeUTF8(const Py_UNICODE *s,
4916 Py_ssize_t size,
4917 const char *errors)
4918{
4919 PyObject *v, *unicode;
4920
4921 unicode = PyUnicode_FromUnicode(s, size);
4922 if (unicode == NULL)
4923 return NULL;
4924 v = _PyUnicode_AsUTF8String(unicode, errors);
4925 Py_DECREF(unicode);
4926 return v;
4927}
4928
4929PyObject *
Alexander Belopolsky40018472011-02-26 01:02:56 +00004930PyUnicode_AsUTF8String(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00004931{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004932 return _PyUnicode_AsUTF8String(unicode, NULL);
Guido van Rossumd57fd912000-03-10 22:53:23 +00004933}
4934
Walter Dörwald41980ca2007-08-16 21:55:45 +00004935/* --- UTF-32 Codec ------------------------------------------------------- */
4936
4937PyObject *
4938PyUnicode_DecodeUTF32(const char *s,
Benjamin Peterson29060642009-01-31 22:14:21 +00004939 Py_ssize_t size,
4940 const char *errors,
4941 int *byteorder)
Walter Dörwald41980ca2007-08-16 21:55:45 +00004942{
4943 return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL);
4944}
4945
4946PyObject *
4947PyUnicode_DecodeUTF32Stateful(const char *s,
Benjamin Peterson29060642009-01-31 22:14:21 +00004948 Py_ssize_t size,
4949 const char *errors,
4950 int *byteorder,
4951 Py_ssize_t *consumed)
Walter Dörwald41980ca2007-08-16 21:55:45 +00004952{
4953 const char *starts = s;
4954 Py_ssize_t startinpos;
4955 Py_ssize_t endinpos;
4956 Py_ssize_t outpos;
Victor Stinner7931d9a2011-11-04 00:22:48 +01004957 PyObject *unicode;
Mark Dickinson7db923c2010-06-12 09:10:14 +00004958 const unsigned char *q, *e;
Walter Dörwald41980ca2007-08-16 21:55:45 +00004959 int bo = 0; /* assume native ordering by default */
4960 const char *errmsg = "";
Walter Dörwald41980ca2007-08-16 21:55:45 +00004961 /* Offsets from q for retrieving bytes in the right order. */
4962#ifdef BYTEORDER_IS_LITTLE_ENDIAN
4963 int iorder[] = {0, 1, 2, 3};
4964#else
4965 int iorder[] = {3, 2, 1, 0};
4966#endif
4967 PyObject *errorHandler = NULL;
4968 PyObject *exc = NULL;
Victor Stinner313a1202010-06-11 23:56:51 +00004969
Walter Dörwald41980ca2007-08-16 21:55:45 +00004970 q = (unsigned char *)s;
4971 e = q + size;
4972
4973 if (byteorder)
4974 bo = *byteorder;
4975
4976 /* Check for BOM marks (U+FEFF) in the input and adjust current
4977 byte order setting accordingly. In native mode, the leading BOM
4978 mark is skipped, in all other modes, it is copied to the output
4979 stream as-is (giving a ZWNBSP character). */
4980 if (bo == 0) {
4981 if (size >= 4) {
4982 const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |
Benjamin Peterson29060642009-01-31 22:14:21 +00004983 (q[iorder[1]] << 8) | q[iorder[0]];
Walter Dörwald41980ca2007-08-16 21:55:45 +00004984#ifdef BYTEORDER_IS_LITTLE_ENDIAN
Benjamin Peterson29060642009-01-31 22:14:21 +00004985 if (bom == 0x0000FEFF) {
4986 q += 4;
4987 bo = -1;
4988 }
4989 else if (bom == 0xFFFE0000) {
4990 q += 4;
4991 bo = 1;
4992 }
Walter Dörwald41980ca2007-08-16 21:55:45 +00004993#else
Benjamin Peterson29060642009-01-31 22:14:21 +00004994 if (bom == 0x0000FEFF) {
4995 q += 4;
4996 bo = 1;
4997 }
4998 else if (bom == 0xFFFE0000) {
4999 q += 4;
5000 bo = -1;
5001 }
Walter Dörwald41980ca2007-08-16 21:55:45 +00005002#endif
Benjamin Peterson29060642009-01-31 22:14:21 +00005003 }
Walter Dörwald41980ca2007-08-16 21:55:45 +00005004 }
5005
5006 if (bo == -1) {
5007 /* force LE */
5008 iorder[0] = 0;
5009 iorder[1] = 1;
5010 iorder[2] = 2;
5011 iorder[3] = 3;
5012 }
5013 else if (bo == 1) {
5014 /* force BE */
5015 iorder[0] = 3;
5016 iorder[1] = 2;
5017 iorder[2] = 1;
5018 iorder[3] = 0;
5019 }
5020
Antoine Pitroucc0cfd32010-06-11 21:46:32 +00005021 /* This might be one to much, because of a BOM */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005022 unicode = PyUnicode_New((size+3)/4, 127);
Antoine Pitroucc0cfd32010-06-11 21:46:32 +00005023 if (!unicode)
5024 return NULL;
5025 if (size == 0)
Victor Stinner7931d9a2011-11-04 00:22:48 +01005026 return unicode;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005027 outpos = 0;
Antoine Pitroucc0cfd32010-06-11 21:46:32 +00005028
Walter Dörwald41980ca2007-08-16 21:55:45 +00005029 while (q < e) {
Benjamin Peterson29060642009-01-31 22:14:21 +00005030 Py_UCS4 ch;
5031 /* remaining bytes at the end? (size should be divisible by 4) */
5032 if (e-q<4) {
5033 if (consumed)
5034 break;
5035 errmsg = "truncated data";
5036 startinpos = ((const char *)q)-starts;
5037 endinpos = ((const char *)e)-starts;
5038 goto utf32Error;
5039 /* The remaining input chars are ignored if the callback
5040 chooses to skip the input */
5041 }
5042 ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |
5043 (q[iorder[1]] << 8) | q[iorder[0]];
Walter Dörwald41980ca2007-08-16 21:55:45 +00005044
Benjamin Peterson29060642009-01-31 22:14:21 +00005045 if (ch >= 0x110000)
5046 {
5047 errmsg = "codepoint not in range(0x110000)";
5048 startinpos = ((const char *)q)-starts;
5049 endinpos = startinpos+4;
5050 goto utf32Error;
5051 }
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005052 if (unicode_putchar(&unicode, &outpos, ch) < 0)
5053 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00005054 q += 4;
5055 continue;
5056 utf32Error:
Benjamin Peterson29060642009-01-31 22:14:21 +00005057 if (unicode_decode_call_errorhandler(
5058 errors, &errorHandler,
5059 "utf32", errmsg,
5060 &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005061 &unicode, &outpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00005062 goto onError;
Walter Dörwald41980ca2007-08-16 21:55:45 +00005063 }
5064
5065 if (byteorder)
5066 *byteorder = bo;
5067
5068 if (consumed)
Benjamin Peterson29060642009-01-31 22:14:21 +00005069 *consumed = (const char *)q-starts;
Walter Dörwald41980ca2007-08-16 21:55:45 +00005070
5071 /* Adjust length */
Victor Stinner16e6a802011-12-12 13:24:15 +01005072 if (unicode_resize(&unicode, outpos) < 0)
Walter Dörwald41980ca2007-08-16 21:55:45 +00005073 goto onError;
5074
5075 Py_XDECREF(errorHandler);
5076 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01005077 return unicode_result(unicode);
Walter Dörwald41980ca2007-08-16 21:55:45 +00005078
Benjamin Peterson29060642009-01-31 22:14:21 +00005079 onError:
Walter Dörwald41980ca2007-08-16 21:55:45 +00005080 Py_DECREF(unicode);
5081 Py_XDECREF(errorHandler);
5082 Py_XDECREF(exc);
5083 return NULL;
5084}
5085
5086PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005087_PyUnicode_EncodeUTF32(PyObject *str,
5088 const char *errors,
5089 int byteorder)
Walter Dörwald41980ca2007-08-16 21:55:45 +00005090{
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005091 int kind;
5092 void *data;
5093 Py_ssize_t len;
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005094 PyObject *v;
Walter Dörwald41980ca2007-08-16 21:55:45 +00005095 unsigned char *p;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005096 Py_ssize_t nsize, bytesize, i;
Walter Dörwald41980ca2007-08-16 21:55:45 +00005097 /* Offsets from p for storing byte pairs in the right order. */
5098#ifdef BYTEORDER_IS_LITTLE_ENDIAN
5099 int iorder[] = {0, 1, 2, 3};
5100#else
5101 int iorder[] = {3, 2, 1, 0};
5102#endif
5103
Benjamin Peterson29060642009-01-31 22:14:21 +00005104#define STORECHAR(CH) \
5105 do { \
5106 p[iorder[3]] = ((CH) >> 24) & 0xff; \
5107 p[iorder[2]] = ((CH) >> 16) & 0xff; \
5108 p[iorder[1]] = ((CH) >> 8) & 0xff; \
5109 p[iorder[0]] = (CH) & 0xff; \
5110 p += 4; \
Walter Dörwald41980ca2007-08-16 21:55:45 +00005111 } while(0)
5112
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005113 if (!PyUnicode_Check(str)) {
5114 PyErr_BadArgument();
5115 return NULL;
5116 }
Benjamin Petersonbac79492012-01-14 13:34:47 -05005117 if (PyUnicode_READY(str) == -1)
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005118 return NULL;
5119 kind = PyUnicode_KIND(str);
5120 data = PyUnicode_DATA(str);
5121 len = PyUnicode_GET_LENGTH(str);
5122
5123 nsize = len + (byteorder == 0);
Neal Norwitz3ce5d922008-08-24 07:08:55 +00005124 bytesize = nsize * 4;
5125 if (bytesize / 4 != nsize)
Benjamin Peterson29060642009-01-31 22:14:21 +00005126 return PyErr_NoMemory();
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005127 v = PyBytes_FromStringAndSize(NULL, bytesize);
Walter Dörwald41980ca2007-08-16 21:55:45 +00005128 if (v == NULL)
5129 return NULL;
5130
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005131 p = (unsigned char *)PyBytes_AS_STRING(v);
Walter Dörwald41980ca2007-08-16 21:55:45 +00005132 if (byteorder == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00005133 STORECHAR(0xFEFF);
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005134 if (len == 0)
Guido van Rossum98297ee2007-11-06 21:34:58 +00005135 goto done;
Walter Dörwald41980ca2007-08-16 21:55:45 +00005136
5137 if (byteorder == -1) {
5138 /* force LE */
5139 iorder[0] = 0;
5140 iorder[1] = 1;
5141 iorder[2] = 2;
5142 iorder[3] = 3;
5143 }
5144 else if (byteorder == 1) {
5145 /* force BE */
5146 iorder[0] = 3;
5147 iorder[1] = 2;
5148 iorder[2] = 1;
5149 iorder[3] = 0;
5150 }
5151
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005152 for (i = 0; i < len; i++)
5153 STORECHAR(PyUnicode_READ(kind, data, i));
Guido van Rossum98297ee2007-11-06 21:34:58 +00005154
5155 done:
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005156 return v;
Walter Dörwald41980ca2007-08-16 21:55:45 +00005157#undef STORECHAR
5158}
5159
Alexander Belopolsky40018472011-02-26 01:02:56 +00005160PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005161PyUnicode_EncodeUTF32(const Py_UNICODE *s,
5162 Py_ssize_t size,
5163 const char *errors,
5164 int byteorder)
5165{
5166 PyObject *result;
5167 PyObject *tmp = PyUnicode_FromUnicode(s, size);
5168 if (tmp == NULL)
5169 return NULL;
5170 result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder);
5171 Py_DECREF(tmp);
5172 return result;
5173}
5174
5175PyObject *
Alexander Belopolsky40018472011-02-26 01:02:56 +00005176PyUnicode_AsUTF32String(PyObject *unicode)
Walter Dörwald41980ca2007-08-16 21:55:45 +00005177{
Victor Stinnerb960b342011-11-20 19:12:52 +01005178 return _PyUnicode_EncodeUTF32(unicode, NULL, 0);
Walter Dörwald41980ca2007-08-16 21:55:45 +00005179}
5180
Guido van Rossumd57fd912000-03-10 22:53:23 +00005181/* --- UTF-16 Codec ------------------------------------------------------- */
5182
Tim Peters772747b2001-08-09 22:21:55 +00005183PyObject *
5184PyUnicode_DecodeUTF16(const char *s,
Benjamin Peterson29060642009-01-31 22:14:21 +00005185 Py_ssize_t size,
5186 const char *errors,
5187 int *byteorder)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005188{
Walter Dörwald69652032004-09-07 20:24:22 +00005189 return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL);
5190}
5191
5192PyObject *
5193PyUnicode_DecodeUTF16Stateful(const char *s,
Benjamin Peterson29060642009-01-31 22:14:21 +00005194 Py_ssize_t size,
5195 const char *errors,
5196 int *byteorder,
5197 Py_ssize_t *consumed)
Walter Dörwald69652032004-09-07 20:24:22 +00005198{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005199 const char *starts = s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005200 Py_ssize_t startinpos;
5201 Py_ssize_t endinpos;
5202 Py_ssize_t outpos;
Victor Stinner7931d9a2011-11-04 00:22:48 +01005203 PyObject *unicode;
Antoine Pitrou63065d72012-05-15 23:48:04 +02005204 const unsigned char *q, *e;
Tim Peters772747b2001-08-09 22:21:55 +00005205 int bo = 0; /* assume native ordering by default */
Antoine Pitrou63065d72012-05-15 23:48:04 +02005206 int native_ordering;
Marc-André Lemburg9542f482000-07-17 18:23:13 +00005207 const char *errmsg = "";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005208 PyObject *errorHandler = NULL;
5209 PyObject *exc = NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005210
Tim Peters772747b2001-08-09 22:21:55 +00005211 q = (unsigned char *)s;
Antoine Pitrou63065d72012-05-15 23:48:04 +02005212 e = q + size;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005213
5214 if (byteorder)
Tim Peters772747b2001-08-09 22:21:55 +00005215 bo = *byteorder;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005216
Marc-André Lemburg489b56e2001-05-21 20:30:15 +00005217 /* Check for BOM marks (U+FEFF) in the input and adjust current
5218 byte order setting accordingly. In native mode, the leading BOM
5219 mark is skipped, in all other modes, it is copied to the output
5220 stream as-is (giving a ZWNBSP character). */
Antoine Pitrou63065d72012-05-15 23:48:04 +02005221 if (bo == 0 && size >= 2) {
5222 const Py_UCS4 bom = (q[1] << 8) | q[0];
5223 if (bom == 0xFEFF) {
5224 q += 2;
5225 bo = -1;
Benjamin Peterson29060642009-01-31 22:14:21 +00005226 }
Antoine Pitrou63065d72012-05-15 23:48:04 +02005227 else if (bom == 0xFFFE) {
5228 q += 2;
5229 bo = 1;
5230 }
5231 if (byteorder)
5232 *byteorder = bo;
Marc-André Lemburg489b56e2001-05-21 20:30:15 +00005233 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00005234
Antoine Pitrou63065d72012-05-15 23:48:04 +02005235 if (q == e) {
5236 if (consumed)
5237 *consumed = size;
5238 Py_INCREF(unicode_empty);
5239 return unicode_empty;
Tim Peters772747b2001-08-09 22:21:55 +00005240 }
Antoine Pitrou63065d72012-05-15 23:48:04 +02005241
Antoine Pitrouab868312009-01-10 15:40:25 +00005242#ifdef BYTEORDER_IS_LITTLE_ENDIAN
Antoine Pitrou63065d72012-05-15 23:48:04 +02005243 native_ordering = bo <= 0;
Antoine Pitrouab868312009-01-10 15:40:25 +00005244#else
Antoine Pitrou63065d72012-05-15 23:48:04 +02005245 native_ordering = bo >= 0;
Antoine Pitrouab868312009-01-10 15:40:25 +00005246#endif
Tim Peters772747b2001-08-09 22:21:55 +00005247
Antoine Pitrou63065d72012-05-15 23:48:04 +02005248 /* Note: size will always be longer than the resulting Unicode
5249 character count */
5250 unicode = PyUnicode_New((e - q + 1) / 2, 127);
5251 if (!unicode)
5252 return NULL;
5253
5254 outpos = 0;
5255 while (1) {
5256 Py_UCS4 ch = 0;
5257 if (e - q >= 2) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005258 int kind = PyUnicode_KIND(unicode);
Antoine Pitrou63065d72012-05-15 23:48:04 +02005259 if (kind == PyUnicode_1BYTE_KIND) {
5260 if (PyUnicode_IS_ASCII(unicode))
5261 ch = asciilib_utf16_decode(&q, e,
5262 PyUnicode_1BYTE_DATA(unicode), &outpos,
5263 native_ordering);
5264 else
5265 ch = ucs1lib_utf16_decode(&q, e,
5266 PyUnicode_1BYTE_DATA(unicode), &outpos,
5267 native_ordering);
5268 } else if (kind == PyUnicode_2BYTE_KIND) {
5269 ch = ucs2lib_utf16_decode(&q, e,
5270 PyUnicode_2BYTE_DATA(unicode), &outpos,
5271 native_ordering);
5272 } else {
5273 assert(kind == PyUnicode_4BYTE_KIND);
5274 ch = ucs4lib_utf16_decode(&q, e,
5275 PyUnicode_4BYTE_DATA(unicode), &outpos,
5276 native_ordering);
Antoine Pitrouab868312009-01-10 15:40:25 +00005277 }
Antoine Pitrouab868312009-01-10 15:40:25 +00005278 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005279
Antoine Pitrou63065d72012-05-15 23:48:04 +02005280 switch (ch)
5281 {
5282 case 0:
5283 /* remaining byte at the end? (size should be even) */
5284 if (q == e || consumed)
5285 goto End;
5286 errmsg = "truncated data";
5287 startinpos = ((const char *)q) - starts;
5288 endinpos = ((const char *)e) - starts;
5289 break;
5290 /* The remaining input chars are ignored if the callback
5291 chooses to skip the input */
5292 case 1:
5293 errmsg = "unexpected end of data";
5294 startinpos = ((const char *)q) - 2 - starts;
5295 endinpos = ((const char *)e) - starts;
5296 break;
5297 case 2:
5298 errmsg = "illegal encoding";
5299 startinpos = ((const char *)q) - 2 - starts;
5300 endinpos = startinpos + 2;
5301 break;
5302 case 3:
5303 errmsg = "illegal UTF-16 surrogate";
5304 startinpos = ((const char *)q) - 4 - starts;
5305 endinpos = startinpos + 2;
5306 break;
5307 default:
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005308 if (unicode_putchar(&unicode, &outpos, ch) < 0)
5309 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00005310 continue;
5311 }
5312
Benjamin Peterson29060642009-01-31 22:14:21 +00005313 if (unicode_decode_call_errorhandler(
Antoine Pitrouab868312009-01-10 15:40:25 +00005314 errors,
5315 &errorHandler,
5316 "utf16", errmsg,
5317 &starts,
5318 (const char **)&e,
5319 &startinpos,
5320 &endinpos,
5321 &exc,
5322 (const char **)&q,
5323 &unicode,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005324 &outpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00005325 goto onError;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005326 }
5327
Antoine Pitrou63065d72012-05-15 23:48:04 +02005328End:
Walter Dörwald69652032004-09-07 20:24:22 +00005329 if (consumed)
Benjamin Peterson29060642009-01-31 22:14:21 +00005330 *consumed = (const char *)q-starts;
Walter Dörwald69652032004-09-07 20:24:22 +00005331
Guido van Rossumd57fd912000-03-10 22:53:23 +00005332 /* Adjust length */
Victor Stinner16e6a802011-12-12 13:24:15 +01005333 if (unicode_resize(&unicode, outpos) < 0)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005334 goto onError;
5335
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005336 Py_XDECREF(errorHandler);
5337 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01005338 return unicode_result(unicode);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005339
Benjamin Peterson29060642009-01-31 22:14:21 +00005340 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00005341 Py_DECREF(unicode);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005342 Py_XDECREF(errorHandler);
5343 Py_XDECREF(exc);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005344 return NULL;
5345}
5346
Tim Peters772747b2001-08-09 22:21:55 +00005347PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005348_PyUnicode_EncodeUTF16(PyObject *str,
5349 const char *errors,
5350 int byteorder)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005351{
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005352 enum PyUnicode_Kind kind;
5353 const void *data;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005354 Py_ssize_t len;
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005355 PyObject *v;
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005356 unsigned short *out;
5357 Py_ssize_t bytesize;
5358 Py_ssize_t pairs;
5359#ifdef WORDS_BIGENDIAN
5360 int native_ordering = byteorder >= 0;
Tim Peters772747b2001-08-09 22:21:55 +00005361#else
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005362 int native_ordering = byteorder <= 0;
Tim Peters772747b2001-08-09 22:21:55 +00005363#endif
5364
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005365 if (!PyUnicode_Check(str)) {
5366 PyErr_BadArgument();
5367 return NULL;
5368 }
Benjamin Petersonbac79492012-01-14 13:34:47 -05005369 if (PyUnicode_READY(str) == -1)
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005370 return NULL;
5371 kind = PyUnicode_KIND(str);
5372 data = PyUnicode_DATA(str);
5373 len = PyUnicode_GET_LENGTH(str);
Victor Stinner0e368262011-11-10 20:12:49 +01005374
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005375 pairs = 0;
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005376 if (kind == PyUnicode_4BYTE_KIND) {
5377 const Py_UCS4 *in = (const Py_UCS4 *)data;
5378 const Py_UCS4 *end = in + len;
5379 while (in < end)
5380 if (*in++ >= 0x10000)
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005381 pairs++;
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005382 }
5383 if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0))
Benjamin Peterson29060642009-01-31 22:14:21 +00005384 return PyErr_NoMemory();
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005385 bytesize = (len + pairs + (byteorder == 0)) * 2;
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005386 v = PyBytes_FromStringAndSize(NULL, bytesize);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005387 if (v == NULL)
5388 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005389
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005390 /* output buffer is 2-bytes aligned */
5391 assert(((Py_uintptr_t)PyBytes_AS_STRING(v) & 1) == 0);
5392 out = (unsigned short *)PyBytes_AS_STRING(v);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005393 if (byteorder == 0)
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005394 *out++ = 0xFEFF;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005395 if (len == 0)
Guido van Rossum98297ee2007-11-06 21:34:58 +00005396 goto done;
Tim Peters772747b2001-08-09 22:21:55 +00005397
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005398 switch (kind) {
5399 case PyUnicode_1BYTE_KIND: {
5400 ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering);
5401 break;
Tim Peters772747b2001-08-09 22:21:55 +00005402 }
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005403 case PyUnicode_2BYTE_KIND: {
5404 ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering);
5405 break;
Tim Peters772747b2001-08-09 22:21:55 +00005406 }
Antoine Pitrou27f6a3b2012-06-15 22:15:23 +02005407 case PyUnicode_4BYTE_KIND: {
5408 ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering);
5409 break;
5410 }
5411 default:
5412 assert(0);
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +00005413 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00005414
5415 done:
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005416 return v;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005417}
5418
Alexander Belopolsky40018472011-02-26 01:02:56 +00005419PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005420PyUnicode_EncodeUTF16(const Py_UNICODE *s,
5421 Py_ssize_t size,
5422 const char *errors,
5423 int byteorder)
5424{
5425 PyObject *result;
5426 PyObject *tmp = PyUnicode_FromUnicode(s, size);
5427 if (tmp == NULL)
5428 return NULL;
5429 result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder);
5430 Py_DECREF(tmp);
5431 return result;
5432}
5433
5434PyObject *
Alexander Belopolsky40018472011-02-26 01:02:56 +00005435PyUnicode_AsUTF16String(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005436{
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005437 return _PyUnicode_EncodeUTF16(unicode, NULL, 0);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005438}
5439
5440/* --- Unicode Escape Codec ----------------------------------------------- */
5441
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005442/* Helper function for PyUnicode_DecodeUnicodeEscape, determines
5443 if all the escapes in the string make it still a valid ASCII string.
5444 Returns -1 if any escapes were found which cause the string to
5445 pop out of ASCII range. Otherwise returns the length of the
5446 required buffer to hold the string.
5447 */
Antoine Pitrou53bb5482011-10-10 23:49:24 +02005448static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005449length_of_escaped_ascii_string(const char *s, Py_ssize_t size)
5450{
5451 const unsigned char *p = (const unsigned char *)s;
5452 const unsigned char *end = p + size;
5453 Py_ssize_t length = 0;
5454
5455 if (size < 0)
5456 return -1;
5457
5458 for (; p < end; ++p) {
5459 if (*p > 127) {
5460 /* Non-ASCII */
5461 return -1;
5462 }
5463 else if (*p != '\\') {
5464 /* Normal character */
5465 ++length;
5466 }
5467 else {
5468 /* Backslash-escape, check next char */
5469 ++p;
5470 /* Escape sequence reaches till end of string or
5471 non-ASCII follow-up. */
5472 if (p >= end || *p > 127)
5473 return -1;
5474 switch (*p) {
5475 case '\n':
5476 /* backslash + \n result in zero characters */
5477 break;
5478 case '\\': case '\'': case '\"':
5479 case 'b': case 'f': case 't':
5480 case 'n': case 'r': case 'v': case 'a':
5481 ++length;
5482 break;
5483 case '0': case '1': case '2': case '3':
5484 case '4': case '5': case '6': case '7':
5485 case 'x': case 'u': case 'U': case 'N':
5486 /* these do not guarantee ASCII characters */
5487 return -1;
5488 default:
5489 /* count the backslash + the other character */
5490 length += 2;
5491 }
5492 }
5493 }
5494 return length;
5495}
5496
Fredrik Lundh06d12682001-01-24 07:59:11 +00005497static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
Marc-André Lemburg0f774e32000-06-28 16:43:35 +00005498
Alexander Belopolsky40018472011-02-26 01:02:56 +00005499PyObject *
5500PyUnicode_DecodeUnicodeEscape(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03005501 Py_ssize_t size,
Victor Stinnerc17f5402011-09-29 00:16:58 +02005502 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005503{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005504 const char *starts = s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005505 Py_ssize_t startinpos;
5506 Py_ssize_t endinpos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005507 int j;
Victor Stinner7931d9a2011-11-04 00:22:48 +01005508 PyObject *v;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005509 const char *end;
Fredrik Lundhccc74732001-02-18 22:13:49 +00005510 char* message;
5511 Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005512 PyObject *errorHandler = NULL;
5513 PyObject *exc = NULL;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005514 Py_ssize_t len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005515 Py_ssize_t i;
Fredrik Lundhccc74732001-02-18 22:13:49 +00005516
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005517 len = length_of_escaped_ascii_string(s, size);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005518
5519 /* After length_of_escaped_ascii_string() there are two alternatives,
5520 either the string is pure ASCII with named escapes like \n, etc.
5521 and we determined it's exact size (common case)
5522 or it contains \x, \u, ... escape sequences. then we create a
5523 legacy wchar string and resize it at the end of this function. */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005524 if (len >= 0) {
5525 v = PyUnicode_New(len, 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005526 if (!v)
5527 goto onError;
5528 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005529 }
5530 else {
5531 /* Escaped strings will always be longer than the resulting
5532 Unicode string, so we start with size here and then reduce the
5533 length after conversion to the true value.
5534 (but if the error callback returns a long replacement string
5535 we'll have to allocate more space) */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005536 v = PyUnicode_New(size, 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005537 if (!v)
5538 goto onError;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005539 len = size;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005540 }
5541
Guido van Rossumd57fd912000-03-10 22:53:23 +00005542 if (size == 0)
Victor Stinner7931d9a2011-11-04 00:22:48 +01005543 return v;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005544 i = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005545 end = s + size;
Fredrik Lundhccc74732001-02-18 22:13:49 +00005546
Guido van Rossumd57fd912000-03-10 22:53:23 +00005547 while (s < end) {
5548 unsigned char c;
Victor Stinner24729f32011-11-10 20:31:37 +01005549 Py_UCS4 x;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005550 int digits;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005551
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005552 /* The only case in which i == ascii_length is a backslash
5553 followed by a newline. */
5554 assert(i <= len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005555
Guido van Rossumd57fd912000-03-10 22:53:23 +00005556 /* Non-escape characters are interpreted as Unicode ordinals */
5557 if (*s != '\\') {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005558 if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0)
5559 goto onError;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005560 continue;
5561 }
5562
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005563 startinpos = s-starts;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005564 /* \ - Escapes */
5565 s++;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00005566 c = *s++;
5567 if (s > end)
5568 c = '\0'; /* Invalid after \ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005569
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005570 /* The only case in which i == ascii_length is a backslash
5571 followed by a newline. */
5572 assert(i < len || (i == len && c == '\n'));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005573
Guido van Rossum8ce8a782007-11-01 19:42:39 +00005574 switch (c) {
Guido van Rossumd57fd912000-03-10 22:53:23 +00005575
Benjamin Peterson29060642009-01-31 22:14:21 +00005576 /* \x escapes */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005577#define WRITECHAR(ch) \
5578 do { \
5579 if (unicode_putchar(&v, &i, ch) < 0) \
5580 goto onError; \
5581 }while(0)
5582
Guido van Rossumd57fd912000-03-10 22:53:23 +00005583 case '\n': break;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005584 case '\\': WRITECHAR('\\'); break;
5585 case '\'': WRITECHAR('\''); break;
5586 case '\"': WRITECHAR('\"'); break;
5587 case 'b': WRITECHAR('\b'); break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005588 /* FF */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005589 case 'f': WRITECHAR('\014'); break;
5590 case 't': WRITECHAR('\t'); break;
5591 case 'n': WRITECHAR('\n'); break;
5592 case 'r': WRITECHAR('\r'); break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005593 /* VT */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005594 case 'v': WRITECHAR('\013'); break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005595 /* BEL, not classic C */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005596 case 'a': WRITECHAR('\007'); break;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005597
Benjamin Peterson29060642009-01-31 22:14:21 +00005598 /* \OOO (octal) escapes */
Guido van Rossumd57fd912000-03-10 22:53:23 +00005599 case '0': case '1': case '2': case '3':
5600 case '4': case '5': case '6': case '7':
Guido van Rossum0e4f6572000-05-01 21:27:20 +00005601 x = s[-1] - '0';
Guido van Rossum8ce8a782007-11-01 19:42:39 +00005602 if (s < end && '0' <= *s && *s <= '7') {
Guido van Rossum0e4f6572000-05-01 21:27:20 +00005603 x = (x<<3) + *s++ - '0';
Guido van Rossum8ce8a782007-11-01 19:42:39 +00005604 if (s < end && '0' <= *s && *s <= '7')
Guido van Rossum0e4f6572000-05-01 21:27:20 +00005605 x = (x<<3) + *s++ - '0';
Guido van Rossumd57fd912000-03-10 22:53:23 +00005606 }
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005607 WRITECHAR(x);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005608 break;
5609
Benjamin Peterson29060642009-01-31 22:14:21 +00005610 /* hex escapes */
5611 /* \xXX */
Guido van Rossumd57fd912000-03-10 22:53:23 +00005612 case 'x':
Fredrik Lundhccc74732001-02-18 22:13:49 +00005613 digits = 2;
5614 message = "truncated \\xXX escape";
5615 goto hexescape;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005616
Benjamin Peterson29060642009-01-31 22:14:21 +00005617 /* \uXXXX */
Guido van Rossumd57fd912000-03-10 22:53:23 +00005618 case 'u':
Fredrik Lundhccc74732001-02-18 22:13:49 +00005619 digits = 4;
5620 message = "truncated \\uXXXX escape";
5621 goto hexescape;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005622
Benjamin Peterson29060642009-01-31 22:14:21 +00005623 /* \UXXXXXXXX */
Fredrik Lundhdf846752000-09-03 11:29:49 +00005624 case 'U':
Fredrik Lundhccc74732001-02-18 22:13:49 +00005625 digits = 8;
5626 message = "truncated \\UXXXXXXXX escape";
5627 hexescape:
5628 chr = 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005629 if (s+digits>end) {
5630 endinpos = size;
5631 if (unicode_decode_call_errorhandler(
Benjamin Peterson29060642009-01-31 22:14:21 +00005632 errors, &errorHandler,
5633 "unicodeescape", "end of string in escape sequence",
5634 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005635 &v, &i))
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005636 goto onError;
5637 goto nextByte;
5638 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005639 for (j = 0; j < digits; ++j) {
5640 c = (unsigned char) s[j];
David Malcolm96960882010-11-05 17:23:41 +00005641 if (!Py_ISXDIGIT(c)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005642 endinpos = (s+j+1)-starts;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005643 if (unicode_decode_call_errorhandler(
Benjamin Peterson29060642009-01-31 22:14:21 +00005644 errors, &errorHandler,
5645 "unicodeescape", message,
5646 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005647 &v, &i))
Fredrik Lundhdf846752000-09-03 11:29:49 +00005648 goto onError;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005649 len = PyUnicode_GET_LENGTH(v);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005650 goto nextByte;
Fredrik Lundhdf846752000-09-03 11:29:49 +00005651 }
5652 chr = (chr<<4) & ~0xF;
5653 if (c >= '0' && c <= '9')
5654 chr += c - '0';
5655 else if (c >= 'a' && c <= 'f')
5656 chr += 10 + c - 'a';
5657 else
5658 chr += 10 + c - 'A';
5659 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005660 s += j;
Jeremy Hylton504de6b2003-10-06 05:08:26 +00005661 if (chr == 0xffffffff && PyErr_Occurred())
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005662 /* _decoding_error will have already written into the
5663 target buffer. */
5664 break;
Fredrik Lundhccc74732001-02-18 22:13:49 +00005665 store:
Fredrik Lundhdf846752000-09-03 11:29:49 +00005666 /* when we get here, chr is a 32-bit unicode character */
Victor Stinner8faf8212011-12-08 22:14:11 +01005667 if (chr <= MAX_UNICODE) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005668 WRITECHAR(chr);
Fredrik Lundhdf846752000-09-03 11:29:49 +00005669 } else {
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005670 endinpos = s-starts;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005671 if (unicode_decode_call_errorhandler(
Benjamin Peterson29060642009-01-31 22:14:21 +00005672 errors, &errorHandler,
5673 "unicodeescape", "illegal Unicode character",
5674 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005675 &v, &i))
Fredrik Lundhdf846752000-09-03 11:29:49 +00005676 goto onError;
5677 }
Fredrik Lundhccc74732001-02-18 22:13:49 +00005678 break;
5679
Benjamin Peterson29060642009-01-31 22:14:21 +00005680 /* \N{name} */
Fredrik Lundhccc74732001-02-18 22:13:49 +00005681 case 'N':
5682 message = "malformed \\N character escape";
5683 if (ucnhash_CAPI == NULL) {
5684 /* load the unicode data module */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005685 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
5686 PyUnicodeData_CAPSULE_NAME, 1);
Fredrik Lundhccc74732001-02-18 22:13:49 +00005687 if (ucnhash_CAPI == NULL)
5688 goto ucnhashError;
5689 }
5690 if (*s == '{') {
5691 const char *start = s+1;
5692 /* look for the closing brace */
5693 while (*s != '}' && s < end)
5694 s++;
5695 if (s > start && s < end && *s == '}') {
5696 /* found a name. look it up in the unicode database */
5697 message = "unknown Unicode character name";
5698 s++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005699 if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1),
Ezio Melotti931b8aa2011-10-21 21:57:36 +03005700 &chr, 0))
Fredrik Lundhccc74732001-02-18 22:13:49 +00005701 goto store;
5702 }
5703 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005704 endinpos = s-starts;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005705 if (unicode_decode_call_errorhandler(
Benjamin Peterson29060642009-01-31 22:14:21 +00005706 errors, &errorHandler,
5707 "unicodeescape", message,
5708 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005709 &v, &i))
Fredrik Lundhccc74732001-02-18 22:13:49 +00005710 goto onError;
Fredrik Lundhccc74732001-02-18 22:13:49 +00005711 break;
5712
5713 default:
Walter Dörwald8c077222002-03-25 11:16:18 +00005714 if (s > end) {
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005715 message = "\\ at end of string";
5716 s--;
5717 endinpos = s-starts;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005718 if (unicode_decode_call_errorhandler(
Benjamin Peterson29060642009-01-31 22:14:21 +00005719 errors, &errorHandler,
5720 "unicodeescape", message,
5721 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005722 &v, &i))
Walter Dörwald8c077222002-03-25 11:16:18 +00005723 goto onError;
5724 }
5725 else {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005726 WRITECHAR('\\');
5727 WRITECHAR(s[-1]);
Walter Dörwald8c077222002-03-25 11:16:18 +00005728 }
Fredrik Lundhccc74732001-02-18 22:13:49 +00005729 break;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005730 }
Benjamin Peterson29060642009-01-31 22:14:21 +00005731 nextByte:
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005732 ;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005733 }
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005734#undef WRITECHAR
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005735
Victor Stinner16e6a802011-12-12 13:24:15 +01005736 if (unicode_resize(&v, i) < 0)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005737 goto onError;
Walter Dörwaldd4ade082003-08-15 15:00:26 +00005738 Py_XDECREF(errorHandler);
5739 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01005740 return unicode_result(v);
Walter Dörwald8c077222002-03-25 11:16:18 +00005741
Benjamin Peterson29060642009-01-31 22:14:21 +00005742 ucnhashError:
Fredrik Lundh06d12682001-01-24 07:59:11 +00005743 PyErr_SetString(
5744 PyExc_UnicodeError,
5745 "\\N escapes not supported (can't load unicodedata module)"
5746 );
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00005747 Py_XDECREF(v);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005748 Py_XDECREF(errorHandler);
5749 Py_XDECREF(exc);
Fredrik Lundhf6056062001-01-20 11:15:25 +00005750 return NULL;
5751
Benjamin Peterson29060642009-01-31 22:14:21 +00005752 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00005753 Py_XDECREF(v);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005754 Py_XDECREF(errorHandler);
5755 Py_XDECREF(exc);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005756 return NULL;
5757}
5758
5759/* Return a Unicode-Escape string version of the Unicode object.
5760
5761 If quotes is true, the string is enclosed in u"" or u'' quotes as
5762 appropriate.
5763
5764*/
5765
Alexander Belopolsky40018472011-02-26 01:02:56 +00005766PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005767PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005768{
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005769 Py_ssize_t i, len;
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005770 PyObject *repr;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005771 char *p;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005772 int kind;
5773 void *data;
5774 Py_ssize_t expandsize = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005775
Thomas Wouters89f507f2006-12-13 04:49:30 +00005776 /* Initial allocation is based on the longest-possible unichr
5777 escape.
5778
5779 In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source
5780 unichr, so in this case it's the longest unichr escape. In
5781 narrow (UTF-16) builds this is five chars per source unichr
5782 since there are two unichrs in the surrogate pair, so in narrow
5783 (UTF-16) builds it's not the longest unichr escape.
5784
5785 In wide or narrow builds '\uxxxx' is 6 chars per source unichr,
5786 so in the narrow (UTF-16) build case it's the longest unichr
5787 escape.
5788 */
5789
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005790 if (!PyUnicode_Check(unicode)) {
5791 PyErr_BadArgument();
5792 return NULL;
5793 }
Benjamin Petersonbac79492012-01-14 13:34:47 -05005794 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005795 return NULL;
5796 len = PyUnicode_GET_LENGTH(unicode);
5797 kind = PyUnicode_KIND(unicode);
5798 data = PyUnicode_DATA(unicode);
Benjamin Petersonead6b532011-12-20 17:23:42 -06005799 switch (kind) {
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005800 case PyUnicode_1BYTE_KIND: expandsize = 4; break;
5801 case PyUnicode_2BYTE_KIND: expandsize = 6; break;
5802 case PyUnicode_4BYTE_KIND: expandsize = 10; break;
5803 }
5804
5805 if (len == 0)
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005806 return PyBytes_FromStringAndSize(NULL, 0);
5807
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005808 if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize)
Benjamin Peterson29060642009-01-31 22:14:21 +00005809 return PyErr_NoMemory();
Neal Norwitz3ce5d922008-08-24 07:08:55 +00005810
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005811 repr = PyBytes_FromStringAndSize(NULL,
Benjamin Peterson29060642009-01-31 22:14:21 +00005812 2
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005813 + expandsize*len
Benjamin Peterson29060642009-01-31 22:14:21 +00005814 + 1);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005815 if (repr == NULL)
5816 return NULL;
5817
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005818 p = PyBytes_AS_STRING(repr);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005819
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005820 for (i = 0; i < len; i++) {
Victor Stinner3326cb62011-11-10 20:15:25 +01005821 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Marc-André Lemburg80d1dd52001-07-25 16:05:59 +00005822
Walter Dörwald79e913e2007-05-12 11:08:06 +00005823 /* Escape backslashes */
5824 if (ch == '\\') {
Guido van Rossumd57fd912000-03-10 22:53:23 +00005825 *p++ = '\\';
5826 *p++ = (char) ch;
Walter Dörwald79e913e2007-05-12 11:08:06 +00005827 continue;
Tim Petersced69f82003-09-16 20:30:58 +00005828 }
Marc-André Lemburg80d1dd52001-07-25 16:05:59 +00005829
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +00005830 /* Map 21-bit characters to '\U00xxxxxx' */
5831 else if (ch >= 0x10000) {
Victor Stinner8faf8212011-12-08 22:14:11 +01005832 assert(ch <= MAX_UNICODE);
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +00005833 *p++ = '\\';
5834 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02005835 *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F];
5836 *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F];
5837 *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F];
5838 *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F];
5839 *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F];
5840 *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F];
5841 *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F];
5842 *p++ = Py_hexdigits[ch & 0x0000000F];
Benjamin Peterson29060642009-01-31 22:14:21 +00005843 continue;
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +00005844 }
Marc-André Lemburg6c6bfb72001-07-20 17:39:11 +00005845
Guido van Rossumd57fd912000-03-10 22:53:23 +00005846 /* Map 16-bit characters to '\uxxxx' */
Marc-André Lemburg6c6bfb72001-07-20 17:39:11 +00005847 if (ch >= 256) {
Guido van Rossumd57fd912000-03-10 22:53:23 +00005848 *p++ = '\\';
5849 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02005850 *p++ = Py_hexdigits[(ch >> 12) & 0x000F];
5851 *p++ = Py_hexdigits[(ch >> 8) & 0x000F];
5852 *p++ = Py_hexdigits[(ch >> 4) & 0x000F];
5853 *p++ = Py_hexdigits[ch & 0x000F];
Guido van Rossumd57fd912000-03-10 22:53:23 +00005854 }
Marc-André Lemburg80d1dd52001-07-25 16:05:59 +00005855
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +00005856 /* Map special whitespace to '\t', \n', '\r' */
5857 else if (ch == '\t') {
5858 *p++ = '\\';
5859 *p++ = 't';
5860 }
5861 else if (ch == '\n') {
5862 *p++ = '\\';
5863 *p++ = 'n';
5864 }
5865 else if (ch == '\r') {
5866 *p++ = '\\';
5867 *p++ = 'r';
5868 }
Marc-André Lemburg80d1dd52001-07-25 16:05:59 +00005869
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +00005870 /* Map non-printable US ASCII to '\xhh' */
Marc-André Lemburg11326de2001-11-28 12:56:20 +00005871 else if (ch < ' ' || ch >= 0x7F) {
Guido van Rossumd57fd912000-03-10 22:53:23 +00005872 *p++ = '\\';
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +00005873 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +02005874 *p++ = Py_hexdigits[(ch >> 4) & 0x000F];
5875 *p++ = Py_hexdigits[ch & 0x000F];
Tim Petersced69f82003-09-16 20:30:58 +00005876 }
Marc-André Lemburg80d1dd52001-07-25 16:05:59 +00005877
Guido van Rossumd57fd912000-03-10 22:53:23 +00005878 /* Copy everything else as-is */
5879 else
5880 *p++ = (char) ch;
5881 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00005882
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00005883 assert(p - PyBytes_AS_STRING(repr) > 0);
5884 if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0)
5885 return NULL;
5886 return repr;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005887}
5888
Alexander Belopolsky40018472011-02-26 01:02:56 +00005889PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005890PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,
5891 Py_ssize_t size)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005892{
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005893 PyObject *result;
5894 PyObject *tmp = PyUnicode_FromUnicode(s, size);
5895 if (tmp == NULL)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005896 return NULL;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01005897 result = PyUnicode_AsUnicodeEscapeString(tmp);
5898 Py_DECREF(tmp);
5899 return result;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005900}
5901
5902/* --- Raw Unicode Escape Codec ------------------------------------------- */
5903
Alexander Belopolsky40018472011-02-26 01:02:56 +00005904PyObject *
5905PyUnicode_DecodeRawUnicodeEscape(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03005906 Py_ssize_t size,
5907 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00005908{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005909 const char *starts = s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005910 Py_ssize_t startinpos;
5911 Py_ssize_t endinpos;
5912 Py_ssize_t outpos;
Victor Stinner7931d9a2011-11-04 00:22:48 +01005913 PyObject *v;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005914 const char *end;
5915 const char *bs;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005916 PyObject *errorHandler = NULL;
5917 PyObject *exc = NULL;
Tim Petersced69f82003-09-16 20:30:58 +00005918
Guido van Rossumd57fd912000-03-10 22:53:23 +00005919 /* Escaped strings will always be longer than the resulting
5920 Unicode string, so we start with size here and then reduce the
Walter Dörwald3aeb6322002-09-02 13:14:32 +00005921 length after conversion to the true value. (But decoding error
5922 handler might have to resize the string) */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005923 v = PyUnicode_New(size, 127);
Guido van Rossumd57fd912000-03-10 22:53:23 +00005924 if (v == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00005925 goto onError;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005926 if (size == 0)
Victor Stinner7931d9a2011-11-04 00:22:48 +01005927 return v;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005928 outpos = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005929 end = s + size;
5930 while (s < end) {
Benjamin Peterson29060642009-01-31 22:14:21 +00005931 unsigned char c;
5932 Py_UCS4 x;
5933 int i;
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +00005934 int count;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005935
Benjamin Peterson29060642009-01-31 22:14:21 +00005936 /* Non-escape characters are interpreted as Unicode ordinals */
5937 if (*s != '\\') {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005938 if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0)
5939 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00005940 continue;
Benjamin Peterson14339b62009-01-31 16:36:08 +00005941 }
Benjamin Peterson29060642009-01-31 22:14:21 +00005942 startinpos = s-starts;
5943
5944 /* \u-escapes are only interpreted iff the number of leading
5945 backslashes if odd */
5946 bs = s;
5947 for (;s < end;) {
5948 if (*s != '\\')
5949 break;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005950 if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0)
5951 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00005952 }
5953 if (((s - bs) & 1) == 0 ||
5954 s >= end ||
5955 (*s != 'u' && *s != 'U')) {
5956 continue;
5957 }
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005958 outpos--;
Benjamin Peterson29060642009-01-31 22:14:21 +00005959 count = *s=='u' ? 4 : 8;
5960 s++;
5961
5962 /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */
Benjamin Peterson29060642009-01-31 22:14:21 +00005963 for (x = 0, i = 0; i < count; ++i, ++s) {
5964 c = (unsigned char)*s;
David Malcolm96960882010-11-05 17:23:41 +00005965 if (!Py_ISXDIGIT(c)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00005966 endinpos = s-starts;
5967 if (unicode_decode_call_errorhandler(
5968 errors, &errorHandler,
5969 "rawunicodeescape", "truncated \\uXXXX",
5970 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005971 &v, &outpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00005972 goto onError;
5973 goto nextByte;
5974 }
5975 x = (x<<4) & ~0xF;
5976 if (c >= '0' && c <= '9')
5977 x += c - '0';
5978 else if (c >= 'a' && c <= 'f')
5979 x += 10 + c - 'a';
5980 else
5981 x += 10 + c - 'A';
5982 }
Victor Stinner8faf8212011-12-08 22:14:11 +01005983 if (x <= MAX_UNICODE) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005984 if (unicode_putchar(&v, &outpos, x) < 0)
5985 goto onError;
Christian Heimesfe337bf2008-03-23 21:54:12 +00005986 } else {
5987 endinpos = s-starts;
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +00005988 if (unicode_decode_call_errorhandler(
5989 errors, &errorHandler,
5990 "rawunicodeescape", "\\Uxxxxxxxx out of range",
Benjamin Peterson29060642009-01-31 22:14:21 +00005991 &starts, &end, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01005992 &v, &outpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00005993 goto onError;
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +00005994 }
Benjamin Peterson29060642009-01-31 22:14:21 +00005995 nextByte:
5996 ;
Guido van Rossumd57fd912000-03-10 22:53:23 +00005997 }
Victor Stinner16e6a802011-12-12 13:24:15 +01005998 if (unicode_resize(&v, outpos) < 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00005999 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006000 Py_XDECREF(errorHandler);
6001 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01006002 return unicode_result(v);
Tim Petersced69f82003-09-16 20:30:58 +00006003
Benjamin Peterson29060642009-01-31 22:14:21 +00006004 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00006005 Py_XDECREF(v);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006006 Py_XDECREF(errorHandler);
6007 Py_XDECREF(exc);
Guido van Rossumd57fd912000-03-10 22:53:23 +00006008 return NULL;
6009}
6010
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006011
Alexander Belopolsky40018472011-02-26 01:02:56 +00006012PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006013PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006014{
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006015 PyObject *repr;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006016 char *p;
6017 char *q;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006018 Py_ssize_t expandsize, pos;
6019 int kind;
6020 void *data;
6021 Py_ssize_t len;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006022
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006023 if (!PyUnicode_Check(unicode)) {
6024 PyErr_BadArgument();
6025 return NULL;
6026 }
Benjamin Petersonbac79492012-01-14 13:34:47 -05006027 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006028 return NULL;
6029 kind = PyUnicode_KIND(unicode);
6030 data = PyUnicode_DATA(unicode);
6031 len = PyUnicode_GET_LENGTH(unicode);
Benjamin Peterson1518e872011-11-23 10:44:52 -06006032 /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6
6033 bytes, and 1 byte characters 4. */
6034 expandsize = kind * 2 + 2;
Victor Stinner0e368262011-11-10 20:12:49 +01006035
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006036 if (len > PY_SSIZE_T_MAX / expandsize)
Benjamin Peterson29060642009-01-31 22:14:21 +00006037 return PyErr_NoMemory();
Benjamin Peterson14339b62009-01-31 16:36:08 +00006038
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006039 repr = PyBytes_FromStringAndSize(NULL, expandsize * len);
Guido van Rossumd57fd912000-03-10 22:53:23 +00006040 if (repr == NULL)
6041 return NULL;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006042 if (len == 0)
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006043 return repr;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006044
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006045 p = q = PyBytes_AS_STRING(repr);
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006046 for (pos = 0; pos < len; pos++) {
6047 Py_UCS4 ch = PyUnicode_READ(kind, data, pos);
Benjamin Peterson29060642009-01-31 22:14:21 +00006048 /* Map 32-bit characters to '\Uxxxxxxxx' */
6049 if (ch >= 0x10000) {
Victor Stinner8faf8212011-12-08 22:14:11 +01006050 assert(ch <= MAX_UNICODE);
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +00006051 *p++ = '\\';
6052 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02006053 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
6054 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
6055 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
6056 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
6057 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
6058 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
6059 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
6060 *p++ = Py_hexdigits[ch & 15];
Tim Petersced69f82003-09-16 20:30:58 +00006061 }
Benjamin Peterson29060642009-01-31 22:14:21 +00006062 /* Map 16-bit characters to '\uxxxx' */
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006063 else if (ch >= 256) {
Guido van Rossumd57fd912000-03-10 22:53:23 +00006064 *p++ = '\\';
6065 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02006066 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
6067 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
6068 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
6069 *p++ = Py_hexdigits[ch & 15];
Guido van Rossumd57fd912000-03-10 22:53:23 +00006070 }
Benjamin Peterson29060642009-01-31 22:14:21 +00006071 /* Copy everything else as-is */
6072 else
Guido van Rossumd57fd912000-03-10 22:53:23 +00006073 *p++ = (char) ch;
6074 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00006075
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006076 assert(p > q);
6077 if (_PyBytes_Resize(&repr, p - q) < 0)
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006078 return NULL;
6079 return repr;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006080}
6081
Alexander Belopolsky40018472011-02-26 01:02:56 +00006082PyObject *
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006083PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
6084 Py_ssize_t size)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006085{
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006086 PyObject *result;
6087 PyObject *tmp = PyUnicode_FromUnicode(s, size);
6088 if (tmp == NULL)
Walter Dörwald711005d2007-05-12 12:03:26 +00006089 return NULL;
Martin v. Löwis1db7c132011-11-10 18:24:32 +01006090 result = PyUnicode_AsRawUnicodeEscapeString(tmp);
6091 Py_DECREF(tmp);
6092 return result;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006093}
6094
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006095/* --- Unicode Internal Codec ------------------------------------------- */
6096
Alexander Belopolsky40018472011-02-26 01:02:56 +00006097PyObject *
6098_PyUnicode_DecodeUnicodeInternal(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006099 Py_ssize_t size,
6100 const char *errors)
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006101{
6102 const char *starts = s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00006103 Py_ssize_t startinpos;
6104 Py_ssize_t endinpos;
6105 Py_ssize_t outpos;
Victor Stinner7931d9a2011-11-04 00:22:48 +01006106 PyObject *v;
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006107 const char *end;
6108 const char *reason;
6109 PyObject *errorHandler = NULL;
6110 PyObject *exc = NULL;
6111
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006112 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Ezio Melotti11060a42011-11-16 09:39:10 +02006113 "unicode_internal codec has been deprecated",
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006114 1))
6115 return NULL;
6116
Thomas Wouters89f507f2006-12-13 04:49:30 +00006117 /* XXX overflow detection missing */
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006118 v = PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE, 127);
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006119 if (v == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00006120 goto onError;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006121 if (PyUnicode_GET_LENGTH(v) == 0)
Victor Stinner7931d9a2011-11-04 00:22:48 +01006122 return v;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006123 outpos = 0;
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006124 end = s + size;
6125
6126 while (s < end) {
Antoine Pitrou0290c7a2011-11-11 13:29:12 +01006127 Py_UNICODE uch;
Antoine Pitrou44c6aff2011-11-11 02:59:42 +01006128 Py_UCS4 ch;
6129 /* We copy the raw representation one byte at a time because the
6130 pointer may be unaligned (see test_codeccallbacks). */
Antoine Pitrou0290c7a2011-11-11 13:29:12 +01006131 ((char *) &uch)[0] = s[0];
6132 ((char *) &uch)[1] = s[1];
Antoine Pitrou44c6aff2011-11-11 02:59:42 +01006133#ifdef Py_UNICODE_WIDE
Antoine Pitrou0290c7a2011-11-11 13:29:12 +01006134 ((char *) &uch)[2] = s[2];
6135 ((char *) &uch)[3] = s[3];
Antoine Pitrou44c6aff2011-11-11 02:59:42 +01006136#endif
Antoine Pitrou0290c7a2011-11-11 13:29:12 +01006137 ch = uch;
6138
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006139 /* We have to sanity check the raw data, otherwise doom looms for
6140 some malformed UCS-4 data. */
6141 if (
Benjamin Peterson29060642009-01-31 22:14:21 +00006142#ifdef Py_UNICODE_WIDE
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006143 ch > 0x10ffff ||
Benjamin Peterson29060642009-01-31 22:14:21 +00006144#endif
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006145 end-s < Py_UNICODE_SIZE
6146 )
Benjamin Peterson29060642009-01-31 22:14:21 +00006147 {
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006148 startinpos = s - starts;
6149 if (end-s < Py_UNICODE_SIZE) {
6150 endinpos = end-starts;
6151 reason = "truncated input";
6152 }
6153 else {
6154 endinpos = s - starts + Py_UNICODE_SIZE;
6155 reason = "illegal code point (> 0x10FFFF)";
6156 }
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006157 if (unicode_decode_call_errorhandler(
6158 errors, &errorHandler,
6159 "unicode_internal", reason,
Walter Dörwalde78178e2007-07-30 13:31:40 +00006160 &starts, &end, &startinpos, &endinpos, &exc, &s,
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006161 &v, &outpos))
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006162 goto onError;
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006163 continue;
6164 }
6165
6166 s += Py_UNICODE_SIZE;
6167#ifndef Py_UNICODE_WIDE
Victor Stinner551ac952011-11-29 22:58:13 +01006168 if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end)
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006169 {
Antoine Pitrou0290c7a2011-11-11 13:29:12 +01006170 Py_UNICODE uch2;
6171 ((char *) &uch2)[0] = s[0];
6172 ((char *) &uch2)[1] = s[1];
Victor Stinner551ac952011-11-29 22:58:13 +01006173 if (Py_UNICODE_IS_LOW_SURROGATE(uch2))
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006174 {
Victor Stinner551ac952011-11-29 22:58:13 +01006175 ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2);
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006176 s += Py_UNICODE_SIZE;
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006177 }
6178 }
Victor Stinner9f4b1e92011-11-10 20:56:30 +01006179#endif
6180
6181 if (unicode_putchar(&v, &outpos, ch) < 0)
6182 goto onError;
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006183 }
6184
Victor Stinner16e6a802011-12-12 13:24:15 +01006185 if (unicode_resize(&v, outpos) < 0)
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006186 goto onError;
6187 Py_XDECREF(errorHandler);
6188 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01006189 return unicode_result(v);
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006190
Benjamin Peterson29060642009-01-31 22:14:21 +00006191 onError:
Walter Dörwalda47d1c02005-08-30 10:23:14 +00006192 Py_XDECREF(v);
6193 Py_XDECREF(errorHandler);
6194 Py_XDECREF(exc);
6195 return NULL;
6196}
6197
Guido van Rossumd57fd912000-03-10 22:53:23 +00006198/* --- Latin-1 Codec ------------------------------------------------------ */
6199
Alexander Belopolsky40018472011-02-26 01:02:56 +00006200PyObject *
6201PyUnicode_DecodeLatin1(const char *s,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006202 Py_ssize_t size,
6203 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006204{
Guido van Rossumd57fd912000-03-10 22:53:23 +00006205 /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
Victor Stinnere57b1c02011-09-28 22:20:48 +02006206 return _PyUnicode_FromUCS1((unsigned char*)s, size);
Guido van Rossumd57fd912000-03-10 22:53:23 +00006207}
6208
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006209/* create or adjust a UnicodeEncodeError */
Alexander Belopolsky40018472011-02-26 01:02:56 +00006210static void
6211make_encode_exception(PyObject **exceptionObject,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006212 const char *encoding,
Martin v. Löwis9e816682011-11-02 12:45:42 +01006213 PyObject *unicode,
6214 Py_ssize_t startpos, Py_ssize_t endpos,
6215 const char *reason)
6216{
6217 if (*exceptionObject == NULL) {
6218 *exceptionObject = PyObject_CallFunction(
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006219 PyExc_UnicodeEncodeError, "sOnns",
Martin v. Löwis9e816682011-11-02 12:45:42 +01006220 encoding, unicode, startpos, endpos, reason);
6221 }
6222 else {
6223 if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos))
6224 goto onError;
6225 if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos))
6226 goto onError;
6227 if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason))
6228 goto onError;
6229 return;
6230 onError:
6231 Py_DECREF(*exceptionObject);
6232 *exceptionObject = NULL;
6233 }
6234}
6235
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006236/* raises a UnicodeEncodeError */
Alexander Belopolsky40018472011-02-26 01:02:56 +00006237static void
6238raise_encode_exception(PyObject **exceptionObject,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006239 const char *encoding,
Martin v. Löwis9e816682011-11-02 12:45:42 +01006240 PyObject *unicode,
6241 Py_ssize_t startpos, Py_ssize_t endpos,
6242 const char *reason)
6243{
Martin v. Löwis12be46c2011-11-04 19:04:15 +01006244 make_encode_exception(exceptionObject,
Martin v. Löwis9e816682011-11-02 12:45:42 +01006245 encoding, unicode, startpos, endpos, reason);
6246 if (*exceptionObject != NULL)
6247 PyCodec_StrictErrors(*exceptionObject);
6248}
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006249
6250/* error handling callback helper:
6251 build arguments, call the callback and check the arguments,
6252 put the result into newpos and return the replacement string, which
6253 has to be freed by the caller */
Alexander Belopolsky40018472011-02-26 01:02:56 +00006254static PyObject *
6255unicode_encode_call_errorhandler(const char *errors,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006256 PyObject **errorHandler,
6257 const char *encoding, const char *reason,
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006258 PyObject *unicode, PyObject **exceptionObject,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006259 Py_ssize_t startpos, Py_ssize_t endpos,
6260 Py_ssize_t *newpos)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006261{
Martin v. Löwisdb12d452009-05-02 18:52:14 +00006262 static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple";
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006263 Py_ssize_t len;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006264 PyObject *restuple;
6265 PyObject *resunicode;
6266
6267 if (*errorHandler == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00006268 *errorHandler = PyCodec_LookupError(errors);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006269 if (*errorHandler == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00006270 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006271 }
6272
Benjamin Petersonbac79492012-01-14 13:34:47 -05006273 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006274 return NULL;
6275 len = PyUnicode_GET_LENGTH(unicode);
6276
Martin v. Löwis12be46c2011-11-04 19:04:15 +01006277 make_encode_exception(exceptionObject,
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006278 encoding, unicode, startpos, endpos, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006279 if (*exceptionObject == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00006280 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006281
6282 restuple = PyObject_CallFunctionObjArgs(
Benjamin Peterson29060642009-01-31 22:14:21 +00006283 *errorHandler, *exceptionObject, NULL);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006284 if (restuple == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00006285 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006286 if (!PyTuple_Check(restuple)) {
Martin v. Löwisdb12d452009-05-02 18:52:14 +00006287 PyErr_SetString(PyExc_TypeError, &argparse[3]);
Benjamin Peterson29060642009-01-31 22:14:21 +00006288 Py_DECREF(restuple);
6289 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006290 }
Martin v. Löwisdb12d452009-05-02 18:52:14 +00006291 if (!PyArg_ParseTuple(restuple, argparse,
Benjamin Peterson29060642009-01-31 22:14:21 +00006292 &resunicode, newpos)) {
6293 Py_DECREF(restuple);
6294 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006295 }
Martin v. Löwisdb12d452009-05-02 18:52:14 +00006296 if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) {
6297 PyErr_SetString(PyExc_TypeError, &argparse[3]);
6298 Py_DECREF(restuple);
6299 return NULL;
6300 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006301 if (*newpos<0)
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006302 *newpos = len + *newpos;
6303 if (*newpos<0 || *newpos>len) {
Benjamin Peterson29060642009-01-31 22:14:21 +00006304 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
6305 Py_DECREF(restuple);
6306 return NULL;
Walter Dörwald2e0b18a2003-01-31 17:19:08 +00006307 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006308 Py_INCREF(resunicode);
6309 Py_DECREF(restuple);
6310 return resunicode;
6311}
6312
Alexander Belopolsky40018472011-02-26 01:02:56 +00006313static PyObject *
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006314unicode_encode_ucs1(PyObject *unicode,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006315 const char *errors,
Victor Stinnerfcd96532011-11-04 00:28:50 +01006316 unsigned int limit)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006317{
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006318 /* input state */
6319 Py_ssize_t pos=0, size;
6320 int kind;
6321 void *data;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006322 /* output object */
6323 PyObject *res;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006324 /* pointer into the output */
6325 char *str;
6326 /* current output position */
Martin v. Löwis18e16552006-02-15 17:27:45 +00006327 Py_ssize_t ressize;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006328 const char *encoding = (limit == 256) ? "latin-1" : "ascii";
6329 const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006330 PyObject *errorHandler = NULL;
6331 PyObject *exc = NULL;
6332 /* the following variable is used for caching string comparisons
6333 * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */
6334 int known_errorHandler = -1;
6335
Benjamin Petersonbac79492012-01-14 13:34:47 -05006336 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006337 return NULL;
6338 size = PyUnicode_GET_LENGTH(unicode);
6339 kind = PyUnicode_KIND(unicode);
6340 data = PyUnicode_DATA(unicode);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006341 /* allocate enough for a simple encoding without
6342 replacements, if we need more, we'll resize */
Guido van Rossum98297ee2007-11-06 21:34:58 +00006343 if (size == 0)
Christian Heimes72b710a2008-05-26 13:28:38 +00006344 return PyBytes_FromStringAndSize(NULL, 0);
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006345 res = PyBytes_FromStringAndSize(NULL, size);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006346 if (res == NULL)
Guido van Rossum98297ee2007-11-06 21:34:58 +00006347 return NULL;
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006348 str = PyBytes_AS_STRING(res);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006349 ressize = size;
6350
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006351 while (pos < size) {
6352 Py_UCS4 c = PyUnicode_READ(kind, data, pos);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006353
Benjamin Peterson29060642009-01-31 22:14:21 +00006354 /* can we encode this? */
6355 if (c<limit) {
6356 /* no overflow check, because we know that the space is enough */
6357 *str++ = (char)c;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006358 ++pos;
Benjamin Peterson14339b62009-01-31 16:36:08 +00006359 }
Benjamin Peterson29060642009-01-31 22:14:21 +00006360 else {
Benjamin Peterson29060642009-01-31 22:14:21 +00006361 Py_ssize_t requiredsize;
6362 PyObject *repunicode;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006363 Py_ssize_t repsize, newpos, respos, i;
Benjamin Peterson29060642009-01-31 22:14:21 +00006364 /* startpos for collecting unencodable chars */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006365 Py_ssize_t collstart = pos;
6366 Py_ssize_t collend = pos;
Benjamin Peterson29060642009-01-31 22:14:21 +00006367 /* find all unecodable characters */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006368 while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit))
Benjamin Peterson29060642009-01-31 22:14:21 +00006369 ++collend;
6370 /* cache callback name lookup (if not done yet, i.e. it's the first error) */
6371 if (known_errorHandler==-1) {
6372 if ((errors==NULL) || (!strcmp(errors, "strict")))
6373 known_errorHandler = 1;
6374 else if (!strcmp(errors, "replace"))
6375 known_errorHandler = 2;
6376 else if (!strcmp(errors, "ignore"))
6377 known_errorHandler = 3;
6378 else if (!strcmp(errors, "xmlcharrefreplace"))
6379 known_errorHandler = 4;
6380 else
6381 known_errorHandler = 0;
6382 }
6383 switch (known_errorHandler) {
6384 case 1: /* strict */
Martin v. Löwis12be46c2011-11-04 19:04:15 +01006385 raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason);
Benjamin Peterson29060642009-01-31 22:14:21 +00006386 goto onError;
6387 case 2: /* replace */
6388 while (collstart++<collend)
6389 *str++ = '?'; /* fall through */
6390 case 3: /* ignore */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006391 pos = collend;
Benjamin Peterson29060642009-01-31 22:14:21 +00006392 break;
6393 case 4: /* xmlcharrefreplace */
6394 respos = str - PyBytes_AS_STRING(res);
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006395 /* determine replacement size */
6396 for (i = collstart, repsize = 0; i < collend; ++i) {
6397 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
6398 if (ch < 10)
Benjamin Peterson29060642009-01-31 22:14:21 +00006399 repsize += 2+1+1;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006400 else if (ch < 100)
Benjamin Peterson29060642009-01-31 22:14:21 +00006401 repsize += 2+2+1;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006402 else if (ch < 1000)
Benjamin Peterson29060642009-01-31 22:14:21 +00006403 repsize += 2+3+1;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006404 else if (ch < 10000)
Benjamin Peterson29060642009-01-31 22:14:21 +00006405 repsize += 2+4+1;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006406 else if (ch < 100000)
Benjamin Peterson29060642009-01-31 22:14:21 +00006407 repsize += 2+5+1;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006408 else if (ch < 1000000)
Benjamin Peterson29060642009-01-31 22:14:21 +00006409 repsize += 2+6+1;
Victor Stinner0d3721d2011-11-22 03:27:53 +01006410 else {
Victor Stinner8faf8212011-12-08 22:14:11 +01006411 assert(ch <= MAX_UNICODE);
Benjamin Peterson29060642009-01-31 22:14:21 +00006412 repsize += 2+7+1;
Victor Stinner0d3721d2011-11-22 03:27:53 +01006413 }
Benjamin Peterson29060642009-01-31 22:14:21 +00006414 }
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006415 requiredsize = respos+repsize+(size-collend);
Benjamin Peterson29060642009-01-31 22:14:21 +00006416 if (requiredsize > ressize) {
6417 if (requiredsize<2*ressize)
6418 requiredsize = 2*ressize;
6419 if (_PyBytes_Resize(&res, requiredsize))
6420 goto onError;
6421 str = PyBytes_AS_STRING(res) + respos;
6422 ressize = requiredsize;
6423 }
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006424 /* generate replacement */
6425 for (i = collstart; i < collend; ++i) {
6426 str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
Benjamin Peterson29060642009-01-31 22:14:21 +00006427 }
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006428 pos = collend;
Benjamin Peterson29060642009-01-31 22:14:21 +00006429 break;
6430 default:
6431 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler,
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006432 encoding, reason, unicode, &exc,
6433 collstart, collend, &newpos);
6434 if (repunicode == NULL || (PyUnicode_Check(repunicode) &&
Benjamin Petersonbac79492012-01-14 13:34:47 -05006435 PyUnicode_READY(repunicode) == -1))
Benjamin Peterson29060642009-01-31 22:14:21 +00006436 goto onError;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006437 if (PyBytes_Check(repunicode)) {
6438 /* Directly copy bytes result to output. */
6439 repsize = PyBytes_Size(repunicode);
6440 if (repsize > 1) {
6441 /* Make room for all additional bytes. */
Amaury Forgeot d'Arc84ec8d92009-06-29 22:36:49 +00006442 respos = str - PyBytes_AS_STRING(res);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006443 if (_PyBytes_Resize(&res, ressize+repsize-1)) {
6444 Py_DECREF(repunicode);
6445 goto onError;
6446 }
Amaury Forgeot d'Arc84ec8d92009-06-29 22:36:49 +00006447 str = PyBytes_AS_STRING(res) + respos;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006448 ressize += repsize-1;
6449 }
6450 memcpy(str, PyBytes_AsString(repunicode), repsize);
6451 str += repsize;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006452 pos = newpos;
Martin v. Löwisdb12d452009-05-02 18:52:14 +00006453 Py_DECREF(repunicode);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006454 break;
Martin v. Löwisdb12d452009-05-02 18:52:14 +00006455 }
Benjamin Peterson29060642009-01-31 22:14:21 +00006456 /* need more space? (at least enough for what we
6457 have+the replacement+the rest of the string, so
6458 we won't have to check space for encodable characters) */
6459 respos = str - PyBytes_AS_STRING(res);
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006460 repsize = PyUnicode_GET_LENGTH(repunicode);
6461 requiredsize = respos+repsize+(size-collend);
Benjamin Peterson29060642009-01-31 22:14:21 +00006462 if (requiredsize > ressize) {
6463 if (requiredsize<2*ressize)
6464 requiredsize = 2*ressize;
6465 if (_PyBytes_Resize(&res, requiredsize)) {
6466 Py_DECREF(repunicode);
6467 goto onError;
6468 }
6469 str = PyBytes_AS_STRING(res) + respos;
6470 ressize = requiredsize;
6471 }
6472 /* check if there is anything unencodable in the replacement
6473 and copy it to the output */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006474 for (i = 0; repsize-->0; ++i, ++str) {
6475 c = PyUnicode_READ_CHAR(repunicode, i);
Benjamin Peterson29060642009-01-31 22:14:21 +00006476 if (c >= limit) {
Martin v. Löwis12be46c2011-11-04 19:04:15 +01006477 raise_encode_exception(&exc, encoding, unicode,
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006478 pos, pos+1, reason);
Benjamin Peterson29060642009-01-31 22:14:21 +00006479 Py_DECREF(repunicode);
6480 goto onError;
6481 }
6482 *str = (char)c;
6483 }
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006484 pos = newpos;
Benjamin Peterson14339b62009-01-31 16:36:08 +00006485 Py_DECREF(repunicode);
Benjamin Peterson14339b62009-01-31 16:36:08 +00006486 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00006487 }
6488 }
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006489 /* Resize if we allocated to much */
6490 size = str - PyBytes_AS_STRING(res);
6491 if (size < ressize) { /* If this falls res will be NULL */
Alexandre Vassalottibad1b922008-12-27 09:49:09 +00006492 assert(size >= 0);
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006493 if (_PyBytes_Resize(&res, size) < 0)
6494 goto onError;
6495 }
6496
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006497 Py_XDECREF(errorHandler);
6498 Py_XDECREF(exc);
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00006499 return res;
6500
6501 onError:
6502 Py_XDECREF(res);
6503 Py_XDECREF(errorHandler);
6504 Py_XDECREF(exc);
6505 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006506}
6507
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006508/* Deprecated */
Alexander Belopolsky40018472011-02-26 01:02:56 +00006509PyObject *
6510PyUnicode_EncodeLatin1(const Py_UNICODE *p,
Ezio Melotti2aa2b3b2011-09-29 00:58:57 +03006511 Py_ssize_t size,
6512 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006513{
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006514 PyObject *result;
6515 PyObject *unicode = PyUnicode_FromUnicode(p, size);
6516 if (unicode == NULL)
6517 return NULL;
6518 result = unicode_encode_ucs1(unicode, errors, 256);
6519 Py_DECREF(unicode);
6520 return result;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006521}
6522
Alexander Belopolsky40018472011-02-26 01:02:56 +00006523PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006524_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006525{
6526 if (!PyUnicode_Check(unicode)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00006527 PyErr_BadArgument();
6528 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006529 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006530 if (PyUnicode_READY(unicode) == -1)
6531 return NULL;
6532 /* Fast path: if it is a one-byte string, construct
6533 bytes object directly. */
6534 if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND)
6535 return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
6536 PyUnicode_GET_LENGTH(unicode));
6537 /* Non-Latin-1 characters present. Defer to above function to
6538 raise the exception. */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006539 return unicode_encode_ucs1(unicode, errors, 256);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006540}
6541
6542PyObject*
6543PyUnicode_AsLatin1String(PyObject *unicode)
6544{
6545 return _PyUnicode_AsLatin1String(unicode, NULL);
Guido van Rossumd57fd912000-03-10 22:53:23 +00006546}
6547
6548/* --- 7-bit ASCII Codec -------------------------------------------------- */
6549
Alexander Belopolsky40018472011-02-26 01:02:56 +00006550PyObject *
6551PyUnicode_DecodeASCII(const char *s,
6552 Py_ssize_t size,
6553 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006554{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006555 const char *starts = s;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006556 PyObject *unicode;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006557 int kind;
6558 void *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +00006559 Py_ssize_t startinpos;
6560 Py_ssize_t endinpos;
6561 Py_ssize_t outpos;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006562 const char *e;
6563 PyObject *errorHandler = NULL;
6564 PyObject *exc = NULL;
Tim Petersced69f82003-09-16 20:30:58 +00006565
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01006566 if (size == 0) {
6567 Py_INCREF(unicode_empty);
6568 return unicode_empty;
6569 }
6570
Guido van Rossumd57fd912000-03-10 22:53:23 +00006571 /* ASCII is equivalent to the first 128 ordinals in Unicode. */
Victor Stinner702c7342011-10-05 13:50:52 +02006572 if (size == 1 && (unsigned char)s[0] < 128)
6573 return get_latin1_char((unsigned char)s[0]);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006574
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006575 unicode = PyUnicode_New(size, 127);
6576 if (unicode == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00006577 goto onError;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006578
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006579 e = s + size;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006580 data = PyUnicode_1BYTE_DATA(unicode);
6581 outpos = ascii_decode(s, e, (Py_UCS1 *)data);
6582 if (outpos == size)
6583 return unicode;
6584
6585 s += outpos;
6586 kind = PyUnicode_1BYTE_KIND;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006587 while (s < e) {
Benjamin Peterson29060642009-01-31 22:14:21 +00006588 register unsigned char c = (unsigned char)*s;
6589 if (c < 128) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006590 PyUnicode_WRITE(kind, data, outpos++, c);
Benjamin Peterson29060642009-01-31 22:14:21 +00006591 ++s;
6592 }
6593 else {
6594 startinpos = s-starts;
6595 endinpos = startinpos + 1;
Benjamin Peterson29060642009-01-31 22:14:21 +00006596 if (unicode_decode_call_errorhandler(
6597 errors, &errorHandler,
6598 "ascii", "ordinal not in range(128)",
6599 &starts, &e, &startinpos, &endinpos, &exc, &s,
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006600 &unicode, &outpos))
Benjamin Peterson29060642009-01-31 22:14:21 +00006601 goto onError;
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006602 kind = PyUnicode_KIND(unicode);
6603 data = PyUnicode_DATA(unicode);
Benjamin Peterson29060642009-01-31 22:14:21 +00006604 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00006605 }
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006606 if (unicode_resize(&unicode, outpos) < 0)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01006607 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006608 Py_XDECREF(errorHandler);
6609 Py_XDECREF(exc);
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006610 assert(_PyUnicode_CheckConsistency(unicode, 1));
6611 return unicode;
Tim Petersced69f82003-09-16 20:30:58 +00006612
Benjamin Peterson29060642009-01-31 22:14:21 +00006613 onError:
Antoine Pitrouca5f91b2012-05-10 16:36:02 +02006614 Py_XDECREF(unicode);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00006615 Py_XDECREF(errorHandler);
6616 Py_XDECREF(exc);
Guido van Rossumd57fd912000-03-10 22:53:23 +00006617 return NULL;
6618}
6619
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006620/* Deprecated */
Alexander Belopolsky40018472011-02-26 01:02:56 +00006621PyObject *
6622PyUnicode_EncodeASCII(const Py_UNICODE *p,
6623 Py_ssize_t size,
6624 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006625{
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006626 PyObject *result;
6627 PyObject *unicode = PyUnicode_FromUnicode(p, size);
6628 if (unicode == NULL)
6629 return NULL;
6630 result = unicode_encode_ucs1(unicode, errors, 128);
6631 Py_DECREF(unicode);
6632 return result;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006633}
6634
Alexander Belopolsky40018472011-02-26 01:02:56 +00006635PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006636_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00006637{
6638 if (!PyUnicode_Check(unicode)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00006639 PyErr_BadArgument();
6640 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00006641 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006642 if (PyUnicode_READY(unicode) == -1)
6643 return NULL;
6644 /* Fast path: if it is an ASCII-only string, construct bytes object
6645 directly. Else defer to above function to raise the exception. */
6646 if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
6647 return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
6648 PyUnicode_GET_LENGTH(unicode));
Martin v. Löwis23e275b2011-11-02 18:02:51 +01006649 return unicode_encode_ucs1(unicode, errors, 128);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006650}
6651
6652PyObject *
6653PyUnicode_AsASCIIString(PyObject *unicode)
6654{
6655 return _PyUnicode_AsASCIIString(unicode, NULL);
Guido van Rossumd57fd912000-03-10 22:53:23 +00006656}
6657
Victor Stinner99b95382011-07-04 14:23:54 +02006658#ifdef HAVE_MBCS
Guido van Rossum2ea3e142000-03-31 17:24:09 +00006659
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00006660/* --- MBCS codecs for Windows -------------------------------------------- */
Guido van Rossum2ea3e142000-03-31 17:24:09 +00006661
Hirokazu Yamamoto35302462009-03-21 13:23:27 +00006662#if SIZEOF_INT < SIZEOF_SIZE_T
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006663#define NEED_RETRY
6664#endif
6665
Victor Stinner3a50e702011-10-18 21:21:00 +02006666#ifndef WC_ERR_INVALID_CHARS
6667# define WC_ERR_INVALID_CHARS 0x0080
6668#endif
6669
6670static char*
6671code_page_name(UINT code_page, PyObject **obj)
6672{
6673 *obj = NULL;
6674 if (code_page == CP_ACP)
6675 return "mbcs";
6676 if (code_page == CP_UTF7)
6677 return "CP_UTF7";
6678 if (code_page == CP_UTF8)
6679 return "CP_UTF8";
6680
6681 *obj = PyBytes_FromFormat("cp%u", code_page);
6682 if (*obj == NULL)
6683 return NULL;
6684 return PyBytes_AS_STRING(*obj);
6685}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006686
Alexander Belopolsky40018472011-02-26 01:02:56 +00006687static int
Victor Stinner3a50e702011-10-18 21:21:00 +02006688is_dbcs_lead_byte(UINT code_page, const char *s, int offset)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006689{
6690 const char *curr = s + offset;
Victor Stinner3a50e702011-10-18 21:21:00 +02006691 const char *prev;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006692
Victor Stinner3a50e702011-10-18 21:21:00 +02006693 if (!IsDBCSLeadByteEx(code_page, *curr))
6694 return 0;
6695
6696 prev = CharPrevExA(code_page, s, curr, 0);
6697 if (prev == curr)
6698 return 1;
6699 /* FIXME: This code is limited to "true" double-byte encodings,
6700 as it assumes an incomplete character consists of a single
6701 byte. */
6702 if (curr - prev == 2)
6703 return 1;
6704 if (!IsDBCSLeadByteEx(code_page, *prev))
6705 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006706 return 0;
6707}
6708
Victor Stinner3a50e702011-10-18 21:21:00 +02006709static DWORD
6710decode_code_page_flags(UINT code_page)
6711{
6712 if (code_page == CP_UTF7) {
6713 /* The CP_UTF7 decoder only supports flags=0 */
6714 return 0;
6715 }
6716 else
6717 return MB_ERR_INVALID_CHARS;
6718}
6719
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006720/*
Victor Stinner3a50e702011-10-18 21:21:00 +02006721 * Decode a byte string from a Windows code page into unicode object in strict
6722 * mode.
6723 *
6724 * Returns consumed size if succeed, returns -2 on decode error, or raise a
6725 * WindowsError and returns -1 on other error.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006726 */
Alexander Belopolsky40018472011-02-26 01:02:56 +00006727static int
Victor Stinner3a50e702011-10-18 21:21:00 +02006728decode_code_page_strict(UINT code_page,
Victor Stinner76a31a62011-11-04 00:05:13 +01006729 PyObject **v,
Victor Stinner3a50e702011-10-18 21:21:00 +02006730 const char *in,
6731 int insize)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006732{
Victor Stinner3a50e702011-10-18 21:21:00 +02006733 const DWORD flags = decode_code_page_flags(code_page);
Victor Stinner24729f32011-11-10 20:31:37 +01006734 wchar_t *out;
Victor Stinner3a50e702011-10-18 21:21:00 +02006735 DWORD outsize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006736
6737 /* First get the size of the result */
Victor Stinner3a50e702011-10-18 21:21:00 +02006738 assert(insize > 0);
6739 outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0);
6740 if (outsize <= 0)
6741 goto error;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006742
6743 if (*v == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00006744 /* Create unicode object */
Victor Stinnerab595942011-12-17 04:59:06 +01006745 /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */
Victor Stinner76a31a62011-11-04 00:05:13 +01006746 *v = (PyObject*)_PyUnicode_New(outsize);
Benjamin Peterson29060642009-01-31 22:14:21 +00006747 if (*v == NULL)
6748 return -1;
Victor Stinner3a50e702011-10-18 21:21:00 +02006749 out = PyUnicode_AS_UNICODE(*v);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006750 }
6751 else {
Benjamin Peterson29060642009-01-31 22:14:21 +00006752 /* Extend unicode object */
Victor Stinner3a50e702011-10-18 21:21:00 +02006753 Py_ssize_t n = PyUnicode_GET_SIZE(*v);
Victor Stinner16e6a802011-12-12 13:24:15 +01006754 if (unicode_resize(v, n + outsize) < 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00006755 return -1;
Victor Stinner3a50e702011-10-18 21:21:00 +02006756 out = PyUnicode_AS_UNICODE(*v) + n;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006757 }
6758
6759 /* Do the conversion */
Victor Stinner3a50e702011-10-18 21:21:00 +02006760 outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize);
6761 if (outsize <= 0)
6762 goto error;
6763 return insize;
Victor Stinner554f3f02010-06-16 23:33:54 +00006764
Victor Stinner3a50e702011-10-18 21:21:00 +02006765error:
6766 if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION)
6767 return -2;
6768 PyErr_SetFromWindowsErr(0);
Victor Stinner554f3f02010-06-16 23:33:54 +00006769 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006770}
6771
Victor Stinner3a50e702011-10-18 21:21:00 +02006772/*
6773 * Decode a byte string from a code page into unicode object with an error
6774 * handler.
6775 *
6776 * Returns consumed size if succeed, or raise a WindowsError or
6777 * UnicodeDecodeError exception and returns -1 on error.
6778 */
6779static int
6780decode_code_page_errors(UINT code_page,
Victor Stinner76a31a62011-11-04 00:05:13 +01006781 PyObject **v,
6782 const char *in, const int size,
Victor Stinner3a50e702011-10-18 21:21:00 +02006783 const char *errors)
6784{
6785 const char *startin = in;
6786 const char *endin = in + size;
6787 const DWORD flags = decode_code_page_flags(code_page);
6788 /* Ideally, we should get reason from FormatMessage. This is the Windows
6789 2000 English version of the message. */
6790 const char *reason = "No mapping for the Unicode character exists "
6791 "in the target code page.";
6792 /* each step cannot decode more than 1 character, but a character can be
6793 represented as a surrogate pair */
6794 wchar_t buffer[2], *startout, *out;
6795 int insize, outsize;
6796 PyObject *errorHandler = NULL;
6797 PyObject *exc = NULL;
6798 PyObject *encoding_obj = NULL;
6799 char *encoding;
6800 DWORD err;
6801 int ret = -1;
6802
6803 assert(size > 0);
6804
6805 encoding = code_page_name(code_page, &encoding_obj);
6806 if (encoding == NULL)
6807 return -1;
6808
6809 if (errors == NULL || strcmp(errors, "strict") == 0) {
6810 /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a
6811 UnicodeDecodeError. */
6812 make_decode_exception(&exc, encoding, in, size, 0, 0, reason);
6813 if (exc != NULL) {
6814 PyCodec_StrictErrors(exc);
6815 Py_CLEAR(exc);
6816 }
6817 goto error;
6818 }
6819
6820 if (*v == NULL) {
6821 /* Create unicode object */
6822 if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) {
6823 PyErr_NoMemory();
6824 goto error;
6825 }
Victor Stinnerab595942011-12-17 04:59:06 +01006826 /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */
Victor Stinner76a31a62011-11-04 00:05:13 +01006827 *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer));
Victor Stinner3a50e702011-10-18 21:21:00 +02006828 if (*v == NULL)
6829 goto error;
6830 startout = PyUnicode_AS_UNICODE(*v);
6831 }
6832 else {
6833 /* Extend unicode object */
6834 Py_ssize_t n = PyUnicode_GET_SIZE(*v);
6835 if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) {
6836 PyErr_NoMemory();
6837 goto error;
6838 }
Victor Stinner16e6a802011-12-12 13:24:15 +01006839 if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0)
Victor Stinner3a50e702011-10-18 21:21:00 +02006840 goto error;
6841 startout = PyUnicode_AS_UNICODE(*v) + n;
6842 }
6843
6844 /* Decode the byte string character per character */
6845 out = startout;
6846 while (in < endin)
6847 {
6848 /* Decode a character */
6849 insize = 1;
6850 do
6851 {
6852 outsize = MultiByteToWideChar(code_page, flags,
6853 in, insize,
6854 buffer, Py_ARRAY_LENGTH(buffer));
6855 if (outsize > 0)
6856 break;
6857 err = GetLastError();
6858 if (err != ERROR_NO_UNICODE_TRANSLATION
6859 && err != ERROR_INSUFFICIENT_BUFFER)
6860 {
6861 PyErr_SetFromWindowsErr(0);
6862 goto error;
6863 }
6864 insize++;
6865 }
6866 /* 4=maximum length of a UTF-8 sequence */
6867 while (insize <= 4 && (in + insize) <= endin);
6868
6869 if (outsize <= 0) {
6870 Py_ssize_t startinpos, endinpos, outpos;
6871
6872 startinpos = in - startin;
6873 endinpos = startinpos + 1;
6874 outpos = out - PyUnicode_AS_UNICODE(*v);
6875 if (unicode_decode_call_errorhandler(
6876 errors, &errorHandler,
6877 encoding, reason,
6878 &startin, &endin, &startinpos, &endinpos, &exc, &in,
Victor Stinner596a6c42011-11-09 00:02:18 +01006879 v, &outpos))
Victor Stinner3a50e702011-10-18 21:21:00 +02006880 {
6881 goto error;
6882 }
Victor Stinner596a6c42011-11-09 00:02:18 +01006883 out = PyUnicode_AS_UNICODE(*v) + outpos;
Victor Stinner3a50e702011-10-18 21:21:00 +02006884 }
6885 else {
6886 in += insize;
6887 memcpy(out, buffer, outsize * sizeof(wchar_t));
6888 out += outsize;
6889 }
6890 }
6891
6892 /* write a NUL character at the end */
6893 *out = 0;
6894
6895 /* Extend unicode object */
6896 outsize = out - startout;
6897 assert(outsize <= PyUnicode_WSTR_LENGTH(*v));
Victor Stinner16e6a802011-12-12 13:24:15 +01006898 if (unicode_resize(v, outsize) < 0)
Victor Stinner3a50e702011-10-18 21:21:00 +02006899 goto error;
Victor Stinner76a31a62011-11-04 00:05:13 +01006900 ret = size;
Victor Stinner3a50e702011-10-18 21:21:00 +02006901
6902error:
6903 Py_XDECREF(encoding_obj);
6904 Py_XDECREF(errorHandler);
6905 Py_XDECREF(exc);
6906 return ret;
6907}
6908
Victor Stinner3a50e702011-10-18 21:21:00 +02006909static PyObject *
6910decode_code_page_stateful(int code_page,
Victor Stinner76a31a62011-11-04 00:05:13 +01006911 const char *s, Py_ssize_t size,
6912 const char *errors, Py_ssize_t *consumed)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006913{
Victor Stinner76a31a62011-11-04 00:05:13 +01006914 PyObject *v = NULL;
6915 int chunk_size, final, converted, done;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006916
Victor Stinner3a50e702011-10-18 21:21:00 +02006917 if (code_page < 0) {
6918 PyErr_SetString(PyExc_ValueError, "invalid code page number");
6919 return NULL;
6920 }
6921
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006922 if (consumed)
Benjamin Peterson29060642009-01-31 22:14:21 +00006923 *consumed = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006924
Victor Stinner76a31a62011-11-04 00:05:13 +01006925 do
6926 {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006927#ifdef NEED_RETRY
Victor Stinner76a31a62011-11-04 00:05:13 +01006928 if (size > INT_MAX) {
6929 chunk_size = INT_MAX;
6930 final = 0;
6931 done = 0;
6932 }
6933 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006934#endif
Victor Stinner76a31a62011-11-04 00:05:13 +01006935 {
6936 chunk_size = (int)size;
6937 final = (consumed == NULL);
6938 done = 1;
6939 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006940
Victor Stinner76a31a62011-11-04 00:05:13 +01006941 /* Skip trailing lead-byte unless 'final' is set */
6942 if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1))
6943 --chunk_size;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006944
Victor Stinner76a31a62011-11-04 00:05:13 +01006945 if (chunk_size == 0 && done) {
6946 if (v != NULL)
6947 break;
6948 Py_INCREF(unicode_empty);
6949 return unicode_empty;
6950 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006951
Victor Stinner76a31a62011-11-04 00:05:13 +01006952
6953 converted = decode_code_page_strict(code_page, &v,
6954 s, chunk_size);
6955 if (converted == -2)
6956 converted = decode_code_page_errors(code_page, &v,
6957 s, chunk_size,
6958 errors);
6959 assert(converted != 0);
6960
6961 if (converted < 0) {
6962 Py_XDECREF(v);
6963 return NULL;
6964 }
6965
6966 if (consumed)
6967 *consumed += converted;
6968
6969 s += converted;
6970 size -= converted;
6971 } while (!done);
Victor Stinner3a50e702011-10-18 21:21:00 +02006972
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01006973 return unicode_result(v);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006974}
6975
Alexander Belopolsky40018472011-02-26 01:02:56 +00006976PyObject *
Victor Stinner3a50e702011-10-18 21:21:00 +02006977PyUnicode_DecodeCodePageStateful(int code_page,
6978 const char *s,
6979 Py_ssize_t size,
6980 const char *errors,
6981 Py_ssize_t *consumed)
6982{
6983 return decode_code_page_stateful(code_page, s, size, errors, consumed);
6984}
6985
6986PyObject *
6987PyUnicode_DecodeMBCSStateful(const char *s,
6988 Py_ssize_t size,
6989 const char *errors,
6990 Py_ssize_t *consumed)
6991{
6992 return decode_code_page_stateful(CP_ACP, s, size, errors, consumed);
6993}
6994
6995PyObject *
Alexander Belopolsky40018472011-02-26 01:02:56 +00006996PyUnicode_DecodeMBCS(const char *s,
6997 Py_ssize_t size,
6998 const char *errors)
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00006999{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007000 return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL);
7001}
7002
Victor Stinner3a50e702011-10-18 21:21:00 +02007003static DWORD
7004encode_code_page_flags(UINT code_page, const char *errors)
7005{
7006 if (code_page == CP_UTF8) {
7007 if (winver.dwMajorVersion >= 6)
7008 /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista
7009 and later */
7010 return WC_ERR_INVALID_CHARS;
7011 else
7012 /* CP_UTF8 only supports flags=0 on Windows older than Vista */
7013 return 0;
7014 }
7015 else if (code_page == CP_UTF7) {
7016 /* CP_UTF7 only supports flags=0 */
7017 return 0;
7018 }
7019 else {
7020 if (errors != NULL && strcmp(errors, "replace") == 0)
7021 return 0;
7022 else
7023 return WC_NO_BEST_FIT_CHARS;
7024 }
7025}
7026
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007027/*
Victor Stinner3a50e702011-10-18 21:21:00 +02007028 * Encode a Unicode string to a Windows code page into a byte string in strict
7029 * mode.
7030 *
7031 * Returns consumed characters if succeed, returns -2 on encode error, or raise
7032 * a WindowsError and returns -1 on other error.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007033 */
Alexander Belopolsky40018472011-02-26 01:02:56 +00007034static int
Victor Stinner3a50e702011-10-18 21:21:00 +02007035encode_code_page_strict(UINT code_page, PyObject **outbytes,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007036 PyObject *unicode, Py_ssize_t offset, int len,
Victor Stinner3a50e702011-10-18 21:21:00 +02007037 const char* errors)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007038{
Victor Stinner554f3f02010-06-16 23:33:54 +00007039 BOOL usedDefaultChar = FALSE;
Victor Stinner3a50e702011-10-18 21:21:00 +02007040 BOOL *pusedDefaultChar = &usedDefaultChar;
7041 int outsize;
Victor Stinner554f3f02010-06-16 23:33:54 +00007042 PyObject *exc = NULL;
Victor Stinner24729f32011-11-10 20:31:37 +01007043 wchar_t *p;
Victor Stinner2fc507f2011-11-04 20:06:39 +01007044 Py_ssize_t size;
Victor Stinner3a50e702011-10-18 21:21:00 +02007045 const DWORD flags = encode_code_page_flags(code_page, NULL);
7046 char *out;
Victor Stinner2fc507f2011-11-04 20:06:39 +01007047 /* Create a substring so that we can get the UTF-16 representation
7048 of just the slice under consideration. */
7049 PyObject *substring;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007050
Martin v. Löwis3d325192011-11-04 18:23:06 +01007051 assert(len > 0);
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007052
Victor Stinner3a50e702011-10-18 21:21:00 +02007053 if (code_page != CP_UTF8 && code_page != CP_UTF7)
Victor Stinner554f3f02010-06-16 23:33:54 +00007054 pusedDefaultChar = &usedDefaultChar;
Victor Stinner3a50e702011-10-18 21:21:00 +02007055 else
Victor Stinner554f3f02010-06-16 23:33:54 +00007056 pusedDefaultChar = NULL;
Victor Stinner554f3f02010-06-16 23:33:54 +00007057
Victor Stinner2fc507f2011-11-04 20:06:39 +01007058 substring = PyUnicode_Substring(unicode, offset, offset+len);
7059 if (substring == NULL)
7060 return -1;
7061 p = PyUnicode_AsUnicodeAndSize(substring, &size);
7062 if (p == NULL) {
7063 Py_DECREF(substring);
7064 return -1;
7065 }
Martin v. Löwis3d325192011-11-04 18:23:06 +01007066
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007067 /* First get the size of the result */
Victor Stinner3a50e702011-10-18 21:21:00 +02007068 outsize = WideCharToMultiByte(code_page, flags,
7069 p, size,
7070 NULL, 0,
7071 NULL, pusedDefaultChar);
7072 if (outsize <= 0)
7073 goto error;
7074 /* If we used a default char, then we failed! */
Victor Stinner2fc507f2011-11-04 20:06:39 +01007075 if (pusedDefaultChar && *pusedDefaultChar) {
7076 Py_DECREF(substring);
Victor Stinner3a50e702011-10-18 21:21:00 +02007077 return -2;
Victor Stinner2fc507f2011-11-04 20:06:39 +01007078 }
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007079
Victor Stinner3a50e702011-10-18 21:21:00 +02007080 if (*outbytes == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007081 /* Create string object */
Victor Stinner3a50e702011-10-18 21:21:00 +02007082 *outbytes = PyBytes_FromStringAndSize(NULL, outsize);
Victor Stinner2fc507f2011-11-04 20:06:39 +01007083 if (*outbytes == NULL) {
7084 Py_DECREF(substring);
Benjamin Peterson29060642009-01-31 22:14:21 +00007085 return -1;
Victor Stinner2fc507f2011-11-04 20:06:39 +01007086 }
Victor Stinner3a50e702011-10-18 21:21:00 +02007087 out = PyBytes_AS_STRING(*outbytes);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007088 }
7089 else {
Benjamin Peterson29060642009-01-31 22:14:21 +00007090 /* Extend string object */
Victor Stinner3a50e702011-10-18 21:21:00 +02007091 const Py_ssize_t n = PyBytes_Size(*outbytes);
7092 if (outsize > PY_SSIZE_T_MAX - n) {
7093 PyErr_NoMemory();
Victor Stinner2fc507f2011-11-04 20:06:39 +01007094 Py_DECREF(substring);
Benjamin Peterson29060642009-01-31 22:14:21 +00007095 return -1;
Victor Stinner3a50e702011-10-18 21:21:00 +02007096 }
Victor Stinner2fc507f2011-11-04 20:06:39 +01007097 if (_PyBytes_Resize(outbytes, n + outsize) < 0) {
7098 Py_DECREF(substring);
Victor Stinner3a50e702011-10-18 21:21:00 +02007099 return -1;
Victor Stinner2fc507f2011-11-04 20:06:39 +01007100 }
Victor Stinner3a50e702011-10-18 21:21:00 +02007101 out = PyBytes_AS_STRING(*outbytes) + n;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007102 }
7103
7104 /* Do the conversion */
Victor Stinner3a50e702011-10-18 21:21:00 +02007105 outsize = WideCharToMultiByte(code_page, flags,
7106 p, size,
7107 out, outsize,
7108 NULL, pusedDefaultChar);
Victor Stinner2fc507f2011-11-04 20:06:39 +01007109 Py_CLEAR(substring);
Victor Stinner3a50e702011-10-18 21:21:00 +02007110 if (outsize <= 0)
7111 goto error;
7112 if (pusedDefaultChar && *pusedDefaultChar)
7113 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007114 return 0;
Victor Stinner554f3f02010-06-16 23:33:54 +00007115
Victor Stinner3a50e702011-10-18 21:21:00 +02007116error:
Victor Stinner2fc507f2011-11-04 20:06:39 +01007117 Py_XDECREF(substring);
Victor Stinner3a50e702011-10-18 21:21:00 +02007118 if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION)
7119 return -2;
7120 PyErr_SetFromWindowsErr(0);
Victor Stinner554f3f02010-06-16 23:33:54 +00007121 return -1;
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007122}
7123
Victor Stinner3a50e702011-10-18 21:21:00 +02007124/*
7125 * Encode a Unicode string to a Windows code page into a byte string using a
7126 * error handler.
7127 *
7128 * Returns consumed characters if succeed, or raise a WindowsError and returns
7129 * -1 on other error.
7130 */
7131static int
7132encode_code_page_errors(UINT code_page, PyObject **outbytes,
Victor Stinner7581cef2011-11-03 22:32:33 +01007133 PyObject *unicode, Py_ssize_t unicode_offset,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007134 Py_ssize_t insize, const char* errors)
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007135{
Victor Stinner3a50e702011-10-18 21:21:00 +02007136 const DWORD flags = encode_code_page_flags(code_page, errors);
Victor Stinner2fc507f2011-11-04 20:06:39 +01007137 Py_ssize_t pos = unicode_offset;
7138 Py_ssize_t endin = unicode_offset + insize;
Victor Stinner3a50e702011-10-18 21:21:00 +02007139 /* Ideally, we should get reason from FormatMessage. This is the Windows
7140 2000 English version of the message. */
7141 const char *reason = "invalid character";
7142 /* 4=maximum length of a UTF-8 sequence */
7143 char buffer[4];
7144 BOOL usedDefaultChar = FALSE, *pusedDefaultChar;
7145 Py_ssize_t outsize;
7146 char *out;
Victor Stinner3a50e702011-10-18 21:21:00 +02007147 PyObject *errorHandler = NULL;
7148 PyObject *exc = NULL;
7149 PyObject *encoding_obj = NULL;
7150 char *encoding;
Martin v. Löwis3d325192011-11-04 18:23:06 +01007151 Py_ssize_t newpos, newoutsize;
Victor Stinner3a50e702011-10-18 21:21:00 +02007152 PyObject *rep;
7153 int ret = -1;
7154
7155 assert(insize > 0);
7156
7157 encoding = code_page_name(code_page, &encoding_obj);
7158 if (encoding == NULL)
7159 return -1;
7160
7161 if (errors == NULL || strcmp(errors, "strict") == 0) {
7162 /* The last error was ERROR_NO_UNICODE_TRANSLATION,
7163 then we raise a UnicodeEncodeError. */
Martin v. Löwis12be46c2011-11-04 19:04:15 +01007164 make_encode_exception(&exc, encoding, unicode, 0, 0, reason);
Victor Stinner3a50e702011-10-18 21:21:00 +02007165 if (exc != NULL) {
7166 PyCodec_StrictErrors(exc);
7167 Py_DECREF(exc);
7168 }
7169 Py_XDECREF(encoding_obj);
7170 return -1;
7171 }
7172
7173 if (code_page != CP_UTF8 && code_page != CP_UTF7)
7174 pusedDefaultChar = &usedDefaultChar;
7175 else
7176 pusedDefaultChar = NULL;
7177
7178 if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) {
7179 PyErr_NoMemory();
7180 goto error;
7181 }
7182 outsize = insize * Py_ARRAY_LENGTH(buffer);
7183
7184 if (*outbytes == NULL) {
7185 /* Create string object */
7186 *outbytes = PyBytes_FromStringAndSize(NULL, outsize);
7187 if (*outbytes == NULL)
7188 goto error;
7189 out = PyBytes_AS_STRING(*outbytes);
7190 }
7191 else {
7192 /* Extend string object */
7193 Py_ssize_t n = PyBytes_Size(*outbytes);
7194 if (n > PY_SSIZE_T_MAX - outsize) {
7195 PyErr_NoMemory();
7196 goto error;
7197 }
7198 if (_PyBytes_Resize(outbytes, n + outsize) < 0)
7199 goto error;
7200 out = PyBytes_AS_STRING(*outbytes) + n;
7201 }
7202
7203 /* Encode the string character per character */
Martin v. Löwis3d325192011-11-04 18:23:06 +01007204 while (pos < endin)
Victor Stinner3a50e702011-10-18 21:21:00 +02007205 {
Victor Stinner2fc507f2011-11-04 20:06:39 +01007206 Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos);
7207 wchar_t chars[2];
7208 int charsize;
7209 if (ch < 0x10000) {
7210 chars[0] = (wchar_t)ch;
7211 charsize = 1;
7212 }
7213 else {
7214 ch -= 0x10000;
7215 chars[0] = 0xd800 + (ch >> 10);
7216 chars[1] = 0xdc00 + (ch & 0x3ff);
7217 charsize = 2;
7218 }
7219
Victor Stinner3a50e702011-10-18 21:21:00 +02007220 outsize = WideCharToMultiByte(code_page, flags,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007221 chars, charsize,
Victor Stinner3a50e702011-10-18 21:21:00 +02007222 buffer, Py_ARRAY_LENGTH(buffer),
7223 NULL, pusedDefaultChar);
7224 if (outsize > 0) {
7225 if (pusedDefaultChar == NULL || !(*pusedDefaultChar))
7226 {
Martin v. Löwis3d325192011-11-04 18:23:06 +01007227 pos++;
Victor Stinner3a50e702011-10-18 21:21:00 +02007228 memcpy(out, buffer, outsize);
7229 out += outsize;
7230 continue;
7231 }
7232 }
7233 else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) {
7234 PyErr_SetFromWindowsErr(0);
7235 goto error;
7236 }
7237
Victor Stinner3a50e702011-10-18 21:21:00 +02007238 rep = unicode_encode_call_errorhandler(
7239 errors, &errorHandler, encoding, reason,
Victor Stinner7581cef2011-11-03 22:32:33 +01007240 unicode, &exc,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007241 pos, pos + 1, &newpos);
Victor Stinner3a50e702011-10-18 21:21:00 +02007242 if (rep == NULL)
7243 goto error;
Martin v. Löwis3d325192011-11-04 18:23:06 +01007244 pos = newpos;
Victor Stinner3a50e702011-10-18 21:21:00 +02007245
7246 if (PyBytes_Check(rep)) {
7247 outsize = PyBytes_GET_SIZE(rep);
7248 if (outsize != 1) {
7249 Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes);
7250 newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1);
7251 if (_PyBytes_Resize(outbytes, newoutsize) < 0) {
7252 Py_DECREF(rep);
7253 goto error;
7254 }
7255 out = PyBytes_AS_STRING(*outbytes) + offset;
7256 }
7257 memcpy(out, PyBytes_AS_STRING(rep), outsize);
7258 out += outsize;
7259 }
7260 else {
7261 Py_ssize_t i;
7262 enum PyUnicode_Kind kind;
7263 void *data;
7264
Benjamin Petersonbac79492012-01-14 13:34:47 -05007265 if (PyUnicode_READY(rep) == -1) {
Victor Stinner3a50e702011-10-18 21:21:00 +02007266 Py_DECREF(rep);
7267 goto error;
7268 }
7269
7270 outsize = PyUnicode_GET_LENGTH(rep);
7271 if (outsize != 1) {
7272 Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes);
7273 newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1);
7274 if (_PyBytes_Resize(outbytes, newoutsize) < 0) {
7275 Py_DECREF(rep);
7276 goto error;
7277 }
7278 out = PyBytes_AS_STRING(*outbytes) + offset;
7279 }
7280 kind = PyUnicode_KIND(rep);
7281 data = PyUnicode_DATA(rep);
7282 for (i=0; i < outsize; i++) {
7283 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
7284 if (ch > 127) {
Martin v. Löwis12be46c2011-11-04 19:04:15 +01007285 raise_encode_exception(&exc,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007286 encoding, unicode,
7287 pos, pos + 1,
Victor Stinner3a50e702011-10-18 21:21:00 +02007288 "unable to encode error handler result to ASCII");
7289 Py_DECREF(rep);
7290 goto error;
7291 }
7292 *out = (unsigned char)ch;
7293 out++;
7294 }
7295 }
7296 Py_DECREF(rep);
7297 }
7298 /* write a NUL byte */
7299 *out = 0;
7300 outsize = out - PyBytes_AS_STRING(*outbytes);
7301 assert(outsize <= PyBytes_GET_SIZE(*outbytes));
7302 if (_PyBytes_Resize(outbytes, outsize) < 0)
7303 goto error;
7304 ret = 0;
7305
7306error:
7307 Py_XDECREF(encoding_obj);
7308 Py_XDECREF(errorHandler);
7309 Py_XDECREF(exc);
7310 return ret;
7311}
7312
Victor Stinner3a50e702011-10-18 21:21:00 +02007313static PyObject *
7314encode_code_page(int code_page,
Victor Stinner7581cef2011-11-03 22:32:33 +01007315 PyObject *unicode,
Victor Stinner3a50e702011-10-18 21:21:00 +02007316 const char *errors)
7317{
Martin v. Löwis3d325192011-11-04 18:23:06 +01007318 Py_ssize_t len;
Victor Stinner3a50e702011-10-18 21:21:00 +02007319 PyObject *outbytes = NULL;
Victor Stinner7581cef2011-11-03 22:32:33 +01007320 Py_ssize_t offset;
Victor Stinner76a31a62011-11-04 00:05:13 +01007321 int chunk_len, ret, done;
Victor Stinner7581cef2011-11-03 22:32:33 +01007322
Benjamin Petersonbac79492012-01-14 13:34:47 -05007323 if (PyUnicode_READY(unicode) == -1)
Victor Stinner2fc507f2011-11-04 20:06:39 +01007324 return NULL;
7325 len = PyUnicode_GET_LENGTH(unicode);
Guido van Rossum03e29f12000-05-04 15:52:20 +00007326
Victor Stinner3a50e702011-10-18 21:21:00 +02007327 if (code_page < 0) {
7328 PyErr_SetString(PyExc_ValueError, "invalid code page number");
7329 return NULL;
7330 }
7331
Martin v. Löwis3d325192011-11-04 18:23:06 +01007332 if (len == 0)
Victor Stinner76a31a62011-11-04 00:05:13 +01007333 return PyBytes_FromStringAndSize(NULL, 0);
7334
Victor Stinner7581cef2011-11-03 22:32:33 +01007335 offset = 0;
7336 do
7337 {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007338#ifdef NEED_RETRY
Victor Stinner2fc507f2011-11-04 20:06:39 +01007339 /* UTF-16 encoding may double the size, so use only INT_MAX/2
Martin v. Löwis3d325192011-11-04 18:23:06 +01007340 chunks. */
7341 if (len > INT_MAX/2) {
7342 chunk_len = INT_MAX/2;
Victor Stinner76a31a62011-11-04 00:05:13 +01007343 done = 0;
7344 }
Victor Stinner7581cef2011-11-03 22:32:33 +01007345 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007346#endif
Victor Stinner76a31a62011-11-04 00:05:13 +01007347 {
Martin v. Löwis3d325192011-11-04 18:23:06 +01007348 chunk_len = (int)len;
Victor Stinner76a31a62011-11-04 00:05:13 +01007349 done = 1;
7350 }
Victor Stinner2fc507f2011-11-04 20:06:39 +01007351
Victor Stinner76a31a62011-11-04 00:05:13 +01007352 ret = encode_code_page_strict(code_page, &outbytes,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007353 unicode, offset, chunk_len,
Victor Stinner76a31a62011-11-04 00:05:13 +01007354 errors);
7355 if (ret == -2)
7356 ret = encode_code_page_errors(code_page, &outbytes,
7357 unicode, offset,
Martin v. Löwis3d325192011-11-04 18:23:06 +01007358 chunk_len, errors);
Victor Stinner7581cef2011-11-03 22:32:33 +01007359 if (ret < 0) {
7360 Py_XDECREF(outbytes);
7361 return NULL;
7362 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007363
Victor Stinner7581cef2011-11-03 22:32:33 +01007364 offset += chunk_len;
Martin v. Löwis3d325192011-11-04 18:23:06 +01007365 len -= chunk_len;
Victor Stinner76a31a62011-11-04 00:05:13 +01007366 } while (!done);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007367
Victor Stinner3a50e702011-10-18 21:21:00 +02007368 return outbytes;
7369}
7370
7371PyObject *
7372PyUnicode_EncodeMBCS(const Py_UNICODE *p,
7373 Py_ssize_t size,
7374 const char *errors)
7375{
Victor Stinner7581cef2011-11-03 22:32:33 +01007376 PyObject *unicode, *res;
7377 unicode = PyUnicode_FromUnicode(p, size);
7378 if (unicode == NULL)
7379 return NULL;
7380 res = encode_code_page(CP_ACP, unicode, errors);
7381 Py_DECREF(unicode);
7382 return res;
Victor Stinner3a50e702011-10-18 21:21:00 +02007383}
7384
7385PyObject *
7386PyUnicode_EncodeCodePage(int code_page,
7387 PyObject *unicode,
7388 const char *errors)
7389{
Victor Stinner7581cef2011-11-03 22:32:33 +01007390 return encode_code_page(code_page, unicode, errors);
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007391}
Guido van Rossum2ea3e142000-03-31 17:24:09 +00007392
Alexander Belopolsky40018472011-02-26 01:02:56 +00007393PyObject *
7394PyUnicode_AsMBCSString(PyObject *unicode)
Mark Hammond0ccda1e2003-07-01 00:13:27 +00007395{
7396 if (!PyUnicode_Check(unicode)) {
7397 PyErr_BadArgument();
7398 return NULL;
7399 }
Victor Stinner7581cef2011-11-03 22:32:33 +01007400 return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL);
Mark Hammond0ccda1e2003-07-01 00:13:27 +00007401}
7402
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007403#undef NEED_RETRY
7404
Victor Stinner99b95382011-07-04 14:23:54 +02007405#endif /* HAVE_MBCS */
Guido van Rossumb7a40ba2000-03-28 02:01:52 +00007406
Guido van Rossumd57fd912000-03-10 22:53:23 +00007407/* --- Character Mapping Codec -------------------------------------------- */
7408
Alexander Belopolsky40018472011-02-26 01:02:56 +00007409PyObject *
7410PyUnicode_DecodeCharmap(const char *s,
7411 Py_ssize_t size,
7412 PyObject *mapping,
7413 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00007414{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007415 const char *starts = s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00007416 Py_ssize_t startinpos;
7417 Py_ssize_t endinpos;
7418 Py_ssize_t outpos;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007419 const char *e;
Victor Stinner7931d9a2011-11-04 00:22:48 +01007420 PyObject *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00007421 Py_ssize_t extrachars = 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007422 PyObject *errorHandler = NULL;
7423 PyObject *exc = NULL;
Tim Petersced69f82003-09-16 20:30:58 +00007424
Guido van Rossumd57fd912000-03-10 22:53:23 +00007425 /* Default to Latin-1 */
7426 if (mapping == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00007427 return PyUnicode_DecodeLatin1(s, size, errors);
Guido van Rossumd57fd912000-03-10 22:53:23 +00007428
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007429 v = PyUnicode_New(size, 127);
Guido van Rossumd57fd912000-03-10 22:53:23 +00007430 if (v == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00007431 goto onError;
Guido van Rossumd57fd912000-03-10 22:53:23 +00007432 if (size == 0)
Victor Stinner7931d9a2011-11-04 00:22:48 +01007433 return v;
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007434 outpos = 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007435 e = s + size;
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00007436 if (PyUnicode_CheckExact(mapping)) {
Victor Stinnerebf3ba82011-11-10 20:30:22 +01007437 Py_ssize_t maplen;
7438 enum PyUnicode_Kind kind;
7439 void *data;
7440 Py_UCS4 x;
7441
Benjamin Petersonbac79492012-01-14 13:34:47 -05007442 if (PyUnicode_READY(mapping) == -1)
Victor Stinnerebf3ba82011-11-10 20:30:22 +01007443 return NULL;
7444
7445 maplen = PyUnicode_GET_LENGTH(mapping);
7446 data = PyUnicode_DATA(mapping);
7447 kind = PyUnicode_KIND(mapping);
Benjamin Peterson29060642009-01-31 22:14:21 +00007448 while (s < e) {
7449 unsigned char ch = *s;
Guido van Rossumd57fd912000-03-10 22:53:23 +00007450
Benjamin Peterson29060642009-01-31 22:14:21 +00007451 if (ch < maplen)
Victor Stinnerebf3ba82011-11-10 20:30:22 +01007452 x = PyUnicode_READ(kind, data, ch);
7453 else
7454 x = 0xfffe; /* invalid value */
Guido van Rossumd57fd912000-03-10 22:53:23 +00007455
Victor Stinnerebf3ba82011-11-10 20:30:22 +01007456 if (x == 0xfffe)
7457 {
Benjamin Peterson29060642009-01-31 22:14:21 +00007458 /* undefined mapping */
Benjamin Peterson29060642009-01-31 22:14:21 +00007459 startinpos = s-starts;
7460 endinpos = startinpos+1;
7461 if (unicode_decode_call_errorhandler(
7462 errors, &errorHandler,
7463 "charmap", "character maps to <undefined>",
7464 &starts, &e, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007465 &v, &outpos)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007466 goto onError;
7467 }
7468 continue;
7469 }
Victor Stinnerebf3ba82011-11-10 20:30:22 +01007470
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007471 if (unicode_putchar(&v, &outpos, x) < 0)
7472 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00007473 ++s;
Benjamin Peterson14339b62009-01-31 16:36:08 +00007474 }
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00007475 }
7476 else {
Benjamin Peterson29060642009-01-31 22:14:21 +00007477 while (s < e) {
7478 unsigned char ch = *s;
7479 PyObject *w, *x;
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00007480
Benjamin Peterson29060642009-01-31 22:14:21 +00007481 /* Get mapping (char ordinal -> integer, Unicode char or None) */
7482 w = PyLong_FromLong((long)ch);
7483 if (w == NULL)
7484 goto onError;
7485 x = PyObject_GetItem(mapping, w);
7486 Py_DECREF(w);
7487 if (x == NULL) {
7488 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
7489 /* No mapping found means: mapping is undefined. */
7490 PyErr_Clear();
7491 x = Py_None;
7492 Py_INCREF(x);
7493 } else
7494 goto onError;
7495 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00007496
Benjamin Peterson29060642009-01-31 22:14:21 +00007497 /* Apply mapping */
7498 if (PyLong_Check(x)) {
7499 long value = PyLong_AS_LONG(x);
7500 if (value < 0 || value > 65535) {
7501 PyErr_SetString(PyExc_TypeError,
7502 "character mapping must be in range(65536)");
7503 Py_DECREF(x);
7504 goto onError;
7505 }
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007506 if (unicode_putchar(&v, &outpos, value) < 0)
7507 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00007508 }
7509 else if (x == Py_None) {
7510 /* undefined mapping */
Benjamin Peterson29060642009-01-31 22:14:21 +00007511 startinpos = s-starts;
7512 endinpos = startinpos+1;
7513 if (unicode_decode_call_errorhandler(
7514 errors, &errorHandler,
7515 "charmap", "character maps to <undefined>",
7516 &starts, &e, &startinpos, &endinpos, &exc, &s,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007517 &v, &outpos)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007518 Py_DECREF(x);
7519 goto onError;
7520 }
7521 Py_DECREF(x);
7522 continue;
7523 }
7524 else if (PyUnicode_Check(x)) {
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007525 Py_ssize_t targetsize;
Benjamin Peterson14339b62009-01-31 16:36:08 +00007526
Benjamin Petersonbac79492012-01-14 13:34:47 -05007527 if (PyUnicode_READY(x) == -1)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007528 goto onError;
7529 targetsize = PyUnicode_GET_LENGTH(x);
7530
7531 if (targetsize == 1) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007532 /* 1-1 mapping */
Victor Stinner62aa4d02011-11-09 00:03:45 +01007533 if (unicode_putchar(&v, &outpos,
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007534 PyUnicode_READ_CHAR(x, 0)) < 0)
7535 goto onError;
7536 }
Benjamin Peterson29060642009-01-31 22:14:21 +00007537 else if (targetsize > 1) {
7538 /* 1-n mapping */
7539 if (targetsize > extrachars) {
7540 /* resize first */
Benjamin Peterson29060642009-01-31 22:14:21 +00007541 Py_ssize_t needed = (targetsize - extrachars) + \
7542 (targetsize << 2);
7543 extrachars += needed;
7544 /* XXX overflow detection missing */
Victor Stinner16e6a802011-12-12 13:24:15 +01007545 if (unicode_resize(&v,
7546 PyUnicode_GET_LENGTH(v) + needed) < 0)
7547 {
Benjamin Peterson29060642009-01-31 22:14:21 +00007548 Py_DECREF(x);
7549 goto onError;
7550 }
Benjamin Peterson29060642009-01-31 22:14:21 +00007551 }
Victor Stinner1b487b42012-05-03 12:29:04 +02007552 if (unicode_widen(&v, outpos, PyUnicode_MAX_CHAR_VALUE(x)) < 0)
Martin v. Löwise9b11c12011-11-08 17:35:34 +01007553 goto onError;
7554 PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize);
7555 outpos += targetsize;
Benjamin Peterson29060642009-01-31 22:14:21 +00007556 extrachars -= targetsize;
7557 }
7558 /* 1-0 mapping: skip the character */
7559 }
7560 else {
7561 /* wrong return value */
7562 PyErr_SetString(PyExc_TypeError,
7563 "character mapping must return integer, None or str");
Benjamin Peterson14339b62009-01-31 16:36:08 +00007564 Py_DECREF(x);
7565 goto onError;
7566 }
Benjamin Peterson29060642009-01-31 22:14:21 +00007567 Py_DECREF(x);
7568 ++s;
Benjamin Peterson14339b62009-01-31 16:36:08 +00007569 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00007570 }
Victor Stinner16e6a802011-12-12 13:24:15 +01007571 if (unicode_resize(&v, outpos) < 0)
Antoine Pitroua8f63c02011-11-08 18:37:16 +01007572 goto onError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007573 Py_XDECREF(errorHandler);
7574 Py_XDECREF(exc);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01007575 return unicode_result(v);
Tim Petersced69f82003-09-16 20:30:58 +00007576
Benjamin Peterson29060642009-01-31 22:14:21 +00007577 onError:
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007578 Py_XDECREF(errorHandler);
7579 Py_XDECREF(exc);
Guido van Rossumd57fd912000-03-10 22:53:23 +00007580 Py_XDECREF(v);
7581 return NULL;
7582}
7583
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007584/* Charmap encoding: the lookup table */
7585
Alexander Belopolsky40018472011-02-26 01:02:56 +00007586struct encoding_map {
Benjamin Peterson29060642009-01-31 22:14:21 +00007587 PyObject_HEAD
7588 unsigned char level1[32];
7589 int count2, count3;
7590 unsigned char level23[1];
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007591};
7592
7593static PyObject*
7594encoding_map_size(PyObject *obj, PyObject* args)
7595{
7596 struct encoding_map *map = (struct encoding_map*)obj;
Benjamin Peterson14339b62009-01-31 16:36:08 +00007597 return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 +
Benjamin Peterson29060642009-01-31 22:14:21 +00007598 128*map->count3);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007599}
7600
7601static PyMethodDef encoding_map_methods[] = {
Benjamin Peterson14339b62009-01-31 16:36:08 +00007602 {"size", encoding_map_size, METH_NOARGS,
Benjamin Peterson29060642009-01-31 22:14:21 +00007603 PyDoc_STR("Return the size (in bytes) of this object") },
7604 { 0 }
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007605};
7606
7607static void
7608encoding_map_dealloc(PyObject* o)
7609{
Benjamin Peterson14339b62009-01-31 16:36:08 +00007610 PyObject_FREE(o);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007611}
7612
7613static PyTypeObject EncodingMapType = {
Benjamin Peterson14339b62009-01-31 16:36:08 +00007614 PyVarObject_HEAD_INIT(NULL, 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00007615 "EncodingMap", /*tp_name*/
7616 sizeof(struct encoding_map), /*tp_basicsize*/
7617 0, /*tp_itemsize*/
7618 /* methods */
7619 encoding_map_dealloc, /*tp_dealloc*/
7620 0, /*tp_print*/
7621 0, /*tp_getattr*/
7622 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007623 0, /*tp_reserved*/
Benjamin Peterson29060642009-01-31 22:14:21 +00007624 0, /*tp_repr*/
7625 0, /*tp_as_number*/
7626 0, /*tp_as_sequence*/
7627 0, /*tp_as_mapping*/
7628 0, /*tp_hash*/
7629 0, /*tp_call*/
7630 0, /*tp_str*/
7631 0, /*tp_getattro*/
7632 0, /*tp_setattro*/
7633 0, /*tp_as_buffer*/
7634 Py_TPFLAGS_DEFAULT, /*tp_flags*/
7635 0, /*tp_doc*/
7636 0, /*tp_traverse*/
7637 0, /*tp_clear*/
7638 0, /*tp_richcompare*/
7639 0, /*tp_weaklistoffset*/
7640 0, /*tp_iter*/
7641 0, /*tp_iternext*/
7642 encoding_map_methods, /*tp_methods*/
7643 0, /*tp_members*/
7644 0, /*tp_getset*/
7645 0, /*tp_base*/
7646 0, /*tp_dict*/
7647 0, /*tp_descr_get*/
7648 0, /*tp_descr_set*/
7649 0, /*tp_dictoffset*/
7650 0, /*tp_init*/
7651 0, /*tp_alloc*/
7652 0, /*tp_new*/
7653 0, /*tp_free*/
7654 0, /*tp_is_gc*/
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007655};
7656
7657PyObject*
7658PyUnicode_BuildEncodingMap(PyObject* string)
7659{
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007660 PyObject *result;
7661 struct encoding_map *mresult;
7662 int i;
7663 int need_dict = 0;
7664 unsigned char level1[32];
7665 unsigned char level2[512];
7666 unsigned char *mlevel1, *mlevel2, *mlevel3;
7667 int count2 = 0, count3 = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007668 int kind;
7669 void *data;
7670 Py_UCS4 ch;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007671
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007672 if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) {
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007673 PyErr_BadArgument();
7674 return NULL;
7675 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007676 kind = PyUnicode_KIND(string);
7677 data = PyUnicode_DATA(string);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007678 memset(level1, 0xFF, sizeof level1);
7679 memset(level2, 0xFF, sizeof level2);
7680
7681 /* If there isn't a one-to-one mapping of NULL to \0,
7682 or if there are non-BMP characters, we need to use
7683 a mapping dictionary. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007684 if (PyUnicode_READ(kind, data, 0) != 0)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007685 need_dict = 1;
7686 for (i = 1; i < 256; i++) {
7687 int l1, l2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007688 ch = PyUnicode_READ(kind, data, i);
7689 if (ch == 0 || ch > 0xFFFF) {
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007690 need_dict = 1;
7691 break;
7692 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007693 if (ch == 0xFFFE)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007694 /* unmapped character */
7695 continue;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007696 l1 = ch >> 11;
7697 l2 = ch >> 7;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007698 if (level1[l1] == 0xFF)
7699 level1[l1] = count2++;
7700 if (level2[l2] == 0xFF)
Benjamin Peterson14339b62009-01-31 16:36:08 +00007701 level2[l2] = count3++;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007702 }
7703
7704 if (count2 >= 0xFF || count3 >= 0xFF)
7705 need_dict = 1;
7706
7707 if (need_dict) {
7708 PyObject *result = PyDict_New();
7709 PyObject *key, *value;
7710 if (!result)
7711 return NULL;
7712 for (i = 0; i < 256; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007713 key = PyLong_FromLong(PyUnicode_READ(kind, data, i));
Christian Heimes217cfd12007-12-02 14:31:20 +00007714 value = PyLong_FromLong(i);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007715 if (!key || !value)
7716 goto failed1;
7717 if (PyDict_SetItem(result, key, value) == -1)
7718 goto failed1;
7719 Py_DECREF(key);
7720 Py_DECREF(value);
7721 }
7722 return result;
7723 failed1:
7724 Py_XDECREF(key);
7725 Py_XDECREF(value);
7726 Py_DECREF(result);
7727 return NULL;
7728 }
7729
7730 /* Create a three-level trie */
7731 result = PyObject_MALLOC(sizeof(struct encoding_map) +
7732 16*count2 + 128*count3 - 1);
7733 if (!result)
7734 return PyErr_NoMemory();
7735 PyObject_Init(result, &EncodingMapType);
7736 mresult = (struct encoding_map*)result;
7737 mresult->count2 = count2;
7738 mresult->count3 = count3;
7739 mlevel1 = mresult->level1;
7740 mlevel2 = mresult->level23;
7741 mlevel3 = mresult->level23 + 16*count2;
7742 memcpy(mlevel1, level1, 32);
7743 memset(mlevel2, 0xFF, 16*count2);
7744 memset(mlevel3, 0, 128*count3);
7745 count3 = 0;
7746 for (i = 1; i < 256; i++) {
7747 int o1, o2, o3, i2, i3;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007748 if (PyUnicode_READ(kind, data, i) == 0xFFFE)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007749 /* unmapped character */
7750 continue;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007751 o1 = PyUnicode_READ(kind, data, i)>>11;
7752 o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007753 i2 = 16*mlevel1[o1] + o2;
7754 if (mlevel2[i2] == 0xFF)
7755 mlevel2[i2] = count3++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02007756 o3 = PyUnicode_READ(kind, data, i) & 0x7F;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007757 i3 = 128*mlevel2[i2] + o3;
7758 mlevel3[i3] = i;
7759 }
7760 return result;
7761}
7762
7763static int
Victor Stinner22168992011-11-20 17:09:18 +01007764encoding_map_lookup(Py_UCS4 c, PyObject *mapping)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007765{
7766 struct encoding_map *map = (struct encoding_map*)mapping;
7767 int l1 = c>>11;
7768 int l2 = (c>>7) & 0xF;
7769 int l3 = c & 0x7F;
7770 int i;
7771
Victor Stinner22168992011-11-20 17:09:18 +01007772 if (c > 0xFFFF)
Benjamin Peterson29060642009-01-31 22:14:21 +00007773 return -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007774 if (c == 0)
7775 return 0;
7776 /* level 1*/
7777 i = map->level1[l1];
7778 if (i == 0xFF) {
7779 return -1;
7780 }
7781 /* level 2*/
7782 i = map->level23[16*i+l2];
7783 if (i == 0xFF) {
7784 return -1;
7785 }
7786 /* level 3 */
7787 i = map->level23[16*map->count2 + 128*i + l3];
7788 if (i == 0) {
7789 return -1;
7790 }
7791 return i;
7792}
7793
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007794/* Lookup the character ch in the mapping. If the character
7795 can't be found, Py_None is returned (or NULL, if another
Fred Drakedb390c12005-10-28 14:39:47 +00007796 error occurred). */
Alexander Belopolsky40018472011-02-26 01:02:56 +00007797static PyObject *
Victor Stinner22168992011-11-20 17:09:18 +01007798charmapencode_lookup(Py_UCS4 c, PyObject *mapping)
Guido van Rossumd57fd912000-03-10 22:53:23 +00007799{
Christian Heimes217cfd12007-12-02 14:31:20 +00007800 PyObject *w = PyLong_FromLong((long)c);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007801 PyObject *x;
7802
7803 if (w == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00007804 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007805 x = PyObject_GetItem(mapping, w);
7806 Py_DECREF(w);
7807 if (x == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007808 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
7809 /* No mapping found means: mapping is undefined. */
7810 PyErr_Clear();
7811 x = Py_None;
7812 Py_INCREF(x);
7813 return x;
7814 } else
7815 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00007816 }
Walter Dörwaldadc72742003-01-08 22:01:33 +00007817 else if (x == Py_None)
Benjamin Peterson29060642009-01-31 22:14:21 +00007818 return x;
Christian Heimes217cfd12007-12-02 14:31:20 +00007819 else if (PyLong_Check(x)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007820 long value = PyLong_AS_LONG(x);
7821 if (value < 0 || value > 255) {
7822 PyErr_SetString(PyExc_TypeError,
7823 "character mapping must be in range(256)");
7824 Py_DECREF(x);
7825 return NULL;
7826 }
7827 return x;
Guido van Rossumd57fd912000-03-10 22:53:23 +00007828 }
Christian Heimes72b710a2008-05-26 13:28:38 +00007829 else if (PyBytes_Check(x))
Benjamin Peterson29060642009-01-31 22:14:21 +00007830 return x;
Guido van Rossumd57fd912000-03-10 22:53:23 +00007831 else {
Benjamin Peterson29060642009-01-31 22:14:21 +00007832 /* wrong return value */
7833 PyErr_Format(PyExc_TypeError,
7834 "character mapping must return integer, bytes or None, not %.400s",
7835 x->ob_type->tp_name);
7836 Py_DECREF(x);
7837 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00007838 }
7839}
7840
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007841static int
Guido van Rossum98297ee2007-11-06 21:34:58 +00007842charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007843{
Benjamin Peterson14339b62009-01-31 16:36:08 +00007844 Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj);
7845 /* exponentially overallocate to minimize reallocations */
7846 if (requiredsize < 2*outsize)
7847 requiredsize = 2*outsize;
7848 if (_PyBytes_Resize(outobj, requiredsize))
7849 return -1;
7850 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007851}
7852
Benjamin Peterson14339b62009-01-31 16:36:08 +00007853typedef enum charmapencode_result {
Benjamin Peterson29060642009-01-31 22:14:21 +00007854 enc_SUCCESS, enc_FAILED, enc_EXCEPTION
Alexander Belopolsky40018472011-02-26 01:02:56 +00007855} charmapencode_result;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007856/* lookup the character, put the result in the output string and adjust
Walter Dörwald827b0552007-05-12 13:23:53 +00007857 various state variables. Resize the output bytes object if not enough
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007858 space is available. Return a new reference to the object that
7859 was put in the output buffer, or Py_None, if the mapping was undefined
7860 (in which case no character was written) or NULL, if a
Andrew M. Kuchling8294de52005-11-02 16:36:12 +00007861 reallocation error occurred. The caller must decref the result */
Alexander Belopolsky40018472011-02-26 01:02:56 +00007862static charmapencode_result
Victor Stinner22168992011-11-20 17:09:18 +01007863charmapencode_output(Py_UCS4 c, PyObject *mapping,
Alexander Belopolsky40018472011-02-26 01:02:56 +00007864 PyObject **outobj, Py_ssize_t *outpos)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007865{
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007866 PyObject *rep;
7867 char *outstart;
Christian Heimes72b710a2008-05-26 13:28:38 +00007868 Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007869
Christian Heimes90aa7642007-12-19 02:45:37 +00007870 if (Py_TYPE(mapping) == &EncodingMapType) {
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007871 int res = encoding_map_lookup(c, mapping);
Benjamin Peterson29060642009-01-31 22:14:21 +00007872 Py_ssize_t requiredsize = *outpos+1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007873 if (res == -1)
7874 return enc_FAILED;
Benjamin Peterson29060642009-01-31 22:14:21 +00007875 if (outsize<requiredsize)
7876 if (charmapencode_resize(outobj, outpos, requiredsize))
7877 return enc_EXCEPTION;
Christian Heimes72b710a2008-05-26 13:28:38 +00007878 outstart = PyBytes_AS_STRING(*outobj);
Benjamin Peterson29060642009-01-31 22:14:21 +00007879 outstart[(*outpos)++] = (char)res;
7880 return enc_SUCCESS;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007881 }
7882
7883 rep = charmapencode_lookup(c, mapping);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007884 if (rep==NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00007885 return enc_EXCEPTION;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007886 else if (rep==Py_None) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007887 Py_DECREF(rep);
7888 return enc_FAILED;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007889 } else {
Benjamin Peterson29060642009-01-31 22:14:21 +00007890 if (PyLong_Check(rep)) {
7891 Py_ssize_t requiredsize = *outpos+1;
7892 if (outsize<requiredsize)
7893 if (charmapencode_resize(outobj, outpos, requiredsize)) {
7894 Py_DECREF(rep);
7895 return enc_EXCEPTION;
7896 }
Christian Heimes72b710a2008-05-26 13:28:38 +00007897 outstart = PyBytes_AS_STRING(*outobj);
Benjamin Peterson29060642009-01-31 22:14:21 +00007898 outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep);
Benjamin Peterson14339b62009-01-31 16:36:08 +00007899 }
Benjamin Peterson29060642009-01-31 22:14:21 +00007900 else {
7901 const char *repchars = PyBytes_AS_STRING(rep);
7902 Py_ssize_t repsize = PyBytes_GET_SIZE(rep);
7903 Py_ssize_t requiredsize = *outpos+repsize;
7904 if (outsize<requiredsize)
7905 if (charmapencode_resize(outobj, outpos, requiredsize)) {
7906 Py_DECREF(rep);
7907 return enc_EXCEPTION;
7908 }
Christian Heimes72b710a2008-05-26 13:28:38 +00007909 outstart = PyBytes_AS_STRING(*outobj);
Benjamin Peterson29060642009-01-31 22:14:21 +00007910 memcpy(outstart + *outpos, repchars, repsize);
7911 *outpos += repsize;
7912 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007913 }
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007914 Py_DECREF(rep);
7915 return enc_SUCCESS;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007916}
7917
7918/* handle an error in PyUnicode_EncodeCharmap
7919 Return 0 on success, -1 on error */
Alexander Belopolsky40018472011-02-26 01:02:56 +00007920static int
7921charmap_encoding_error(
Martin v. Löwis23e275b2011-11-02 18:02:51 +01007922 PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007923 PyObject **exceptionObject,
Walter Dörwalde5402fb2003-08-14 20:25:29 +00007924 int *known_errorHandler, PyObject **errorHandler, const char *errors,
Guido van Rossum98297ee2007-11-06 21:34:58 +00007925 PyObject **res, Py_ssize_t *respos)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007926{
7927 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01007928 Py_ssize_t size, repsize;
Martin v. Löwis18e16552006-02-15 17:27:45 +00007929 Py_ssize_t newpos;
Victor Stinnerae4f7c82011-11-20 18:28:55 +01007930 enum PyUnicode_Kind kind;
7931 void *data;
7932 Py_ssize_t index;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007933 /* startpos for collecting unencodable chars */
Martin v. Löwis18e16552006-02-15 17:27:45 +00007934 Py_ssize_t collstartpos = *inpos;
7935 Py_ssize_t collendpos = *inpos+1;
7936 Py_ssize_t collpos;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007937 char *encoding = "charmap";
7938 char *reason = "character maps to <undefined>";
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007939 charmapencode_result x;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01007940 Py_UCS4 ch;
Brian Curtin2787ea42011-11-02 15:09:37 -05007941 int val;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007942
Benjamin Petersonbac79492012-01-14 13:34:47 -05007943 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis23e275b2011-11-02 18:02:51 +01007944 return -1;
7945 size = PyUnicode_GET_LENGTH(unicode);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007946 /* find all unencodable characters */
7947 while (collendpos < size) {
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00007948 PyObject *rep;
Christian Heimes90aa7642007-12-19 02:45:37 +00007949 if (Py_TYPE(mapping) == &EncodingMapType) {
Martin v. Löwis23e275b2011-11-02 18:02:51 +01007950 ch = PyUnicode_READ_CHAR(unicode, collendpos);
Brian Curtin2787ea42011-11-02 15:09:37 -05007951 val = encoding_map_lookup(ch, mapping);
7952 if (val != -1)
Benjamin Peterson29060642009-01-31 22:14:21 +00007953 break;
7954 ++collendpos;
7955 continue;
7956 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00007957
Martin v. Löwis23e275b2011-11-02 18:02:51 +01007958 ch = PyUnicode_READ_CHAR(unicode, collendpos);
7959 rep = charmapencode_lookup(ch, mapping);
Benjamin Peterson29060642009-01-31 22:14:21 +00007960 if (rep==NULL)
7961 return -1;
7962 else if (rep!=Py_None) {
7963 Py_DECREF(rep);
7964 break;
7965 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00007966 Py_DECREF(rep);
Benjamin Peterson29060642009-01-31 22:14:21 +00007967 ++collendpos;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007968 }
7969 /* cache callback name lookup
7970 * (if not done yet, i.e. it's the first error) */
7971 if (*known_errorHandler==-1) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007972 if ((errors==NULL) || (!strcmp(errors, "strict")))
7973 *known_errorHandler = 1;
7974 else if (!strcmp(errors, "replace"))
7975 *known_errorHandler = 2;
7976 else if (!strcmp(errors, "ignore"))
7977 *known_errorHandler = 3;
7978 else if (!strcmp(errors, "xmlcharrefreplace"))
7979 *known_errorHandler = 4;
7980 else
7981 *known_errorHandler = 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00007982 }
7983 switch (*known_errorHandler) {
Benjamin Peterson14339b62009-01-31 16:36:08 +00007984 case 1: /* strict */
Martin v. Löwis12be46c2011-11-04 19:04:15 +01007985 raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
Benjamin Peterson14339b62009-01-31 16:36:08 +00007986 return -1;
7987 case 2: /* replace */
7988 for (collpos = collstartpos; collpos<collendpos; ++collpos) {
Benjamin Peterson29060642009-01-31 22:14:21 +00007989 x = charmapencode_output('?', mapping, res, respos);
7990 if (x==enc_EXCEPTION) {
7991 return -1;
7992 }
7993 else if (x==enc_FAILED) {
Martin v. Löwis12be46c2011-11-04 19:04:15 +01007994 raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
Benjamin Peterson29060642009-01-31 22:14:21 +00007995 return -1;
7996 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00007997 }
7998 /* fall through */
7999 case 3: /* ignore */
8000 *inpos = collendpos;
8001 break;
8002 case 4: /* xmlcharrefreplace */
8003 /* generate replacement (temporarily (mis)uses p) */
8004 for (collpos = collstartpos; collpos < collendpos; ++collpos) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008005 char buffer[2+29+1+1];
8006 char *cp;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008007 sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos));
Benjamin Peterson29060642009-01-31 22:14:21 +00008008 for (cp = buffer; *cp; ++cp) {
8009 x = charmapencode_output(*cp, mapping, res, respos);
8010 if (x==enc_EXCEPTION)
8011 return -1;
8012 else if (x==enc_FAILED) {
Martin v. Löwis12be46c2011-11-04 19:04:15 +01008013 raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
Benjamin Peterson29060642009-01-31 22:14:21 +00008014 return -1;
8015 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008016 }
8017 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008018 *inpos = collendpos;
8019 break;
8020 default:
8021 repunicode = unicode_encode_call_errorhandler(errors, errorHandler,
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008022 encoding, reason, unicode, exceptionObject,
Benjamin Peterson29060642009-01-31 22:14:21 +00008023 collstartpos, collendpos, &newpos);
Benjamin Peterson14339b62009-01-31 16:36:08 +00008024 if (repunicode == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008025 return -1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00008026 if (PyBytes_Check(repunicode)) {
8027 /* Directly copy bytes result to output. */
8028 Py_ssize_t outsize = PyBytes_Size(*res);
8029 Py_ssize_t requiredsize;
8030 repsize = PyBytes_Size(repunicode);
8031 requiredsize = *respos + repsize;
8032 if (requiredsize > outsize)
8033 /* Make room for all additional bytes. */
8034 if (charmapencode_resize(res, respos, requiredsize)) {
8035 Py_DECREF(repunicode);
8036 return -1;
8037 }
8038 memcpy(PyBytes_AsString(*res) + *respos,
8039 PyBytes_AsString(repunicode), repsize);
8040 *respos += repsize;
8041 *inpos = newpos;
Martin v. Löwisdb12d452009-05-02 18:52:14 +00008042 Py_DECREF(repunicode);
Martin v. Löwis011e8422009-05-05 04:43:17 +00008043 break;
Martin v. Löwisdb12d452009-05-02 18:52:14 +00008044 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008045 /* generate replacement */
Benjamin Petersonbac79492012-01-14 13:34:47 -05008046 if (PyUnicode_READY(repunicode) == -1) {
Victor Stinnerae4f7c82011-11-20 18:28:55 +01008047 Py_DECREF(repunicode);
8048 return -1;
8049 }
Victor Stinner9e30aa52011-11-21 02:49:52 +01008050 repsize = PyUnicode_GET_LENGTH(repunicode);
Victor Stinnerae4f7c82011-11-20 18:28:55 +01008051 data = PyUnicode_DATA(repunicode);
8052 kind = PyUnicode_KIND(repunicode);
8053 for (index = 0; index < repsize; index++) {
8054 Py_UCS4 repch = PyUnicode_READ(kind, data, index);
8055 x = charmapencode_output(repch, mapping, res, respos);
Benjamin Peterson29060642009-01-31 22:14:21 +00008056 if (x==enc_EXCEPTION) {
Victor Stinnerae4f7c82011-11-20 18:28:55 +01008057 Py_DECREF(repunicode);
Benjamin Peterson29060642009-01-31 22:14:21 +00008058 return -1;
8059 }
8060 else if (x==enc_FAILED) {
8061 Py_DECREF(repunicode);
Martin v. Löwis12be46c2011-11-04 19:04:15 +01008062 raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason);
Benjamin Peterson29060642009-01-31 22:14:21 +00008063 return -1;
8064 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008065 }
8066 *inpos = newpos;
8067 Py_DECREF(repunicode);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008068 }
8069 return 0;
8070}
8071
Alexander Belopolsky40018472011-02-26 01:02:56 +00008072PyObject *
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008073_PyUnicode_EncodeCharmap(PyObject *unicode,
8074 PyObject *mapping,
8075 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00008076{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008077 /* output object */
8078 PyObject *res = NULL;
8079 /* current input position */
Martin v. Löwis18e16552006-02-15 17:27:45 +00008080 Py_ssize_t inpos = 0;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008081 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008082 /* current output position */
Martin v. Löwis18e16552006-02-15 17:27:45 +00008083 Py_ssize_t respos = 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008084 PyObject *errorHandler = NULL;
8085 PyObject *exc = NULL;
8086 /* the following variable is used for caching string comparisons
8087 * -1=not initialized, 0=unknown, 1=strict, 2=replace,
8088 * 3=ignore, 4=xmlcharrefreplace */
8089 int known_errorHandler = -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008090
Benjamin Petersonbac79492012-01-14 13:34:47 -05008091 if (PyUnicode_READY(unicode) == -1)
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008092 return NULL;
8093 size = PyUnicode_GET_LENGTH(unicode);
8094
Guido van Rossumd57fd912000-03-10 22:53:23 +00008095 /* Default to Latin-1 */
8096 if (mapping == NULL)
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008097 return unicode_encode_ucs1(unicode, errors, 256);
Guido van Rossumd57fd912000-03-10 22:53:23 +00008098
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008099 /* allocate enough for a simple encoding without
8100 replacements, if we need more, we'll resize */
Christian Heimes72b710a2008-05-26 13:28:38 +00008101 res = PyBytes_FromStringAndSize(NULL, size);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008102 if (res == NULL)
8103 goto onError;
Marc-André Lemburgb7520772000-08-14 11:29:19 +00008104 if (size == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00008105 return res;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008106
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008107 while (inpos<size) {
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008108 Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos);
Benjamin Peterson29060642009-01-31 22:14:21 +00008109 /* try to encode it */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008110 charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos);
Benjamin Peterson29060642009-01-31 22:14:21 +00008111 if (x==enc_EXCEPTION) /* error */
8112 goto onError;
8113 if (x==enc_FAILED) { /* unencodable character */
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008114 if (charmap_encoding_error(unicode, &inpos, mapping,
Benjamin Peterson29060642009-01-31 22:14:21 +00008115 &exc,
8116 &known_errorHandler, &errorHandler, errors,
8117 &res, &respos)) {
8118 goto onError;
8119 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008120 }
Benjamin Peterson29060642009-01-31 22:14:21 +00008121 else
8122 /* done with this character => adjust input position */
8123 ++inpos;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008124 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00008125
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008126 /* Resize if we allocated to much */
Christian Heimes72b710a2008-05-26 13:28:38 +00008127 if (respos<PyBytes_GET_SIZE(res))
Alexandre Vassalotti44531cb2008-12-27 09:16:49 +00008128 if (_PyBytes_Resize(&res, respos) < 0)
8129 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +00008130
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008131 Py_XDECREF(exc);
8132 Py_XDECREF(errorHandler);
8133 return res;
8134
Benjamin Peterson29060642009-01-31 22:14:21 +00008135 onError:
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008136 Py_XDECREF(res);
8137 Py_XDECREF(exc);
8138 Py_XDECREF(errorHandler);
Guido van Rossumd57fd912000-03-10 22:53:23 +00008139 return NULL;
8140}
8141
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008142/* Deprecated */
8143PyObject *
8144PyUnicode_EncodeCharmap(const Py_UNICODE *p,
8145 Py_ssize_t size,
8146 PyObject *mapping,
8147 const char *errors)
8148{
8149 PyObject *result;
8150 PyObject *unicode = PyUnicode_FromUnicode(p, size);
8151 if (unicode == NULL)
8152 return NULL;
8153 result = _PyUnicode_EncodeCharmap(unicode, mapping, errors);
8154 Py_DECREF(unicode);
Victor Stinnerfc026c92011-11-04 00:24:51 +01008155 return result;
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008156}
8157
Alexander Belopolsky40018472011-02-26 01:02:56 +00008158PyObject *
8159PyUnicode_AsCharmapString(PyObject *unicode,
8160 PyObject *mapping)
Guido van Rossumd57fd912000-03-10 22:53:23 +00008161{
8162 if (!PyUnicode_Check(unicode) || mapping == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008163 PyErr_BadArgument();
8164 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008165 }
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008166 return _PyUnicode_EncodeCharmap(unicode, mapping, NULL);
Guido van Rossumd57fd912000-03-10 22:53:23 +00008167}
8168
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008169/* create or adjust a UnicodeTranslateError */
Alexander Belopolsky40018472011-02-26 01:02:56 +00008170static void
8171make_translate_exception(PyObject **exceptionObject,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008172 PyObject *unicode,
Alexander Belopolsky40018472011-02-26 01:02:56 +00008173 Py_ssize_t startpos, Py_ssize_t endpos,
8174 const char *reason)
Guido van Rossumd57fd912000-03-10 22:53:23 +00008175{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008176 if (*exceptionObject == NULL) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008177 *exceptionObject = _PyUnicodeTranslateError_Create(
8178 unicode, startpos, endpos, reason);
Guido van Rossumd57fd912000-03-10 22:53:23 +00008179 }
8180 else {
Benjamin Peterson29060642009-01-31 22:14:21 +00008181 if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos))
8182 goto onError;
8183 if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos))
8184 goto onError;
8185 if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason))
8186 goto onError;
8187 return;
8188 onError:
8189 Py_DECREF(*exceptionObject);
8190 *exceptionObject = NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008191 }
8192}
8193
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008194/* raises a UnicodeTranslateError */
Alexander Belopolsky40018472011-02-26 01:02:56 +00008195static void
8196raise_translate_exception(PyObject **exceptionObject,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008197 PyObject *unicode,
Alexander Belopolsky40018472011-02-26 01:02:56 +00008198 Py_ssize_t startpos, Py_ssize_t endpos,
8199 const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008200{
8201 make_translate_exception(exceptionObject,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008202 unicode, startpos, endpos, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008203 if (*exceptionObject != NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008204 PyCodec_StrictErrors(*exceptionObject);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008205}
8206
8207/* error handling callback helper:
8208 build arguments, call the callback and check the arguments,
8209 put the result into newpos and return the replacement string, which
8210 has to be freed by the caller */
Alexander Belopolsky40018472011-02-26 01:02:56 +00008211static PyObject *
8212unicode_translate_call_errorhandler(const char *errors,
8213 PyObject **errorHandler,
8214 const char *reason,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008215 PyObject *unicode, PyObject **exceptionObject,
Alexander Belopolsky40018472011-02-26 01:02:56 +00008216 Py_ssize_t startpos, Py_ssize_t endpos,
8217 Py_ssize_t *newpos)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008218{
Benjamin Peterson142957c2008-07-04 19:55:29 +00008219 static char *argparse = "O!n;translating error handler must return (str, int) tuple";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008220
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008221 Py_ssize_t i_newpos;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008222 PyObject *restuple;
8223 PyObject *resunicode;
8224
8225 if (*errorHandler == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008226 *errorHandler = PyCodec_LookupError(errors);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008227 if (*errorHandler == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008228 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008229 }
8230
8231 make_translate_exception(exceptionObject,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008232 unicode, startpos, endpos, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008233 if (*exceptionObject == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008234 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008235
8236 restuple = PyObject_CallFunctionObjArgs(
Benjamin Peterson29060642009-01-31 22:14:21 +00008237 *errorHandler, *exceptionObject, NULL);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008238 if (restuple == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008239 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008240 if (!PyTuple_Check(restuple)) {
Benjamin Petersond75fcb42009-02-19 04:22:03 +00008241 PyErr_SetString(PyExc_TypeError, &argparse[4]);
Benjamin Peterson29060642009-01-31 22:14:21 +00008242 Py_DECREF(restuple);
8243 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008244 }
8245 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type,
Benjamin Peterson29060642009-01-31 22:14:21 +00008246 &resunicode, &i_newpos)) {
8247 Py_DECREF(restuple);
8248 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008249 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00008250 if (i_newpos<0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008251 *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos;
Martin v. Löwis18e16552006-02-15 17:27:45 +00008252 else
8253 *newpos = i_newpos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008254 if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008255 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
8256 Py_DECREF(restuple);
8257 return NULL;
Walter Dörwald2e0b18a2003-01-31 17:19:08 +00008258 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008259 Py_INCREF(resunicode);
8260 Py_DECREF(restuple);
8261 return resunicode;
8262}
8263
8264/* Lookup the character ch in the mapping and put the result in result,
8265 which must be decrefed by the caller.
8266 Return 0 on success, -1 on error */
Alexander Belopolsky40018472011-02-26 01:02:56 +00008267static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008268charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008269{
Christian Heimes217cfd12007-12-02 14:31:20 +00008270 PyObject *w = PyLong_FromLong((long)c);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008271 PyObject *x;
8272
8273 if (w == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008274 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008275 x = PyObject_GetItem(mapping, w);
8276 Py_DECREF(w);
8277 if (x == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008278 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
8279 /* No mapping found means: use 1:1 mapping. */
8280 PyErr_Clear();
8281 *result = NULL;
8282 return 0;
8283 } else
8284 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008285 }
8286 else if (x == Py_None) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008287 *result = x;
8288 return 0;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008289 }
Christian Heimes217cfd12007-12-02 14:31:20 +00008290 else if (PyLong_Check(x)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008291 long value = PyLong_AS_LONG(x);
8292 long max = PyUnicode_GetMax();
8293 if (value < 0 || value > max) {
8294 PyErr_Format(PyExc_TypeError,
Guido van Rossum5a2f7e602007-10-24 21:13:09 +00008295 "character mapping must be in range(0x%x)", max+1);
Benjamin Peterson29060642009-01-31 22:14:21 +00008296 Py_DECREF(x);
8297 return -1;
8298 }
8299 *result = x;
8300 return 0;
8301 }
8302 else if (PyUnicode_Check(x)) {
8303 *result = x;
8304 return 0;
8305 }
8306 else {
8307 /* wrong return value */
8308 PyErr_SetString(PyExc_TypeError,
8309 "character mapping must return integer, None or str");
Benjamin Peterson14339b62009-01-31 16:36:08 +00008310 Py_DECREF(x);
8311 return -1;
8312 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008313}
8314/* ensure that *outobj is at least requiredsize characters long,
Benjamin Peterson29060642009-01-31 22:14:21 +00008315 if not reallocate and adjust various state variables.
8316 Return 0 on success, -1 on error */
Alexander Belopolsky40018472011-02-26 01:02:56 +00008317static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008318charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize,
Benjamin Peterson29060642009-01-31 22:14:21 +00008319 Py_ssize_t requiredsize)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008320{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008321 Py_ssize_t oldsize = *psize;
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +00008322 Py_UCS4 *new_outobj;
Walter Dörwald4894c302003-10-24 14:25:28 +00008323 if (requiredsize > oldsize) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008324 /* exponentially overallocate to minimize reallocations */
8325 if (requiredsize < 2 * oldsize)
8326 requiredsize = 2 * oldsize;
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +00008327 new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4));
8328 if (new_outobj == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +00008329 return -1;
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +00008330 *outobj = new_outobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008331 *psize = requiredsize;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008332 }
8333 return 0;
8334}
8335/* lookup the character, put the result in the output string and adjust
8336 various state variables. Return a new reference to the object that
8337 was put in the output buffer in *result, or Py_None, if the mapping was
8338 undefined (in which case no character was written).
8339 The called must decref result.
8340 Return 0 on success, -1 on error. */
Alexander Belopolsky40018472011-02-26 01:02:56 +00008341static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008342charmaptranslate_output(PyObject *input, Py_ssize_t ipos,
8343 PyObject *mapping, Py_UCS4 **output,
8344 Py_ssize_t *osize, Py_ssize_t *opos,
Alexander Belopolsky40018472011-02-26 01:02:56 +00008345 PyObject **res)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008346{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008347 Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos);
8348 if (charmaptranslate_lookup(curinp, mapping, res))
Benjamin Peterson29060642009-01-31 22:14:21 +00008349 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008350 if (*res==NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008351 /* not found => default to 1:1 mapping */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008352 (*output)[(*opos)++] = curinp;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008353 }
8354 else if (*res==Py_None)
Benjamin Peterson29060642009-01-31 22:14:21 +00008355 ;
Christian Heimes217cfd12007-12-02 14:31:20 +00008356 else if (PyLong_Check(*res)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008357 /* no overflow check, because we know that the space is enough */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008358 (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008359 }
8360 else if (PyUnicode_Check(*res)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008361 Py_ssize_t repsize;
8362 if (PyUnicode_READY(*res) == -1)
8363 return -1;
8364 repsize = PyUnicode_GET_LENGTH(*res);
Benjamin Peterson29060642009-01-31 22:14:21 +00008365 if (repsize==1) {
8366 /* no overflow check, because we know that the space is enough */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008367 (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0);
Benjamin Peterson29060642009-01-31 22:14:21 +00008368 }
8369 else if (repsize!=0) {
8370 /* more than one character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008371 Py_ssize_t requiredsize = *opos +
8372 (PyUnicode_GET_LENGTH(input) - ipos) +
Benjamin Peterson29060642009-01-31 22:14:21 +00008373 repsize - 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008374 Py_ssize_t i;
8375 if (charmaptranslate_makespace(output, osize, requiredsize))
Benjamin Peterson29060642009-01-31 22:14:21 +00008376 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008377 for(i = 0; i < repsize; i++)
8378 (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i);
Benjamin Peterson29060642009-01-31 22:14:21 +00008379 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008380 }
8381 else
Benjamin Peterson29060642009-01-31 22:14:21 +00008382 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008383 return 0;
8384}
8385
Alexander Belopolsky40018472011-02-26 01:02:56 +00008386PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008387_PyUnicode_TranslateCharmap(PyObject *input,
8388 PyObject *mapping,
8389 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00008390{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008391 /* input object */
8392 char *idata;
8393 Py_ssize_t size, i;
8394 int kind;
8395 /* output buffer */
8396 Py_UCS4 *output = NULL;
8397 Py_ssize_t osize;
8398 PyObject *res;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008399 /* current output position */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008400 Py_ssize_t opos;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008401 char *reason = "character maps to <undefined>";
8402 PyObject *errorHandler = NULL;
8403 PyObject *exc = NULL;
8404 /* the following variable is used for caching string comparisons
8405 * -1=not initialized, 0=unknown, 1=strict, 2=replace,
8406 * 3=ignore, 4=xmlcharrefreplace */
8407 int known_errorHandler = -1;
8408
Guido van Rossumd57fd912000-03-10 22:53:23 +00008409 if (mapping == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008410 PyErr_BadArgument();
8411 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008412 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008413
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008414 if (PyUnicode_READY(input) == -1)
8415 return NULL;
8416 idata = (char*)PyUnicode_DATA(input);
8417 kind = PyUnicode_KIND(input);
8418 size = PyUnicode_GET_LENGTH(input);
8419 i = 0;
8420
8421 if (size == 0) {
8422 Py_INCREF(input);
8423 return input;
8424 }
8425
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008426 /* allocate enough for a simple 1:1 translation without
8427 replacements, if we need more, we'll resize */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008428 osize = size;
8429 output = PyMem_Malloc(osize * sizeof(Py_UCS4));
8430 opos = 0;
8431 if (output == NULL) {
8432 PyErr_NoMemory();
Benjamin Peterson29060642009-01-31 22:14:21 +00008433 goto onError;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008434 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00008435
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008436 while (i<size) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008437 /* try to encode it */
8438 PyObject *x = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008439 if (charmaptranslate_output(input, i, mapping,
8440 &output, &osize, &opos, &x)) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008441 Py_XDECREF(x);
8442 goto onError;
8443 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008444 Py_XDECREF(x);
Benjamin Peterson29060642009-01-31 22:14:21 +00008445 if (x!=Py_None) /* it worked => adjust input pointer */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008446 ++i;
Benjamin Peterson29060642009-01-31 22:14:21 +00008447 else { /* untranslatable character */
8448 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
8449 Py_ssize_t repsize;
8450 Py_ssize_t newpos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008451 Py_ssize_t uni2;
Benjamin Peterson29060642009-01-31 22:14:21 +00008452 /* startpos for collecting untranslatable chars */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008453 Py_ssize_t collstart = i;
8454 Py_ssize_t collend = i+1;
8455 Py_ssize_t coll;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008456
Benjamin Peterson29060642009-01-31 22:14:21 +00008457 /* find all untranslatable characters */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008458 while (collend < size) {
8459 if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x))
Benjamin Peterson29060642009-01-31 22:14:21 +00008460 goto onError;
8461 Py_XDECREF(x);
8462 if (x!=Py_None)
8463 break;
8464 ++collend;
8465 }
8466 /* cache callback name lookup
8467 * (if not done yet, i.e. it's the first error) */
8468 if (known_errorHandler==-1) {
8469 if ((errors==NULL) || (!strcmp(errors, "strict")))
8470 known_errorHandler = 1;
8471 else if (!strcmp(errors, "replace"))
8472 known_errorHandler = 2;
8473 else if (!strcmp(errors, "ignore"))
8474 known_errorHandler = 3;
8475 else if (!strcmp(errors, "xmlcharrefreplace"))
8476 known_errorHandler = 4;
8477 else
8478 known_errorHandler = 0;
8479 }
8480 switch (known_errorHandler) {
8481 case 1: /* strict */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008482 raise_translate_exception(&exc, input, collstart,
8483 collend, reason);
Benjamin Peterson14339b62009-01-31 16:36:08 +00008484 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +00008485 case 2: /* replace */
8486 /* No need to check for space, this is a 1:1 replacement */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008487 for (coll = collstart; coll<collend; coll++)
8488 output[opos++] = '?';
Benjamin Peterson29060642009-01-31 22:14:21 +00008489 /* fall through */
8490 case 3: /* ignore */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008491 i = collend;
Benjamin Peterson29060642009-01-31 22:14:21 +00008492 break;
8493 case 4: /* xmlcharrefreplace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008494 /* generate replacement (temporarily (mis)uses i) */
8495 for (i = collstart; i < collend; ++i) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008496 char buffer[2+29+1+1];
8497 char *cp;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008498 sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i));
8499 if (charmaptranslate_makespace(&output, &osize,
8500 opos+strlen(buffer)+(size-collend)))
Benjamin Peterson29060642009-01-31 22:14:21 +00008501 goto onError;
8502 for (cp = buffer; *cp; ++cp)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008503 output[opos++] = *cp;
Benjamin Peterson29060642009-01-31 22:14:21 +00008504 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008505 i = collend;
Benjamin Peterson29060642009-01-31 22:14:21 +00008506 break;
8507 default:
8508 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008509 reason, input, &exc,
8510 collstart, collend, &newpos);
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01008511 if (repunicode == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008512 goto onError;
Benjamin Peterson9ca3ffa2012-01-01 16:04:29 -06008513 if (PyUnicode_READY(repunicode) == -1) {
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01008514 Py_DECREF(repunicode);
8515 goto onError;
8516 }
Benjamin Peterson29060642009-01-31 22:14:21 +00008517 /* generate replacement */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008518 repsize = PyUnicode_GET_LENGTH(repunicode);
8519 if (charmaptranslate_makespace(&output, &osize,
8520 opos+repsize+(size-collend))) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008521 Py_DECREF(repunicode);
8522 goto onError;
8523 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008524 for (uni2 = 0; repsize-->0; ++uni2)
8525 output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2);
8526 i = newpos;
Benjamin Peterson29060642009-01-31 22:14:21 +00008527 Py_DECREF(repunicode);
Benjamin Peterson14339b62009-01-31 16:36:08 +00008528 }
Benjamin Peterson14339b62009-01-31 16:36:08 +00008529 }
8530 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008531 res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos);
8532 if (!res)
8533 goto onError;
8534 PyMem_Free(output);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008535 Py_XDECREF(exc);
8536 Py_XDECREF(errorHandler);
8537 return res;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008538
Benjamin Peterson29060642009-01-31 22:14:21 +00008539 onError:
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008540 PyMem_Free(output);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00008541 Py_XDECREF(exc);
8542 Py_XDECREF(errorHandler);
Guido van Rossumd57fd912000-03-10 22:53:23 +00008543 return NULL;
8544}
8545
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008546/* Deprecated. Use PyUnicode_Translate instead. */
8547PyObject *
8548PyUnicode_TranslateCharmap(const Py_UNICODE *p,
8549 Py_ssize_t size,
8550 PyObject *mapping,
8551 const char *errors)
8552{
8553 PyObject *unicode = PyUnicode_FromUnicode(p, size);
8554 if (!unicode)
8555 return NULL;
8556 return _PyUnicode_TranslateCharmap(unicode, mapping, errors);
8557}
8558
Alexander Belopolsky40018472011-02-26 01:02:56 +00008559PyObject *
8560PyUnicode_Translate(PyObject *str,
8561 PyObject *mapping,
8562 const char *errors)
Guido van Rossumd57fd912000-03-10 22:53:23 +00008563{
8564 PyObject *result;
Tim Petersced69f82003-09-16 20:30:58 +00008565
Guido van Rossumd57fd912000-03-10 22:53:23 +00008566 str = PyUnicode_FromObject(str);
8567 if (str == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00008568 goto onError;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008569 result = _PyUnicode_TranslateCharmap(str, mapping, errors);
Guido van Rossumd57fd912000-03-10 22:53:23 +00008570 Py_DECREF(str);
8571 return result;
Tim Petersced69f82003-09-16 20:30:58 +00008572
Benjamin Peterson29060642009-01-31 22:14:21 +00008573 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +00008574 Py_XDECREF(str);
8575 return NULL;
8576}
Tim Petersced69f82003-09-16 20:30:58 +00008577
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008578static Py_UCS4
Victor Stinner9310abb2011-10-05 00:59:23 +02008579fix_decimal_and_space_to_ascii(PyObject *self)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008580{
8581 /* No need to call PyUnicode_READY(self) because this function is only
8582 called as a callback from fixup() which does it already. */
8583 const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
8584 const int kind = PyUnicode_KIND(self);
8585 void *data = PyUnicode_DATA(self);
Victor Stinnere6abb482012-05-02 01:15:40 +02008586 Py_UCS4 maxchar = 127, ch, fixed;
Benjamin Peterson821e4cf2012-01-12 15:40:18 -05008587 int modified = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008588 Py_ssize_t i;
8589
8590 for (i = 0; i < len; ++i) {
8591 ch = PyUnicode_READ(kind, data, i);
8592 fixed = 0;
8593 if (ch > 127) {
8594 if (Py_UNICODE_ISSPACE(ch))
8595 fixed = ' ';
8596 else {
8597 const int decimal = Py_UNICODE_TODECIMAL(ch);
8598 if (decimal >= 0)
8599 fixed = '0' + decimal;
8600 }
8601 if (fixed != 0) {
Benjamin Peterson821e4cf2012-01-12 15:40:18 -05008602 modified = 1;
Victor Stinnere6abb482012-05-02 01:15:40 +02008603 maxchar = MAX_MAXCHAR(maxchar, fixed);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008604 PyUnicode_WRITE(kind, data, i, fixed);
8605 }
Victor Stinnere6abb482012-05-02 01:15:40 +02008606 else
8607 maxchar = MAX_MAXCHAR(maxchar, ch);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008608 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008609 }
8610
Benjamin Peterson821e4cf2012-01-12 15:40:18 -05008611 return (modified) ? maxchar : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008612}
8613
8614PyObject *
8615_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
8616{
8617 if (!PyUnicode_Check(unicode)) {
8618 PyErr_BadInternalCall();
8619 return NULL;
8620 }
8621 if (PyUnicode_READY(unicode) == -1)
8622 return NULL;
8623 if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) {
8624 /* If the string is already ASCII, just return the same string */
8625 Py_INCREF(unicode);
8626 return unicode;
8627 }
Victor Stinner9310abb2011-10-05 00:59:23 +02008628 return fixup(unicode, fix_decimal_and_space_to_ascii);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008629}
8630
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00008631PyObject *
8632PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
8633 Py_ssize_t length)
8634{
Victor Stinnerf0124502011-11-21 23:12:56 +01008635 PyObject *decimal;
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00008636 Py_ssize_t i;
Victor Stinnerf0124502011-11-21 23:12:56 +01008637 Py_UCS4 maxchar;
8638 enum PyUnicode_Kind kind;
8639 void *data;
8640
Victor Stinner99d7ad02012-02-22 13:37:39 +01008641 maxchar = 127;
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00008642 for (i = 0; i < length; i++) {
Victor Stinnerf0124502011-11-21 23:12:56 +01008643 Py_UNICODE ch = s[i];
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00008644 if (ch > 127) {
8645 int decimal = Py_UNICODE_TODECIMAL(ch);
8646 if (decimal >= 0)
Victor Stinnerf0124502011-11-21 23:12:56 +01008647 ch = '0' + decimal;
Victor Stinnere6abb482012-05-02 01:15:40 +02008648 maxchar = MAX_MAXCHAR(maxchar, ch);
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00008649 }
8650 }
Victor Stinnerf0124502011-11-21 23:12:56 +01008651
8652 /* Copy to a new string */
8653 decimal = PyUnicode_New(length, maxchar);
8654 if (decimal == NULL)
8655 return decimal;
8656 kind = PyUnicode_KIND(decimal);
8657 data = PyUnicode_DATA(decimal);
8658 /* Iterate over code points */
8659 for (i = 0; i < length; i++) {
8660 Py_UNICODE ch = s[i];
8661 if (ch > 127) {
8662 int decimal = Py_UNICODE_TODECIMAL(ch);
8663 if (decimal >= 0)
8664 ch = '0' + decimal;
8665 }
8666 PyUnicode_WRITE(kind, data, i, ch);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008667 }
Victor Stinnerd3df8ab2011-11-22 01:22:34 +01008668 return unicode_result(decimal);
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00008669}
Guido van Rossum9e896b32000-04-05 20:11:21 +00008670/* --- Decimal Encoder ---------------------------------------------------- */
8671
Alexander Belopolsky40018472011-02-26 01:02:56 +00008672int
8673PyUnicode_EncodeDecimal(Py_UNICODE *s,
8674 Py_ssize_t length,
8675 char *output,
8676 const char *errors)
Guido van Rossum9e896b32000-04-05 20:11:21 +00008677{
Martin v. Löwis23e275b2011-11-02 18:02:51 +01008678 PyObject *unicode;
Victor Stinner6345be92011-11-25 20:09:01 +01008679 Py_ssize_t i;
Victor Stinner42bf7752011-11-21 22:52:58 +01008680 enum PyUnicode_Kind kind;
8681 void *data;
Guido van Rossum9e896b32000-04-05 20:11:21 +00008682
8683 if (output == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00008684 PyErr_BadArgument();
8685 return -1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00008686 }
8687
Victor Stinner42bf7752011-11-21 22:52:58 +01008688 unicode = PyUnicode_FromUnicode(s, length);
8689 if (unicode == NULL)
8690 return -1;
8691
Benjamin Petersonbac79492012-01-14 13:34:47 -05008692 if (PyUnicode_READY(unicode) == -1) {
Victor Stinner6345be92011-11-25 20:09:01 +01008693 Py_DECREF(unicode);
8694 return -1;
8695 }
Victor Stinner42bf7752011-11-21 22:52:58 +01008696 kind = PyUnicode_KIND(unicode);
8697 data = PyUnicode_DATA(unicode);
8698
Victor Stinnerb84d7232011-11-22 01:50:07 +01008699 for (i=0; i < length; ) {
Victor Stinner6345be92011-11-25 20:09:01 +01008700 PyObject *exc;
8701 Py_UCS4 ch;
Benjamin Peterson29060642009-01-31 22:14:21 +00008702 int decimal;
Victor Stinner6345be92011-11-25 20:09:01 +01008703 Py_ssize_t startpos;
8704
8705 ch = PyUnicode_READ(kind, data, i);
Tim Petersced69f82003-09-16 20:30:58 +00008706
Benjamin Peterson29060642009-01-31 22:14:21 +00008707 if (Py_UNICODE_ISSPACE(ch)) {
Benjamin Peterson14339b62009-01-31 16:36:08 +00008708 *output++ = ' ';
Victor Stinnerb84d7232011-11-22 01:50:07 +01008709 i++;
Benjamin Peterson29060642009-01-31 22:14:21 +00008710 continue;
Benjamin Peterson14339b62009-01-31 16:36:08 +00008711 }
Benjamin Peterson29060642009-01-31 22:14:21 +00008712 decimal = Py_UNICODE_TODECIMAL(ch);
8713 if (decimal >= 0) {
8714 *output++ = '0' + decimal;
Victor Stinnerb84d7232011-11-22 01:50:07 +01008715 i++;
Benjamin Peterson29060642009-01-31 22:14:21 +00008716 continue;
8717 }
8718 if (0 < ch && ch < 256) {
8719 *output++ = (char)ch;
Victor Stinnerb84d7232011-11-22 01:50:07 +01008720 i++;
Benjamin Peterson29060642009-01-31 22:14:21 +00008721 continue;
8722 }
Victor Stinner6345be92011-11-25 20:09:01 +01008723
Victor Stinner42bf7752011-11-21 22:52:58 +01008724 startpos = i;
Victor Stinner6345be92011-11-25 20:09:01 +01008725 exc = NULL;
8726 raise_encode_exception(&exc, "decimal", unicode,
8727 startpos, startpos+1,
8728 "invalid decimal Unicode string");
8729 Py_XDECREF(exc);
8730 Py_DECREF(unicode);
8731 return -1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00008732 }
8733 /* 0-terminate the output string */
8734 *output++ = '\0';
Victor Stinner42bf7752011-11-21 22:52:58 +01008735 Py_DECREF(unicode);
Guido van Rossum9e896b32000-04-05 20:11:21 +00008736 return 0;
Guido van Rossum9e896b32000-04-05 20:11:21 +00008737}
8738
Guido van Rossumd57fd912000-03-10 22:53:23 +00008739/* --- Helpers ------------------------------------------------------------ */
8740
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008741static Py_ssize_t
Victor Stinner794d5672011-10-10 03:21:36 +02008742any_find_slice(int direction, PyObject* s1, PyObject* s2,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008743 Py_ssize_t start,
8744 Py_ssize_t end)
8745{
8746 int kind1, kind2, kind;
8747 void *buf1, *buf2;
8748 Py_ssize_t len1, len2, result;
8749
8750 kind1 = PyUnicode_KIND(s1);
8751 kind2 = PyUnicode_KIND(s2);
8752 kind = kind1 > kind2 ? kind1 : kind2;
8753 buf1 = PyUnicode_DATA(s1);
8754 buf2 = PyUnicode_DATA(s2);
8755 if (kind1 != kind)
8756 buf1 = _PyUnicode_AsKind(s1, kind);
8757 if (!buf1)
8758 return -2;
8759 if (kind2 != kind)
8760 buf2 = _PyUnicode_AsKind(s2, kind);
8761 if (!buf2) {
8762 if (kind1 != kind) PyMem_Free(buf1);
8763 return -2;
8764 }
8765 len1 = PyUnicode_GET_LENGTH(s1);
8766 len2 = PyUnicode_GET_LENGTH(s2);
8767
Victor Stinner794d5672011-10-10 03:21:36 +02008768 if (direction > 0) {
Benjamin Petersonead6b532011-12-20 17:23:42 -06008769 switch (kind) {
Victor Stinner794d5672011-10-10 03:21:36 +02008770 case PyUnicode_1BYTE_KIND:
8771 if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2))
8772 result = asciilib_find_slice(buf1, len1, buf2, len2, start, end);
8773 else
8774 result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end);
8775 break;
8776 case PyUnicode_2BYTE_KIND:
8777 result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end);
8778 break;
8779 case PyUnicode_4BYTE_KIND:
8780 result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end);
8781 break;
8782 default:
8783 assert(0); result = -2;
8784 }
8785 }
8786 else {
Benjamin Petersonead6b532011-12-20 17:23:42 -06008787 switch (kind) {
Victor Stinner794d5672011-10-10 03:21:36 +02008788 case PyUnicode_1BYTE_KIND:
8789 if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2))
8790 result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end);
8791 else
8792 result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end);
8793 break;
8794 case PyUnicode_2BYTE_KIND:
8795 result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end);
8796 break;
8797 case PyUnicode_4BYTE_KIND:
8798 result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end);
8799 break;
8800 default:
8801 assert(0); result = -2;
8802 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008803 }
8804
8805 if (kind1 != kind)
8806 PyMem_Free(buf1);
8807 if (kind2 != kind)
8808 PyMem_Free(buf2);
8809
8810 return result;
8811}
8812
8813Py_ssize_t
Victor Stinner41a863c2012-02-24 00:37:51 +01008814_PyUnicode_InsertThousandsGrouping(
8815 PyObject *unicode, Py_ssize_t index,
8816 Py_ssize_t n_buffer,
8817 void *digits, Py_ssize_t n_digits,
8818 Py_ssize_t min_width,
8819 const char *grouping, PyObject *thousands_sep,
8820 Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008821{
Victor Stinner41a863c2012-02-24 00:37:51 +01008822 unsigned int kind, thousands_sep_kind;
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008823 char *data, *thousands_sep_data;
Victor Stinner41a863c2012-02-24 00:37:51 +01008824 Py_ssize_t thousands_sep_len;
8825 Py_ssize_t len;
8826
8827 if (unicode != NULL) {
8828 kind = PyUnicode_KIND(unicode);
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008829 data = (char *) PyUnicode_DATA(unicode) + index * kind;
Victor Stinner41a863c2012-02-24 00:37:51 +01008830 }
8831 else {
8832 kind = PyUnicode_1BYTE_KIND;
8833 data = NULL;
8834 }
8835 thousands_sep_kind = PyUnicode_KIND(thousands_sep);
8836 thousands_sep_data = PyUnicode_DATA(thousands_sep);
8837 thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep);
8838 if (unicode != NULL && thousands_sep_kind != kind) {
Victor Stinner90f50d42012-02-24 01:44:47 +01008839 if (thousands_sep_kind < kind) {
8840 thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind);
8841 if (!thousands_sep_data)
8842 return -1;
8843 }
8844 else {
8845 data = _PyUnicode_AsKind(unicode, thousands_sep_kind);
8846 if (!data)
8847 return -1;
8848 }
Victor Stinner41a863c2012-02-24 00:37:51 +01008849 }
8850
Benjamin Petersonead6b532011-12-20 17:23:42 -06008851 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008852 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02008853 if (unicode != NULL && PyUnicode_IS_ASCII(unicode))
Victor Stinner41a863c2012-02-24 00:37:51 +01008854 len = asciilib_InsertThousandsGrouping(
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008855 (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits,
Victor Stinner41a863c2012-02-24 00:37:51 +01008856 min_width, grouping,
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008857 (Py_UCS1 *) thousands_sep_data, thousands_sep_len);
Victor Stinnerc3cec782011-10-05 21:24:08 +02008858 else
Victor Stinner41a863c2012-02-24 00:37:51 +01008859 len = ucs1lib_InsertThousandsGrouping(
Victor Stinnerc3cec782011-10-05 21:24:08 +02008860 (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits,
Victor Stinner41a863c2012-02-24 00:37:51 +01008861 min_width, grouping,
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008862 (Py_UCS1 *) thousands_sep_data, thousands_sep_len);
Victor Stinner41a863c2012-02-24 00:37:51 +01008863 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008864 case PyUnicode_2BYTE_KIND:
Victor Stinner41a863c2012-02-24 00:37:51 +01008865 len = ucs2lib_InsertThousandsGrouping(
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008866 (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits,
Victor Stinner41a863c2012-02-24 00:37:51 +01008867 min_width, grouping,
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008868 (Py_UCS2 *) thousands_sep_data, thousands_sep_len);
Victor Stinner41a863c2012-02-24 00:37:51 +01008869 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008870 case PyUnicode_4BYTE_KIND:
Victor Stinner41a863c2012-02-24 00:37:51 +01008871 len = ucs4lib_InsertThousandsGrouping(
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008872 (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits,
Victor Stinner41a863c2012-02-24 00:37:51 +01008873 min_width, grouping,
Antoine Pitrou842c0f12012-02-24 13:30:46 +01008874 (Py_UCS4 *) thousands_sep_data, thousands_sep_len);
Victor Stinner41a863c2012-02-24 00:37:51 +01008875 break;
8876 default:
8877 assert(0);
8878 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008879 }
Victor Stinner90f50d42012-02-24 01:44:47 +01008880 if (unicode != NULL && thousands_sep_kind != kind) {
8881 if (thousands_sep_kind < kind)
8882 PyMem_Free(thousands_sep_data);
8883 else
8884 PyMem_Free(data);
8885 }
Victor Stinner41a863c2012-02-24 00:37:51 +01008886 if (unicode == NULL) {
8887 *maxchar = 127;
8888 if (len != n_digits) {
Victor Stinnere6abb482012-05-02 01:15:40 +02008889 *maxchar = MAX_MAXCHAR(*maxchar,
8890 PyUnicode_MAX_CHAR_VALUE(thousands_sep));
Victor Stinner41a863c2012-02-24 00:37:51 +01008891 }
8892 }
8893 return len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008894}
8895
8896
Thomas Wouters477c8d52006-05-27 19:21:47 +00008897/* helper macro to fixup start/end slice values */
Antoine Pitrouf2c54842010-01-13 08:07:53 +00008898#define ADJUST_INDICES(start, end, len) \
8899 if (end > len) \
8900 end = len; \
8901 else if (end < 0) { \
8902 end += len; \
8903 if (end < 0) \
8904 end = 0; \
8905 } \
8906 if (start < 0) { \
8907 start += len; \
8908 if (start < 0) \
8909 start = 0; \
8910 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008911
Alexander Belopolsky40018472011-02-26 01:02:56 +00008912Py_ssize_t
8913PyUnicode_Count(PyObject *str,
8914 PyObject *substr,
8915 Py_ssize_t start,
8916 Py_ssize_t end)
Guido van Rossumd57fd912000-03-10 22:53:23 +00008917{
Martin v. Löwis18e16552006-02-15 17:27:45 +00008918 Py_ssize_t result;
Victor Stinner9db1a8b2011-10-23 20:04:37 +02008919 PyObject* str_obj;
8920 PyObject* sub_obj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008921 int kind1, kind2, kind;
8922 void *buf1 = NULL, *buf2 = NULL;
8923 Py_ssize_t len1, len2;
Tim Petersced69f82003-09-16 20:30:58 +00008924
Victor Stinner9db1a8b2011-10-23 20:04:37 +02008925 str_obj = PyUnicode_FromObject(str);
Benjamin Peterson22a29702012-01-02 09:00:30 -06008926 if (!str_obj)
Benjamin Peterson29060642009-01-31 22:14:21 +00008927 return -1;
Victor Stinner9db1a8b2011-10-23 20:04:37 +02008928 sub_obj = PyUnicode_FromObject(substr);
Benjamin Peterson22a29702012-01-02 09:00:30 -06008929 if (!sub_obj) {
8930 Py_DECREF(str_obj);
8931 return -1;
8932 }
Benjamin Peterson4c13a4a2012-01-02 09:07:38 -06008933 if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) {
Benjamin Peterson5e458f52012-01-02 10:12:13 -06008934 Py_DECREF(sub_obj);
Benjamin Peterson29060642009-01-31 22:14:21 +00008935 Py_DECREF(str_obj);
8936 return -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00008937 }
Tim Petersced69f82003-09-16 20:30:58 +00008938
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008939 kind1 = PyUnicode_KIND(str_obj);
8940 kind2 = PyUnicode_KIND(sub_obj);
Antoine Pitroue45c0c52012-05-12 15:49:07 +02008941 kind = kind1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008942 buf1 = PyUnicode_DATA(str_obj);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008943 buf2 = PyUnicode_DATA(sub_obj);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -05008944 if (kind2 != kind) {
Antoine Pitrou758153b2012-05-12 15:51:51 +02008945 if (kind2 > kind) {
8946 Py_DECREF(sub_obj);
8947 Py_DECREF(str_obj);
Antoine Pitroue45c0c52012-05-12 15:49:07 +02008948 return 0;
Antoine Pitrou758153b2012-05-12 15:51:51 +02008949 }
Victor Stinner7931d9a2011-11-04 00:22:48 +01008950 buf2 = _PyUnicode_AsKind(sub_obj, kind);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -05008951 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008952 if (!buf2)
8953 goto onError;
8954 len1 = PyUnicode_GET_LENGTH(str_obj);
8955 len2 = PyUnicode_GET_LENGTH(sub_obj);
8956
8957 ADJUST_INDICES(start, end, len1);
Benjamin Petersonead6b532011-12-20 17:23:42 -06008958 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008959 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02008960 if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj))
8961 result = asciilib_count(
8962 ((Py_UCS1*)buf1) + start, end - start,
8963 buf2, len2, PY_SSIZE_T_MAX
8964 );
8965 else
8966 result = ucs1lib_count(
8967 ((Py_UCS1*)buf1) + start, end - start,
8968 buf2, len2, PY_SSIZE_T_MAX
8969 );
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008970 break;
8971 case PyUnicode_2BYTE_KIND:
8972 result = ucs2lib_count(
8973 ((Py_UCS2*)buf1) + start, end - start,
8974 buf2, len2, PY_SSIZE_T_MAX
8975 );
8976 break;
8977 case PyUnicode_4BYTE_KIND:
8978 result = ucs4lib_count(
8979 ((Py_UCS4*)buf1) + start, end - start,
8980 buf2, len2, PY_SSIZE_T_MAX
8981 );
8982 break;
8983 default:
8984 assert(0); result = 0;
8985 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008986
8987 Py_DECREF(sub_obj);
8988 Py_DECREF(str_obj);
8989
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008990 if (kind2 != kind)
8991 PyMem_Free(buf2);
8992
Guido van Rossumd57fd912000-03-10 22:53:23 +00008993 return result;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008994 onError:
8995 Py_DECREF(sub_obj);
8996 Py_DECREF(str_obj);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008997 if (kind2 != kind && buf2)
8998 PyMem_Free(buf2);
8999 return -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009000}
9001
Alexander Belopolsky40018472011-02-26 01:02:56 +00009002Py_ssize_t
9003PyUnicode_Find(PyObject *str,
9004 PyObject *sub,
9005 Py_ssize_t start,
9006 Py_ssize_t end,
9007 int direction)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009008{
Martin v. Löwis18e16552006-02-15 17:27:45 +00009009 Py_ssize_t result;
Tim Petersced69f82003-09-16 20:30:58 +00009010
Guido van Rossumd57fd912000-03-10 22:53:23 +00009011 str = PyUnicode_FromObject(str);
Benjamin Peterson22a29702012-01-02 09:00:30 -06009012 if (!str)
Benjamin Peterson29060642009-01-31 22:14:21 +00009013 return -2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00009014 sub = PyUnicode_FromObject(sub);
Benjamin Peterson22a29702012-01-02 09:00:30 -06009015 if (!sub) {
9016 Py_DECREF(str);
9017 return -2;
9018 }
9019 if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) {
9020 Py_DECREF(sub);
Benjamin Peterson29060642009-01-31 22:14:21 +00009021 Py_DECREF(str);
9022 return -2;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009023 }
Tim Petersced69f82003-09-16 20:30:58 +00009024
Victor Stinner794d5672011-10-10 03:21:36 +02009025 result = any_find_slice(direction,
9026 str, sub, start, end
9027 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00009028
Guido van Rossumd57fd912000-03-10 22:53:23 +00009029 Py_DECREF(str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00009030 Py_DECREF(sub);
9031
Guido van Rossumd57fd912000-03-10 22:53:23 +00009032 return result;
9033}
9034
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009035Py_ssize_t
9036PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
9037 Py_ssize_t start, Py_ssize_t end,
9038 int direction)
9039{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009040 int kind;
Antoine Pitrouf0b934b2011-10-13 18:55:09 +02009041 Py_ssize_t result;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009042 if (PyUnicode_READY(str) == -1)
9043 return -2;
Victor Stinner267aa242011-10-02 01:08:37 +02009044 if (start < 0 || end < 0) {
9045 PyErr_SetString(PyExc_IndexError, "string index out of range");
9046 return -2;
9047 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009048 if (end > PyUnicode_GET_LENGTH(str))
9049 end = PyUnicode_GET_LENGTH(str);
9050 kind = PyUnicode_KIND(str);
Antoine Pitrouf0b934b2011-10-13 18:55:09 +02009051 result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start,
9052 kind, end-start, ch, direction);
9053 if (result == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009054 return -1;
Antoine Pitrouf0b934b2011-10-13 18:55:09 +02009055 else
9056 return start + result;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009057}
9058
Alexander Belopolsky40018472011-02-26 01:02:56 +00009059static int
Victor Stinner9db1a8b2011-10-23 20:04:37 +02009060tailmatch(PyObject *self,
9061 PyObject *substring,
Alexander Belopolsky40018472011-02-26 01:02:56 +00009062 Py_ssize_t start,
9063 Py_ssize_t end,
9064 int direction)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009065{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009066 int kind_self;
9067 int kind_sub;
9068 void *data_self;
9069 void *data_sub;
9070 Py_ssize_t offset;
9071 Py_ssize_t i;
9072 Py_ssize_t end_sub;
9073
9074 if (PyUnicode_READY(self) == -1 ||
9075 PyUnicode_READY(substring) == -1)
9076 return 0;
9077
9078 if (PyUnicode_GET_LENGTH(substring) == 0)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009079 return 1;
9080
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009081 ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self));
9082 end -= PyUnicode_GET_LENGTH(substring);
Guido van Rossumd57fd912000-03-10 22:53:23 +00009083 if (end < start)
Benjamin Peterson29060642009-01-31 22:14:21 +00009084 return 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009085
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009086 kind_self = PyUnicode_KIND(self);
9087 data_self = PyUnicode_DATA(self);
9088 kind_sub = PyUnicode_KIND(substring);
9089 data_sub = PyUnicode_DATA(substring);
9090 end_sub = PyUnicode_GET_LENGTH(substring) - 1;
9091
9092 if (direction > 0)
9093 offset = end;
9094 else
9095 offset = start;
9096
9097 if (PyUnicode_READ(kind_self, data_self, offset) ==
9098 PyUnicode_READ(kind_sub, data_sub, 0) &&
9099 PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
9100 PyUnicode_READ(kind_sub, data_sub, end_sub)) {
9101 /* If both are of the same kind, memcmp is sufficient */
9102 if (kind_self == kind_sub) {
9103 return ! memcmp((char *)data_self +
Martin v. Löwisc47adb02011-10-07 20:55:35 +02009104 (offset * PyUnicode_KIND(substring)),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009105 data_sub,
9106 PyUnicode_GET_LENGTH(substring) *
Martin v. Löwisc47adb02011-10-07 20:55:35 +02009107 PyUnicode_KIND(substring));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009108 }
9109 /* otherwise we have to compare each character by first accesing it */
9110 else {
9111 /* We do not need to compare 0 and len(substring)-1 because
9112 the if statement above ensured already that they are equal
9113 when we end up here. */
9114 // TODO: honor direction and do a forward or backwards search
9115 for (i = 1; i < end_sub; ++i) {
9116 if (PyUnicode_READ(kind_self, data_self, offset + i) !=
9117 PyUnicode_READ(kind_sub, data_sub, i))
9118 return 0;
9119 }
Benjamin Peterson29060642009-01-31 22:14:21 +00009120 return 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009121 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00009122 }
9123
9124 return 0;
9125}
9126
Alexander Belopolsky40018472011-02-26 01:02:56 +00009127Py_ssize_t
9128PyUnicode_Tailmatch(PyObject *str,
9129 PyObject *substr,
9130 Py_ssize_t start,
9131 Py_ssize_t end,
9132 int direction)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009133{
Martin v. Löwis18e16552006-02-15 17:27:45 +00009134 Py_ssize_t result;
Tim Petersced69f82003-09-16 20:30:58 +00009135
Guido van Rossumd57fd912000-03-10 22:53:23 +00009136 str = PyUnicode_FromObject(str);
9137 if (str == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00009138 return -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009139 substr = PyUnicode_FromObject(substr);
9140 if (substr == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +00009141 Py_DECREF(str);
9142 return -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009143 }
Tim Petersced69f82003-09-16 20:30:58 +00009144
Victor Stinner9db1a8b2011-10-23 20:04:37 +02009145 result = tailmatch(str, substr,
Benjamin Peterson29060642009-01-31 22:14:21 +00009146 start, end, direction);
Guido van Rossumd57fd912000-03-10 22:53:23 +00009147 Py_DECREF(str);
9148 Py_DECREF(substr);
9149 return result;
9150}
9151
Guido van Rossumd57fd912000-03-10 22:53:23 +00009152/* Apply fixfct filter to the Unicode object self and return a
9153 reference to the modified object */
9154
Alexander Belopolsky40018472011-02-26 01:02:56 +00009155static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +02009156fixup(PyObject *self,
9157 Py_UCS4 (*fixfct)(PyObject *s))
Guido van Rossumd57fd912000-03-10 22:53:23 +00009158{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009159 PyObject *u;
9160 Py_UCS4 maxchar_old, maxchar_new = 0;
Victor Stinnereaab6042011-12-11 22:22:39 +01009161 PyObject *v;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009162
Victor Stinnerbf6e5602011-12-12 01:53:47 +01009163 u = _PyUnicode_Copy(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +00009164 if (u == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00009165 return NULL;
Victor Stinner87af4f22011-11-21 23:03:47 +01009166 maxchar_old = PyUnicode_MAX_CHAR_VALUE(u);
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00009167
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009168 /* fix functions return the new maximum character in a string,
9169 if the kind of the resulting unicode object does not change,
9170 everything is fine. Otherwise we need to change the string kind
9171 and re-run the fix function. */
Victor Stinner9310abb2011-10-05 00:59:23 +02009172 maxchar_new = fixfct(u);
Victor Stinnereaab6042011-12-11 22:22:39 +01009173
9174 if (maxchar_new == 0) {
9175 /* no changes */;
9176 if (PyUnicode_CheckExact(self)) {
9177 Py_DECREF(u);
9178 Py_INCREF(self);
9179 return self;
9180 }
9181 else
9182 return u;
9183 }
9184
Victor Stinnere6abb482012-05-02 01:15:40 +02009185 maxchar_new = align_maxchar(maxchar_new);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009186
Victor Stinnereaab6042011-12-11 22:22:39 +01009187 if (maxchar_new == maxchar_old)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009188 return u;
Victor Stinnereaab6042011-12-11 22:22:39 +01009189
9190 /* In case the maximum character changed, we need to
9191 convert the string to the new category. */
9192 v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new);
9193 if (v == NULL) {
9194 Py_DECREF(u);
9195 return NULL;
9196 }
9197 if (maxchar_new > maxchar_old) {
9198 /* If the maxchar increased so that the kind changed, not all
9199 characters are representable anymore and we need to fix the
9200 string again. This only happens in very few cases. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02009201 _PyUnicode_FastCopyCharacters(v, 0,
9202 self, 0, PyUnicode_GET_LENGTH(self));
Victor Stinnereaab6042011-12-11 22:22:39 +01009203 maxchar_old = fixfct(v);
9204 assert(maxchar_old > 0 && maxchar_old <= maxchar_new);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009205 }
9206 else {
Victor Stinnerd3f08822012-05-29 12:57:52 +02009207 _PyUnicode_FastCopyCharacters(v, 0,
9208 u, 0, PyUnicode_GET_LENGTH(self));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009209 }
Victor Stinnereaab6042011-12-11 22:22:39 +01009210 Py_DECREF(u);
9211 assert(_PyUnicode_CheckConsistency(v, 1));
9212 return v;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009213}
9214
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009215static PyObject *
9216ascii_upper_or_lower(PyObject *self, int lower)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009217{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009218 Py_ssize_t len = PyUnicode_GET_LENGTH(self);
9219 char *resdata, *data = PyUnicode_DATA(self);
9220 PyObject *res;
Tim Petersced69f82003-09-16 20:30:58 +00009221
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009222 res = PyUnicode_New(len, 127);
9223 if (res == NULL)
9224 return NULL;
9225 resdata = PyUnicode_DATA(res);
9226 if (lower)
9227 _Py_bytes_lower(resdata, data, len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009228 else
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009229 _Py_bytes_upper(resdata, data, len);
9230 return res;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009231}
9232
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009233static Py_UCS4
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009234handle_capital_sigma(int kind, void *data, Py_ssize_t length, Py_ssize_t i)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009235{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009236 Py_ssize_t j;
9237 int final_sigma;
9238 Py_UCS4 c;
9239 /* U+03A3 is in the Final_Sigma context when, it is found like this:
Tim Petersced69f82003-09-16 20:30:58 +00009240
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009241 \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased})
9242
9243 where ! is a negation and \p{xxx} is a character with property xxx.
9244 */
9245 for (j = i - 1; j >= 0; j--) {
9246 c = PyUnicode_READ(kind, data, j);
9247 if (!_PyUnicode_IsCaseIgnorable(c))
9248 break;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009249 }
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009250 final_sigma = j >= 0 && _PyUnicode_IsCased(c);
9251 if (final_sigma) {
9252 for (j = i + 1; j < length; j++) {
9253 c = PyUnicode_READ(kind, data, j);
9254 if (!_PyUnicode_IsCaseIgnorable(c))
9255 break;
9256 }
9257 final_sigma = j == length || !_PyUnicode_IsCased(c);
9258 }
9259 return (final_sigma) ? 0x3C2 : 0x3C3;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009260}
9261
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009262static int
9263lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i,
9264 Py_UCS4 c, Py_UCS4 *mapped)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009265{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009266 /* Obscure special case. */
9267 if (c == 0x3A3) {
9268 mapped[0] = handle_capital_sigma(kind, data, length, i);
9269 return 1;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009270 }
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009271 return _PyUnicode_ToLowerFull(c, mapped);
Guido van Rossumd57fd912000-03-10 22:53:23 +00009272}
9273
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009274static Py_ssize_t
9275do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009276{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009277 Py_ssize_t i, k = 0;
9278 int n_res, j;
9279 Py_UCS4 c, mapped[3];
Tim Petersced69f82003-09-16 20:30:58 +00009280
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009281 c = PyUnicode_READ(kind, data, 0);
9282 n_res = _PyUnicode_ToUpperFull(c, mapped);
9283 for (j = 0; j < n_res; j++) {
Victor Stinnere6abb482012-05-02 01:15:40 +02009284 *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]);
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009285 res[k++] = mapped[j];
Guido van Rossumd57fd912000-03-10 22:53:23 +00009286 }
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009287 for (i = 1; i < length; i++) {
9288 c = PyUnicode_READ(kind, data, i);
9289 n_res = lower_ucs4(kind, data, length, i, c, mapped);
9290 for (j = 0; j < n_res; j++) {
Victor Stinnere6abb482012-05-02 01:15:40 +02009291 *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]);
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009292 res[k++] = mapped[j];
Marc-André Lemburgfde66e12001-01-29 11:14:16 +00009293 }
Marc-André Lemburgfde66e12001-01-29 11:14:16 +00009294 }
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009295 return k;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009296}
9297
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009298static Py_ssize_t
9299do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) {
9300 Py_ssize_t i, k = 0;
9301
9302 for (i = 0; i < length; i++) {
9303 Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
9304 int n_res, j;
9305 if (Py_UNICODE_ISUPPER(c)) {
9306 n_res = lower_ucs4(kind, data, length, i, c, mapped);
9307 }
9308 else if (Py_UNICODE_ISLOWER(c)) {
9309 n_res = _PyUnicode_ToUpperFull(c, mapped);
9310 }
9311 else {
9312 n_res = 1;
9313 mapped[0] = c;
9314 }
9315 for (j = 0; j < n_res; j++) {
Victor Stinnere6abb482012-05-02 01:15:40 +02009316 *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]);
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009317 res[k++] = mapped[j];
9318 }
9319 }
9320 return k;
9321}
9322
9323static Py_ssize_t
9324do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res,
9325 Py_UCS4 *maxchar, int lower)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009326{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009327 Py_ssize_t i, k = 0;
9328
9329 for (i = 0; i < length; i++) {
9330 Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
9331 int n_res, j;
9332 if (lower)
9333 n_res = lower_ucs4(kind, data, length, i, c, mapped);
9334 else
9335 n_res = _PyUnicode_ToUpperFull(c, mapped);
9336 for (j = 0; j < n_res; j++) {
Victor Stinnere6abb482012-05-02 01:15:40 +02009337 *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]);
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009338 res[k++] = mapped[j];
9339 }
9340 }
9341 return k;
9342}
9343
9344static Py_ssize_t
9345do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
9346{
9347 return do_upper_or_lower(kind, data, length, res, maxchar, 0);
9348}
9349
9350static Py_ssize_t
9351do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
9352{
9353 return do_upper_or_lower(kind, data, length, res, maxchar, 1);
9354}
9355
Benjamin Petersone51757f2012-01-12 21:10:29 -05009356static Py_ssize_t
Benjamin Petersond5890c82012-01-14 13:23:30 -05009357do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
9358{
9359 Py_ssize_t i, k = 0;
9360
9361 for (i = 0; i < length; i++) {
9362 Py_UCS4 c = PyUnicode_READ(kind, data, i);
9363 Py_UCS4 mapped[3];
9364 int j, n_res = _PyUnicode_ToFoldedFull(c, mapped);
9365 for (j = 0; j < n_res; j++) {
Victor Stinnere6abb482012-05-02 01:15:40 +02009366 *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]);
Benjamin Petersond5890c82012-01-14 13:23:30 -05009367 res[k++] = mapped[j];
9368 }
9369 }
9370 return k;
9371}
9372
9373static Py_ssize_t
Benjamin Petersone51757f2012-01-12 21:10:29 -05009374do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar)
9375{
9376 Py_ssize_t i, k = 0;
9377 int previous_is_cased;
9378
9379 previous_is_cased = 0;
9380 for (i = 0; i < length; i++) {
9381 const Py_UCS4 c = PyUnicode_READ(kind, data, i);
9382 Py_UCS4 mapped[3];
9383 int n_res, j;
9384
9385 if (previous_is_cased)
9386 n_res = lower_ucs4(kind, data, length, i, c, mapped);
9387 else
9388 n_res = _PyUnicode_ToTitleFull(c, mapped);
9389
9390 for (j = 0; j < n_res; j++) {
Victor Stinnere6abb482012-05-02 01:15:40 +02009391 *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]);
Benjamin Petersone51757f2012-01-12 21:10:29 -05009392 res[k++] = mapped[j];
9393 }
9394
9395 previous_is_cased = _PyUnicode_IsCased(c);
9396 }
9397 return k;
9398}
9399
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009400static PyObject *
9401case_operation(PyObject *self,
9402 Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *))
9403{
9404 PyObject *res = NULL;
9405 Py_ssize_t length, newlength = 0;
9406 int kind, outkind;
9407 void *data, *outdata;
9408 Py_UCS4 maxchar = 0, *tmp, *tmpend;
9409
Benjamin Petersoneea48462012-01-16 14:28:50 -05009410 assert(PyUnicode_IS_READY(self));
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05009411
9412 kind = PyUnicode_KIND(self);
9413 data = PyUnicode_DATA(self);
9414 length = PyUnicode_GET_LENGTH(self);
9415 tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length);
9416 if (tmp == NULL)
9417 return PyErr_NoMemory();
9418 newlength = perform(kind, data, length, tmp, &maxchar);
9419 res = PyUnicode_New(newlength, maxchar);
9420 if (res == NULL)
9421 goto leave;
9422 tmpend = tmp + newlength;
9423 outdata = PyUnicode_DATA(res);
9424 outkind = PyUnicode_KIND(res);
9425 switch (outkind) {
9426 case PyUnicode_1BYTE_KIND:
9427 _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata);
9428 break;
9429 case PyUnicode_2BYTE_KIND:
9430 _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata);
9431 break;
9432 case PyUnicode_4BYTE_KIND:
9433 memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength);
9434 break;
9435 default:
9436 assert(0);
9437 break;
9438 }
9439 leave:
9440 PyMem_FREE(tmp);
9441 return res;
9442}
9443
Tim Peters8ce9f162004-08-27 01:49:32 +00009444PyObject *
9445PyUnicode_Join(PyObject *separator, PyObject *seq)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009446{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009447 PyObject *sep = NULL;
Victor Stinnerdd077322011-10-07 17:02:31 +02009448 Py_ssize_t seplen;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009449 PyObject *res = NULL; /* the result */
Tim Peters05eba1f2004-08-27 21:32:02 +00009450 PyObject *fseq; /* PySequence_Fast(seq) */
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009451 Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */
9452 PyObject **items;
Tim Peters8ce9f162004-08-27 01:49:32 +00009453 PyObject *item;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009454 Py_ssize_t sz, i, res_offset;
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02009455 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009456 Py_UCS4 item_maxchar;
Victor Stinnerdd077322011-10-07 17:02:31 +02009457 int use_memcpy;
9458 unsigned char *res_data = NULL, *sep_data = NULL;
9459 PyObject *last_obj;
9460 unsigned int kind = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009461
Tim Peters05eba1f2004-08-27 21:32:02 +00009462 fseq = PySequence_Fast(seq, "");
9463 if (fseq == NULL) {
Benjamin Peterson14339b62009-01-31 16:36:08 +00009464 return NULL;
Tim Peters8ce9f162004-08-27 01:49:32 +00009465 }
9466
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009467 /* NOTE: the following code can't call back into Python code,
9468 * so we are sure that fseq won't be mutated.
Tim Peters91879ab2004-08-27 22:35:44 +00009469 */
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009470
Tim Peters05eba1f2004-08-27 21:32:02 +00009471 seqlen = PySequence_Fast_GET_SIZE(fseq);
9472 /* If empty sequence, return u"". */
9473 if (seqlen == 0) {
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02009474 Py_DECREF(fseq);
9475 Py_INCREF(unicode_empty);
9476 res = unicode_empty;
9477 return res;
Tim Peters05eba1f2004-08-27 21:32:02 +00009478 }
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02009479
Tim Peters05eba1f2004-08-27 21:32:02 +00009480 /* If singleton sequence with an exact Unicode, return that. */
Victor Stinnerdd077322011-10-07 17:02:31 +02009481 last_obj = NULL;
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02009482 items = PySequence_Fast_ITEMS(fseq);
Victor Stinneracf47b82011-10-06 12:32:37 +02009483 if (seqlen == 1) {
9484 if (PyUnicode_CheckExact(items[0])) {
9485 res = items[0];
9486 Py_INCREF(res);
9487 Py_DECREF(fseq);
9488 return res;
9489 }
Victor Stinnerdd077322011-10-07 17:02:31 +02009490 seplen = 0;
Victor Stinnerc6f0df72011-10-06 15:58:54 +02009491 maxchar = 0;
Tim Peters8ce9f162004-08-27 01:49:32 +00009492 }
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009493 else {
Victor Stinneracf47b82011-10-06 12:32:37 +02009494 /* Set up sep and seplen */
9495 if (separator == NULL) {
9496 /* fall back to a blank space separator */
9497 sep = PyUnicode_FromOrdinal(' ');
9498 if (!sep)
9499 goto onError;
Victor Stinnerdd077322011-10-07 17:02:31 +02009500 seplen = 1;
Victor Stinneracf47b82011-10-06 12:32:37 +02009501 maxchar = 32;
Tim Peters05eba1f2004-08-27 21:32:02 +00009502 }
Victor Stinneracf47b82011-10-06 12:32:37 +02009503 else {
9504 if (!PyUnicode_Check(separator)) {
9505 PyErr_Format(PyExc_TypeError,
9506 "separator: expected str instance,"
9507 " %.80s found",
9508 Py_TYPE(separator)->tp_name);
9509 goto onError;
9510 }
9511 if (PyUnicode_READY(separator))
9512 goto onError;
9513 sep = separator;
9514 seplen = PyUnicode_GET_LENGTH(separator);
9515 maxchar = PyUnicode_MAX_CHAR_VALUE(separator);
9516 /* inc refcount to keep this code path symmetric with the
9517 above case of a blank separator */
9518 Py_INCREF(sep);
9519 }
Victor Stinnerdd077322011-10-07 17:02:31 +02009520 last_obj = sep;
Tim Peters05eba1f2004-08-27 21:32:02 +00009521 }
9522
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009523 /* There are at least two things to join, or else we have a subclass
9524 * of str in the sequence.
9525 * Do a pre-pass to figure out the total amount of space we'll
9526 * need (sz), and see whether all argument are strings.
9527 */
9528 sz = 0;
Victor Stinnerdd077322011-10-07 17:02:31 +02009529#ifdef Py_DEBUG
9530 use_memcpy = 0;
9531#else
9532 use_memcpy = 1;
9533#endif
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009534 for (i = 0; i < seqlen; i++) {
9535 const Py_ssize_t old_sz = sz;
9536 item = items[i];
Benjamin Peterson29060642009-01-31 22:14:21 +00009537 if (!PyUnicode_Check(item)) {
9538 PyErr_Format(PyExc_TypeError,
9539 "sequence item %zd: expected str instance,"
9540 " %.80s found",
9541 i, Py_TYPE(item)->tp_name);
9542 goto onError;
9543 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009544 if (PyUnicode_READY(item) == -1)
9545 goto onError;
9546 sz += PyUnicode_GET_LENGTH(item);
9547 item_maxchar = PyUnicode_MAX_CHAR_VALUE(item);
Victor Stinnere6abb482012-05-02 01:15:40 +02009548 maxchar = MAX_MAXCHAR(maxchar, item_maxchar);
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009549 if (i != 0)
9550 sz += seplen;
9551 if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
9552 PyErr_SetString(PyExc_OverflowError,
Benjamin Peterson29060642009-01-31 22:14:21 +00009553 "join() result is too long for a Python string");
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009554 goto onError;
9555 }
Victor Stinnerdd077322011-10-07 17:02:31 +02009556 if (use_memcpy && last_obj != NULL) {
9557 if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item))
9558 use_memcpy = 0;
9559 }
9560 last_obj = item;
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009561 }
Tim Petersced69f82003-09-16 20:30:58 +00009562
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009563 res = PyUnicode_New(sz, maxchar);
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009564 if (res == NULL)
9565 goto onError;
Tim Peters91879ab2004-08-27 22:35:44 +00009566
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009567 /* Catenate everything. */
Victor Stinnerdd077322011-10-07 17:02:31 +02009568#ifdef Py_DEBUG
9569 use_memcpy = 0;
9570#else
9571 if (use_memcpy) {
9572 res_data = PyUnicode_1BYTE_DATA(res);
9573 kind = PyUnicode_KIND(res);
9574 if (seplen != 0)
9575 sep_data = PyUnicode_1BYTE_DATA(sep);
9576 }
9577#endif
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009578 for (i = 0, res_offset = 0; i < seqlen; ++i) {
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +02009579 Py_ssize_t itemlen;
Antoine Pitrouaf14b792008-08-07 21:50:41 +00009580 item = items[i];
Benjamin Peterson29060642009-01-31 22:14:21 +00009581 /* Copy item, and maybe the separator. */
Victor Stinner9ce5a832011-10-03 23:36:02 +02009582 if (i && seplen != 0) {
Victor Stinnerdd077322011-10-07 17:02:31 +02009583 if (use_memcpy) {
9584 Py_MEMCPY(res_data,
9585 sep_data,
Martin v. Löwisc47adb02011-10-07 20:55:35 +02009586 kind * seplen);
9587 res_data += kind * seplen;
Victor Stinnerdd077322011-10-07 17:02:31 +02009588 }
9589 else {
Victor Stinnerd3f08822012-05-29 12:57:52 +02009590 _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen);
Victor Stinnerdd077322011-10-07 17:02:31 +02009591 res_offset += seplen;
9592 }
Benjamin Peterson29060642009-01-31 22:14:21 +00009593 }
Victor Stinner9ce5a832011-10-03 23:36:02 +02009594 itemlen = PyUnicode_GET_LENGTH(item);
9595 if (itemlen != 0) {
Victor Stinnerdd077322011-10-07 17:02:31 +02009596 if (use_memcpy) {
9597 Py_MEMCPY(res_data,
9598 PyUnicode_DATA(item),
Martin v. Löwisc47adb02011-10-07 20:55:35 +02009599 kind * itemlen);
9600 res_data += kind * itemlen;
Victor Stinnerdd077322011-10-07 17:02:31 +02009601 }
9602 else {
Victor Stinnerd3f08822012-05-29 12:57:52 +02009603 _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen);
Victor Stinnerdd077322011-10-07 17:02:31 +02009604 res_offset += itemlen;
9605 }
Victor Stinner9ce5a832011-10-03 23:36:02 +02009606 }
Tim Peters05eba1f2004-08-27 21:32:02 +00009607 }
Victor Stinnerdd077322011-10-07 17:02:31 +02009608 if (use_memcpy)
9609 assert(res_data == PyUnicode_1BYTE_DATA(res)
Martin v. Löwisc47adb02011-10-07 20:55:35 +02009610 + kind * PyUnicode_GET_LENGTH(res));
Victor Stinnerdd077322011-10-07 17:02:31 +02009611 else
9612 assert(res_offset == PyUnicode_GET_LENGTH(res));
Tim Peters8ce9f162004-08-27 01:49:32 +00009613
Tim Peters05eba1f2004-08-27 21:32:02 +00009614 Py_DECREF(fseq);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009615 Py_XDECREF(sep);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02009616 assert(_PyUnicode_CheckConsistency(res, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009617 return res;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009618
Benjamin Peterson29060642009-01-31 22:14:21 +00009619 onError:
Tim Peters05eba1f2004-08-27 21:32:02 +00009620 Py_DECREF(fseq);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009621 Py_XDECREF(sep);
Tim Peters8ce9f162004-08-27 01:49:32 +00009622 Py_XDECREF(res);
Guido van Rossumd57fd912000-03-10 22:53:23 +00009623 return NULL;
9624}
9625
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009626#define FILL(kind, data, value, start, length) \
9627 do { \
9628 Py_ssize_t i_ = 0; \
9629 assert(kind != PyUnicode_WCHAR_KIND); \
9630 switch ((kind)) { \
9631 case PyUnicode_1BYTE_KIND: { \
9632 unsigned char * to_ = (unsigned char *)((data)) + (start); \
Victor Stinnerf2c76aa2012-05-03 13:10:40 +02009633 memset(to_, (unsigned char)value, (length)); \
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009634 break; \
9635 } \
9636 case PyUnicode_2BYTE_KIND: { \
9637 Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \
9638 for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
9639 break; \
9640 } \
Benjamin Petersone157cf12012-01-01 15:56:20 -06009641 case PyUnicode_4BYTE_KIND: { \
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009642 Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \
9643 for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
9644 break; \
Benjamin Petersone157cf12012-01-01 15:56:20 -06009645 default: assert(0); \
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009646 } \
9647 } \
9648 } while (0)
9649
Victor Stinnerd3f08822012-05-29 12:57:52 +02009650void
9651_PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length,
9652 Py_UCS4 fill_char)
9653{
9654 const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode);
9655 const void *data = PyUnicode_DATA(unicode);
9656 assert(PyUnicode_IS_READY(unicode));
9657 assert(unicode_modifiable(unicode));
9658 assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode));
9659 assert(start >= 0);
9660 assert(start + length <= PyUnicode_GET_LENGTH(unicode));
9661 FILL(kind, data, fill_char, start, length);
9662}
9663
Victor Stinner3fe55312012-01-04 00:33:50 +01009664Py_ssize_t
9665PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length,
9666 Py_UCS4 fill_char)
9667{
9668 Py_ssize_t maxlen;
Victor Stinner3fe55312012-01-04 00:33:50 +01009669
9670 if (!PyUnicode_Check(unicode)) {
9671 PyErr_BadInternalCall();
9672 return -1;
9673 }
9674 if (PyUnicode_READY(unicode) == -1)
9675 return -1;
9676 if (unicode_check_modifiable(unicode))
9677 return -1;
9678
Victor Stinnerd3f08822012-05-29 12:57:52 +02009679 if (start < 0) {
9680 PyErr_SetString(PyExc_IndexError, "string index out of range");
9681 return -1;
9682 }
Victor Stinner3fe55312012-01-04 00:33:50 +01009683 if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) {
9684 PyErr_SetString(PyExc_ValueError,
9685 "fill character is bigger than "
9686 "the string maximum character");
9687 return -1;
9688 }
9689
9690 maxlen = PyUnicode_GET_LENGTH(unicode) - start;
9691 length = Py_MIN(maxlen, length);
9692 if (length <= 0)
9693 return 0;
9694
Victor Stinnerd3f08822012-05-29 12:57:52 +02009695 _PyUnicode_FastFill(unicode, start, length, fill_char);
Victor Stinner3fe55312012-01-04 00:33:50 +01009696 return length;
9697}
9698
Victor Stinner9310abb2011-10-05 00:59:23 +02009699static PyObject *
9700pad(PyObject *self,
Alexander Belopolsky40018472011-02-26 01:02:56 +00009701 Py_ssize_t left,
9702 Py_ssize_t right,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009703 Py_UCS4 fill)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009704{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009705 PyObject *u;
9706 Py_UCS4 maxchar;
Victor Stinner6c7a52a2011-09-28 21:39:17 +02009707 int kind;
9708 void *data;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009709
9710 if (left < 0)
9711 left = 0;
9712 if (right < 0)
9713 right = 0;
9714
Victor Stinnerc4b49542011-12-11 22:44:26 +01009715 if (left == 0 && right == 0)
9716 return unicode_result_unchanged(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +00009717
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009718 if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) ||
9719 right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) {
Neal Norwitz3ce5d922008-08-24 07:08:55 +00009720 PyErr_SetString(PyExc_OverflowError, "padded string is too long");
9721 return NULL;
9722 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009723 maxchar = PyUnicode_MAX_CHAR_VALUE(self);
Victor Stinnere6abb482012-05-02 01:15:40 +02009724 maxchar = MAX_MAXCHAR(maxchar, fill);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009725 u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar);
Victor Stinner6c7a52a2011-09-28 21:39:17 +02009726 if (!u)
9727 return NULL;
9728
9729 kind = PyUnicode_KIND(u);
9730 data = PyUnicode_DATA(u);
9731 if (left)
9732 FILL(kind, data, fill, 0, left);
9733 if (right)
9734 FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
Victor Stinnerd3f08822012-05-29 12:57:52 +02009735 _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self));
Victor Stinnerbb10a1f2011-10-05 01:34:17 +02009736 assert(_PyUnicode_CheckConsistency(u, 1));
9737 return u;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009738}
9739
Alexander Belopolsky40018472011-02-26 01:02:56 +00009740PyObject *
9741PyUnicode_Splitlines(PyObject *string, int keepends)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009742{
Guido van Rossumd57fd912000-03-10 22:53:23 +00009743 PyObject *list;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009744
9745 string = PyUnicode_FromObject(string);
Benjamin Peterson22a29702012-01-02 09:00:30 -06009746 if (string == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +00009747 return NULL;
Benjamin Peterson22a29702012-01-02 09:00:30 -06009748 if (PyUnicode_READY(string) == -1) {
9749 Py_DECREF(string);
9750 return NULL;
9751 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00009752
Benjamin Petersonead6b532011-12-20 17:23:42 -06009753 switch (PyUnicode_KIND(string)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009754 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02009755 if (PyUnicode_IS_ASCII(string))
9756 list = asciilib_splitlines(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009757 string, PyUnicode_1BYTE_DATA(string),
Victor Stinnerc3cec782011-10-05 21:24:08 +02009758 PyUnicode_GET_LENGTH(string), keepends);
9759 else
9760 list = ucs1lib_splitlines(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009761 string, PyUnicode_1BYTE_DATA(string),
Victor Stinnerc3cec782011-10-05 21:24:08 +02009762 PyUnicode_GET_LENGTH(string), keepends);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009763 break;
9764 case PyUnicode_2BYTE_KIND:
9765 list = ucs2lib_splitlines(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009766 string, PyUnicode_2BYTE_DATA(string),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009767 PyUnicode_GET_LENGTH(string), keepends);
9768 break;
9769 case PyUnicode_4BYTE_KIND:
9770 list = ucs4lib_splitlines(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009771 string, PyUnicode_4BYTE_DATA(string),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009772 PyUnicode_GET_LENGTH(string), keepends);
9773 break;
9774 default:
9775 assert(0);
9776 list = 0;
9777 }
Guido van Rossumd57fd912000-03-10 22:53:23 +00009778 Py_DECREF(string);
9779 return list;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009780}
9781
Alexander Belopolsky40018472011-02-26 01:02:56 +00009782static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +02009783split(PyObject *self,
9784 PyObject *substring,
Alexander Belopolsky40018472011-02-26 01:02:56 +00009785 Py_ssize_t maxcount)
Guido van Rossumd57fd912000-03-10 22:53:23 +00009786{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009787 int kind1, kind2, kind;
9788 void *buf1, *buf2;
9789 Py_ssize_t len1, len2;
9790 PyObject* out;
9791
Guido van Rossumd57fd912000-03-10 22:53:23 +00009792 if (maxcount < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009793 maxcount = PY_SSIZE_T_MAX;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009794
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009795 if (PyUnicode_READY(self) == -1)
9796 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009797
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009798 if (substring == NULL)
Benjamin Petersonead6b532011-12-20 17:23:42 -06009799 switch (PyUnicode_KIND(self)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009800 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02009801 if (PyUnicode_IS_ASCII(self))
9802 return asciilib_split_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009803 self, PyUnicode_1BYTE_DATA(self),
Victor Stinnerc3cec782011-10-05 21:24:08 +02009804 PyUnicode_GET_LENGTH(self), maxcount
9805 );
9806 else
9807 return ucs1lib_split_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009808 self, PyUnicode_1BYTE_DATA(self),
Victor Stinnerc3cec782011-10-05 21:24:08 +02009809 PyUnicode_GET_LENGTH(self), maxcount
9810 );
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009811 case PyUnicode_2BYTE_KIND:
9812 return ucs2lib_split_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009813 self, PyUnicode_2BYTE_DATA(self),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009814 PyUnicode_GET_LENGTH(self), maxcount
9815 );
9816 case PyUnicode_4BYTE_KIND:
9817 return ucs4lib_split_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009818 self, PyUnicode_4BYTE_DATA(self),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009819 PyUnicode_GET_LENGTH(self), maxcount
9820 );
9821 default:
9822 assert(0);
9823 return NULL;
9824 }
9825
9826 if (PyUnicode_READY(substring) == -1)
9827 return NULL;
9828
9829 kind1 = PyUnicode_KIND(self);
9830 kind2 = PyUnicode_KIND(substring);
9831 kind = kind1 > kind2 ? kind1 : kind2;
9832 buf1 = PyUnicode_DATA(self);
9833 buf2 = PyUnicode_DATA(substring);
9834 if (kind1 != kind)
Victor Stinner7931d9a2011-11-04 00:22:48 +01009835 buf1 = _PyUnicode_AsKind(self, kind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009836 if (!buf1)
9837 return NULL;
9838 if (kind2 != kind)
Victor Stinner7931d9a2011-11-04 00:22:48 +01009839 buf2 = _PyUnicode_AsKind(substring, kind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009840 if (!buf2) {
9841 if (kind1 != kind) PyMem_Free(buf1);
9842 return NULL;
9843 }
9844 len1 = PyUnicode_GET_LENGTH(self);
9845 len2 = PyUnicode_GET_LENGTH(substring);
9846
Benjamin Petersonead6b532011-12-20 17:23:42 -06009847 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009848 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02009849 if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring))
9850 out = asciilib_split(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009851 self, buf1, len1, buf2, len2, maxcount);
Victor Stinnerc3cec782011-10-05 21:24:08 +02009852 else
9853 out = ucs1lib_split(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009854 self, buf1, len1, buf2, len2, maxcount);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009855 break;
9856 case PyUnicode_2BYTE_KIND:
9857 out = ucs2lib_split(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009858 self, buf1, len1, buf2, len2, maxcount);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009859 break;
9860 case PyUnicode_4BYTE_KIND:
9861 out = ucs4lib_split(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009862 self, buf1, len1, buf2, len2, maxcount);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009863 break;
9864 default:
9865 out = NULL;
9866 }
9867 if (kind1 != kind)
9868 PyMem_Free(buf1);
9869 if (kind2 != kind)
9870 PyMem_Free(buf2);
9871 return out;
Guido van Rossumd57fd912000-03-10 22:53:23 +00009872}
9873
Alexander Belopolsky40018472011-02-26 01:02:56 +00009874static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +02009875rsplit(PyObject *self,
9876 PyObject *substring,
Alexander Belopolsky40018472011-02-26 01:02:56 +00009877 Py_ssize_t maxcount)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +00009878{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009879 int kind1, kind2, kind;
9880 void *buf1, *buf2;
9881 Py_ssize_t len1, len2;
9882 PyObject* out;
9883
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +00009884 if (maxcount < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009885 maxcount = PY_SSIZE_T_MAX;
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +00009886
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009887 if (PyUnicode_READY(self) == -1)
9888 return NULL;
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +00009889
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009890 if (substring == NULL)
Benjamin Petersonead6b532011-12-20 17:23:42 -06009891 switch (PyUnicode_KIND(self)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009892 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02009893 if (PyUnicode_IS_ASCII(self))
9894 return asciilib_rsplit_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009895 self, PyUnicode_1BYTE_DATA(self),
Victor Stinnerc3cec782011-10-05 21:24:08 +02009896 PyUnicode_GET_LENGTH(self), maxcount
9897 );
9898 else
9899 return ucs1lib_rsplit_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009900 self, PyUnicode_1BYTE_DATA(self),
Victor Stinnerc3cec782011-10-05 21:24:08 +02009901 PyUnicode_GET_LENGTH(self), maxcount
9902 );
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009903 case PyUnicode_2BYTE_KIND:
9904 return ucs2lib_rsplit_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009905 self, PyUnicode_2BYTE_DATA(self),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009906 PyUnicode_GET_LENGTH(self), maxcount
9907 );
9908 case PyUnicode_4BYTE_KIND:
9909 return ucs4lib_rsplit_whitespace(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009910 self, PyUnicode_4BYTE_DATA(self),
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009911 PyUnicode_GET_LENGTH(self), maxcount
9912 );
9913 default:
9914 assert(0);
9915 return NULL;
9916 }
9917
9918 if (PyUnicode_READY(substring) == -1)
9919 return NULL;
9920
9921 kind1 = PyUnicode_KIND(self);
9922 kind2 = PyUnicode_KIND(substring);
9923 kind = kind1 > kind2 ? kind1 : kind2;
9924 buf1 = PyUnicode_DATA(self);
9925 buf2 = PyUnicode_DATA(substring);
9926 if (kind1 != kind)
Victor Stinner7931d9a2011-11-04 00:22:48 +01009927 buf1 = _PyUnicode_AsKind(self, kind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009928 if (!buf1)
9929 return NULL;
9930 if (kind2 != kind)
Victor Stinner7931d9a2011-11-04 00:22:48 +01009931 buf2 = _PyUnicode_AsKind(substring, kind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009932 if (!buf2) {
9933 if (kind1 != kind) PyMem_Free(buf1);
9934 return NULL;
9935 }
9936 len1 = PyUnicode_GET_LENGTH(self);
9937 len2 = PyUnicode_GET_LENGTH(substring);
9938
Benjamin Petersonead6b532011-12-20 17:23:42 -06009939 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009940 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02009941 if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring))
9942 out = asciilib_rsplit(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009943 self, buf1, len1, buf2, len2, maxcount);
Victor Stinnerc3cec782011-10-05 21:24:08 +02009944 else
9945 out = ucs1lib_rsplit(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009946 self, buf1, len1, buf2, len2, maxcount);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009947 break;
9948 case PyUnicode_2BYTE_KIND:
9949 out = ucs2lib_rsplit(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009950 self, buf1, len1, buf2, len2, maxcount);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009951 break;
9952 case PyUnicode_4BYTE_KIND:
9953 out = ucs4lib_rsplit(
Victor Stinner7931d9a2011-11-04 00:22:48 +01009954 self, buf1, len1, buf2, len2, maxcount);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009955 break;
9956 default:
9957 out = NULL;
9958 }
9959 if (kind1 != kind)
9960 PyMem_Free(buf1);
9961 if (kind2 != kind)
9962 PyMem_Free(buf2);
9963 return out;
9964}
9965
9966static Py_ssize_t
Victor Stinnerc3cec782011-10-05 21:24:08 +02009967anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1,
9968 PyObject *str2, void *buf2, Py_ssize_t len2, Py_ssize_t offset)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009969{
Benjamin Petersonead6b532011-12-20 17:23:42 -06009970 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009971 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +02009972 if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2))
9973 return asciilib_find(buf1, len1, buf2, len2, offset);
9974 else
9975 return ucs1lib_find(buf1, len1, buf2, len2, offset);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009976 case PyUnicode_2BYTE_KIND:
9977 return ucs2lib_find(buf1, len1, buf2, len2, offset);
9978 case PyUnicode_4BYTE_KIND:
9979 return ucs4lib_find(buf1, len1, buf2, len2, offset);
9980 }
9981 assert(0);
9982 return -1;
9983}
9984
9985static Py_ssize_t
Victor Stinnerc3cec782011-10-05 21:24:08 +02009986anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen,
9987 PyObject *str1, void *buf1, Py_ssize_t len1, Py_ssize_t maxcount)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02009988{
Benjamin Petersonc0b95d12011-12-20 17:24:05 -06009989 switch (kind) {
9990 case PyUnicode_1BYTE_KIND:
9991 if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1))
9992 return asciilib_count(sbuf, slen, buf1, len1, maxcount);
9993 else
9994 return ucs1lib_count(sbuf, slen, buf1, len1, maxcount);
9995 case PyUnicode_2BYTE_KIND:
9996 return ucs2lib_count(sbuf, slen, buf1, len1, maxcount);
9997 case PyUnicode_4BYTE_KIND:
9998 return ucs4lib_count(sbuf, slen, buf1, len1, maxcount);
9999 }
10000 assert(0);
10001 return 0;
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000010002}
10003
Alexander Belopolsky40018472011-02-26 01:02:56 +000010004static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010005replace(PyObject *self, PyObject *str1,
10006 PyObject *str2, Py_ssize_t maxcount)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010007{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010008 PyObject *u;
10009 char *sbuf = PyUnicode_DATA(self);
10010 char *buf1 = PyUnicode_DATA(str1);
10011 char *buf2 = PyUnicode_DATA(str2);
10012 int srelease = 0, release1 = 0, release2 = 0;
10013 int skind = PyUnicode_KIND(self);
10014 int kind1 = PyUnicode_KIND(str1);
10015 int kind2 = PyUnicode_KIND(str2);
10016 Py_ssize_t slen = PyUnicode_GET_LENGTH(self);
10017 Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1);
10018 Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2);
Victor Stinner49a0a212011-10-12 23:46:10 +020010019 int mayshrink;
10020 Py_UCS4 maxchar, maxchar_str2;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010021
10022 if (maxcount < 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000010023 maxcount = PY_SSIZE_T_MAX;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010024 else if (maxcount == 0 || slen == 0)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010025 goto nothing;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010026
Victor Stinner59de0ee2011-10-07 10:01:28 +020010027 if (str1 == str2)
10028 goto nothing;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010029 if (skind < kind1)
10030 /* substring too wide to be present */
10031 goto nothing;
10032
Victor Stinner49a0a212011-10-12 23:46:10 +020010033 maxchar = PyUnicode_MAX_CHAR_VALUE(self);
10034 maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2);
10035 /* Replacing str1 with str2 may cause a maxchar reduction in the
10036 result string. */
10037 mayshrink = (maxchar_str2 < maxchar);
Victor Stinnere6abb482012-05-02 01:15:40 +020010038 maxchar = MAX_MAXCHAR(maxchar, maxchar_str2);
Victor Stinner49a0a212011-10-12 23:46:10 +020010039
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010040 if (len1 == len2) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000010041 /* same length */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010042 if (len1 == 0)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010043 goto nothing;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010044 if (len1 == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000010045 /* replace characters */
Victor Stinner49a0a212011-10-12 23:46:10 +020010046 Py_UCS4 u1, u2;
10047 int rkind;
Victor Stinnerf6441102011-12-18 02:43:08 +010010048 Py_ssize_t index, pos;
10049 char *src;
10050
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010051 u1 = PyUnicode_READ_CHAR(str1, 0);
Victor Stinnerf6441102011-12-18 02:43:08 +010010052 pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1);
10053 if (pos < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +000010054 goto nothing;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010055 u2 = PyUnicode_READ_CHAR(str2, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010056 u = PyUnicode_New(slen, maxchar);
Thomas Wouters477c8d52006-05-27 19:21:47 +000010057 if (!u)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010058 goto error;
Victor Stinnerd3f08822012-05-29 12:57:52 +020010059 _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010060 rkind = PyUnicode_KIND(u);
Victor Stinnerf6441102011-12-18 02:43:08 +010010061
10062 PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2);
10063 index = 0;
10064 src = sbuf;
10065 while (--maxcount)
10066 {
10067 pos++;
10068 src += pos * PyUnicode_KIND(self);
10069 slen -= pos;
10070 index += pos;
10071 pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1);
10072 if (pos < 0)
10073 break;
10074 PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2);
10075 }
Victor Stinner49a0a212011-10-12 23:46:10 +020010076 }
10077 else {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010078 int rkind = skind;
10079 char *res;
Victor Stinnerf6441102011-12-18 02:43:08 +010010080 Py_ssize_t i;
Victor Stinner25a4b292011-10-06 12:31:55 +020010081
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010082 if (kind1 < rkind) {
10083 /* widen substring */
10084 buf1 = _PyUnicode_AsKind(str1, rkind);
10085 if (!buf1) goto error;
10086 release1 = 1;
10087 }
Victor Stinnerc3cec782011-10-05 21:24:08 +020010088 i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +000010089 if (i < 0)
10090 goto nothing;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010091 if (rkind > kind2) {
10092 /* widen replacement */
10093 buf2 = _PyUnicode_AsKind(str2, rkind);
10094 if (!buf2) goto error;
10095 release2 = 1;
10096 }
10097 else if (rkind < kind2) {
10098 /* widen self and buf1 */
10099 rkind = kind2;
10100 if (release1) PyMem_Free(buf1);
10101 sbuf = _PyUnicode_AsKind(self, rkind);
10102 if (!sbuf) goto error;
10103 srelease = 1;
10104 buf1 = _PyUnicode_AsKind(str1, rkind);
10105 if (!buf1) goto error;
10106 release1 = 1;
10107 }
Victor Stinner49a0a212011-10-12 23:46:10 +020010108 u = PyUnicode_New(slen, maxchar);
10109 if (!u)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010110 goto error;
Victor Stinner49a0a212011-10-12 23:46:10 +020010111 assert(PyUnicode_KIND(u) == rkind);
10112 res = PyUnicode_DATA(u);
Victor Stinner25a4b292011-10-06 12:31:55 +020010113
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010114 memcpy(res, sbuf, rkind * slen);
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010115 /* change everything in-place, starting with this one */
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010116 memcpy(res + rkind * i,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010117 buf2,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010118 rkind * len2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010119 i += len1;
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010120
10121 while ( --maxcount > 0) {
Victor Stinnerc3cec782011-10-05 21:24:08 +020010122 i = anylib_find(rkind, self,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010123 sbuf+rkind*i, slen-i,
Victor Stinnerc3cec782011-10-05 21:24:08 +020010124 str1, buf1, len1, i);
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010125 if (i == -1)
10126 break;
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010127 memcpy(res + rkind * i,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010128 buf2,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010129 rkind * len2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010130 i += len1;
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010131 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000010132 }
Victor Stinner49a0a212011-10-12 23:46:10 +020010133 }
10134 else {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010135 Py_ssize_t n, i, j, ires;
10136 Py_ssize_t product, new_size;
10137 int rkind = skind;
10138 char *res;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010139
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010140 if (kind1 < rkind) {
Victor Stinner49a0a212011-10-12 23:46:10 +020010141 /* widen substring */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010142 buf1 = _PyUnicode_AsKind(str1, rkind);
10143 if (!buf1) goto error;
10144 release1 = 1;
10145 }
Victor Stinnerc3cec782011-10-05 21:24:08 +020010146 n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount);
Thomas Wouters477c8d52006-05-27 19:21:47 +000010147 if (n == 0)
10148 goto nothing;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010149 if (kind2 < rkind) {
Victor Stinner49a0a212011-10-12 23:46:10 +020010150 /* widen replacement */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010151 buf2 = _PyUnicode_AsKind(str2, rkind);
10152 if (!buf2) goto error;
10153 release2 = 1;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010154 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010155 else if (kind2 > rkind) {
Victor Stinner49a0a212011-10-12 23:46:10 +020010156 /* widen self and buf1 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010157 rkind = kind2;
10158 sbuf = _PyUnicode_AsKind(self, rkind);
10159 if (!sbuf) goto error;
10160 srelease = 1;
10161 if (release1) PyMem_Free(buf1);
10162 buf1 = _PyUnicode_AsKind(str1, rkind);
10163 if (!buf1) goto error;
10164 release1 = 1;
10165 }
10166 /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) -
10167 PyUnicode_GET_LENGTH(str1))); */
10168 product = n * (len2-len1);
10169 if ((product / (len2-len1)) != n) {
10170 PyErr_SetString(PyExc_OverflowError,
10171 "replace string is too long");
10172 goto error;
10173 }
10174 new_size = slen + product;
Victor Stinner49a0a212011-10-12 23:46:10 +020010175 if (new_size == 0) {
10176 Py_INCREF(unicode_empty);
10177 u = unicode_empty;
10178 goto done;
10179 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010180 if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) {
10181 PyErr_SetString(PyExc_OverflowError,
10182 "replace string is too long");
10183 goto error;
10184 }
Victor Stinner49a0a212011-10-12 23:46:10 +020010185 u = PyUnicode_New(new_size, maxchar);
10186 if (!u)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010187 goto error;
Victor Stinner49a0a212011-10-12 23:46:10 +020010188 assert(PyUnicode_KIND(u) == rkind);
10189 res = PyUnicode_DATA(u);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010190 ires = i = 0;
10191 if (len1 > 0) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000010192 while (n-- > 0) {
10193 /* look for next match */
Victor Stinnerc3cec782011-10-05 21:24:08 +020010194 j = anylib_find(rkind, self,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010195 sbuf + rkind * i, slen-i,
Victor Stinnerc3cec782011-10-05 21:24:08 +020010196 str1, buf1, len1, i);
Antoine Pitrouf2c54842010-01-13 08:07:53 +000010197 if (j == -1)
10198 break;
10199 else if (j > i) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000010200 /* copy unchanged part [i:j] */
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010201 memcpy(res + rkind * ires,
10202 sbuf + rkind * i,
10203 rkind * (j-i));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010204 ires += j - i;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010205 }
10206 /* copy substitution string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010207 if (len2 > 0) {
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010208 memcpy(res + rkind * ires,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010209 buf2,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010210 rkind * len2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010211 ires += len2;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010212 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010213 i = j + len1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010214 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010215 if (i < slen)
Thomas Wouters477c8d52006-05-27 19:21:47 +000010216 /* copy tail [i:] */
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010217 memcpy(res + rkind * ires,
10218 sbuf + rkind * i,
10219 rkind * (slen-i));
Victor Stinner49a0a212011-10-12 23:46:10 +020010220 }
10221 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +000010222 /* interleave */
10223 while (n > 0) {
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010224 memcpy(res + rkind * ires,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010225 buf2,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010226 rkind * len2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010227 ires += len2;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010228 if (--n <= 0)
10229 break;
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010230 memcpy(res + rkind * ires,
10231 sbuf + rkind * i,
10232 rkind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010233 ires++;
10234 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010235 }
Martin v. Löwisc47adb02011-10-07 20:55:35 +020010236 memcpy(res + rkind * ires,
10237 sbuf + rkind * i,
10238 rkind * (slen-i));
Thomas Wouters477c8d52006-05-27 19:21:47 +000010239 }
Victor Stinner49a0a212011-10-12 23:46:10 +020010240 }
10241
10242 if (mayshrink) {
Victor Stinner25a4b292011-10-06 12:31:55 +020010243 unicode_adjust_maxchar(&u);
10244 if (u == NULL)
10245 goto error;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010246 }
Victor Stinner49a0a212011-10-12 23:46:10 +020010247
10248 done:
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010249 if (srelease)
10250 PyMem_FREE(sbuf);
10251 if (release1)
10252 PyMem_FREE(buf1);
10253 if (release2)
10254 PyMem_FREE(buf2);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020010255 assert(_PyUnicode_CheckConsistency(u, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010256 return u;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010257
Benjamin Peterson29060642009-01-31 22:14:21 +000010258 nothing:
Thomas Wouters477c8d52006-05-27 19:21:47 +000010259 /* nothing to replace; return original string (when possible) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010260 if (srelease)
10261 PyMem_FREE(sbuf);
10262 if (release1)
10263 PyMem_FREE(buf1);
10264 if (release2)
10265 PyMem_FREE(buf2);
Victor Stinnerc4b49542011-12-11 22:44:26 +010010266 return unicode_result_unchanged(self);
10267
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010268 error:
10269 if (srelease && sbuf)
10270 PyMem_FREE(sbuf);
10271 if (release1 && buf1)
10272 PyMem_FREE(buf1);
10273 if (release2 && buf2)
10274 PyMem_FREE(buf2);
10275 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010276}
10277
10278/* --- Unicode Object Methods --------------------------------------------- */
10279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010280PyDoc_STRVAR(title__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000010281 "S.title() -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010282\n\
10283Return a titlecased version of S, i.e. words start with title case\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010284characters, all remaining cased characters have lower case.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010285
10286static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020010287unicode_title(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010288{
Benjamin Petersoneea48462012-01-16 14:28:50 -050010289 if (PyUnicode_READY(self) == -1)
10290 return NULL;
Victor Stinnerb0800dc2012-02-25 00:47:08 +010010291 return case_operation(self, do_title);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010292}
10293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010294PyDoc_STRVAR(capitalize__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000010295 "S.capitalize() -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010296\n\
10297Return a capitalized version of S, i.e. make the first character\n\
Senthil Kumarane51ee8a2010-07-05 12:00:56 +000010298have upper case and the rest lower case.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010299
10300static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020010301unicode_capitalize(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010302{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -050010303 if (PyUnicode_READY(self) == -1)
10304 return NULL;
10305 if (PyUnicode_GET_LENGTH(self) == 0)
10306 return unicode_result_unchanged(self);
Victor Stinnerb0800dc2012-02-25 00:47:08 +010010307 return case_operation(self, do_capitalize);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010308}
10309
Benjamin Petersond5890c82012-01-14 13:23:30 -050010310PyDoc_STRVAR(casefold__doc__,
10311 "S.casefold() -> str\n\
10312\n\
10313Return a version of S suitable for caseless comparisons.");
10314
10315static PyObject *
10316unicode_casefold(PyObject *self)
10317{
10318 if (PyUnicode_READY(self) == -1)
10319 return NULL;
10320 if (PyUnicode_IS_ASCII(self))
10321 return ascii_upper_or_lower(self, 1);
Victor Stinnerb0800dc2012-02-25 00:47:08 +010010322 return case_operation(self, do_casefold);
Benjamin Petersond5890c82012-01-14 13:23:30 -050010323}
10324
10325
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000010326/* Argument converter. Coerces to a single unicode character */
10327
10328static int
10329convert_uc(PyObject *obj, void *addr)
10330{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010331 Py_UCS4 *fillcharloc = (Py_UCS4 *)addr;
Benjamin Peterson14339b62009-01-31 16:36:08 +000010332 PyObject *uniobj;
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000010333
Benjamin Peterson14339b62009-01-31 16:36:08 +000010334 uniobj = PyUnicode_FromObject(obj);
10335 if (uniobj == NULL) {
10336 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson29060642009-01-31 22:14:21 +000010337 "The fill character cannot be converted to Unicode");
Benjamin Peterson14339b62009-01-31 16:36:08 +000010338 return 0;
10339 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010340 if (PyUnicode_GET_LENGTH(uniobj) != 1) {
Benjamin Peterson14339b62009-01-31 16:36:08 +000010341 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson29060642009-01-31 22:14:21 +000010342 "The fill character must be exactly one character long");
Benjamin Peterson14339b62009-01-31 16:36:08 +000010343 Py_DECREF(uniobj);
10344 return 0;
10345 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010346 *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0);
Benjamin Peterson14339b62009-01-31 16:36:08 +000010347 Py_DECREF(uniobj);
10348 return 1;
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000010349}
10350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010351PyDoc_STRVAR(center__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000010352 "S.center(width[, fillchar]) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010353\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000010354Return S centered in a string of length width. Padding is\n\
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000010355done using the specified fill character (default is a space)");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010356
10357static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +020010358unicode_center(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010359{
Martin v. Löwis18e16552006-02-15 17:27:45 +000010360 Py_ssize_t marg, left;
10361 Py_ssize_t width;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010362 Py_UCS4 fillchar = ' ';
10363
Victor Stinnere9a29352011-10-01 02:14:59 +020010364 if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010365 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010366
Benjamin Petersonbac79492012-01-14 13:34:47 -050010367 if (PyUnicode_READY(self) == -1)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010368 return NULL;
10369
Victor Stinnerc4b49542011-12-11 22:44:26 +010010370 if (PyUnicode_GET_LENGTH(self) >= width)
10371 return unicode_result_unchanged(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010372
Victor Stinnerc4b49542011-12-11 22:44:26 +010010373 marg = width - PyUnicode_GET_LENGTH(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010374 left = marg / 2 + (marg & width & 1);
10375
Victor Stinner9310abb2011-10-05 00:59:23 +020010376 return pad(self, left, marg - left, fillchar);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010377}
10378
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010379/* This function assumes that str1 and str2 are readied by the caller. */
10380
Marc-André Lemburge5034372000-08-08 08:04:29 +000010381static int
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010382unicode_compare(PyObject *str1, PyObject *str2)
Marc-André Lemburge5034372000-08-08 08:04:29 +000010383{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010384 int kind1, kind2;
10385 void *data1, *data2;
10386 Py_ssize_t len1, len2, i;
Marc-André Lemburge5034372000-08-08 08:04:29 +000010387
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010388 kind1 = PyUnicode_KIND(str1);
10389 kind2 = PyUnicode_KIND(str2);
10390 data1 = PyUnicode_DATA(str1);
10391 data2 = PyUnicode_DATA(str2);
10392 len1 = PyUnicode_GET_LENGTH(str1);
10393 len2 = PyUnicode_GET_LENGTH(str2);
Marc-André Lemburge5034372000-08-08 08:04:29 +000010394
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010395 for (i = 0; i < len1 && i < len2; ++i) {
10396 Py_UCS4 c1, c2;
10397 c1 = PyUnicode_READ(kind1, data1, i);
10398 c2 = PyUnicode_READ(kind2, data2, i);
Fredrik Lundh45714e92001-06-26 16:39:36 +000010399
10400 if (c1 != c2)
10401 return (c1 < c2) ? -1 : 1;
Marc-André Lemburge5034372000-08-08 08:04:29 +000010402 }
10403
10404 return (len1 < len2) ? -1 : (len1 != len2);
10405}
10406
Alexander Belopolsky40018472011-02-26 01:02:56 +000010407int
10408PyUnicode_Compare(PyObject *left, PyObject *right)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010409{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010410 if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
10411 if (PyUnicode_READY(left) == -1 ||
10412 PyUnicode_READY(right) == -1)
10413 return -1;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010414 return unicode_compare(left, right);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010415 }
Guido van Rossum09dc34f2007-05-04 04:17:33 +000010416 PyErr_Format(PyExc_TypeError,
10417 "Can't compare %.100s and %.100s",
10418 left->ob_type->tp_name,
10419 right->ob_type->tp_name);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010420 return -1;
10421}
10422
Martin v. Löwis5b222132007-06-10 09:51:05 +000010423int
10424PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
10425{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010426 Py_ssize_t i;
10427 int kind;
10428 void *data;
10429 Py_UCS4 chr;
10430
Victor Stinner910337b2011-10-03 03:20:16 +020010431 assert(_PyUnicode_CHECK(uni));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010432 if (PyUnicode_READY(uni) == -1)
10433 return -1;
10434 kind = PyUnicode_KIND(uni);
10435 data = PyUnicode_DATA(uni);
Martin v. Löwis5b222132007-06-10 09:51:05 +000010436 /* Compare Unicode string and source character set string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010437 for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
10438 if (chr != str[i])
10439 return (chr < (unsigned char)(str[i])) ? -1 : 1;
Benjamin Peterson8667a9b2010-01-09 21:45:28 +000010440 /* This check keeps Python strings that end in '\0' from comparing equal
10441 to C strings identical up to that point. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010442 if (PyUnicode_GET_LENGTH(uni) != i || chr)
Benjamin Peterson29060642009-01-31 22:14:21 +000010443 return 1; /* uni is longer */
Martin v. Löwis5b222132007-06-10 09:51:05 +000010444 if (str[i])
Benjamin Peterson29060642009-01-31 22:14:21 +000010445 return -1; /* str is longer */
Martin v. Löwis5b222132007-06-10 09:51:05 +000010446 return 0;
10447}
10448
Antoine Pitrou51f3ef92008-12-20 13:14:23 +000010449
Benjamin Peterson29060642009-01-31 22:14:21 +000010450#define TEST_COND(cond) \
Benjamin Peterson14339b62009-01-31 16:36:08 +000010451 ((cond) ? Py_True : Py_False)
Antoine Pitrou51f3ef92008-12-20 13:14:23 +000010452
Alexander Belopolsky40018472011-02-26 01:02:56 +000010453PyObject *
10454PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000010455{
10456 int result;
Benjamin Peterson14339b62009-01-31 16:36:08 +000010457
Antoine Pitrou51f3ef92008-12-20 13:14:23 +000010458 if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
10459 PyObject *v;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010460 if (PyUnicode_READY(left) == -1 ||
10461 PyUnicode_READY(right) == -1)
10462 return NULL;
10463 if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) ||
10464 PyUnicode_KIND(left) != PyUnicode_KIND(right)) {
Antoine Pitrou51f3ef92008-12-20 13:14:23 +000010465 if (op == Py_EQ) {
10466 Py_INCREF(Py_False);
10467 return Py_False;
10468 }
10469 if (op == Py_NE) {
10470 Py_INCREF(Py_True);
10471 return Py_True;
10472 }
10473 }
10474 if (left == right)
10475 result = 0;
10476 else
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010477 result = unicode_compare(left, right);
Benjamin Peterson14339b62009-01-31 16:36:08 +000010478
Antoine Pitrou51f3ef92008-12-20 13:14:23 +000010479 /* Convert the return value to a Boolean */
10480 switch (op) {
10481 case Py_EQ:
10482 v = TEST_COND(result == 0);
10483 break;
10484 case Py_NE:
10485 v = TEST_COND(result != 0);
10486 break;
10487 case Py_LE:
10488 v = TEST_COND(result <= 0);
10489 break;
10490 case Py_GE:
10491 v = TEST_COND(result >= 0);
10492 break;
10493 case Py_LT:
10494 v = TEST_COND(result == -1);
10495 break;
10496 case Py_GT:
10497 v = TEST_COND(result == 1);
10498 break;
10499 default:
10500 PyErr_BadArgument();
10501 return NULL;
10502 }
10503 Py_INCREF(v);
10504 return v;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000010505 }
Benjamin Peterson14339b62009-01-31 16:36:08 +000010506
Brian Curtindfc80e32011-08-10 20:28:54 -050010507 Py_RETURN_NOTIMPLEMENTED;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000010508}
10509
Alexander Belopolsky40018472011-02-26 01:02:56 +000010510int
10511PyUnicode_Contains(PyObject *container, PyObject *element)
Guido van Rossum403d68b2000-03-13 15:55:09 +000010512{
Thomas Wouters477c8d52006-05-27 19:21:47 +000010513 PyObject *str, *sub;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010514 int kind1, kind2, kind;
10515 void *buf1, *buf2;
10516 Py_ssize_t len1, len2;
Martin v. Löwis18e16552006-02-15 17:27:45 +000010517 int result;
Guido van Rossum403d68b2000-03-13 15:55:09 +000010518
10519 /* Coerce the two arguments */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010520 sub = PyUnicode_FromObject(element);
10521 if (!sub) {
Benjamin Peterson29060642009-01-31 22:14:21 +000010522 PyErr_Format(PyExc_TypeError,
10523 "'in <string>' requires string as left operand, not %s",
10524 element->ob_type->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +000010525 return -1;
Guido van Rossum403d68b2000-03-13 15:55:09 +000010526 }
10527
Thomas Wouters477c8d52006-05-27 19:21:47 +000010528 str = PyUnicode_FromObject(container);
Benjamin Peterson22a29702012-01-02 09:00:30 -060010529 if (!str) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000010530 Py_DECREF(sub);
10531 return -1;
10532 }
Benjamin Peterson22a29702012-01-02 09:00:30 -060010533 if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) {
10534 Py_DECREF(sub);
10535 Py_DECREF(str);
10536 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010537
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010538 kind1 = PyUnicode_KIND(str);
10539 kind2 = PyUnicode_KIND(sub);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -050010540 kind = kind1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010541 buf1 = PyUnicode_DATA(str);
10542 buf2 = PyUnicode_DATA(sub);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -050010543 if (kind2 != kind) {
Antoine Pitrou758153b2012-05-12 15:51:51 +020010544 if (kind2 > kind) {
10545 Py_DECREF(sub);
10546 Py_DECREF(str);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -050010547 return 0;
Antoine Pitrou758153b2012-05-12 15:51:51 +020010548 }
Victor Stinner7931d9a2011-11-04 00:22:48 +010010549 buf2 = _PyUnicode_AsKind(sub, kind);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -050010550 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010551 if (!buf2) {
10552 Py_DECREF(sub);
Benjamin Peterson1ff2e352012-05-11 17:41:20 -050010553 Py_DECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010554 return -1;
10555 }
10556 len1 = PyUnicode_GET_LENGTH(str);
10557 len2 = PyUnicode_GET_LENGTH(sub);
10558
Benjamin Petersonead6b532011-12-20 17:23:42 -060010559 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010560 case PyUnicode_1BYTE_KIND:
10561 result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1;
10562 break;
10563 case PyUnicode_2BYTE_KIND:
10564 result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1;
10565 break;
10566 case PyUnicode_4BYTE_KIND:
10567 result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1;
10568 break;
10569 default:
10570 result = -1;
10571 assert(0);
10572 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010573
10574 Py_DECREF(str);
10575 Py_DECREF(sub);
10576
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010577 if (kind2 != kind)
10578 PyMem_Free(buf2);
10579
Guido van Rossum403d68b2000-03-13 15:55:09 +000010580 return result;
Guido van Rossum403d68b2000-03-13 15:55:09 +000010581}
10582
Guido van Rossumd57fd912000-03-10 22:53:23 +000010583/* Concat to string or Unicode object giving a new Unicode object. */
10584
Alexander Belopolsky40018472011-02-26 01:02:56 +000010585PyObject *
10586PyUnicode_Concat(PyObject *left, PyObject *right)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010587{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010588 PyObject *u = NULL, *v = NULL, *w;
Victor Stinner127226b2011-10-13 01:12:34 +020010589 Py_UCS4 maxchar, maxchar2;
Victor Stinner488fa492011-12-12 00:01:39 +010010590 Py_ssize_t u_len, v_len, new_len;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010591
10592 /* Coerce the two arguments */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010593 u = PyUnicode_FromObject(left);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010594 if (u == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +000010595 goto onError;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010596 v = PyUnicode_FromObject(right);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010597 if (v == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +000010598 goto onError;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010599
10600 /* Shortcuts */
Victor Stinnera464fc12011-10-02 20:39:30 +020010601 if (v == unicode_empty) {
Benjamin Peterson29060642009-01-31 22:14:21 +000010602 Py_DECREF(v);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010603 return u;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010604 }
Victor Stinnera464fc12011-10-02 20:39:30 +020010605 if (u == unicode_empty) {
Benjamin Peterson29060642009-01-31 22:14:21 +000010606 Py_DECREF(u);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010607 return v;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010608 }
10609
Victor Stinner488fa492011-12-12 00:01:39 +010010610 u_len = PyUnicode_GET_LENGTH(u);
10611 v_len = PyUnicode_GET_LENGTH(v);
10612 if (u_len > PY_SSIZE_T_MAX - v_len) {
10613 PyErr_SetString(PyExc_OverflowError,
10614 "strings are too large to concat");
10615 goto onError;
10616 }
10617 new_len = u_len + v_len;
10618
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010619 maxchar = PyUnicode_MAX_CHAR_VALUE(u);
Victor Stinner127226b2011-10-13 01:12:34 +020010620 maxchar2 = PyUnicode_MAX_CHAR_VALUE(v);
Victor Stinnere6abb482012-05-02 01:15:40 +020010621 maxchar = MAX_MAXCHAR(maxchar, maxchar2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010622
Guido van Rossumd57fd912000-03-10 22:53:23 +000010623 /* Concat the two Unicode strings */
Victor Stinner488fa492011-12-12 00:01:39 +010010624 w = PyUnicode_New(new_len, maxchar);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010625 if (w == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +000010626 goto onError;
Victor Stinnerd3f08822012-05-29 12:57:52 +020010627 _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len);
10628 _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010629 Py_DECREF(u);
10630 Py_DECREF(v);
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020010631 assert(_PyUnicode_CheckConsistency(w, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010632 return w;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010633
Benjamin Peterson29060642009-01-31 22:14:21 +000010634 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +000010635 Py_XDECREF(u);
10636 Py_XDECREF(v);
10637 return NULL;
10638}
10639
Walter Dörwald1ab83302007-05-18 17:15:44 +000010640void
Victor Stinner23e56682011-10-03 03:54:37 +020010641PyUnicode_Append(PyObject **p_left, PyObject *right)
Walter Dörwald1ab83302007-05-18 17:15:44 +000010642{
Victor Stinner23e56682011-10-03 03:54:37 +020010643 PyObject *left, *res;
Victor Stinner488fa492011-12-12 00:01:39 +010010644 Py_UCS4 maxchar, maxchar2;
10645 Py_ssize_t left_len, right_len, new_len;
Victor Stinner23e56682011-10-03 03:54:37 +020010646
10647 if (p_left == NULL) {
10648 if (!PyErr_Occurred())
10649 PyErr_BadInternalCall();
Benjamin Peterson14339b62009-01-31 16:36:08 +000010650 return;
10651 }
Victor Stinner23e56682011-10-03 03:54:37 +020010652 left = *p_left;
10653 if (right == NULL || !PyUnicode_Check(left)) {
10654 if (!PyErr_Occurred())
10655 PyErr_BadInternalCall();
10656 goto error;
10657 }
10658
Benjamin Petersonbac79492012-01-14 13:34:47 -050010659 if (PyUnicode_READY(left) == -1)
Victor Stinnere1335c72011-10-04 20:53:03 +020010660 goto error;
Benjamin Petersonbac79492012-01-14 13:34:47 -050010661 if (PyUnicode_READY(right) == -1)
Victor Stinnere1335c72011-10-04 20:53:03 +020010662 goto error;
10663
Victor Stinner488fa492011-12-12 00:01:39 +010010664 /* Shortcuts */
10665 if (left == unicode_empty) {
10666 Py_DECREF(left);
10667 Py_INCREF(right);
10668 *p_left = right;
10669 return;
10670 }
10671 if (right == unicode_empty)
10672 return;
10673
10674 left_len = PyUnicode_GET_LENGTH(left);
10675 right_len = PyUnicode_GET_LENGTH(right);
10676 if (left_len > PY_SSIZE_T_MAX - right_len) {
10677 PyErr_SetString(PyExc_OverflowError,
10678 "strings are too large to concat");
10679 goto error;
10680 }
10681 new_len = left_len + right_len;
10682
10683 if (unicode_modifiable(left)
10684 && PyUnicode_CheckExact(right)
10685 && PyUnicode_KIND(right) <= PyUnicode_KIND(left)
Victor Stinnerb0923652011-10-04 01:17:31 +020010686 /* Don't resize for ascii += latin1. Convert ascii to latin1 requires
10687 to change the structure size, but characters are stored just after
Georg Brandl7597add2011-10-05 16:36:47 +020010688 the structure, and so it requires to move all characters which is
Victor Stinnerb0923652011-10-04 01:17:31 +020010689 not so different than duplicating the string. */
Victor Stinner488fa492011-12-12 00:01:39 +010010690 && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right)))
10691 {
10692 /* append inplace */
10693 if (unicode_resize(p_left, new_len) != 0) {
10694 /* XXX if _PyUnicode_Resize() fails, 'left' has been
10695 * deallocated so it cannot be put back into
10696 * 'variable'. The MemoryError is raised when there
10697 * is no value in 'variable', which might (very
10698 * remotely) be a cause of incompatibilities.
10699 */
10700 goto error;
Victor Stinner23e56682011-10-03 03:54:37 +020010701 }
Victor Stinner488fa492011-12-12 00:01:39 +010010702 /* copy 'right' into the newly allocated area of 'left' */
Victor Stinnerd3f08822012-05-29 12:57:52 +020010703 _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len);
Victor Stinner23e56682011-10-03 03:54:37 +020010704 }
Victor Stinner488fa492011-12-12 00:01:39 +010010705 else {
10706 maxchar = PyUnicode_MAX_CHAR_VALUE(left);
10707 maxchar2 = PyUnicode_MAX_CHAR_VALUE(right);
Victor Stinnere6abb482012-05-02 01:15:40 +020010708 maxchar = MAX_MAXCHAR(maxchar, maxchar2);
Victor Stinner23e56682011-10-03 03:54:37 +020010709
Victor Stinner488fa492011-12-12 00:01:39 +010010710 /* Concat the two Unicode strings */
10711 res = PyUnicode_New(new_len, maxchar);
10712 if (res == NULL)
10713 goto error;
Victor Stinnerd3f08822012-05-29 12:57:52 +020010714 _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len);
10715 _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len);
Victor Stinner488fa492011-12-12 00:01:39 +010010716 Py_DECREF(left);
10717 *p_left = res;
10718 }
10719 assert(_PyUnicode_CheckConsistency(*p_left, 1));
Victor Stinner23e56682011-10-03 03:54:37 +020010720 return;
10721
10722error:
Victor Stinner488fa492011-12-12 00:01:39 +010010723 Py_CLEAR(*p_left);
Walter Dörwald1ab83302007-05-18 17:15:44 +000010724}
10725
10726void
10727PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right)
10728{
Benjamin Peterson14339b62009-01-31 16:36:08 +000010729 PyUnicode_Append(pleft, right);
10730 Py_XDECREF(right);
Walter Dörwald1ab83302007-05-18 17:15:44 +000010731}
10732
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010733PyDoc_STRVAR(count__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000010734 "S.count(sub[, start[, end]]) -> int\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010735\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +000010736Return the number of non-overlapping occurrences of substring sub in\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000010737string S[start:end]. Optional arguments start and end are\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010738interpreted as in slice notation.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010739
10740static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010741unicode_count(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010742{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010743 PyObject *substring;
Martin v. Löwis18e16552006-02-15 17:27:45 +000010744 Py_ssize_t start = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010745 Py_ssize_t end = PY_SSIZE_T_MAX;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010746 PyObject *result;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010747 int kind1, kind2, kind;
10748 void *buf1, *buf2;
10749 Py_ssize_t len1, len2, iresult;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010750
Jesus Ceaac451502011-04-20 17:09:23 +020010751 if (!stringlib_parse_args_finds_unicode("count", args, &substring,
10752 &start, &end))
Benjamin Peterson29060642009-01-31 22:14:21 +000010753 return NULL;
Tim Petersced69f82003-09-16 20:30:58 +000010754
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010755 kind1 = PyUnicode_KIND(self);
10756 kind2 = PyUnicode_KIND(substring);
Benjamin Petersonb63f49f2012-05-03 18:31:07 -040010757 if (kind2 > kind1)
10758 return PyLong_FromLong(0);
10759 kind = kind1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010760 buf1 = PyUnicode_DATA(self);
10761 buf2 = PyUnicode_DATA(substring);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010762 if (kind2 != kind)
Victor Stinner7931d9a2011-11-04 00:22:48 +010010763 buf2 = _PyUnicode_AsKind(substring, kind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010764 if (!buf2) {
10765 Py_DECREF(substring);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010766 return NULL;
10767 }
10768 len1 = PyUnicode_GET_LENGTH(self);
10769 len2 = PyUnicode_GET_LENGTH(substring);
10770
10771 ADJUST_INDICES(start, end, len1);
Benjamin Petersonead6b532011-12-20 17:23:42 -060010772 switch (kind) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010773 case PyUnicode_1BYTE_KIND:
10774 iresult = ucs1lib_count(
10775 ((Py_UCS1*)buf1) + start, end - start,
10776 buf2, len2, PY_SSIZE_T_MAX
10777 );
10778 break;
10779 case PyUnicode_2BYTE_KIND:
10780 iresult = ucs2lib_count(
10781 ((Py_UCS2*)buf1) + start, end - start,
10782 buf2, len2, PY_SSIZE_T_MAX
10783 );
10784 break;
10785 case PyUnicode_4BYTE_KIND:
10786 iresult = ucs4lib_count(
10787 ((Py_UCS4*)buf1) + start, end - start,
10788 buf2, len2, PY_SSIZE_T_MAX
10789 );
10790 break;
10791 default:
10792 assert(0); iresult = 0;
10793 }
10794
10795 result = PyLong_FromSsize_t(iresult);
10796
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010797 if (kind2 != kind)
10798 PyMem_Free(buf2);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010799
10800 Py_DECREF(substring);
Thomas Wouters477c8d52006-05-27 19:21:47 +000010801
Guido van Rossumd57fd912000-03-10 22:53:23 +000010802 return result;
10803}
10804
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010805PyDoc_STRVAR(encode__doc__,
Victor Stinnerc911bbf2010-11-07 19:04:46 +000010806 "S.encode(encoding='utf-8', errors='strict') -> bytes\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010807\n\
Victor Stinnere14e2122010-11-07 18:41:46 +000010808Encode S using the codec registered for encoding. Default encoding\n\
10809is 'utf-8'. errors may be given to set a different error\n\
Fred Drakee4315f52000-05-09 19:53:39 +000010810handling scheme. Default is 'strict' meaning that encoding errors raise\n\
Walter Dörwald3aeb6322002-09-02 13:14:32 +000010811a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
10812'xmlcharrefreplace' as well as any other name registered with\n\
10813codecs.register_error that can handle UnicodeEncodeErrors.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010814
10815static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010816unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010817{
Benjamin Peterson308d6372009-09-18 21:42:35 +000010818 static char *kwlist[] = {"encoding", "errors", 0};
Guido van Rossumd57fd912000-03-10 22:53:23 +000010819 char *encoding = NULL;
10820 char *errors = NULL;
Guido van Rossum35d94282007-08-27 18:20:11 +000010821
Benjamin Peterson308d6372009-09-18 21:42:35 +000010822 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode",
10823 kwlist, &encoding, &errors))
Guido van Rossumd57fd912000-03-10 22:53:23 +000010824 return NULL;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010825 return PyUnicode_AsEncodedString(self, encoding, errors);
Marc-André Lemburgd2d45982004-07-08 17:57:32 +000010826}
10827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010828PyDoc_STRVAR(expandtabs__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000010829 "S.expandtabs([tabsize]) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010830\n\
10831Return a copy of S where all tab characters are expanded using spaces.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010832If tabsize is not given, a tab size of 8 characters is assumed.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010833
10834static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010835unicode_expandtabs(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010836{
Antoine Pitroue71d5742011-10-04 15:55:09 +020010837 Py_ssize_t i, j, line_pos, src_len, incr;
10838 Py_UCS4 ch;
10839 PyObject *u;
10840 void *src_data, *dest_data;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010841 int tabsize = 8;
Antoine Pitroue71d5742011-10-04 15:55:09 +020010842 int kind;
Antoine Pitroue19aa382011-10-04 16:04:01 +020010843 int found;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010844
10845 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
Benjamin Peterson29060642009-01-31 22:14:21 +000010846 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010847
Antoine Pitrou22425222011-10-04 19:10:51 +020010848 if (PyUnicode_READY(self) == -1)
10849 return NULL;
10850
Thomas Wouters7e474022000-07-16 12:04:32 +000010851 /* First pass: determine size of output string */
Antoine Pitroue71d5742011-10-04 15:55:09 +020010852 src_len = PyUnicode_GET_LENGTH(self);
10853 i = j = line_pos = 0;
10854 kind = PyUnicode_KIND(self);
10855 src_data = PyUnicode_DATA(self);
Antoine Pitroue19aa382011-10-04 16:04:01 +020010856 found = 0;
Antoine Pitroue71d5742011-10-04 15:55:09 +020010857 for (; i < src_len; i++) {
10858 ch = PyUnicode_READ(kind, src_data, i);
10859 if (ch == '\t') {
Antoine Pitroue19aa382011-10-04 16:04:01 +020010860 found = 1;
Benjamin Peterson29060642009-01-31 22:14:21 +000010861 if (tabsize > 0) {
Antoine Pitroue71d5742011-10-04 15:55:09 +020010862 incr = tabsize - (line_pos % tabsize); /* cannot overflow */
Benjamin Peterson29060642009-01-31 22:14:21 +000010863 if (j > PY_SSIZE_T_MAX - incr)
Antoine Pitroue71d5742011-10-04 15:55:09 +020010864 goto overflow;
10865 line_pos += incr;
Benjamin Peterson29060642009-01-31 22:14:21 +000010866 j += incr;
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010867 }
Benjamin Peterson29060642009-01-31 22:14:21 +000010868 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000010869 else {
Benjamin Peterson29060642009-01-31 22:14:21 +000010870 if (j > PY_SSIZE_T_MAX - 1)
Antoine Pitroue71d5742011-10-04 15:55:09 +020010871 goto overflow;
10872 line_pos++;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010873 j++;
Antoine Pitroue71d5742011-10-04 15:55:09 +020010874 if (ch == '\n' || ch == '\r')
10875 line_pos = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010876 }
Antoine Pitroue71d5742011-10-04 15:55:09 +020010877 }
Victor Stinnerc4b49542011-12-11 22:44:26 +010010878 if (!found)
10879 return unicode_result_unchanged(self);
Guido van Rossumcd16bf62007-06-13 18:07:49 +000010880
Guido van Rossumd57fd912000-03-10 22:53:23 +000010881 /* Second pass: create output string and fill it */
Antoine Pitroue71d5742011-10-04 15:55:09 +020010882 u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self));
Guido van Rossumd57fd912000-03-10 22:53:23 +000010883 if (!u)
10884 return NULL;
Antoine Pitroue71d5742011-10-04 15:55:09 +020010885 dest_data = PyUnicode_DATA(u);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010886
Antoine Pitroue71d5742011-10-04 15:55:09 +020010887 i = j = line_pos = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010888
Antoine Pitroue71d5742011-10-04 15:55:09 +020010889 for (; i < src_len; i++) {
10890 ch = PyUnicode_READ(kind, src_data, i);
10891 if (ch == '\t') {
Benjamin Peterson29060642009-01-31 22:14:21 +000010892 if (tabsize > 0) {
Antoine Pitroue71d5742011-10-04 15:55:09 +020010893 incr = tabsize - (line_pos % tabsize);
10894 line_pos += incr;
Victor Stinnerda79e632012-02-22 13:37:04 +010010895 FILL(kind, dest_data, ' ', j, incr);
10896 j += incr;
Benjamin Peterson29060642009-01-31 22:14:21 +000010897 }
Benjamin Peterson14339b62009-01-31 16:36:08 +000010898 }
Benjamin Peterson29060642009-01-31 22:14:21 +000010899 else {
Antoine Pitroue71d5742011-10-04 15:55:09 +020010900 line_pos++;
10901 PyUnicode_WRITE(kind, dest_data, j, ch);
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010902 j++;
Antoine Pitroue71d5742011-10-04 15:55:09 +020010903 if (ch == '\n' || ch == '\r')
10904 line_pos = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010905 }
Antoine Pitroue71d5742011-10-04 15:55:09 +020010906 }
10907 assert (j == PyUnicode_GET_LENGTH(u));
Victor Stinnerd3df8ab2011-11-22 01:22:34 +010010908 return unicode_result(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010909
Antoine Pitroue71d5742011-10-04 15:55:09 +020010910 overflow:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010911 PyErr_SetString(PyExc_OverflowError, "new string is too long");
10912 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010913}
10914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010915PyDoc_STRVAR(find__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000010916 "S.find(sub[, start[, end]]) -> int\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010917\n\
10918Return the lowest index in S where substring sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +080010919such that sub is contained within S[start:end]. Optional\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000010920arguments start and end are interpreted as in slice notation.\n\
10921\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010922Return -1 on failure.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000010923
10924static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010925unicode_find(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010926{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010927 PyObject *substring;
Christian Heimes9cd17752007-11-18 19:35:23 +000010928 Py_ssize_t start;
10929 Py_ssize_t end;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010930 Py_ssize_t result;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010931
Jesus Ceaac451502011-04-20 17:09:23 +020010932 if (!stringlib_parse_args_finds_unicode("find", args, &substring,
10933 &start, &end))
Guido van Rossumd57fd912000-03-10 22:53:23 +000010934 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010935
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010936 if (PyUnicode_READY(self) == -1)
10937 return NULL;
10938 if (PyUnicode_READY(substring) == -1)
10939 return NULL;
10940
Victor Stinner7931d9a2011-11-04 00:22:48 +010010941 result = any_find_slice(1, self, substring, start, end);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010942
10943 Py_DECREF(substring);
Thomas Wouters477c8d52006-05-27 19:21:47 +000010944
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010945 if (result == -2)
10946 return NULL;
10947
Christian Heimes217cfd12007-12-02 14:31:20 +000010948 return PyLong_FromSsize_t(result);
Guido van Rossumd57fd912000-03-10 22:53:23 +000010949}
10950
10951static PyObject *
Victor Stinner2fe5ced2011-10-02 00:25:40 +020010952unicode_getitem(PyObject *self, Py_ssize_t index)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010953{
Victor Stinnerb6cd0142012-05-03 02:17:04 +020010954 void *data;
10955 enum PyUnicode_Kind kind;
10956 Py_UCS4 ch;
10957 PyObject *res;
10958
10959 if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) {
10960 PyErr_BadArgument();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010961 return NULL;
Victor Stinnerb6cd0142012-05-03 02:17:04 +020010962 }
10963 if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) {
10964 PyErr_SetString(PyExc_IndexError, "string index out of range");
10965 return NULL;
10966 }
10967 kind = PyUnicode_KIND(self);
10968 data = PyUnicode_DATA(self);
10969 ch = PyUnicode_READ(kind, data, index);
10970 if (ch < 256)
10971 return get_latin1_char(ch);
10972
10973 res = PyUnicode_New(1, ch);
10974 if (res == NULL)
10975 return NULL;
10976 kind = PyUnicode_KIND(res);
10977 data = PyUnicode_DATA(res);
10978 PyUnicode_WRITE(kind, data, 0, ch);
10979 assert(_PyUnicode_CheckConsistency(res, 1));
10980 return res;
Guido van Rossumd57fd912000-03-10 22:53:23 +000010981}
10982
Guido van Rossumc2504932007-09-18 19:42:40 +000010983/* Believe it or not, this produces the same value for ASCII strings
Mark Dickinson57e683e2011-09-24 18:18:40 +010010984 as bytes_hash(). */
Benjamin Peterson8f67d082010-10-17 20:54:53 +000010985static Py_hash_t
Victor Stinner9db1a8b2011-10-23 20:04:37 +020010986unicode_hash(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000010987{
Guido van Rossumc2504932007-09-18 19:42:40 +000010988 Py_ssize_t len;
Mark Dickinson57e683e2011-09-24 18:18:40 +010010989 Py_uhash_t x;
Guido van Rossumc2504932007-09-18 19:42:40 +000010990
Benjamin Petersonf6622c82012-04-09 14:53:07 -040010991#ifdef Py_DEBUG
Benjamin Peterson69e97272012-02-21 11:08:50 -050010992 assert(_Py_HashSecret_Initialized);
Benjamin Petersonf6622c82012-04-09 14:53:07 -040010993#endif
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020010994 if (_PyUnicode_HASH(self) != -1)
10995 return _PyUnicode_HASH(self);
10996 if (PyUnicode_READY(self) == -1)
10997 return -1;
10998 len = PyUnicode_GET_LENGTH(self);
Georg Brandl16fa2a12012-02-21 00:50:13 +010010999 /*
11000 We make the hash of the empty string be 0, rather than using
11001 (prefix ^ suffix), since this slightly obfuscates the hash secret
11002 */
11003 if (len == 0) {
11004 _PyUnicode_HASH(self) = 0;
11005 return 0;
11006 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011007
11008 /* The hash function as a macro, gets expanded three times below. */
Georg Brandl2fb477c2012-02-21 00:33:36 +010011009#define HASH(P) \
11010 x ^= (Py_uhash_t) *P << 7; \
11011 while (--len >= 0) \
11012 x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011013
Georg Brandl2fb477c2012-02-21 00:33:36 +010011014 x = (Py_uhash_t) _Py_HashSecret.prefix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011015 switch (PyUnicode_KIND(self)) {
11016 case PyUnicode_1BYTE_KIND: {
11017 const unsigned char *c = PyUnicode_1BYTE_DATA(self);
11018 HASH(c);
11019 break;
11020 }
11021 case PyUnicode_2BYTE_KIND: {
11022 const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self);
11023 HASH(s);
11024 break;
11025 }
11026 default: {
11027 Py_UCS4 *l;
11028 assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND &&
11029 "Impossible switch case in unicode_hash");
11030 l = PyUnicode_4BYTE_DATA(self);
11031 HASH(l);
11032 break;
11033 }
11034 }
Georg Brandl2fb477c2012-02-21 00:33:36 +010011035 x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self);
11036 x ^= (Py_uhash_t) _Py_HashSecret.suffix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011037
Guido van Rossumc2504932007-09-18 19:42:40 +000011038 if (x == -1)
11039 x = -2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011040 _PyUnicode_HASH(self) = x;
Guido van Rossumc2504932007-09-18 19:42:40 +000011041 return x;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011042}
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011043#undef HASH
Guido van Rossumd57fd912000-03-10 22:53:23 +000011044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011045PyDoc_STRVAR(index__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011046 "S.index(sub[, start[, end]]) -> int\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011047\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011048Like S.find() but raise ValueError when the substring is not found.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011049
11050static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011051unicode_index(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011052{
Martin v. Löwis18e16552006-02-15 17:27:45 +000011053 Py_ssize_t result;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011054 PyObject *substring;
Christian Heimes9cd17752007-11-18 19:35:23 +000011055 Py_ssize_t start;
11056 Py_ssize_t end;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011057
Jesus Ceaac451502011-04-20 17:09:23 +020011058 if (!stringlib_parse_args_finds_unicode("index", args, &substring,
11059 &start, &end))
Guido van Rossumd57fd912000-03-10 22:53:23 +000011060 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011061
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011062 if (PyUnicode_READY(self) == -1)
11063 return NULL;
11064 if (PyUnicode_READY(substring) == -1)
11065 return NULL;
11066
Victor Stinner7931d9a2011-11-04 00:22:48 +010011067 result = any_find_slice(1, self, substring, start, end);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011068
11069 Py_DECREF(substring);
Thomas Wouters477c8d52006-05-27 19:21:47 +000011070
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011071 if (result == -2)
11072 return NULL;
11073
Guido van Rossumd57fd912000-03-10 22:53:23 +000011074 if (result < 0) {
11075 PyErr_SetString(PyExc_ValueError, "substring not found");
11076 return NULL;
11077 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011078
Christian Heimes217cfd12007-12-02 14:31:20 +000011079 return PyLong_FromSsize_t(result);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011080}
11081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011082PyDoc_STRVAR(islower__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011083 "S.islower() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011084\n\
Guido van Rossum77f6a652002-04-03 22:41:51 +000011085Return True if all cased characters in S are lowercase and there is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011086at least one cased character in S, False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011087
11088static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011089unicode_islower(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011090{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011091 Py_ssize_t i, length;
11092 int kind;
11093 void *data;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011094 int cased;
11095
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011096 if (PyUnicode_READY(self) == -1)
11097 return NULL;
11098 length = PyUnicode_GET_LENGTH(self);
11099 kind = PyUnicode_KIND(self);
11100 data = PyUnicode_DATA(self);
11101
Guido van Rossumd57fd912000-03-10 22:53:23 +000011102 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011103 if (length == 1)
11104 return PyBool_FromLong(
11105 Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0)));
Guido van Rossumd57fd912000-03-10 22:53:23 +000011106
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011107 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011108 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011109 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011110
Guido van Rossumd57fd912000-03-10 22:53:23 +000011111 cased = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011112 for (i = 0; i < length; i++) {
11113 const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Tim Petersced69f82003-09-16 20:30:58 +000011114
Benjamin Peterson29060642009-01-31 22:14:21 +000011115 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch))
11116 return PyBool_FromLong(0);
11117 else if (!cased && Py_UNICODE_ISLOWER(ch))
11118 cased = 1;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011119 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011120 return PyBool_FromLong(cased);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011121}
11122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011123PyDoc_STRVAR(isupper__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011124 "S.isupper() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011125\n\
Martin v. Löwis6828e182003-10-18 09:55:08 +000011126Return True if all cased characters in S are uppercase and there is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011127at least one cased character in S, False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011128
11129static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011130unicode_isupper(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011131{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011132 Py_ssize_t i, length;
11133 int kind;
11134 void *data;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011135 int cased;
11136
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011137 if (PyUnicode_READY(self) == -1)
11138 return NULL;
11139 length = PyUnicode_GET_LENGTH(self);
11140 kind = PyUnicode_KIND(self);
11141 data = PyUnicode_DATA(self);
11142
Guido van Rossumd57fd912000-03-10 22:53:23 +000011143 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011144 if (length == 1)
11145 return PyBool_FromLong(
11146 Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011147
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011148 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011149 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011150 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011151
Guido van Rossumd57fd912000-03-10 22:53:23 +000011152 cased = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011153 for (i = 0; i < length; i++) {
11154 const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Tim Petersced69f82003-09-16 20:30:58 +000011155
Benjamin Peterson29060642009-01-31 22:14:21 +000011156 if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch))
11157 return PyBool_FromLong(0);
11158 else if (!cased && Py_UNICODE_ISUPPER(ch))
11159 cased = 1;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011160 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011161 return PyBool_FromLong(cased);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011162}
11163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011164PyDoc_STRVAR(istitle__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011165 "S.istitle() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011166\n\
Martin v. Löwis6828e182003-10-18 09:55:08 +000011167Return True if S is a titlecased string and there is at least one\n\
11168character in S, i.e. upper- and titlecase characters may only\n\
11169follow uncased characters and lowercase characters only cased ones.\n\
11170Return False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011171
11172static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011173unicode_istitle(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011174{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011175 Py_ssize_t i, length;
11176 int kind;
11177 void *data;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011178 int cased, previous_is_cased;
11179
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011180 if (PyUnicode_READY(self) == -1)
11181 return NULL;
11182 length = PyUnicode_GET_LENGTH(self);
11183 kind = PyUnicode_KIND(self);
11184 data = PyUnicode_DATA(self);
11185
Guido van Rossumd57fd912000-03-10 22:53:23 +000011186 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011187 if (length == 1) {
11188 Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
11189 return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) ||
11190 (Py_UNICODE_ISUPPER(ch) != 0));
11191 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000011192
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011193 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011194 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011195 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011196
Guido van Rossumd57fd912000-03-10 22:53:23 +000011197 cased = 0;
11198 previous_is_cased = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011199 for (i = 0; i < length; i++) {
11200 const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Tim Petersced69f82003-09-16 20:30:58 +000011201
Benjamin Peterson29060642009-01-31 22:14:21 +000011202 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {
11203 if (previous_is_cased)
11204 return PyBool_FromLong(0);
11205 previous_is_cased = 1;
11206 cased = 1;
11207 }
11208 else if (Py_UNICODE_ISLOWER(ch)) {
11209 if (!previous_is_cased)
11210 return PyBool_FromLong(0);
11211 previous_is_cased = 1;
11212 cased = 1;
11213 }
11214 else
11215 previous_is_cased = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011216 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011217 return PyBool_FromLong(cased);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011218}
11219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011220PyDoc_STRVAR(isspace__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011221 "S.isspace() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011222\n\
Martin v. Löwis6828e182003-10-18 09:55:08 +000011223Return True if all characters in S are whitespace\n\
11224and there is at least one character in S, False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011225
11226static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011227unicode_isspace(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011228{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011229 Py_ssize_t i, length;
11230 int kind;
11231 void *data;
11232
11233 if (PyUnicode_READY(self) == -1)
11234 return NULL;
11235 length = PyUnicode_GET_LENGTH(self);
11236 kind = PyUnicode_KIND(self);
11237 data = PyUnicode_DATA(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011238
Guido van Rossumd57fd912000-03-10 22:53:23 +000011239 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011240 if (length == 1)
11241 return PyBool_FromLong(
11242 Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0)));
Guido van Rossumd57fd912000-03-10 22:53:23 +000011243
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011244 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011245 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011246 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011247
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011248 for (i = 0; i < length; i++) {
11249 const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Ezio Melotti93e7afc2011-08-22 14:08:38 +030011250 if (!Py_UNICODE_ISSPACE(ch))
Benjamin Peterson29060642009-01-31 22:14:21 +000011251 return PyBool_FromLong(0);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011252 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011253 return PyBool_FromLong(1);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011254}
11255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011256PyDoc_STRVAR(isalpha__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011257 "S.isalpha() -> bool\n\
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011258\n\
Martin v. Löwis6828e182003-10-18 09:55:08 +000011259Return True if all characters in S are alphabetic\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011260and there is at least one character in S, False otherwise.");
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011261
11262static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011263unicode_isalpha(PyObject *self)
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011264{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011265 Py_ssize_t i, length;
11266 int kind;
11267 void *data;
11268
11269 if (PyUnicode_READY(self) == -1)
11270 return NULL;
11271 length = PyUnicode_GET_LENGTH(self);
11272 kind = PyUnicode_KIND(self);
11273 data = PyUnicode_DATA(self);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011274
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011275 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011276 if (length == 1)
11277 return PyBool_FromLong(
11278 Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0)));
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011279
11280 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011281 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011282 return PyBool_FromLong(0);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011283
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011284 for (i = 0; i < length; i++) {
11285 if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i)))
Benjamin Peterson29060642009-01-31 22:14:21 +000011286 return PyBool_FromLong(0);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011287 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011288 return PyBool_FromLong(1);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011289}
11290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011291PyDoc_STRVAR(isalnum__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011292 "S.isalnum() -> bool\n\
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011293\n\
Martin v. Löwis6828e182003-10-18 09:55:08 +000011294Return True if all characters in S are alphanumeric\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011295and there is at least one character in S, False otherwise.");
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011296
11297static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011298unicode_isalnum(PyObject *self)
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011299{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011300 int kind;
11301 void *data;
11302 Py_ssize_t len, i;
11303
11304 if (PyUnicode_READY(self) == -1)
11305 return NULL;
11306
11307 kind = PyUnicode_KIND(self);
11308 data = PyUnicode_DATA(self);
11309 len = PyUnicode_GET_LENGTH(self);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011310
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011311 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011312 if (len == 1) {
11313 const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
11314 return PyBool_FromLong(Py_UNICODE_ISALNUM(ch));
11315 }
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011316
11317 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011318 if (len == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011319 return PyBool_FromLong(0);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011320
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011321 for (i = 0; i < len; i++) {
11322 const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Ezio Melotti93e7afc2011-08-22 14:08:38 +030011323 if (!Py_UNICODE_ISALNUM(ch))
Benjamin Peterson29060642009-01-31 22:14:21 +000011324 return PyBool_FromLong(0);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011325 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011326 return PyBool_FromLong(1);
Marc-André Lemburga7acf422000-07-05 09:49:44 +000011327}
11328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011329PyDoc_STRVAR(isdecimal__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011330 "S.isdecimal() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011331\n\
Guido van Rossum77f6a652002-04-03 22:41:51 +000011332Return True if there are only decimal characters in S,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011333False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011334
11335static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011336unicode_isdecimal(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011337{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011338 Py_ssize_t i, length;
11339 int kind;
11340 void *data;
11341
11342 if (PyUnicode_READY(self) == -1)
11343 return NULL;
11344 length = PyUnicode_GET_LENGTH(self);
11345 kind = PyUnicode_KIND(self);
11346 data = PyUnicode_DATA(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011347
Guido van Rossumd57fd912000-03-10 22:53:23 +000011348 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011349 if (length == 1)
11350 return PyBool_FromLong(
11351 Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0)));
Guido van Rossumd57fd912000-03-10 22:53:23 +000011352
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011353 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011354 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011355 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011356
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011357 for (i = 0; i < length; i++) {
11358 if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i)))
Benjamin Peterson29060642009-01-31 22:14:21 +000011359 return PyBool_FromLong(0);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011360 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011361 return PyBool_FromLong(1);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011362}
11363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011364PyDoc_STRVAR(isdigit__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011365 "S.isdigit() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011366\n\
Martin v. Löwis6828e182003-10-18 09:55:08 +000011367Return True if all characters in S are digits\n\
11368and there is at least one character in S, False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011369
11370static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011371unicode_isdigit(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011372{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011373 Py_ssize_t i, length;
11374 int kind;
11375 void *data;
11376
11377 if (PyUnicode_READY(self) == -1)
11378 return NULL;
11379 length = PyUnicode_GET_LENGTH(self);
11380 kind = PyUnicode_KIND(self);
11381 data = PyUnicode_DATA(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011382
Guido van Rossumd57fd912000-03-10 22:53:23 +000011383 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011384 if (length == 1) {
11385 const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
11386 return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch));
11387 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000011388
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011389 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011390 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011391 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011392
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011393 for (i = 0; i < length; i++) {
11394 if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i)))
Benjamin Peterson29060642009-01-31 22:14:21 +000011395 return PyBool_FromLong(0);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011396 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011397 return PyBool_FromLong(1);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011398}
11399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011400PyDoc_STRVAR(isnumeric__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011401 "S.isnumeric() -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011402\n\
Guido van Rossum77f6a652002-04-03 22:41:51 +000011403Return True if there are only numeric characters in S,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011404False otherwise.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011405
11406static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011407unicode_isnumeric(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011408{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011409 Py_ssize_t i, length;
11410 int kind;
11411 void *data;
11412
11413 if (PyUnicode_READY(self) == -1)
11414 return NULL;
11415 length = PyUnicode_GET_LENGTH(self);
11416 kind = PyUnicode_KIND(self);
11417 data = PyUnicode_DATA(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011418
Guido van Rossumd57fd912000-03-10 22:53:23 +000011419 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011420 if (length == 1)
11421 return PyBool_FromLong(
11422 Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0)));
Guido van Rossumd57fd912000-03-10 22:53:23 +000011423
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011424 /* Special case for empty strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011425 if (length == 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000011426 return PyBool_FromLong(0);
Marc-André Lemburg60bc8092000-06-14 09:18:32 +000011427
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011428 for (i = 0; i < length; i++) {
11429 if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i)))
Benjamin Peterson29060642009-01-31 22:14:21 +000011430 return PyBool_FromLong(0);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011431 }
Guido van Rossum77f6a652002-04-03 22:41:51 +000011432 return PyBool_FromLong(1);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011433}
11434
Martin v. Löwis47383402007-08-15 07:32:56 +000011435int
11436PyUnicode_IsIdentifier(PyObject *self)
11437{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011438 int kind;
11439 void *data;
11440 Py_ssize_t i;
Ezio Melotti93e7afc2011-08-22 14:08:38 +030011441 Py_UCS4 first;
Martin v. Löwis47383402007-08-15 07:32:56 +000011442
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011443 if (PyUnicode_READY(self) == -1) {
11444 Py_FatalError("identifier not ready");
Benjamin Peterson29060642009-01-31 22:14:21 +000011445 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011446 }
11447
11448 /* Special case for empty strings */
11449 if (PyUnicode_GET_LENGTH(self) == 0)
11450 return 0;
11451 kind = PyUnicode_KIND(self);
11452 data = PyUnicode_DATA(self);
Martin v. Löwis47383402007-08-15 07:32:56 +000011453
11454 /* PEP 3131 says that the first character must be in
11455 XID_Start and subsequent characters in XID_Continue,
11456 and for the ASCII range, the 2.x rules apply (i.e
Benjamin Peterson14339b62009-01-31 16:36:08 +000011457 start with letters and underscore, continue with
Martin v. Löwis47383402007-08-15 07:32:56 +000011458 letters, digits, underscore). However, given the current
11459 definition of XID_Start and XID_Continue, it is sufficient
11460 to check just for these, except that _ must be allowed
11461 as starting an identifier. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011462 first = PyUnicode_READ(kind, data, 0);
Benjamin Petersonf413b802011-08-12 22:17:18 -050011463 if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */)
Martin v. Löwis47383402007-08-15 07:32:56 +000011464 return 0;
11465
Benjamin Peterson9c6e6a02011-09-28 08:09:05 -040011466 for (i = 1; i < PyUnicode_GET_LENGTH(self); i++)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011467 if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i)))
Benjamin Peterson29060642009-01-31 22:14:21 +000011468 return 0;
Martin v. Löwis47383402007-08-15 07:32:56 +000011469 return 1;
11470}
11471
11472PyDoc_STRVAR(isidentifier__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011473 "S.isidentifier() -> bool\n\
Martin v. Löwis47383402007-08-15 07:32:56 +000011474\n\
11475Return True if S is a valid identifier according\n\
11476to the language definition.");
11477
11478static PyObject*
11479unicode_isidentifier(PyObject *self)
11480{
11481 return PyBool_FromLong(PyUnicode_IsIdentifier(self));
11482}
11483
Georg Brandl559e5d72008-06-11 18:37:52 +000011484PyDoc_STRVAR(isprintable__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011485 "S.isprintable() -> bool\n\
Georg Brandl559e5d72008-06-11 18:37:52 +000011486\n\
11487Return True if all characters in S are considered\n\
11488printable in repr() or S is empty, False otherwise.");
11489
11490static PyObject*
11491unicode_isprintable(PyObject *self)
11492{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011493 Py_ssize_t i, length;
11494 int kind;
11495 void *data;
11496
11497 if (PyUnicode_READY(self) == -1)
11498 return NULL;
11499 length = PyUnicode_GET_LENGTH(self);
11500 kind = PyUnicode_KIND(self);
11501 data = PyUnicode_DATA(self);
Georg Brandl559e5d72008-06-11 18:37:52 +000011502
11503 /* Shortcut for single character strings */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011504 if (length == 1)
11505 return PyBool_FromLong(
11506 Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0)));
Georg Brandl559e5d72008-06-11 18:37:52 +000011507
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011508 for (i = 0; i < length; i++) {
11509 if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) {
Georg Brandl559e5d72008-06-11 18:37:52 +000011510 Py_RETURN_FALSE;
11511 }
11512 }
11513 Py_RETURN_TRUE;
11514}
11515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011516PyDoc_STRVAR(join__doc__,
Georg Brandl495f7b52009-10-27 15:28:25 +000011517 "S.join(iterable) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011518\n\
11519Return a string which is the concatenation of the strings in the\n\
Georg Brandl495f7b52009-10-27 15:28:25 +000011520iterable. The separator between elements is S.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011521
11522static PyObject*
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000011523unicode_join(PyObject *self, PyObject *data)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011524{
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000011525 return PyUnicode_Join(self, data);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011526}
11527
Martin v. Löwis18e16552006-02-15 17:27:45 +000011528static Py_ssize_t
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011529unicode_length(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011530{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011531 if (PyUnicode_READY(self) == -1)
11532 return -1;
11533 return PyUnicode_GET_LENGTH(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011534}
11535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011536PyDoc_STRVAR(ljust__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011537 "S.ljust(width[, fillchar]) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011538\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000011539Return S left-justified in a Unicode string of length width. Padding is\n\
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000011540done using the specified fill character (default is a space).");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011541
11542static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +020011543unicode_ljust(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011544{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011545 Py_ssize_t width;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011546 Py_UCS4 fillchar = ' ';
11547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011548 if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar))
Guido van Rossumd57fd912000-03-10 22:53:23 +000011549 return NULL;
11550
Benjamin Petersonbac79492012-01-14 13:34:47 -050011551 if (PyUnicode_READY(self) == -1)
Victor Stinnerc4b49542011-12-11 22:44:26 +010011552 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011553
Victor Stinnerc4b49542011-12-11 22:44:26 +010011554 if (PyUnicode_GET_LENGTH(self) >= width)
11555 return unicode_result_unchanged(self);
11556
11557 return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011558}
11559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011560PyDoc_STRVAR(lower__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011561 "S.lower() -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011562\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011563Return a copy of the string S converted to lowercase.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011564
11565static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020011566unicode_lower(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011567{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -050011568 if (PyUnicode_READY(self) == -1)
11569 return NULL;
11570 if (PyUnicode_IS_ASCII(self))
11571 return ascii_upper_or_lower(self, 1);
Victor Stinnerb0800dc2012-02-25 00:47:08 +010011572 return case_operation(self, do_lower);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011573}
11574
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011575#define LEFTSTRIP 0
11576#define RIGHTSTRIP 1
11577#define BOTHSTRIP 2
11578
11579/* Arrays indexed by above */
11580static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
11581
11582#define STRIPNAME(i) (stripformat[i]+3)
11583
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011584/* externally visible for str.strip(unicode) */
11585PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011586_PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj)
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011587{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011588 void *data;
11589 int kind;
11590 Py_ssize_t i, j, len;
11591 BLOOM_MASK sepmask;
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011592
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011593 if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1)
11594 return NULL;
11595
11596 kind = PyUnicode_KIND(self);
11597 data = PyUnicode_DATA(self);
11598 len = PyUnicode_GET_LENGTH(self);
11599 sepmask = make_bloom_mask(PyUnicode_KIND(sepobj),
11600 PyUnicode_DATA(sepobj),
11601 PyUnicode_GET_LENGTH(sepobj));
Thomas Wouters477c8d52006-05-27 19:21:47 +000011602
Benjamin Peterson14339b62009-01-31 16:36:08 +000011603 i = 0;
11604 if (striptype != RIGHTSTRIP) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011605 while (i < len &&
11606 BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) {
Benjamin Peterson29060642009-01-31 22:14:21 +000011607 i++;
11608 }
Benjamin Peterson14339b62009-01-31 16:36:08 +000011609 }
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011610
Benjamin Peterson14339b62009-01-31 16:36:08 +000011611 j = len;
11612 if (striptype != LEFTSTRIP) {
Benjamin Peterson29060642009-01-31 22:14:21 +000011613 do {
11614 j--;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011615 } while (j >= i &&
11616 BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj));
Benjamin Peterson29060642009-01-31 22:14:21 +000011617 j++;
Benjamin Peterson14339b62009-01-31 16:36:08 +000011618 }
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011619
Victor Stinner7931d9a2011-11-04 00:22:48 +010011620 return PyUnicode_Substring(self, i, j);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011621}
11622
11623PyObject*
11624PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
11625{
11626 unsigned char *data;
11627 int kind;
Victor Stinner12bab6d2011-10-01 01:53:49 +020011628 Py_ssize_t length;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011629
Victor Stinnerde636f32011-10-01 03:55:54 +020011630 if (PyUnicode_READY(self) == -1)
11631 return NULL;
11632
Victor Stinner684d5fd2012-05-03 02:32:34 +020011633 length = PyUnicode_GET_LENGTH(self);
11634 end = Py_MIN(end, length);
Victor Stinnerde636f32011-10-01 03:55:54 +020011635
Victor Stinner684d5fd2012-05-03 02:32:34 +020011636 if (start == 0 && end == length)
Victor Stinnerc4b49542011-12-11 22:44:26 +010011637 return unicode_result_unchanged(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011638
Victor Stinnerde636f32011-10-01 03:55:54 +020011639 if (start < 0 || end < 0) {
Victor Stinner12bab6d2011-10-01 01:53:49 +020011640 PyErr_SetString(PyExc_IndexError, "string index out of range");
11641 return NULL;
11642 }
Victor Stinner684d5fd2012-05-03 02:32:34 +020011643 if (start >= length || end < start) {
Victor Stinner3a7f7972012-05-03 03:36:40 +020011644 Py_INCREF(unicode_empty);
11645 return unicode_empty;
Victor Stinner684d5fd2012-05-03 02:32:34 +020011646 }
Victor Stinner12bab6d2011-10-01 01:53:49 +020011647
Victor Stinner684d5fd2012-05-03 02:32:34 +020011648 length = end - start;
Victor Stinnerb9275c12011-10-05 14:01:42 +020011649 if (PyUnicode_IS_ASCII(self)) {
Victor Stinnerb9275c12011-10-05 14:01:42 +020011650 data = PyUnicode_1BYTE_DATA(self);
Victor Stinnerd3f08822012-05-29 12:57:52 +020011651 return _PyUnicode_FromASCII((char*)(data + start), length);
Victor Stinnerb9275c12011-10-05 14:01:42 +020011652 }
11653 else {
11654 kind = PyUnicode_KIND(self);
11655 data = PyUnicode_1BYTE_DATA(self);
11656 return PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +020011657 data + kind * start,
Victor Stinnerb9275c12011-10-05 14:01:42 +020011658 length);
11659 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011660}
Guido van Rossumd57fd912000-03-10 22:53:23 +000011661
11662static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011663do_strip(PyObject *self, int striptype)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011664{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011665 int kind;
11666 void *data;
11667 Py_ssize_t len, i, j;
11668
11669 if (PyUnicode_READY(self) == -1)
11670 return NULL;
11671
11672 kind = PyUnicode_KIND(self);
11673 data = PyUnicode_DATA(self);
11674 len = PyUnicode_GET_LENGTH(self);
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011675
Benjamin Peterson14339b62009-01-31 16:36:08 +000011676 i = 0;
11677 if (striptype != RIGHTSTRIP) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011678 while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
Benjamin Peterson14339b62009-01-31 16:36:08 +000011679 i++;
11680 }
11681 }
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011682
Benjamin Peterson14339b62009-01-31 16:36:08 +000011683 j = len;
11684 if (striptype != LEFTSTRIP) {
11685 do {
11686 j--;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011687 } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j)));
Benjamin Peterson14339b62009-01-31 16:36:08 +000011688 j++;
11689 }
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011690
Victor Stinner7931d9a2011-11-04 00:22:48 +010011691 return PyUnicode_Substring(self, i, j);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011692}
11693
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011694
11695static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011696do_argstrip(PyObject *self, int striptype, PyObject *args)
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011697{
Benjamin Peterson14339b62009-01-31 16:36:08 +000011698 PyObject *sep = NULL;
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011699
Benjamin Peterson14339b62009-01-31 16:36:08 +000011700 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
11701 return NULL;
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011702
Benjamin Peterson14339b62009-01-31 16:36:08 +000011703 if (sep != NULL && sep != Py_None) {
11704 if (PyUnicode_Check(sep))
11705 return _PyUnicode_XStrip(self, striptype, sep);
11706 else {
11707 PyErr_Format(PyExc_TypeError,
Benjamin Peterson29060642009-01-31 22:14:21 +000011708 "%s arg must be None or str",
11709 STRIPNAME(striptype));
Benjamin Peterson14339b62009-01-31 16:36:08 +000011710 return NULL;
11711 }
11712 }
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011713
Benjamin Peterson14339b62009-01-31 16:36:08 +000011714 return do_strip(self, striptype);
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011715}
11716
11717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011718PyDoc_STRVAR(strip__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011719 "S.strip([chars]) -> str\n\
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011720\n\
11721Return a copy of the string S with leading and trailing\n\
11722whitespace removed.\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000011723If chars is given and not None, remove characters in chars instead.");
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011724
11725static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011726unicode_strip(PyObject *self, PyObject *args)
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011727{
Benjamin Peterson14339b62009-01-31 16:36:08 +000011728 if (PyTuple_GET_SIZE(args) == 0)
11729 return do_strip(self, BOTHSTRIP); /* Common case */
11730 else
11731 return do_argstrip(self, BOTHSTRIP, args);
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011732}
11733
11734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011735PyDoc_STRVAR(lstrip__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011736 "S.lstrip([chars]) -> str\n\
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011737\n\
11738Return a copy of the string S with leading whitespace removed.\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000011739If chars is given and not None, remove characters in chars instead.");
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011740
11741static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011742unicode_lstrip(PyObject *self, PyObject *args)
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011743{
Benjamin Peterson14339b62009-01-31 16:36:08 +000011744 if (PyTuple_GET_SIZE(args) == 0)
11745 return do_strip(self, LEFTSTRIP); /* Common case */
11746 else
11747 return do_argstrip(self, LEFTSTRIP, args);
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011748}
11749
11750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011751PyDoc_STRVAR(rstrip__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000011752 "S.rstrip([chars]) -> str\n\
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011753\n\
11754Return a copy of the string S with trailing whitespace removed.\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000011755If chars is given and not None, remove characters in chars instead.");
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011756
11757static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011758unicode_rstrip(PyObject *self, PyObject *args)
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011759{
Benjamin Peterson14339b62009-01-31 16:36:08 +000011760 if (PyTuple_GET_SIZE(args) == 0)
11761 return do_strip(self, RIGHTSTRIP); /* Common case */
11762 else
11763 return do_argstrip(self, RIGHTSTRIP, args);
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000011764}
11765
11766
Guido van Rossumd57fd912000-03-10 22:53:23 +000011767static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011768unicode_repeat(PyObject *str, Py_ssize_t len)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011769{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011770 PyObject *u;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011771 Py_ssize_t nchars, n;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011772
Georg Brandl222de0f2009-04-12 12:01:50 +000011773 if (len < 1) {
11774 Py_INCREF(unicode_empty);
Victor Stinnera464fc12011-10-02 20:39:30 +020011775 return unicode_empty;
Georg Brandl222de0f2009-04-12 12:01:50 +000011776 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000011777
Victor Stinnerc4b49542011-12-11 22:44:26 +010011778 /* no repeat, return original string */
11779 if (len == 1)
11780 return unicode_result_unchanged(str);
Tim Peters8f422462000-09-09 06:13:41 +000011781
Benjamin Petersonbac79492012-01-14 13:34:47 -050011782 if (PyUnicode_READY(str) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011783 return NULL;
11784
Victor Stinnerc759f3e2011-10-01 03:09:58 +020011785 if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) {
Victor Stinner67ca64c2011-10-01 02:47:29 +020011786 PyErr_SetString(PyExc_OverflowError,
11787 "repeated string is too long");
11788 return NULL;
11789 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011790 nchars = len * PyUnicode_GET_LENGTH(str);
Victor Stinner67ca64c2011-10-01 02:47:29 +020011791
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011792 u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str));
Guido van Rossumd57fd912000-03-10 22:53:23 +000011793 if (!u)
11794 return NULL;
Victor Stinner67ca64c2011-10-01 02:47:29 +020011795 assert(PyUnicode_KIND(u) == PyUnicode_KIND(str));
Guido van Rossumd57fd912000-03-10 22:53:23 +000011796
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011797 if (PyUnicode_GET_LENGTH(str) == 1) {
11798 const int kind = PyUnicode_KIND(str);
11799 const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0);
Victor Stinner73f53b52011-12-18 03:26:31 +010011800 if (kind == PyUnicode_1BYTE_KIND) {
11801 void *to = PyUnicode_DATA(u);
Victor Stinner67ca64c2011-10-01 02:47:29 +020011802 memset(to, (unsigned char)fill_char, len);
Victor Stinner73f53b52011-12-18 03:26:31 +010011803 }
11804 else if (kind == PyUnicode_2BYTE_KIND) {
11805 Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u);
Victor Stinner67ca64c2011-10-01 02:47:29 +020011806 for (n = 0; n < len; ++n)
Victor Stinner73f53b52011-12-18 03:26:31 +010011807 ucs2[n] = fill_char;
11808 } else {
11809 Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u);
11810 assert(kind == PyUnicode_4BYTE_KIND);
11811 for (n = 0; n < len; ++n)
11812 ucs4[n] = fill_char;
Victor Stinner67ca64c2011-10-01 02:47:29 +020011813 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011814 }
11815 else {
11816 /* number of characters copied this far */
11817 Py_ssize_t done = PyUnicode_GET_LENGTH(str);
Martin v. Löwisc47adb02011-10-07 20:55:35 +020011818 const Py_ssize_t char_size = PyUnicode_KIND(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011819 char *to = (char *) PyUnicode_DATA(u);
11820 Py_MEMCPY(to, PyUnicode_DATA(str),
11821 PyUnicode_GET_LENGTH(str) * char_size);
Benjamin Peterson29060642009-01-31 22:14:21 +000011822 while (done < nchars) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011823 n = (done <= nchars-done) ? done : nchars-done;
11824 Py_MEMCPY(to + (done * char_size), to, n * char_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +000011825 done += n;
Benjamin Peterson29060642009-01-31 22:14:21 +000011826 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000011827 }
11828
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020011829 assert(_PyUnicode_CheckConsistency(u, 1));
Victor Stinner9db1a8b2011-10-23 20:04:37 +020011830 return u;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011831}
11832
Alexander Belopolsky40018472011-02-26 01:02:56 +000011833PyObject *
11834PyUnicode_Replace(PyObject *obj,
11835 PyObject *subobj,
11836 PyObject *replobj,
11837 Py_ssize_t maxcount)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011838{
11839 PyObject *self;
11840 PyObject *str1;
11841 PyObject *str2;
11842 PyObject *result;
11843
11844 self = PyUnicode_FromObject(obj);
Benjamin Peterson22a29702012-01-02 09:00:30 -060011845 if (self == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +000011846 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011847 str1 = PyUnicode_FromObject(subobj);
Benjamin Peterson22a29702012-01-02 09:00:30 -060011848 if (str1 == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +000011849 Py_DECREF(self);
11850 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011851 }
11852 str2 = PyUnicode_FromObject(replobj);
Benjamin Peterson22a29702012-01-02 09:00:30 -060011853 if (str2 == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +000011854 Py_DECREF(self);
11855 Py_DECREF(str1);
11856 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011857 }
Benjamin Peterson22a29702012-01-02 09:00:30 -060011858 if (PyUnicode_READY(self) == -1 ||
11859 PyUnicode_READY(str1) == -1 ||
11860 PyUnicode_READY(str2) == -1)
11861 result = NULL;
11862 else
11863 result = replace(self, str1, str2, maxcount);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011864 Py_DECREF(self);
11865 Py_DECREF(str1);
11866 Py_DECREF(str2);
11867 return result;
11868}
11869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011870PyDoc_STRVAR(replace__doc__,
Ezio Melottic1897e72010-06-26 18:50:39 +000011871 "S.replace(old, new[, count]) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000011872\n\
11873Return a copy of S with all occurrences of substring\n\
Georg Brandlf08a9dd2008-06-10 16:57:31 +000011874old replaced by new. If the optional argument count is\n\
11875given, only the first count occurrences are replaced.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000011876
11877static PyObject*
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011878unicode_replace(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011879{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011880 PyObject *str1;
11881 PyObject *str2;
Martin v. Löwis18e16552006-02-15 17:27:45 +000011882 Py_ssize_t maxcount = -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +000011883 PyObject *result;
11884
Martin v. Löwis18e16552006-02-15 17:27:45 +000011885 if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))
Guido van Rossumd57fd912000-03-10 22:53:23 +000011886 return NULL;
Benjamin Peterson22a29702012-01-02 09:00:30 -060011887 if (PyUnicode_READY(self) == -1)
Benjamin Peterson29060642009-01-31 22:14:21 +000011888 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011889 str1 = PyUnicode_FromObject(str1);
Benjamin Peterson22a29702012-01-02 09:00:30 -060011890 if (str1 == NULL)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011891 return NULL;
11892 str2 = PyUnicode_FromObject(str2);
Benjamin Peterson22a29702012-01-02 09:00:30 -060011893 if (str2 == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +000011894 Py_DECREF(str1);
11895 return NULL;
Walter Dörwaldf6b56ae2003-02-09 23:42:56 +000011896 }
Benjamin Peterson22a29702012-01-02 09:00:30 -060011897 if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1)
11898 result = NULL;
11899 else
11900 result = replace(self, str1, str2, maxcount);
Guido van Rossumd57fd912000-03-10 22:53:23 +000011901
11902 Py_DECREF(str1);
11903 Py_DECREF(str2);
11904 return result;
11905}
11906
Alexander Belopolsky40018472011-02-26 01:02:56 +000011907static PyObject *
11908unicode_repr(PyObject *unicode)
Guido van Rossumd57fd912000-03-10 22:53:23 +000011909{
Walter Dörwald79e913e2007-05-12 11:08:06 +000011910 PyObject *repr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011911 Py_ssize_t isize;
11912 Py_ssize_t osize, squote, dquote, i, o;
11913 Py_UCS4 max, quote;
11914 int ikind, okind;
11915 void *idata, *odata;
Walter Dörwald79e913e2007-05-12 11:08:06 +000011916
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011917 if (PyUnicode_READY(unicode) == -1)
Walter Dörwald79e913e2007-05-12 11:08:06 +000011918 return NULL;
11919
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011920 isize = PyUnicode_GET_LENGTH(unicode);
11921 idata = PyUnicode_DATA(unicode);
Walter Dörwald79e913e2007-05-12 11:08:06 +000011922
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011923 /* Compute length of output, quote characters, and
11924 maximum character */
11925 osize = 2; /* quotes */
11926 max = 127;
11927 squote = dquote = 0;
11928 ikind = PyUnicode_KIND(unicode);
11929 for (i = 0; i < isize; i++) {
11930 Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
11931 switch (ch) {
11932 case '\'': squote++; osize++; break;
11933 case '"': dquote++; osize++; break;
11934 case '\\': case '\t': case '\r': case '\n':
11935 osize += 2; break;
11936 default:
11937 /* Fast-path ASCII */
11938 if (ch < ' ' || ch == 0x7f)
11939 osize += 4; /* \xHH */
11940 else if (ch < 0x7f)
11941 osize++;
11942 else if (Py_UNICODE_ISPRINTABLE(ch)) {
11943 osize++;
11944 max = ch > max ? ch : max;
11945 }
11946 else if (ch < 0x100)
11947 osize += 4; /* \xHH */
11948 else if (ch < 0x10000)
11949 osize += 6; /* \uHHHH */
11950 else
11951 osize += 10; /* \uHHHHHHHH */
11952 }
11953 }
11954
11955 quote = '\'';
11956 if (squote) {
11957 if (dquote)
11958 /* Both squote and dquote present. Use squote,
11959 and escape them */
11960 osize += squote;
11961 else
11962 quote = '"';
11963 }
11964
11965 repr = PyUnicode_New(osize, max);
11966 if (repr == NULL)
11967 return NULL;
11968 okind = PyUnicode_KIND(repr);
11969 odata = PyUnicode_DATA(repr);
11970
11971 PyUnicode_WRITE(okind, odata, 0, quote);
11972 PyUnicode_WRITE(okind, odata, osize-1, quote);
11973
11974 for (i = 0, o = 1; i < isize; i++) {
11975 Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
Walter Dörwald79e913e2007-05-12 11:08:06 +000011976
11977 /* Escape quotes and backslashes */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011978 if ((ch == quote) || (ch == '\\')) {
11979 PyUnicode_WRITE(okind, odata, o++, '\\');
11980 PyUnicode_WRITE(okind, odata, o++, ch);
Walter Dörwald79e913e2007-05-12 11:08:06 +000011981 continue;
11982 }
11983
Benjamin Peterson29060642009-01-31 22:14:21 +000011984 /* Map special whitespace to '\t', \n', '\r' */
Georg Brandl559e5d72008-06-11 18:37:52 +000011985 if (ch == '\t') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011986 PyUnicode_WRITE(okind, odata, o++, '\\');
11987 PyUnicode_WRITE(okind, odata, o++, 't');
Walter Dörwald79e913e2007-05-12 11:08:06 +000011988 }
11989 else if (ch == '\n') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011990 PyUnicode_WRITE(okind, odata, o++, '\\');
11991 PyUnicode_WRITE(okind, odata, o++, 'n');
Walter Dörwald79e913e2007-05-12 11:08:06 +000011992 }
11993 else if (ch == '\r') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011994 PyUnicode_WRITE(okind, odata, o++, '\\');
11995 PyUnicode_WRITE(okind, odata, o++, 'r');
Walter Dörwald79e913e2007-05-12 11:08:06 +000011996 }
11997
11998 /* Map non-printable US ASCII to '\xhh' */
Georg Brandl559e5d72008-06-11 18:37:52 +000011999 else if (ch < ' ' || ch == 0x7F) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012000 PyUnicode_WRITE(okind, odata, o++, '\\');
12001 PyUnicode_WRITE(okind, odata, o++, 'x');
Victor Stinnerf5cff562011-10-14 02:13:11 +020012002 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]);
12003 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]);
Walter Dörwald79e913e2007-05-12 11:08:06 +000012004 }
12005
Georg Brandl559e5d72008-06-11 18:37:52 +000012006 /* Copy ASCII characters as-is */
12007 else if (ch < 0x7F) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012008 PyUnicode_WRITE(okind, odata, o++, ch);
Georg Brandl559e5d72008-06-11 18:37:52 +000012009 }
12010
Benjamin Peterson29060642009-01-31 22:14:21 +000012011 /* Non-ASCII characters */
Georg Brandl559e5d72008-06-11 18:37:52 +000012012 else {
Benjamin Peterson14339b62009-01-31 16:36:08 +000012013 /* Map Unicode whitespace and control characters
Georg Brandl559e5d72008-06-11 18:37:52 +000012014 (categories Z* and C* except ASCII space)
12015 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012016 if (!Py_UNICODE_ISPRINTABLE(ch)) {
Kristján Valur Jónsson55e5dc82012-06-06 21:58:08 +000012017 PyUnicode_WRITE(okind, odata, o++, '\\');
Georg Brandl559e5d72008-06-11 18:37:52 +000012018 /* Map 8-bit characters to '\xhh' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012019 if (ch <= 0xff) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012020 PyUnicode_WRITE(okind, odata, o++, 'x');
Victor Stinnerf5cff562011-10-14 02:13:11 +020012021 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]);
12022 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]);
Georg Brandl559e5d72008-06-11 18:37:52 +000012023 }
Kristján Valur Jónsson55e5dc82012-06-06 21:58:08 +000012024 /* Map 16-bit characters to '\uxxxx' */
12025 else if (ch <= 0xffff) {
12026 PyUnicode_WRITE(okind, odata, o++, 'u');
Victor Stinnerf5cff562011-10-14 02:13:11 +020012027 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]);
12028 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]);
12029 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]);
12030 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]);
Georg Brandl559e5d72008-06-11 18:37:52 +000012031 }
Kristján Valur Jónsson55e5dc82012-06-06 21:58:08 +000012032 /* Map 21-bit characters to '\U00xxxxxx' */
Georg Brandl559e5d72008-06-11 18:37:52 +000012033 else {
Kristján Valur Jónsson55e5dc82012-06-06 21:58:08 +000012034 PyUnicode_WRITE(okind, odata, o++, 'U');
12035 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]);
12036 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]);
12037 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]);
12038 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]);
Victor Stinnerf5cff562011-10-14 02:13:11 +020012039 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]);
12040 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]);
12041 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]);
12042 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]);
Georg Brandl559e5d72008-06-11 18:37:52 +000012043 }
12044 }
12045 /* Copy characters as-is */
12046 else {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012047 PyUnicode_WRITE(okind, odata, o++, ch);
Georg Brandl559e5d72008-06-11 18:37:52 +000012048 }
12049 }
Walter Dörwald79e913e2007-05-12 11:08:06 +000012050 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012051 /* Closing quote already added at the beginning */
Victor Stinner05d11892011-10-06 01:13:58 +020012052 assert(_PyUnicode_CheckConsistency(repr, 1));
Walter Dörwald79e913e2007-05-12 11:08:06 +000012053 return repr;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012054}
12055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012056PyDoc_STRVAR(rfind__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012057 "S.rfind(sub[, start[, end]]) -> int\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012058\n\
12059Return the highest index in S where substring sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +080012060such that sub is contained within S[start:end]. Optional\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012061arguments start and end are interpreted as in slice notation.\n\
12062\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012063Return -1 on failure.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012064
12065static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012066unicode_rfind(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012067{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012068 PyObject *substring;
Christian Heimes9cd17752007-11-18 19:35:23 +000012069 Py_ssize_t start;
12070 Py_ssize_t end;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012071 Py_ssize_t result;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012072
Jesus Ceaac451502011-04-20 17:09:23 +020012073 if (!stringlib_parse_args_finds_unicode("rfind", args, &substring,
12074 &start, &end))
Benjamin Peterson14339b62009-01-31 16:36:08 +000012075 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012076
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012077 if (PyUnicode_READY(self) == -1)
12078 return NULL;
12079 if (PyUnicode_READY(substring) == -1)
12080 return NULL;
12081
Victor Stinner7931d9a2011-11-04 00:22:48 +010012082 result = any_find_slice(-1, self, substring, start, end);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012083
12084 Py_DECREF(substring);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012085
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012086 if (result == -2)
12087 return NULL;
12088
Christian Heimes217cfd12007-12-02 14:31:20 +000012089 return PyLong_FromSsize_t(result);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012090}
12091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012092PyDoc_STRVAR(rindex__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012093 "S.rindex(sub[, start[, end]]) -> int\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012094\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012095Like S.rfind() but raise ValueError when the substring is not found.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012096
12097static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012098unicode_rindex(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012099{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012100 PyObject *substring;
Christian Heimes9cd17752007-11-18 19:35:23 +000012101 Py_ssize_t start;
12102 Py_ssize_t end;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012103 Py_ssize_t result;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012104
Jesus Ceaac451502011-04-20 17:09:23 +020012105 if (!stringlib_parse_args_finds_unicode("rindex", args, &substring,
12106 &start, &end))
Benjamin Peterson14339b62009-01-31 16:36:08 +000012107 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012108
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012109 if (PyUnicode_READY(self) == -1)
12110 return NULL;
12111 if (PyUnicode_READY(substring) == -1)
12112 return NULL;
12113
Victor Stinner7931d9a2011-11-04 00:22:48 +010012114 result = any_find_slice(-1, self, substring, start, end);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012115
12116 Py_DECREF(substring);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012117
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012118 if (result == -2)
12119 return NULL;
12120
Guido van Rossumd57fd912000-03-10 22:53:23 +000012121 if (result < 0) {
12122 PyErr_SetString(PyExc_ValueError, "substring not found");
12123 return NULL;
12124 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012125
Christian Heimes217cfd12007-12-02 14:31:20 +000012126 return PyLong_FromSsize_t(result);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012127}
12128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012129PyDoc_STRVAR(rjust__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012130 "S.rjust(width[, fillchar]) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012131\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000012132Return S right-justified in a string of length width. Padding is\n\
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000012133done using the specified fill character (default is a space).");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012134
12135static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +020012136unicode_rjust(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012137{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012138 Py_ssize_t width;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012139 Py_UCS4 fillchar = ' ';
12140
Victor Stinnere9a29352011-10-01 02:14:59 +020012141 if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012142 return NULL;
Raymond Hettinger4f8f9762003-11-26 08:21:35 +000012143
Benjamin Petersonbac79492012-01-14 13:34:47 -050012144 if (PyUnicode_READY(self) == -1)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012145 return NULL;
12146
Victor Stinnerc4b49542011-12-11 22:44:26 +010012147 if (PyUnicode_GET_LENGTH(self) >= width)
12148 return unicode_result_unchanged(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012149
Victor Stinnerc4b49542011-12-11 22:44:26 +010012150 return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012151}
12152
Alexander Belopolsky40018472011-02-26 01:02:56 +000012153PyObject *
12154PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012155{
12156 PyObject *result;
Tim Petersced69f82003-09-16 20:30:58 +000012157
Guido van Rossumd57fd912000-03-10 22:53:23 +000012158 s = PyUnicode_FromObject(s);
12159 if (s == NULL)
Benjamin Peterson14339b62009-01-31 16:36:08 +000012160 return NULL;
Benjamin Peterson29060642009-01-31 22:14:21 +000012161 if (sep != NULL) {
12162 sep = PyUnicode_FromObject(sep);
12163 if (sep == NULL) {
12164 Py_DECREF(s);
12165 return NULL;
12166 }
Guido van Rossumd57fd912000-03-10 22:53:23 +000012167 }
12168
Victor Stinner9310abb2011-10-05 00:59:23 +020012169 result = split(s, sep, maxsplit);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012170
12171 Py_DECREF(s);
12172 Py_XDECREF(sep);
12173 return result;
12174}
12175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012176PyDoc_STRVAR(split__doc__,
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012177 "S.split(sep=None, maxsplit=-1) -> list of strings\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012178\n\
12179Return a list of the words in S, using sep as the\n\
12180delimiter string. If maxsplit is given, at most maxsplit\n\
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000012181splits are done. If sep is not specified or is None, any\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000012182whitespace string is a separator and empty strings are\n\
12183removed from the result.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012184
12185static PyObject*
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012186unicode_split(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012187{
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012188 static char *kwlist[] = {"sep", "maxsplit", 0};
Guido van Rossumd57fd912000-03-10 22:53:23 +000012189 PyObject *substring = Py_None;
Martin v. Löwis18e16552006-02-15 17:27:45 +000012190 Py_ssize_t maxcount = -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012191
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012192 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split",
12193 kwlist, &substring, &maxcount))
Guido van Rossumd57fd912000-03-10 22:53:23 +000012194 return NULL;
12195
12196 if (substring == Py_None)
Benjamin Peterson29060642009-01-31 22:14:21 +000012197 return split(self, NULL, maxcount);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012198 else if (PyUnicode_Check(substring))
Victor Stinner9310abb2011-10-05 00:59:23 +020012199 return split(self, substring, maxcount);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012200 else
Victor Stinner7931d9a2011-11-04 00:22:48 +010012201 return PyUnicode_Split(self, substring, maxcount);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012202}
12203
Thomas Wouters477c8d52006-05-27 19:21:47 +000012204PyObject *
12205PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
12206{
12207 PyObject* str_obj;
12208 PyObject* sep_obj;
12209 PyObject* out;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012210 int kind1, kind2, kind;
12211 void *buf1 = NULL, *buf2 = NULL;
12212 Py_ssize_t len1, len2;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012213
12214 str_obj = PyUnicode_FromObject(str_in);
Benjamin Peterson22a29702012-01-02 09:00:30 -060012215 if (!str_obj)
Benjamin Peterson29060642009-01-31 22:14:21 +000012216 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012217 sep_obj = PyUnicode_FromObject(sep_in);
Benjamin Peterson22a29702012-01-02 09:00:30 -060012218 if (!sep_obj) {
12219 Py_DECREF(str_obj);
12220 return NULL;
12221 }
12222 if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) {
12223 Py_DECREF(sep_obj);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012224 Py_DECREF(str_obj);
12225 return NULL;
12226 }
12227
Victor Stinner14f8f022011-10-05 20:58:25 +020012228 kind1 = PyUnicode_KIND(str_obj);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012229 kind2 = PyUnicode_KIND(sep_obj);
Victor Stinner14f8f022011-10-05 20:58:25 +020012230 kind = Py_MAX(kind1, kind2);
12231 buf1 = PyUnicode_DATA(str_obj);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012232 if (kind1 != kind)
Victor Stinner14f8f022011-10-05 20:58:25 +020012233 buf1 = _PyUnicode_AsKind(str_obj, kind);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012234 if (!buf1)
12235 goto onError;
12236 buf2 = PyUnicode_DATA(sep_obj);
12237 if (kind2 != kind)
12238 buf2 = _PyUnicode_AsKind(sep_obj, kind);
12239 if (!buf2)
12240 goto onError;
12241 len1 = PyUnicode_GET_LENGTH(str_obj);
12242 len2 = PyUnicode_GET_LENGTH(sep_obj);
12243
Benjamin Petersonead6b532011-12-20 17:23:42 -060012244 switch (PyUnicode_KIND(str_obj)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012245 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +020012246 if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj))
12247 out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
12248 else
12249 out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012250 break;
12251 case PyUnicode_2BYTE_KIND:
12252 out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
12253 break;
12254 case PyUnicode_4BYTE_KIND:
12255 out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
12256 break;
12257 default:
12258 assert(0);
12259 out = 0;
12260 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000012261
12262 Py_DECREF(sep_obj);
12263 Py_DECREF(str_obj);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012264 if (kind1 != kind)
12265 PyMem_Free(buf1);
12266 if (kind2 != kind)
12267 PyMem_Free(buf2);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012268
12269 return out;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012270 onError:
12271 Py_DECREF(sep_obj);
12272 Py_DECREF(str_obj);
12273 if (kind1 != kind && buf1)
12274 PyMem_Free(buf1);
12275 if (kind2 != kind && buf2)
12276 PyMem_Free(buf2);
12277 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012278}
12279
12280
12281PyObject *
12282PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
12283{
12284 PyObject* str_obj;
12285 PyObject* sep_obj;
12286 PyObject* out;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012287 int kind1, kind2, kind;
12288 void *buf1 = NULL, *buf2 = NULL;
12289 Py_ssize_t len1, len2;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012290
12291 str_obj = PyUnicode_FromObject(str_in);
12292 if (!str_obj)
Benjamin Peterson29060642009-01-31 22:14:21 +000012293 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012294 sep_obj = PyUnicode_FromObject(sep_in);
12295 if (!sep_obj) {
12296 Py_DECREF(str_obj);
12297 return NULL;
12298 }
12299
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012300 kind1 = PyUnicode_KIND(str_in);
12301 kind2 = PyUnicode_KIND(sep_obj);
Georg Brandl4cb0de22011-09-28 21:49:49 +020012302 kind = Py_MAX(kind1, kind2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012303 buf1 = PyUnicode_DATA(str_in);
12304 if (kind1 != kind)
12305 buf1 = _PyUnicode_AsKind(str_in, kind);
12306 if (!buf1)
12307 goto onError;
12308 buf2 = PyUnicode_DATA(sep_obj);
12309 if (kind2 != kind)
12310 buf2 = _PyUnicode_AsKind(sep_obj, kind);
12311 if (!buf2)
12312 goto onError;
12313 len1 = PyUnicode_GET_LENGTH(str_obj);
12314 len2 = PyUnicode_GET_LENGTH(sep_obj);
12315
Benjamin Petersonead6b532011-12-20 17:23:42 -060012316 switch (PyUnicode_KIND(str_in)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012317 case PyUnicode_1BYTE_KIND:
Victor Stinnerc3cec782011-10-05 21:24:08 +020012318 if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj))
12319 out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
12320 else
12321 out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012322 break;
12323 case PyUnicode_2BYTE_KIND:
12324 out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
12325 break;
12326 case PyUnicode_4BYTE_KIND:
12327 out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
12328 break;
12329 default:
12330 assert(0);
12331 out = 0;
12332 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000012333
12334 Py_DECREF(sep_obj);
12335 Py_DECREF(str_obj);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012336 if (kind1 != kind)
12337 PyMem_Free(buf1);
12338 if (kind2 != kind)
12339 PyMem_Free(buf2);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012340
12341 return out;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012342 onError:
12343 Py_DECREF(sep_obj);
12344 Py_DECREF(str_obj);
12345 if (kind1 != kind && buf1)
12346 PyMem_Free(buf1);
12347 if (kind2 != kind && buf2)
12348 PyMem_Free(buf2);
12349 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012350}
12351
12352PyDoc_STRVAR(partition__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012353 "S.partition(sep) -> (head, sep, tail)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +000012354\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +000012355Search for the separator sep in S, and return the part before it,\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +000012356the separator itself, and the part after it. If the separator is not\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000012357found, return S and two empty strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +000012358
12359static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020012360unicode_partition(PyObject *self, PyObject *separator)
Thomas Wouters477c8d52006-05-27 19:21:47 +000012361{
Victor Stinner9310abb2011-10-05 00:59:23 +020012362 return PyUnicode_Partition(self, separator);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012363}
12364
12365PyDoc_STRVAR(rpartition__doc__,
Ezio Melotti5b2b2422010-01-25 11:58:28 +000012366 "S.rpartition(sep) -> (head, sep, tail)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +000012367\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +000012368Search for the separator sep in S, starting at the end of S, and return\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +000012369the part before it, the separator itself, and the part after it. If the\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000012370separator is not found, return two empty strings and S.");
Thomas Wouters477c8d52006-05-27 19:21:47 +000012371
12372static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020012373unicode_rpartition(PyObject *self, PyObject *separator)
Thomas Wouters477c8d52006-05-27 19:21:47 +000012374{
Victor Stinner9310abb2011-10-05 00:59:23 +020012375 return PyUnicode_RPartition(self, separator);
Thomas Wouters477c8d52006-05-27 19:21:47 +000012376}
12377
Alexander Belopolsky40018472011-02-26 01:02:56 +000012378PyObject *
12379PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012380{
12381 PyObject *result;
Benjamin Peterson14339b62009-01-31 16:36:08 +000012382
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012383 s = PyUnicode_FromObject(s);
12384 if (s == NULL)
Benjamin Peterson14339b62009-01-31 16:36:08 +000012385 return NULL;
Benjamin Peterson29060642009-01-31 22:14:21 +000012386 if (sep != NULL) {
12387 sep = PyUnicode_FromObject(sep);
12388 if (sep == NULL) {
12389 Py_DECREF(s);
12390 return NULL;
12391 }
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012392 }
12393
Victor Stinner9310abb2011-10-05 00:59:23 +020012394 result = rsplit(s, sep, maxsplit);
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012395
12396 Py_DECREF(s);
12397 Py_XDECREF(sep);
12398 return result;
12399}
12400
12401PyDoc_STRVAR(rsplit__doc__,
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012402 "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012403\n\
12404Return a list of the words in S, using sep as the\n\
12405delimiter string, starting at the end of the string and\n\
12406working to the front. If maxsplit is given, at most maxsplit\n\
12407splits are done. If sep is not specified, any whitespace string\n\
12408is a separator.");
12409
12410static PyObject*
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012411unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012412{
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012413 static char *kwlist[] = {"sep", "maxsplit", 0};
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012414 PyObject *substring = Py_None;
Martin v. Löwis18e16552006-02-15 17:27:45 +000012415 Py_ssize_t maxcount = -1;
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012416
Ezio Melotticda6b6d2012-02-26 09:39:55 +020012417 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit",
12418 kwlist, &substring, &maxcount))
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012419 return NULL;
12420
12421 if (substring == Py_None)
Benjamin Peterson29060642009-01-31 22:14:21 +000012422 return rsplit(self, NULL, maxcount);
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012423 else if (PyUnicode_Check(substring))
Victor Stinner9310abb2011-10-05 00:59:23 +020012424 return rsplit(self, substring, maxcount);
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012425 else
Victor Stinner9310abb2011-10-05 00:59:23 +020012426 return PyUnicode_RSplit(self, substring, maxcount);
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +000012427}
12428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012429PyDoc_STRVAR(splitlines__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012430 "S.splitlines([keepends]) -> list of strings\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012431\n\
12432Return a list of the lines in S, breaking at line boundaries.\n\
Guido van Rossum86662912000-04-11 15:38:46 +000012433Line breaks are not included in the resulting list unless keepends\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012434is given and true.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012435
12436static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012437unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012438{
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010012439 static char *kwlist[] = {"keepends", 0};
Guido van Rossum86662912000-04-11 15:38:46 +000012440 int keepends = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012441
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010012442 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines",
12443 kwlist, &keepends))
Guido van Rossumd57fd912000-03-10 22:53:23 +000012444 return NULL;
12445
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012446 return PyUnicode_Splitlines(self, keepends);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012447}
12448
12449static
Guido van Rossumf15a29f2007-05-04 00:41:39 +000012450PyObject *unicode_str(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012451{
Victor Stinnerc4b49542011-12-11 22:44:26 +010012452 return unicode_result_unchanged(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012453}
12454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012455PyDoc_STRVAR(swapcase__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012456 "S.swapcase() -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012457\n\
12458Return a copy of S with uppercase characters converted to lowercase\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012459and vice versa.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012460
12461static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020012462unicode_swapcase(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012463{
Benjamin Petersoneea48462012-01-16 14:28:50 -050012464 if (PyUnicode_READY(self) == -1)
12465 return NULL;
Victor Stinnerb0800dc2012-02-25 00:47:08 +010012466 return case_operation(self, do_swapcase);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012467}
12468
Georg Brandlceee0772007-11-27 23:48:05 +000012469PyDoc_STRVAR(maketrans__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012470 "str.maketrans(x[, y[, z]]) -> dict (static method)\n\
Georg Brandlceee0772007-11-27 23:48:05 +000012471\n\
12472Return a translation table usable for str.translate().\n\
12473If there is only one argument, it must be a dictionary mapping Unicode\n\
12474ordinals (integers) or characters to Unicode ordinals, strings or None.\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000012475Character keys will be then converted to ordinals.\n\
Georg Brandlceee0772007-11-27 23:48:05 +000012476If there are two arguments, they must be strings of equal length, and\n\
12477in the resulting dictionary, each character in x will be mapped to the\n\
12478character at the same position in y. If there is a third argument, it\n\
12479must be a string, whose characters will be mapped to None in the result.");
12480
12481static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012482unicode_maketrans(PyObject *null, PyObject *args)
Georg Brandlceee0772007-11-27 23:48:05 +000012483{
12484 PyObject *x, *y = NULL, *z = NULL;
12485 PyObject *new = NULL, *key, *value;
12486 Py_ssize_t i = 0;
12487 int res;
Benjamin Peterson14339b62009-01-31 16:36:08 +000012488
Georg Brandlceee0772007-11-27 23:48:05 +000012489 if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z))
12490 return NULL;
12491 new = PyDict_New();
12492 if (!new)
12493 return NULL;
12494 if (y != NULL) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012495 int x_kind, y_kind, z_kind;
12496 void *x_data, *y_data, *z_data;
12497
Georg Brandlceee0772007-11-27 23:48:05 +000012498 /* x must be a string too, of equal length */
Georg Brandlceee0772007-11-27 23:48:05 +000012499 if (!PyUnicode_Check(x)) {
12500 PyErr_SetString(PyExc_TypeError, "first maketrans argument must "
12501 "be a string if there is a second argument");
12502 goto err;
12503 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012504 if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) {
Georg Brandlceee0772007-11-27 23:48:05 +000012505 PyErr_SetString(PyExc_ValueError, "the first two maketrans "
12506 "arguments must have equal length");
12507 goto err;
12508 }
12509 /* create entries for translating chars in x to those in y */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012510 x_kind = PyUnicode_KIND(x);
12511 y_kind = PyUnicode_KIND(y);
12512 x_data = PyUnicode_DATA(x);
12513 y_data = PyUnicode_DATA(y);
12514 for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) {
12515 key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i));
Benjamin Peterson53aa1d72011-12-20 13:29:45 -060012516 if (!key)
Georg Brandlceee0772007-11-27 23:48:05 +000012517 goto err;
Benjamin Peterson822c7902011-12-20 13:32:50 -060012518 value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i));
Benjamin Peterson53aa1d72011-12-20 13:29:45 -060012519 if (!value) {
12520 Py_DECREF(key);
12521 goto err;
12522 }
Georg Brandlceee0772007-11-27 23:48:05 +000012523 res = PyDict_SetItem(new, key, value);
12524 Py_DECREF(key);
12525 Py_DECREF(value);
12526 if (res < 0)
12527 goto err;
12528 }
12529 /* create entries for deleting chars in z */
12530 if (z != NULL) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012531 z_kind = PyUnicode_KIND(z);
12532 z_data = PyUnicode_DATA(z);
Victor Stinnerc4f281e2011-10-11 22:11:42 +020012533 for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012534 key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i));
Georg Brandlceee0772007-11-27 23:48:05 +000012535 if (!key)
12536 goto err;
12537 res = PyDict_SetItem(new, key, Py_None);
12538 Py_DECREF(key);
12539 if (res < 0)
12540 goto err;
12541 }
12542 }
12543 } else {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012544 int kind;
12545 void *data;
12546
Georg Brandlceee0772007-11-27 23:48:05 +000012547 /* x must be a dict */
Raymond Hettinger3ad05762009-05-29 22:11:22 +000012548 if (!PyDict_CheckExact(x)) {
Georg Brandlceee0772007-11-27 23:48:05 +000012549 PyErr_SetString(PyExc_TypeError, "if you give only one argument "
12550 "to maketrans it must be a dict");
12551 goto err;
12552 }
12553 /* copy entries into the new dict, converting string keys to int keys */
12554 while (PyDict_Next(x, &i, &key, &value)) {
12555 if (PyUnicode_Check(key)) {
12556 /* convert string keys to integer keys */
12557 PyObject *newkey;
Victor Stinnerc4f281e2011-10-11 22:11:42 +020012558 if (PyUnicode_GET_LENGTH(key) != 1) {
Georg Brandlceee0772007-11-27 23:48:05 +000012559 PyErr_SetString(PyExc_ValueError, "string keys in translate "
12560 "table must be of length 1");
12561 goto err;
12562 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012563 kind = PyUnicode_KIND(key);
12564 data = PyUnicode_DATA(key);
12565 newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
Georg Brandlceee0772007-11-27 23:48:05 +000012566 if (!newkey)
12567 goto err;
12568 res = PyDict_SetItem(new, newkey, value);
12569 Py_DECREF(newkey);
12570 if (res < 0)
12571 goto err;
Christian Heimes217cfd12007-12-02 14:31:20 +000012572 } else if (PyLong_Check(key)) {
Georg Brandlceee0772007-11-27 23:48:05 +000012573 /* just keep integer keys */
12574 if (PyDict_SetItem(new, key, value) < 0)
12575 goto err;
12576 } else {
12577 PyErr_SetString(PyExc_TypeError, "keys in translate table must "
12578 "be strings or integers");
12579 goto err;
12580 }
12581 }
12582 }
12583 return new;
12584 err:
12585 Py_DECREF(new);
12586 return NULL;
12587}
12588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012589PyDoc_STRVAR(translate__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012590 "S.translate(table) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012591\n\
12592Return a copy of the string S, where all characters have been mapped\n\
12593through the given translation table, which must be a mapping of\n\
Benjamin Peterson142957c2008-07-04 19:55:29 +000012594Unicode ordinals to Unicode ordinals, strings, or None.\n\
Walter Dörwald5c1ee172002-09-04 20:31:32 +000012595Unmapped characters are left untouched. Characters mapped to None\n\
12596are deleted.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012597
12598static PyObject*
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012599unicode_translate(PyObject *self, PyObject *table)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012600{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012601 return _PyUnicode_TranslateCharmap(self, table, "ignore");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012602}
12603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012604PyDoc_STRVAR(upper__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012605 "S.upper() -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012606\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012607Return a copy of S converted to uppercase.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012608
12609static PyObject*
Victor Stinner9310abb2011-10-05 00:59:23 +020012610unicode_upper(PyObject *self)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012611{
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -050012612 if (PyUnicode_READY(self) == -1)
12613 return NULL;
12614 if (PyUnicode_IS_ASCII(self))
12615 return ascii_upper_or_lower(self, 0);
Victor Stinnerb0800dc2012-02-25 00:47:08 +010012616 return case_operation(self, do_upper);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012617}
12618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012619PyDoc_STRVAR(zfill__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012620 "S.zfill(width) -> str\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012621\n\
Benjamin Peterson9aa42992008-09-10 21:57:34 +000012622Pad a numeric string S with zeros on the left, to fill a field\n\
12623of the specified width. The string S is never truncated.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012624
12625static PyObject *
Victor Stinner9310abb2011-10-05 00:59:23 +020012626unicode_zfill(PyObject *self, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012627{
Martin v. Löwis18e16552006-02-15 17:27:45 +000012628 Py_ssize_t fill;
Victor Stinner9310abb2011-10-05 00:59:23 +020012629 PyObject *u;
Martin v. Löwis18e16552006-02-15 17:27:45 +000012630 Py_ssize_t width;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012631 int kind;
12632 void *data;
12633 Py_UCS4 chr;
12634
Martin v. Löwis18e16552006-02-15 17:27:45 +000012635 if (!PyArg_ParseTuple(args, "n:zfill", &width))
Guido van Rossumd57fd912000-03-10 22:53:23 +000012636 return NULL;
12637
Benjamin Petersonbac79492012-01-14 13:34:47 -050012638 if (PyUnicode_READY(self) == -1)
Victor Stinnerc4b49542011-12-11 22:44:26 +010012639 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012640
Victor Stinnerc4b49542011-12-11 22:44:26 +010012641 if (PyUnicode_GET_LENGTH(self) >= width)
12642 return unicode_result_unchanged(self);
12643
12644 fill = width - PyUnicode_GET_LENGTH(self);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012645
12646 u = pad(self, fill, 0, '0');
12647
Walter Dörwald068325e2002-04-15 13:36:47 +000012648 if (u == NULL)
12649 return NULL;
12650
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012651 kind = PyUnicode_KIND(u);
12652 data = PyUnicode_DATA(u);
12653 chr = PyUnicode_READ(kind, data, fill);
12654
12655 if (chr == '+' || chr == '-') {
Guido van Rossumd57fd912000-03-10 22:53:23 +000012656 /* move sign to beginning of string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012657 PyUnicode_WRITE(kind, data, 0, chr);
12658 PyUnicode_WRITE(kind, data, fill, '0');
Guido van Rossumd57fd912000-03-10 22:53:23 +000012659 }
12660
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020012661 assert(_PyUnicode_CheckConsistency(u, 1));
Victor Stinner7931d9a2011-11-04 00:22:48 +010012662 return u;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012663}
Guido van Rossumd57fd912000-03-10 22:53:23 +000012664
12665#if 0
Alexander Belopolsky942af5a2010-12-04 03:38:46 +000012666static PyObject *
12667unicode__decimal2ascii(PyObject *self)
12668{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012669 return PyUnicode_TransformDecimalAndSpaceToASCII(self);
Alexander Belopolsky942af5a2010-12-04 03:38:46 +000012670}
Guido van Rossumd57fd912000-03-10 22:53:23 +000012671#endif
12672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012673PyDoc_STRVAR(startswith__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012674 "S.startswith(prefix[, start[, end]]) -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012675\n\
Guido van Rossuma7132182003-04-09 19:32:45 +000012676Return True if S starts with the specified prefix, False otherwise.\n\
12677With optional start, test S beginning at that position.\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012678With optional end, stop comparing S at that position.\n\
12679prefix can also be a tuple of strings to try.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012680
12681static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012682unicode_startswith(PyObject *self,
Benjamin Peterson29060642009-01-31 22:14:21 +000012683 PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012684{
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012685 PyObject *subobj;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012686 PyObject *substring;
Martin v. Löwis18e16552006-02-15 17:27:45 +000012687 Py_ssize_t start = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012688 Py_ssize_t end = PY_SSIZE_T_MAX;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012689 int result;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012690
Jesus Ceaac451502011-04-20 17:09:23 +020012691 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Benjamin Peterson29060642009-01-31 22:14:21 +000012692 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012693 if (PyTuple_Check(subobj)) {
12694 Py_ssize_t i;
12695 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012696 substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i));
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012697 if (substring == NULL)
12698 return NULL;
12699 result = tailmatch(self, substring, start, end, -1);
12700 Py_DECREF(substring);
12701 if (result) {
12702 Py_RETURN_TRUE;
12703 }
12704 }
12705 /* nothing matched */
12706 Py_RETURN_FALSE;
12707 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012708 substring = PyUnicode_FromObject(subobj);
Ezio Melottiba42fd52011-04-26 06:09:45 +030012709 if (substring == NULL) {
12710 if (PyErr_ExceptionMatches(PyExc_TypeError))
12711 PyErr_Format(PyExc_TypeError, "startswith first arg must be str or "
12712 "a tuple of str, not %s", Py_TYPE(subobj)->tp_name);
Benjamin Peterson29060642009-01-31 22:14:21 +000012713 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +030012714 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012715 result = tailmatch(self, substring, start, end, -1);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012716 Py_DECREF(substring);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012717 return PyBool_FromLong(result);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012718}
12719
12720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012721PyDoc_STRVAR(endswith__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012722 "S.endswith(suffix[, start[, end]]) -> bool\n\
Guido van Rossumd57fd912000-03-10 22:53:23 +000012723\n\
Guido van Rossuma7132182003-04-09 19:32:45 +000012724Return True if S ends with the specified suffix, False otherwise.\n\
12725With optional start, test S beginning at that position.\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012726With optional end, stop comparing S at that position.\n\
12727suffix can also be a tuple of strings to try.");
Guido van Rossumd57fd912000-03-10 22:53:23 +000012728
12729static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012730unicode_endswith(PyObject *self,
Benjamin Peterson29060642009-01-31 22:14:21 +000012731 PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000012732{
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012733 PyObject *subobj;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012734 PyObject *substring;
Martin v. Löwis18e16552006-02-15 17:27:45 +000012735 Py_ssize_t start = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012736 Py_ssize_t end = PY_SSIZE_T_MAX;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012737 int result;
Guido van Rossumd57fd912000-03-10 22:53:23 +000012738
Jesus Ceaac451502011-04-20 17:09:23 +020012739 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Benjamin Peterson29060642009-01-31 22:14:21 +000012740 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012741 if (PyTuple_Check(subobj)) {
12742 Py_ssize_t i;
12743 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012744 substring = PyUnicode_FromObject(
Benjamin Peterson29060642009-01-31 22:14:21 +000012745 PyTuple_GET_ITEM(subobj, i));
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012746 if (substring == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +000012747 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012748 result = tailmatch(self, substring, start, end, +1);
12749 Py_DECREF(substring);
12750 if (result) {
12751 Py_RETURN_TRUE;
12752 }
12753 }
12754 Py_RETURN_FALSE;
12755 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012756 substring = PyUnicode_FromObject(subobj);
Ezio Melottiba42fd52011-04-26 06:09:45 +030012757 if (substring == NULL) {
12758 if (PyErr_ExceptionMatches(PyExc_TypeError))
12759 PyErr_Format(PyExc_TypeError, "endswith first arg must be str or "
12760 "a tuple of str, not %s", Py_TYPE(subobj)->tp_name);
Benjamin Peterson29060642009-01-31 22:14:21 +000012761 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +030012762 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012763 result = tailmatch(self, substring, start, end, +1);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012764 Py_DECREF(substring);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000012765 return PyBool_FromLong(result);
Guido van Rossumd57fd912000-03-10 22:53:23 +000012766}
12767
Victor Stinner202fdca2012-05-07 12:47:02 +020012768Py_LOCAL_INLINE(void)
Victor Stinner3b1a74a2012-05-09 22:25:00 +020012769_PyUnicodeWriter_Update(_PyUnicodeWriter *writer)
Victor Stinner202fdca2012-05-07 12:47:02 +020012770{
Victor Stinnerd3f08822012-05-29 12:57:52 +020012771 writer->size = PyUnicode_GET_LENGTH(writer->buffer);
Victor Stinner202fdca2012-05-07 12:47:02 +020012772 writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer);
12773 writer->data = PyUnicode_DATA(writer->buffer);
12774 writer->kind = PyUnicode_KIND(writer->buffer);
12775}
12776
Victor Stinnerd3f08822012-05-29 12:57:52 +020012777void
12778_PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length)
Victor Stinner202fdca2012-05-07 12:47:02 +020012779{
Victor Stinnerd3f08822012-05-29 12:57:52 +020012780 memset(writer, 0, sizeof(*writer));
12781#ifdef Py_DEBUG
12782 writer->kind = 5; /* invalid kind */
12783#endif
12784 writer->min_length = Py_MAX(min_length, 100);
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012785 writer->overallocate = (min_length > 0);
Victor Stinner202fdca2012-05-07 12:47:02 +020012786}
12787
Victor Stinnerd3f08822012-05-29 12:57:52 +020012788int
12789_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
12790 Py_ssize_t length, Py_UCS4 maxchar)
Victor Stinner202fdca2012-05-07 12:47:02 +020012791{
12792 Py_ssize_t newlen;
12793 PyObject *newbuffer;
12794
Victor Stinnerd3f08822012-05-29 12:57:52 +020012795 assert(length > 0);
12796
Victor Stinner202fdca2012-05-07 12:47:02 +020012797 if (length > PY_SSIZE_T_MAX - writer->pos) {
12798 PyErr_NoMemory();
12799 return -1;
12800 }
12801 newlen = writer->pos + length;
12802
Victor Stinnerd3f08822012-05-29 12:57:52 +020012803 if (writer->buffer == NULL) {
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012804 if (writer->overallocate) {
Victor Stinnerd3f08822012-05-29 12:57:52 +020012805 /* overallocate 25% to limit the number of resize */
12806 if (newlen <= (PY_SSIZE_T_MAX - newlen / 4))
12807 newlen += newlen / 4;
12808 if (newlen < writer->min_length)
12809 newlen = writer->min_length;
12810 }
12811 writer->buffer = PyUnicode_New(newlen, maxchar);
12812 if (writer->buffer == NULL)
12813 return -1;
12814 _PyUnicodeWriter_Update(writer);
12815 return 0;
12816 }
Victor Stinner202fdca2012-05-07 12:47:02 +020012817
Victor Stinnerd3f08822012-05-29 12:57:52 +020012818 if (newlen > writer->size) {
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012819 if (writer->overallocate) {
Victor Stinnerd3f08822012-05-29 12:57:52 +020012820 /* overallocate 25% to limit the number of resize */
12821 if (newlen <= (PY_SSIZE_T_MAX - newlen / 4))
12822 newlen += newlen / 4;
12823 if (newlen < writer->min_length)
12824 newlen = writer->min_length;
12825 }
12826
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012827 if (maxchar > writer->maxchar || writer->readonly) {
Victor Stinner202fdca2012-05-07 12:47:02 +020012828 /* resize + widen */
12829 newbuffer = PyUnicode_New(newlen, maxchar);
12830 if (newbuffer == NULL)
12831 return -1;
Victor Stinnerd3f08822012-05-29 12:57:52 +020012832 _PyUnicode_FastCopyCharacters(newbuffer, 0,
12833 writer->buffer, 0, writer->pos);
Victor Stinner202fdca2012-05-07 12:47:02 +020012834 Py_DECREF(writer->buffer);
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012835 writer->readonly = 0;
Victor Stinner202fdca2012-05-07 12:47:02 +020012836 }
12837 else {
12838 newbuffer = resize_compact(writer->buffer, newlen);
12839 if (newbuffer == NULL)
12840 return -1;
12841 }
12842 writer->buffer = newbuffer;
Victor Stinner3b1a74a2012-05-09 22:25:00 +020012843 _PyUnicodeWriter_Update(writer);
Victor Stinner202fdca2012-05-07 12:47:02 +020012844 }
12845 else if (maxchar > writer->maxchar) {
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012846 assert(!writer->readonly);
Victor Stinnerd3f08822012-05-29 12:57:52 +020012847 newbuffer = PyUnicode_New(writer->size, maxchar);
12848 if (newbuffer == NULL)
Victor Stinner202fdca2012-05-07 12:47:02 +020012849 return -1;
Victor Stinnerd3f08822012-05-29 12:57:52 +020012850 _PyUnicode_FastCopyCharacters(newbuffer, 0,
12851 writer->buffer, 0, writer->pos);
12852 Py_DECREF(writer->buffer);
12853 writer->buffer = newbuffer;
Victor Stinner3b1a74a2012-05-09 22:25:00 +020012854 _PyUnicodeWriter_Update(writer);
Victor Stinner202fdca2012-05-07 12:47:02 +020012855 }
12856 return 0;
12857}
12858
Victor Stinnerd3f08822012-05-29 12:57:52 +020012859int
12860_PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str)
12861{
12862 Py_UCS4 maxchar;
12863 Py_ssize_t len;
12864
12865 if (PyUnicode_READY(str) == -1)
12866 return -1;
12867 len = PyUnicode_GET_LENGTH(str);
12868 if (len == 0)
12869 return 0;
12870 maxchar = PyUnicode_MAX_CHAR_VALUE(str);
12871 if (maxchar > writer->maxchar || len > writer->size - writer->pos) {
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012872 if (writer->buffer == NULL && !writer->overallocate) {
Victor Stinnerd3f08822012-05-29 12:57:52 +020012873 Py_INCREF(str);
12874 writer->buffer = str;
12875 _PyUnicodeWriter_Update(writer);
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012876 writer->readonly = 1;
Victor Stinnerd3f08822012-05-29 12:57:52 +020012877 writer->size = 0;
12878 writer->pos += len;
12879 return 0;
12880 }
12881 if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1)
12882 return -1;
12883 }
12884 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
12885 str, 0, len);
12886 writer->pos += len;
12887 return 0;
12888}
12889
12890PyObject *
Victor Stinner3b1a74a2012-05-09 22:25:00 +020012891_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer)
Victor Stinner202fdca2012-05-07 12:47:02 +020012892{
Victor Stinnerd3f08822012-05-29 12:57:52 +020012893 if (writer->pos == 0) {
12894 Py_XDECREF(writer->buffer);
12895 Py_INCREF(unicode_empty);
12896 return unicode_empty;
12897 }
Victor Stinnerd7b7c742012-06-04 22:52:12 +020012898 if (writer->readonly) {
Victor Stinnerd3f08822012-05-29 12:57:52 +020012899 assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos);
12900 return writer->buffer;
12901 }
12902 if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) {
12903 PyObject *newbuffer;
12904 newbuffer = resize_compact(writer->buffer, writer->pos);
12905 if (newbuffer == NULL) {
12906 Py_DECREF(writer->buffer);
12907 return NULL;
12908 }
12909 writer->buffer = newbuffer;
Victor Stinner202fdca2012-05-07 12:47:02 +020012910 }
Victor Stinnerf59c28c2012-05-09 03:24:14 +020012911 assert(_PyUnicode_CheckConsistency(writer->buffer, 1));
Victor Stinner202fdca2012-05-07 12:47:02 +020012912 return writer->buffer;
12913}
12914
Victor Stinnerd3f08822012-05-29 12:57:52 +020012915void
Victor Stinner3b1a74a2012-05-09 22:25:00 +020012916_PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer)
Victor Stinner202fdca2012-05-07 12:47:02 +020012917{
12918 Py_CLEAR(writer->buffer);
12919}
12920
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012921#include "stringlib/unicode_format.h"
Eric Smith8c663262007-08-25 02:26:07 +000012922
12923PyDoc_STRVAR(format__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012924 "S.format(*args, **kwargs) -> str\n\
Eric Smith8c663262007-08-25 02:26:07 +000012925\n\
Eric Smith51d2fd92010-11-06 19:27:37 +000012926Return a formatted version of S, using substitutions from args and kwargs.\n\
12927The substitutions are identified by braces ('{' and '}').");
Eric Smith8c663262007-08-25 02:26:07 +000012928
Eric Smith27bbca62010-11-04 17:06:58 +000012929PyDoc_STRVAR(format_map__doc__,
12930 "S.format_map(mapping) -> str\n\
12931\n\
Eric Smith51d2fd92010-11-06 19:27:37 +000012932Return a formatted version of S, using substitutions from mapping.\n\
12933The substitutions are identified by braces ('{' and '}').");
Eric Smith27bbca62010-11-04 17:06:58 +000012934
Eric Smith4a7d76d2008-05-30 18:10:19 +000012935static PyObject *
12936unicode__format__(PyObject* self, PyObject* args)
12937{
Victor Stinnerd3f08822012-05-29 12:57:52 +020012938 PyObject *format_spec;
12939 _PyUnicodeWriter writer;
12940 int ret;
Eric Smith4a7d76d2008-05-30 18:10:19 +000012941
12942 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
12943 return NULL;
12944
Victor Stinnerd3f08822012-05-29 12:57:52 +020012945 if (PyUnicode_READY(self) == -1)
12946 return NULL;
12947 _PyUnicodeWriter_Init(&writer, 0);
12948 ret = _PyUnicode_FormatAdvancedWriter(&writer,
12949 self, format_spec, 0,
12950 PyUnicode_GET_LENGTH(format_spec));
12951 if (ret == -1) {
12952 _PyUnicodeWriter_Dealloc(&writer);
12953 return NULL;
12954 }
12955 return _PyUnicodeWriter_Finish(&writer);
Eric Smith4a7d76d2008-05-30 18:10:19 +000012956}
12957
Eric Smith8c663262007-08-25 02:26:07 +000012958PyDoc_STRVAR(p_format__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012959 "S.__format__(format_spec) -> str\n\
Eric Smith8c663262007-08-25 02:26:07 +000012960\n\
Eric Smith51d2fd92010-11-06 19:27:37 +000012961Return a formatted version of S as described by format_spec.");
Eric Smith8c663262007-08-25 02:26:07 +000012962
12963static PyObject *
Victor Stinner9db1a8b2011-10-23 20:04:37 +020012964unicode__sizeof__(PyObject *v)
Georg Brandlc28e1fa2008-06-10 19:20:26 +000012965{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012966 Py_ssize_t size;
12967
12968 /* If it's a compact object, account for base structure +
12969 character data. */
12970 if (PyUnicode_IS_COMPACT_ASCII(v))
12971 size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1;
12972 else if (PyUnicode_IS_COMPACT(v))
12973 size = sizeof(PyCompactUnicodeObject) +
Martin v. Löwisc47adb02011-10-07 20:55:35 +020012974 (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012975 else {
12976 /* If it is a two-block object, account for base object, and
12977 for character block if present. */
12978 size = sizeof(PyUnicodeObject);
Victor Stinnerc3c74152011-10-02 20:39:55 +020012979 if (_PyUnicode_DATA_ANY(v))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012980 size += (PyUnicode_GET_LENGTH(v) + 1) *
Martin v. Löwisc47adb02011-10-07 20:55:35 +020012981 PyUnicode_KIND(v);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012982 }
12983 /* If the wstr pointer is present, account for it unless it is shared
Victor Stinnera3be6132011-10-03 02:16:37 +020012984 with the data pointer. Check if the data is not shared. */
Victor Stinner03490912011-10-03 23:45:12 +020012985 if (_PyUnicode_HAS_WSTR_MEMORY(v))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012986 size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t);
Victor Stinner829c0ad2011-10-03 01:08:02 +020012987 if (_PyUnicode_HAS_UTF8_MEMORY(v))
Victor Stinnere90fe6a2011-10-01 16:48:13 +020012988 size += PyUnicode_UTF8_LENGTH(v) + 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020012989
12990 return PyLong_FromSsize_t(size);
Georg Brandlc28e1fa2008-06-10 19:20:26 +000012991}
12992
12993PyDoc_STRVAR(sizeof__doc__,
Benjamin Peterson29060642009-01-31 22:14:21 +000012994 "S.__sizeof__() -> size of S in memory, in bytes");
Georg Brandlc28e1fa2008-06-10 19:20:26 +000012995
12996static PyObject *
Victor Stinner034f6cf2011-09-30 02:26:44 +020012997unicode_getnewargs(PyObject *v)
Guido van Rossum5d9113d2003-01-29 17:58:45 +000012998{
Victor Stinnerbf6e5602011-12-12 01:53:47 +010012999 PyObject *copy = _PyUnicode_Copy(v);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013000 if (!copy)
13001 return NULL;
13002 return Py_BuildValue("(N)", copy);
Guido van Rossum5d9113d2003-01-29 17:58:45 +000013003}
13004
Guido van Rossumd57fd912000-03-10 22:53:23 +000013005static PyMethodDef unicode_methods[] = {
Benjamin Peterson28a4dce2010-12-12 01:33:04 +000013006 {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013007 {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__},
Ezio Melotticda6b6d2012-02-26 09:39:55 +020013008 {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__},
13009 {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013010 {"join", (PyCFunction) unicode_join, METH_O, join__doc__},
13011 {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__},
Benjamin Petersond5890c82012-01-14 13:23:30 -050013012 {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013013 {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__},
13014 {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__},
13015 {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__},
13016 {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__},
13017 {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +000013018 {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013019 {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__},
13020 {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__},
13021 {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__},
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000013022 {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013023 {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__},
13024 {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__},
13025 {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__},
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000013026 {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +000013027 {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__},
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010013028 {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__},
Walter Dörwaldde02bcb2002-04-22 17:42:37 +000013029 {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013030 {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__},
13031 {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__},
13032 {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__},
13033 {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__},
13034 {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__},
13035 {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__},
13036 {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__},
13037 {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__},
13038 {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__},
13039 {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__},
13040 {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__},
13041 {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__},
13042 {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
13043 {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
Martin v. Löwis47383402007-08-15 07:32:56 +000013044 {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__},
Georg Brandl559e5d72008-06-11 18:37:52 +000013045 {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000013046 {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
Eric Smith9cd1e092007-08-31 18:39:38 +000013047 {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__},
Eric Smith27bbca62010-11-04 17:06:58 +000013048 {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__},
Eric Smith4a7d76d2008-05-30 18:10:19 +000013049 {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__},
Georg Brandlceee0772007-11-27 23:48:05 +000013050 {"maketrans", (PyCFunction) unicode_maketrans,
13051 METH_VARARGS | METH_STATIC, maketrans__doc__},
Georg Brandlc28e1fa2008-06-10 19:20:26 +000013052 {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__},
Walter Dörwald068325e2002-04-15 13:36:47 +000013053#if 0
Alexander Belopolsky942af5a2010-12-04 03:38:46 +000013054 /* These methods are just used for debugging the implementation. */
Alexander Belopolsky942af5a2010-12-04 03:38:46 +000013055 {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS},
Guido van Rossumd57fd912000-03-10 22:53:23 +000013056#endif
13057
Benjamin Peterson14339b62009-01-31 16:36:08 +000013058 {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS},
Guido van Rossumd57fd912000-03-10 22:53:23 +000013059 {NULL, NULL}
13060};
13061
Neil Schemenauerce30bc92002-11-18 16:10:18 +000013062static PyObject *
13063unicode_mod(PyObject *v, PyObject *w)
13064{
Brian Curtindfc80e32011-08-10 20:28:54 -050013065 if (!PyUnicode_Check(v))
13066 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson29060642009-01-31 22:14:21 +000013067 return PyUnicode_Format(v, w);
Neil Schemenauerce30bc92002-11-18 16:10:18 +000013068}
13069
13070static PyNumberMethods unicode_as_number = {
Benjamin Peterson14339b62009-01-31 16:36:08 +000013071 0, /*nb_add*/
13072 0, /*nb_subtract*/
13073 0, /*nb_multiply*/
13074 unicode_mod, /*nb_remainder*/
Neil Schemenauerce30bc92002-11-18 16:10:18 +000013075};
13076
Guido van Rossumd57fd912000-03-10 22:53:23 +000013077static PySequenceMethods unicode_as_sequence = {
Benjamin Peterson14339b62009-01-31 16:36:08 +000013078 (lenfunc) unicode_length, /* sq_length */
13079 PyUnicode_Concat, /* sq_concat */
13080 (ssizeargfunc) unicode_repeat, /* sq_repeat */
13081 (ssizeargfunc) unicode_getitem, /* sq_item */
13082 0, /* sq_slice */
13083 0, /* sq_ass_item */
13084 0, /* sq_ass_slice */
13085 PyUnicode_Contains, /* sq_contains */
Guido van Rossumd57fd912000-03-10 22:53:23 +000013086};
13087
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013088static PyObject*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013089unicode_subscript(PyObject* self, PyObject* item)
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013090{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013091 if (PyUnicode_READY(self) == -1)
13092 return NULL;
13093
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000013094 if (PyIndex_Check(item)) {
13095 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013096 if (i == -1 && PyErr_Occurred())
13097 return NULL;
13098 if (i < 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013099 i += PyUnicode_GET_LENGTH(self);
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013100 return unicode_getitem(self, i);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013101 } else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000013102 Py_ssize_t start, stop, step, slicelength, cur, i;
Antoine Pitrou7aec4012011-10-04 19:08:01 +020013103 PyObject *result;
13104 void *src_data, *dest_data;
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013105 int src_kind, dest_kind;
Victor Stinnerc80d6d22011-10-05 14:13:28 +020013106 Py_UCS4 ch, max_char, kind_limit;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013107
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013108 if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
Benjamin Peterson29060642009-01-31 22:14:21 +000013109 &start, &stop, &step, &slicelength) < 0) {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013110 return NULL;
13111 }
13112
13113 if (slicelength <= 0) {
Victor Stinner382955f2011-12-11 21:44:00 +010013114 Py_INCREF(unicode_empty);
13115 return unicode_empty;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013116 } else if (start == 0 && step == 1 &&
Victor Stinnerc4b49542011-12-11 22:44:26 +010013117 slicelength == PyUnicode_GET_LENGTH(self)) {
13118 return unicode_result_unchanged(self);
Thomas Woutersed03b412007-08-28 21:37:11 +000013119 } else if (step == 1) {
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013120 return PyUnicode_Substring(self,
Victor Stinner12bab6d2011-10-01 01:53:49 +020013121 start, start + slicelength);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013122 }
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013123 /* General case */
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013124 src_kind = PyUnicode_KIND(self);
13125 src_data = PyUnicode_DATA(self);
Victor Stinner55c99112011-10-13 01:17:06 +020013126 if (!PyUnicode_IS_ASCII(self)) {
13127 kind_limit = kind_maxchar_limit(src_kind);
13128 max_char = 0;
13129 for (cur = start, i = 0; i < slicelength; cur += step, i++) {
13130 ch = PyUnicode_READ(src_kind, src_data, cur);
13131 if (ch > max_char) {
13132 max_char = ch;
13133 if (max_char >= kind_limit)
13134 break;
13135 }
Victor Stinnerc80d6d22011-10-05 14:13:28 +020013136 }
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013137 }
Victor Stinner55c99112011-10-13 01:17:06 +020013138 else
13139 max_char = 127;
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013140 result = PyUnicode_New(slicelength, max_char);
Antoine Pitrou7aec4012011-10-04 19:08:01 +020013141 if (result == NULL)
13142 return NULL;
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013143 dest_kind = PyUnicode_KIND(result);
Antoine Pitrou7aec4012011-10-04 19:08:01 +020013144 dest_data = PyUnicode_DATA(result);
13145
13146 for (cur = start, i = 0; i < slicelength; cur += step, i++) {
Antoine Pitrou875f29b2011-10-04 20:00:49 +020013147 Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur);
13148 PyUnicode_WRITE(dest_kind, dest_data, i, ch);
Antoine Pitrou7aec4012011-10-04 19:08:01 +020013149 }
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020013150 assert(_PyUnicode_CheckConsistency(result, 1));
Antoine Pitrou7aec4012011-10-04 19:08:01 +020013151 return result;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013152 } else {
13153 PyErr_SetString(PyExc_TypeError, "string indices must be integers");
13154 return NULL;
13155 }
13156}
13157
13158static PyMappingMethods unicode_as_mapping = {
Benjamin Peterson14339b62009-01-31 16:36:08 +000013159 (lenfunc)unicode_length, /* mp_length */
13160 (binaryfunc)unicode_subscript, /* mp_subscript */
13161 (objobjargproc)0, /* mp_ass_subscript */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +000013162};
13163
Guido van Rossumd57fd912000-03-10 22:53:23 +000013164
Guido van Rossumd57fd912000-03-10 22:53:23 +000013165/* Helpers for PyUnicode_Format() */
13166
13167static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000013168getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
Guido van Rossumd57fd912000-03-10 22:53:23 +000013169{
Martin v. Löwis18e16552006-02-15 17:27:45 +000013170 Py_ssize_t argidx = *p_argidx;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013171 if (argidx < arglen) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013172 (*p_argidx)++;
13173 if (arglen < 0)
13174 return args;
13175 else
13176 return PyTuple_GetItem(args, argidx);
Guido van Rossumd57fd912000-03-10 22:53:23 +000013177 }
13178 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson29060642009-01-31 22:14:21 +000013179 "not enough arguments for format string");
Guido van Rossumd57fd912000-03-10 22:53:23 +000013180 return NULL;
13181}
13182
Mark Dickinsonf489caf2009-05-01 11:42:00 +000013183/* Returns a new reference to a PyUnicode object, or NULL on failure. */
Guido van Rossumd57fd912000-03-10 22:53:23 +000013184
Victor Stinnerd3f08822012-05-29 12:57:52 +020013185static int
13186formatfloat(PyObject *v, int flags, int prec, int type,
13187 PyObject **p_output, _PyUnicodeWriter *writer)
Guido van Rossumd57fd912000-03-10 22:53:23 +000013188{
Mark Dickinsonf489caf2009-05-01 11:42:00 +000013189 char *p;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013190 double x;
Victor Stinnerd3f08822012-05-29 12:57:52 +020013191 Py_ssize_t len;
Tim Petersced69f82003-09-16 20:30:58 +000013192
Guido van Rossumd57fd912000-03-10 22:53:23 +000013193 x = PyFloat_AsDouble(v);
13194 if (x == -1.0 && PyErr_Occurred())
Victor Stinnerd3f08822012-05-29 12:57:52 +020013195 return -1;
Mark Dickinsonf489caf2009-05-01 11:42:00 +000013196
Guido van Rossumd57fd912000-03-10 22:53:23 +000013197 if (prec < 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000013198 prec = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +000013199
Eric Smith0923d1d2009-04-16 20:16:10 +000013200 p = PyOS_double_to_string(x, type, prec,
13201 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
Mark Dickinsonf489caf2009-05-01 11:42:00 +000013202 if (p == NULL)
Victor Stinnerd3f08822012-05-29 12:57:52 +020013203 return -1;
13204 len = strlen(p);
13205 if (writer) {
13206 if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1)
13207 return -1;
Victor Stinner184252a2012-06-16 02:57:41 +020013208 unicode_write_cstr(writer->buffer, writer->pos, p, len);
Victor Stinnerd3f08822012-05-29 12:57:52 +020013209 writer->pos += len;
13210 }
13211 else
13212 *p_output = _PyUnicode_FromASCII(p, len);
Eric Smith0923d1d2009-04-16 20:16:10 +000013213 PyMem_Free(p);
Victor Stinnerd3f08822012-05-29 12:57:52 +020013214 return 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013215}
13216
Victor Stinnerd0880d52012-04-27 23:40:13 +020013217/* formatlong() emulates the format codes d, u, o, x and X, and
13218 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
13219 * Python's regular ints.
13220 * Return value: a new PyUnicodeObject*, or NULL if error.
13221 * The output string is of the form
13222 * "-"? ("0x" | "0X")? digit+
13223 * "0x"/"0X" are present only for x and X conversions, with F_ALT
13224 * set in flags. The case of hex digits will be correct,
13225 * There will be at least prec digits, zero-filled on the left if
13226 * necessary to get that many.
13227 * val object to be converted
13228 * flags bitmask of format flags; only F_ALT is looked at
13229 * prec minimum number of digits; 0-fill on left if needed
13230 * type a character in [duoxX]; u acts the same as d
13231 *
13232 * CAUTION: o, x and X conversions on regular ints can never
13233 * produce a '-' sign, but can for Python's unbounded ints.
13234 */
Tim Peters38fd5b62000-09-21 05:43:11 +000013235static PyObject*
13236formatlong(PyObject *val, int flags, int prec, int type)
13237{
Victor Stinnerd0880d52012-04-27 23:40:13 +020013238 PyObject *result = NULL;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013239 char *buf;
Victor Stinnerd0880d52012-04-27 23:40:13 +020013240 Py_ssize_t i;
13241 int sign; /* 1 if '-', else 0 */
13242 int len; /* number of characters */
13243 Py_ssize_t llen;
13244 int numdigits; /* len == numnondigits + numdigits */
13245 int numnondigits = 0;
Tim Peters38fd5b62000-09-21 05:43:11 +000013246
Victor Stinnerd0880d52012-04-27 23:40:13 +020013247 /* Avoid exceeding SSIZE_T_MAX */
13248 if (prec > INT_MAX-3) {
13249 PyErr_SetString(PyExc_OverflowError,
13250 "precision too large");
Benjamin Peterson14339b62009-01-31 16:36:08 +000013251 return NULL;
Victor Stinnerd0880d52012-04-27 23:40:13 +020013252 }
13253
13254 assert(PyLong_Check(val));
13255
13256 switch (type) {
13257 case 'd':
13258 case 'u':
13259 /* Special-case boolean: we want 0/1 */
Victor Stinnerb11d91d2012-04-28 00:25:34 +020013260 if (PyBool_Check(val))
13261 result = PyNumber_ToBase(val, 10);
13262 else
13263 result = Py_TYPE(val)->tp_str(val);
Victor Stinnerd0880d52012-04-27 23:40:13 +020013264 break;
13265 case 'o':
13266 numnondigits = 2;
13267 result = PyNumber_ToBase(val, 8);
13268 break;
13269 case 'x':
13270 case 'X':
13271 numnondigits = 2;
13272 result = PyNumber_ToBase(val, 16);
13273 break;
13274 default:
13275 assert(!"'type' not in [duoxX]");
13276 }
13277 if (!result)
13278 return NULL;
13279
13280 assert(unicode_modifiable(result));
13281 assert(PyUnicode_IS_READY(result));
13282 assert(PyUnicode_IS_ASCII(result));
13283
13284 /* To modify the string in-place, there can only be one reference. */
13285 if (Py_REFCNT(result) != 1) {
13286 PyErr_BadInternalCall();
13287 return NULL;
13288 }
13289 buf = PyUnicode_DATA(result);
13290 llen = PyUnicode_GET_LENGTH(result);
13291 if (llen > INT_MAX) {
13292 PyErr_SetString(PyExc_ValueError,
13293 "string too large in _PyBytes_FormatLong");
13294 return NULL;
13295 }
13296 len = (int)llen;
13297 sign = buf[0] == '-';
13298 numnondigits += sign;
13299 numdigits = len - numnondigits;
13300 assert(numdigits > 0);
13301
13302 /* Get rid of base marker unless F_ALT */
13303 if (((flags & F_ALT) == 0 &&
13304 (type == 'o' || type == 'x' || type == 'X'))) {
13305 assert(buf[sign] == '0');
13306 assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' ||
13307 buf[sign+1] == 'o');
13308 numnondigits -= 2;
13309 buf += 2;
13310 len -= 2;
13311 if (sign)
13312 buf[0] = '-';
13313 assert(len == numnondigits + numdigits);
13314 assert(numdigits > 0);
13315 }
13316
13317 /* Fill with leading zeroes to meet minimum width. */
13318 if (prec > numdigits) {
13319 PyObject *r1 = PyBytes_FromStringAndSize(NULL,
13320 numnondigits + prec);
13321 char *b1;
13322 if (!r1) {
13323 Py_DECREF(result);
13324 return NULL;
13325 }
13326 b1 = PyBytes_AS_STRING(r1);
13327 for (i = 0; i < numnondigits; ++i)
13328 *b1++ = *buf++;
13329 for (i = 0; i < prec - numdigits; i++)
13330 *b1++ = '0';
13331 for (i = 0; i < numdigits; i++)
13332 *b1++ = *buf++;
13333 *b1 = '\0';
13334 Py_DECREF(result);
13335 result = r1;
13336 buf = PyBytes_AS_STRING(result);
13337 len = numnondigits + prec;
13338 }
13339
13340 /* Fix up case for hex conversions. */
13341 if (type == 'X') {
13342 /* Need to convert all lower case letters to upper case.
13343 and need to convert 0x to 0X (and -0x to -0X). */
13344 for (i = 0; i < len; i++)
13345 if (buf[i] >= 'a' && buf[i] <= 'x')
13346 buf[i] -= 'a'-'A';
13347 }
13348 if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) {
13349 PyObject *unicode;
Victor Stinnerd3f08822012-05-29 12:57:52 +020013350 unicode = _PyUnicode_FromASCII(buf, len);
Victor Stinnerd0880d52012-04-27 23:40:13 +020013351 Py_DECREF(result);
13352 result = unicode;
13353 }
Benjamin Peterson14339b62009-01-31 16:36:08 +000013354 return result;
Tim Peters38fd5b62000-09-21 05:43:11 +000013355}
13356
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013357static Py_UCS4
13358formatchar(PyObject *v)
Guido van Rossumd57fd912000-03-10 22:53:23 +000013359{
Amaury Forgeot d'Arca4db6862008-07-04 21:26:43 +000013360 /* presume that the buffer is at least 3 characters long */
Marc-André Lemburgd4ab4a52000-06-08 17:54:00 +000013361 if (PyUnicode_Check(v)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013362 if (PyUnicode_GET_LENGTH(v) == 1) {
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013363 return PyUnicode_READ_CHAR(v, 0);
Benjamin Peterson29060642009-01-31 22:14:21 +000013364 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013365 goto onError;
13366 }
13367 else {
13368 /* Integer input truncated to a character */
13369 long x;
13370 x = PyLong_AsLong(v);
13371 if (x == -1 && PyErr_Occurred())
13372 goto onError;
13373
Victor Stinner8faf8212011-12-08 22:14:11 +010013374 if (x < 0 || x > MAX_UNICODE) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013375 PyErr_SetString(PyExc_OverflowError,
13376 "%c arg not in range(0x110000)");
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013377 return (Py_UCS4) -1;
Benjamin Peterson29060642009-01-31 22:14:21 +000013378 }
13379
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013380 return (Py_UCS4) x;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013381 }
Amaury Forgeot d'Arca4db6862008-07-04 21:26:43 +000013382
Benjamin Peterson29060642009-01-31 22:14:21 +000013383 onError:
Marc-André Lemburgd4ab4a52000-06-08 17:54:00 +000013384 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson29060642009-01-31 22:14:21 +000013385 "%c requires int or char");
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013386 return (Py_UCS4) -1;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013387}
13388
Alexander Belopolsky40018472011-02-26 01:02:56 +000013389PyObject *
13390PyUnicode_Format(PyObject *format, PyObject *args)
Guido van Rossumd57fd912000-03-10 22:53:23 +000013391{
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013392 Py_ssize_t fmtcnt, fmtpos, arglen, argidx;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013393 int args_owned = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013394 PyObject *dict = NULL;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013395 PyObject *temp = NULL;
13396 PyObject *second = NULL;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013397 PyObject *uformat;
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013398 void *fmt;
13399 enum PyUnicode_Kind kind, fmtkind;
Victor Stinner3b1a74a2012-05-09 22:25:00 +020013400 _PyUnicodeWriter writer;
Victor Stinneree4544c2012-05-09 22:24:08 +020013401 Py_ssize_t sublen;
13402 Py_UCS4 maxchar;
Tim Petersced69f82003-09-16 20:30:58 +000013403
Guido van Rossumd57fd912000-03-10 22:53:23 +000013404 if (format == NULL || args == NULL) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013405 PyErr_BadInternalCall();
13406 return NULL;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013407 }
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013408 uformat = PyUnicode_FromObject(format);
Benjamin Peterson22a29702012-01-02 09:00:30 -060013409 if (uformat == NULL)
Benjamin Peterson29060642009-01-31 22:14:21 +000013410 return NULL;
Benjamin Peterson22a29702012-01-02 09:00:30 -060013411 if (PyUnicode_READY(uformat) == -1)
13412 Py_DECREF(uformat);
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013413
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013414 fmt = PyUnicode_DATA(uformat);
13415 fmtkind = PyUnicode_KIND(uformat);
13416 fmtcnt = PyUnicode_GET_LENGTH(uformat);
13417 fmtpos = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013418
Victor Stinnerd3f08822012-05-29 12:57:52 +020013419 _PyUnicodeWriter_Init(&writer, fmtcnt + 100);
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013420
Guido van Rossumd57fd912000-03-10 22:53:23 +000013421 if (PyTuple_Check(args)) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013422 arglen = PyTuple_Size(args);
13423 argidx = 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013424 }
13425 else {
Benjamin Peterson29060642009-01-31 22:14:21 +000013426 arglen = -1;
13427 argidx = -2;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013428 }
Christian Heimes90aa7642007-12-19 02:45:37 +000013429 if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) &&
Christian Heimesf3863112007-11-22 07:46:41 +000013430 !PyUnicode_Check(args))
Benjamin Peterson29060642009-01-31 22:14:21 +000013431 dict = args;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013432
13433 while (--fmtcnt >= 0) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013434 if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') {
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013435 Py_ssize_t nonfmtpos;
13436 nonfmtpos = fmtpos++;
13437 while (fmtcnt >= 0 &&
13438 PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') {
13439 fmtpos++;
13440 fmtcnt--;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013441 }
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013442 if (fmtcnt < 0)
13443 fmtpos--;
Victor Stinneree4544c2012-05-09 22:24:08 +020013444 sublen = fmtpos - nonfmtpos;
13445 maxchar = _PyUnicode_FindMaxChar(uformat,
13446 nonfmtpos, nonfmtpos + sublen);
Victor Stinner3b1a74a2012-05-09 22:25:00 +020013447 if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1)
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013448 goto onError;
Victor Stinneree4544c2012-05-09 22:24:08 +020013449
Victor Stinnerd3f08822012-05-29 12:57:52 +020013450 _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos,
13451 uformat, nonfmtpos, sublen);
Victor Stinneree4544c2012-05-09 22:24:08 +020013452 writer.pos += sublen;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013453 }
13454 else {
Benjamin Peterson29060642009-01-31 22:14:21 +000013455 /* Got a format specifier */
13456 int flags = 0;
13457 Py_ssize_t width = -1;
13458 int prec = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013459 Py_UCS4 c = '\0';
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013460 Py_UCS4 fill;
13461 int sign;
13462 Py_UCS4 signchar;
Benjamin Peterson29060642009-01-31 22:14:21 +000013463 int isnumok;
13464 PyObject *v = NULL;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013465 void *pbuf = NULL;
13466 Py_ssize_t pindex, len;
Victor Stinneree4544c2012-05-09 22:24:08 +020013467 Py_UCS4 bufmaxchar;
13468 Py_ssize_t buflen;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013469
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013470 fmtpos++;
Victor Stinner438106b2012-05-02 00:41:57 +020013471 c = PyUnicode_READ(fmtkind, fmt, fmtpos);
13472 if (c == '(') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013473 Py_ssize_t keystart;
Benjamin Peterson29060642009-01-31 22:14:21 +000013474 Py_ssize_t keylen;
13475 PyObject *key;
13476 int pcount = 1;
Christian Heimesa612dc02008-02-24 13:08:18 +000013477
Benjamin Peterson29060642009-01-31 22:14:21 +000013478 if (dict == NULL) {
13479 PyErr_SetString(PyExc_TypeError,
13480 "format requires a mapping");
13481 goto onError;
13482 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013483 ++fmtpos;
Benjamin Peterson29060642009-01-31 22:14:21 +000013484 --fmtcnt;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013485 keystart = fmtpos;
Benjamin Peterson29060642009-01-31 22:14:21 +000013486 /* Skip over balanced parentheses */
13487 while (pcount > 0 && --fmtcnt >= 0) {
Victor Stinnerbff7c962012-05-03 01:44:59 +020013488 c = PyUnicode_READ(fmtkind, fmt, fmtpos);
13489 if (c == ')')
Benjamin Peterson29060642009-01-31 22:14:21 +000013490 --pcount;
Victor Stinnerbff7c962012-05-03 01:44:59 +020013491 else if (c == '(')
Benjamin Peterson29060642009-01-31 22:14:21 +000013492 ++pcount;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013493 fmtpos++;
Benjamin Peterson29060642009-01-31 22:14:21 +000013494 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013495 keylen = fmtpos - keystart - 1;
Benjamin Peterson29060642009-01-31 22:14:21 +000013496 if (fmtcnt < 0 || pcount > 0) {
13497 PyErr_SetString(PyExc_ValueError,
13498 "incomplete format key");
13499 goto onError;
13500 }
Victor Stinner7931d9a2011-11-04 00:22:48 +010013501 key = PyUnicode_Substring(uformat,
Victor Stinner12bab6d2011-10-01 01:53:49 +020013502 keystart, keystart + keylen);
Benjamin Peterson29060642009-01-31 22:14:21 +000013503 if (key == NULL)
13504 goto onError;
13505 if (args_owned) {
13506 Py_DECREF(args);
13507 args_owned = 0;
13508 }
13509 args = PyObject_GetItem(dict, key);
13510 Py_DECREF(key);
13511 if (args == NULL) {
13512 goto onError;
13513 }
13514 args_owned = 1;
13515 arglen = -1;
13516 argidx = -2;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013517 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013518 while (--fmtcnt >= 0) {
Victor Stinner438106b2012-05-02 00:41:57 +020013519 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
13520 switch (c) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013521 case '-': flags |= F_LJUST; continue;
13522 case '+': flags |= F_SIGN; continue;
13523 case ' ': flags |= F_BLANK; continue;
13524 case '#': flags |= F_ALT; continue;
13525 case '0': flags |= F_ZERO; continue;
13526 }
13527 break;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013528 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013529 if (c == '*') {
13530 v = getnextarg(args, arglen, &argidx);
13531 if (v == NULL)
13532 goto onError;
13533 if (!PyLong_Check(v)) {
13534 PyErr_SetString(PyExc_TypeError,
13535 "* wants int");
13536 goto onError;
13537 }
13538 width = PyLong_AsLong(v);
13539 if (width == -1 && PyErr_Occurred())
13540 goto onError;
13541 if (width < 0) {
13542 flags |= F_LJUST;
13543 width = -width;
13544 }
13545 if (--fmtcnt >= 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013546 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
Benjamin Peterson29060642009-01-31 22:14:21 +000013547 }
13548 else if (c >= '0' && c <= '9') {
13549 width = c - '0';
13550 while (--fmtcnt >= 0) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013551 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
Benjamin Peterson29060642009-01-31 22:14:21 +000013552 if (c < '0' || c > '9')
13553 break;
Martin v. Löwisb05c0732012-05-15 13:45:49 +020013554 /* Since c is unsigned, the RHS would end up as unsigned,
13555 mixing signed and unsigned comparison. Since c is between
13556 '0' and '9', casting to int is safe. */
13557 if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013558 PyErr_SetString(PyExc_ValueError,
13559 "width too big");
Benjamin Peterson14339b62009-01-31 16:36:08 +000013560 goto onError;
Benjamin Peterson29060642009-01-31 22:14:21 +000013561 }
13562 width = width*10 + (c - '0');
13563 }
13564 }
13565 if (c == '.') {
13566 prec = 0;
13567 if (--fmtcnt >= 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013568 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
Benjamin Peterson29060642009-01-31 22:14:21 +000013569 if (c == '*') {
13570 v = getnextarg(args, arglen, &argidx);
13571 if (v == NULL)
13572 goto onError;
13573 if (!PyLong_Check(v)) {
13574 PyErr_SetString(PyExc_TypeError,
13575 "* wants int");
13576 goto onError;
13577 }
13578 prec = PyLong_AsLong(v);
13579 if (prec == -1 && PyErr_Occurred())
13580 goto onError;
13581 if (prec < 0)
13582 prec = 0;
13583 if (--fmtcnt >= 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013584 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
Benjamin Peterson29060642009-01-31 22:14:21 +000013585 }
13586 else if (c >= '0' && c <= '9') {
13587 prec = c - '0';
13588 while (--fmtcnt >= 0) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013589 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
Benjamin Peterson29060642009-01-31 22:14:21 +000013590 if (c < '0' || c > '9')
13591 break;
Martin v. Löwisb05c0732012-05-15 13:45:49 +020013592 if (prec > (INT_MAX - ((int)c - '0')) / 10) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013593 PyErr_SetString(PyExc_ValueError,
13594 "prec too big");
13595 goto onError;
13596 }
13597 prec = prec*10 + (c - '0');
13598 }
13599 }
13600 } /* prec */
13601 if (fmtcnt >= 0) {
13602 if (c == 'h' || c == 'l' || c == 'L') {
13603 if (--fmtcnt >= 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013604 c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
Benjamin Peterson29060642009-01-31 22:14:21 +000013605 }
13606 }
13607 if (fmtcnt < 0) {
13608 PyErr_SetString(PyExc_ValueError,
13609 "incomplete format");
13610 goto onError;
13611 }
Victor Stinnerd3f08822012-05-29 12:57:52 +020013612 if (fmtcnt == 0)
Victor Stinnerd7b7c742012-06-04 22:52:12 +020013613 writer.overallocate = 0;
Victor Stinneraff3cc62012-04-30 05:19:21 +020013614
13615 if (c == '%') {
Victor Stinner3b1a74a2012-05-09 22:25:00 +020013616 if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1)
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013617 goto onError;
Victor Stinneree4544c2012-05-09 22:24:08 +020013618 PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '%');
13619 writer.pos += 1;
Victor Stinneraff3cc62012-04-30 05:19:21 +020013620 continue;
Benjamin Peterson29060642009-01-31 22:14:21 +000013621 }
Victor Stinneraff3cc62012-04-30 05:19:21 +020013622
Victor Stinneraff3cc62012-04-30 05:19:21 +020013623 v = getnextarg(args, arglen, &argidx);
13624 if (v == NULL)
13625 goto onError;
13626
Benjamin Peterson29060642009-01-31 22:14:21 +000013627 sign = 0;
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013628 signchar = '\0';
Benjamin Peterson29060642009-01-31 22:14:21 +000013629 fill = ' ';
13630 switch (c) {
13631
Benjamin Peterson29060642009-01-31 22:14:21 +000013632 case 's':
13633 case 'r':
13634 case 'a':
Victor Stinnerd3f08822012-05-29 12:57:52 +020013635 if (PyLong_CheckExact(v) && width == -1 && prec == -1) {
13636 /* Fast path */
13637 if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1)
13638 goto onError;
13639 goto nextarg;
13640 }
13641
Victor Stinner808fc0a2010-03-22 12:50:40 +000013642 if (PyUnicode_CheckExact(v) && c == 's') {
Benjamin Peterson29060642009-01-31 22:14:21 +000013643 temp = v;
13644 Py_INCREF(temp);
Benjamin Peterson14339b62009-01-31 16:36:08 +000013645 }
13646 else {
Benjamin Peterson29060642009-01-31 22:14:21 +000013647 if (c == 's')
13648 temp = PyObject_Str(v);
13649 else if (c == 'r')
13650 temp = PyObject_Repr(v);
13651 else
13652 temp = PyObject_ASCII(v);
Benjamin Peterson29060642009-01-31 22:14:21 +000013653 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013654 break;
13655
13656 case 'i':
13657 case 'd':
13658 case 'u':
13659 case 'o':
13660 case 'x':
13661 case 'X':
Victor Stinnerd3f08822012-05-29 12:57:52 +020013662 if (PyLong_CheckExact(v)
13663 && width == -1 && prec == -1
13664 && !(flags & (F_SIGN | F_BLANK)))
13665 {
13666 /* Fast path */
13667 switch(c)
13668 {
13669 case 'd':
13670 case 'i':
13671 case 'u':
13672 if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1)
13673 goto onError;
13674 goto nextarg;
13675 case 'x':
13676 if (_PyLong_FormatWriter(&writer, v, 16, flags & F_ALT) == -1)
13677 goto onError;
13678 goto nextarg;
13679 case 'o':
13680 if (_PyLong_FormatWriter(&writer, v, 8, flags & F_ALT) == -1)
13681 goto onError;
13682 goto nextarg;
13683 default:
13684 break;
13685 }
13686 }
13687
Benjamin Peterson29060642009-01-31 22:14:21 +000013688 isnumok = 0;
13689 if (PyNumber_Check(v)) {
13690 PyObject *iobj=NULL;
13691
13692 if (PyLong_Check(v)) {
13693 iobj = v;
13694 Py_INCREF(iobj);
13695 }
13696 else {
13697 iobj = PyNumber_Long(v);
13698 }
13699 if (iobj!=NULL) {
13700 if (PyLong_Check(iobj)) {
13701 isnumok = 1;
Victor Stinneraff3cc62012-04-30 05:19:21 +020013702 sign = 1;
Senthil Kumaran9ebe08d2011-07-03 21:03:16 -070013703 temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c));
Benjamin Peterson29060642009-01-31 22:14:21 +000013704 Py_DECREF(iobj);
Benjamin Peterson29060642009-01-31 22:14:21 +000013705 }
13706 else {
13707 Py_DECREF(iobj);
13708 }
13709 }
13710 }
13711 if (!isnumok) {
13712 PyErr_Format(PyExc_TypeError,
13713 "%%%c format: a number is required, "
13714 "not %.200s", (char)c, Py_TYPE(v)->tp_name);
13715 goto onError;
13716 }
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013717 if (flags & F_ZERO)
Benjamin Peterson29060642009-01-31 22:14:21 +000013718 fill = '0';
13719 break;
13720
13721 case 'e':
13722 case 'E':
13723 case 'f':
13724 case 'F':
13725 case 'g':
13726 case 'G':
Victor Stinnerd3f08822012-05-29 12:57:52 +020013727 if (width == -1 && prec == -1
13728 && !(flags & (F_SIGN | F_BLANK)))
13729 {
13730 /* Fast path */
13731 if (formatfloat(v, flags, prec, c, NULL, &writer) == -1)
13732 goto onError;
13733 goto nextarg;
13734 }
13735
Benjamin Peterson29060642009-01-31 22:14:21 +000013736 sign = 1;
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013737 if (flags & F_ZERO)
Benjamin Peterson29060642009-01-31 22:14:21 +000013738 fill = '0';
Victor Stinnerd3f08822012-05-29 12:57:52 +020013739 if (formatfloat(v, flags, prec, c, &temp, NULL) == -1)
13740 temp = NULL;
Benjamin Peterson29060642009-01-31 22:14:21 +000013741 break;
13742
13743 case 'c':
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013744 {
13745 Py_UCS4 ch = formatchar(v);
13746 if (ch == (Py_UCS4) -1)
Benjamin Peterson29060642009-01-31 22:14:21 +000013747 goto onError;
Victor Stinnerd3f08822012-05-29 12:57:52 +020013748 if (width == -1 && prec == -1) {
13749 /* Fast path */
13750 if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
13751 goto onError;
13752 PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
13753 writer.pos += 1;
13754 goto nextarg;
13755 }
Victor Stinnerb5c3ea32012-05-02 00:29:36 +020013756 temp = PyUnicode_FromOrdinal(ch);
Benjamin Peterson29060642009-01-31 22:14:21 +000013757 break;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013758 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013759
13760 default:
13761 PyErr_Format(PyExc_ValueError,
13762 "unsupported format character '%c' (0x%x) "
13763 "at index %zd",
13764 (31<=c && c<=126) ? (char)c : '?',
13765 (int)c,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013766 fmtpos - 1);
Benjamin Peterson29060642009-01-31 22:14:21 +000013767 goto onError;
13768 }
Victor Stinneraff3cc62012-04-30 05:19:21 +020013769 if (temp == NULL)
13770 goto onError;
13771 assert (PyUnicode_Check(temp));
Victor Stinnerd3f08822012-05-29 12:57:52 +020013772
13773 if (width == -1 && prec == -1
13774 && !(flags & (F_SIGN | F_BLANK)))
13775 {
13776 /* Fast path */
13777 if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1)
13778 goto onError;
13779 goto nextarg;
13780 }
13781
Victor Stinneraff3cc62012-04-30 05:19:21 +020013782 if (PyUnicode_READY(temp) == -1) {
13783 Py_CLEAR(temp);
13784 goto onError;
13785 }
13786 kind = PyUnicode_KIND(temp);
13787 pbuf = PyUnicode_DATA(temp);
13788 len = PyUnicode_GET_LENGTH(temp);
13789
13790 if (c == 's' || c == 'r' || c == 'a') {
13791 if (prec >= 0 && len > prec)
13792 len = prec;
13793 }
13794
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013795 /* pbuf is initialized here. */
13796 pindex = 0;
Benjamin Peterson29060642009-01-31 22:14:21 +000013797 if (sign) {
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013798 Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex);
13799 if (ch == '-' || ch == '+') {
13800 signchar = ch;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013801 len--;
13802 pindex++;
Benjamin Peterson29060642009-01-31 22:14:21 +000013803 }
13804 else if (flags & F_SIGN)
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013805 signchar = '+';
Benjamin Peterson29060642009-01-31 22:14:21 +000013806 else if (flags & F_BLANK)
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013807 signchar = ' ';
Benjamin Peterson29060642009-01-31 22:14:21 +000013808 else
13809 sign = 0;
13810 }
13811 if (width < len)
13812 width = len;
Victor Stinneree4544c2012-05-09 22:24:08 +020013813
13814 /* Compute the length and maximum character of the
13815 written characters */
13816 bufmaxchar = 127;
13817 if (!(flags & F_LJUST)) {
13818 if (sign) {
13819 if ((width-1) > len)
Victor Stinnerd3f08822012-05-29 12:57:52 +020013820 bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill);
Victor Stinneree4544c2012-05-09 22:24:08 +020013821 }
13822 else {
13823 if (width > len)
Victor Stinnerd3f08822012-05-29 12:57:52 +020013824 bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill);
Victor Stinneree4544c2012-05-09 22:24:08 +020013825 }
13826 }
13827 maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len);
Victor Stinnerd3f08822012-05-29 12:57:52 +020013828 bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar);
Victor Stinneree4544c2012-05-09 22:24:08 +020013829
13830 buflen = width;
13831 if (sign && len == width)
13832 buflen++;
13833
Victor Stinner3b1a74a2012-05-09 22:25:00 +020013834 if (_PyUnicodeWriter_Prepare(&writer, buflen, bufmaxchar) == -1)
Victor Stinneree4544c2012-05-09 22:24:08 +020013835 goto onError;
13836
13837 /* Write characters */
Benjamin Peterson29060642009-01-31 22:14:21 +000013838 if (sign) {
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013839 if (fill != ' ') {
Victor Stinneree4544c2012-05-09 22:24:08 +020013840 PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar);
13841 writer.pos += 1;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013842 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013843 if (width > len)
13844 width--;
13845 }
13846 if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013847 assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013848 assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c);
Benjamin Peterson29060642009-01-31 22:14:21 +000013849 if (fill != ' ') {
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013850 PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0');
13851 PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c);
13852 writer.pos += 2;
13853 pindex += 2;
Benjamin Peterson29060642009-01-31 22:14:21 +000013854 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013855 width -= 2;
13856 if (width < 0)
13857 width = 0;
13858 len -= 2;
13859 }
13860 if (width > len && !(flags & F_LJUST)) {
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013861 sublen = width - len;
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013862 FILL(writer.kind, writer.data, fill, writer.pos, sublen);
13863 writer.pos += sublen;
Antoine Pitrou978b9d22011-10-07 12:35:48 +020013864 width = len;
Benjamin Peterson29060642009-01-31 22:14:21 +000013865 }
13866 if (fill == ' ') {
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013867 if (sign) {
Victor Stinneree4544c2012-05-09 22:24:08 +020013868 PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar);
13869 writer.pos += 1;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013870 }
Benjamin Peterson29060642009-01-31 22:14:21 +000013871 if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020013872 assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
13873 assert(PyUnicode_READ(kind, pbuf, pindex+1) == c);
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013874 PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0');
13875 PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c);
13876 writer.pos += 2;
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013877 pindex += 2;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013878 }
13879 }
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013880
Victor Stinnerc9d369f2012-06-16 02:22:37 +020013881 if (len) {
13882 _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos,
13883 temp, pindex, len);
13884 writer.pos += len;
13885 }
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013886 if (width > len) {
Victor Stinneree4544c2012-05-09 22:24:08 +020013887 sublen = width - len;
Victor Stinnerf2c76aa2012-05-03 13:10:40 +020013888 FILL(writer.kind, writer.data, ' ', writer.pos, sublen);
13889 writer.pos += sublen;
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013890 }
Victor Stinneree4544c2012-05-09 22:24:08 +020013891
Victor Stinnerd3f08822012-05-29 12:57:52 +020013892nextarg:
Benjamin Peterson29060642009-01-31 22:14:21 +000013893 if (dict && (argidx < arglen) && c != '%') {
13894 PyErr_SetString(PyExc_TypeError,
13895 "not all arguments converted during string formatting");
Benjamin Peterson29060642009-01-31 22:14:21 +000013896 goto onError;
13897 }
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013898 Py_CLEAR(temp);
Benjamin Peterson29060642009-01-31 22:14:21 +000013899 } /* '%' */
Guido van Rossumd57fd912000-03-10 22:53:23 +000013900 } /* until end */
13901 if (argidx < arglen && !dict) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013902 PyErr_SetString(PyExc_TypeError,
13903 "not all arguments converted during string formatting");
13904 goto onError;
Guido van Rossumd57fd912000-03-10 22:53:23 +000013905 }
13906
13907 if (args_owned) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013908 Py_DECREF(args);
Guido van Rossumd57fd912000-03-10 22:53:23 +000013909 }
13910 Py_DECREF(uformat);
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013911 Py_XDECREF(temp);
13912 Py_XDECREF(second);
Victor Stinner3b1a74a2012-05-09 22:25:00 +020013913 return _PyUnicodeWriter_Finish(&writer);
Guido van Rossumd57fd912000-03-10 22:53:23 +000013914
Benjamin Peterson29060642009-01-31 22:14:21 +000013915 onError:
Guido van Rossumd57fd912000-03-10 22:53:23 +000013916 Py_DECREF(uformat);
Antoine Pitrou5c0ba362011-10-07 01:54:09 +020013917 Py_XDECREF(temp);
13918 Py_XDECREF(second);
Victor Stinner3b1a74a2012-05-09 22:25:00 +020013919 _PyUnicodeWriter_Dealloc(&writer);
Guido van Rossumd57fd912000-03-10 22:53:23 +000013920 if (args_owned) {
Benjamin Peterson29060642009-01-31 22:14:21 +000013921 Py_DECREF(args);
Guido van Rossumd57fd912000-03-10 22:53:23 +000013922 }
13923 return NULL;
13924}
13925
Jeremy Hylton938ace62002-07-17 16:30:39 +000013926static PyObject *
Guido van Rossume023fe02001-08-30 03:12:59 +000013927unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
13928
Tim Peters6d6c1a32001-08-02 04:15:00 +000013929static PyObject *
13930unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13931{
Benjamin Peterson29060642009-01-31 22:14:21 +000013932 PyObject *x = NULL;
Benjamin Peterson14339b62009-01-31 16:36:08 +000013933 static char *kwlist[] = {"object", "encoding", "errors", 0};
13934 char *encoding = NULL;
13935 char *errors = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000013936
Benjamin Peterson14339b62009-01-31 16:36:08 +000013937 if (type != &PyUnicode_Type)
13938 return unicode_subtype_new(type, args, kwds);
13939 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str",
Benjamin Peterson29060642009-01-31 22:14:21 +000013940 kwlist, &x, &encoding, &errors))
Benjamin Peterson14339b62009-01-31 16:36:08 +000013941 return NULL;
Victor Stinner382955f2011-12-11 21:44:00 +010013942 if (x == NULL) {
13943 Py_INCREF(unicode_empty);
13944 return unicode_empty;
13945 }
Benjamin Peterson14339b62009-01-31 16:36:08 +000013946 if (encoding == NULL && errors == NULL)
13947 return PyObject_Str(x);
13948 else
Benjamin Peterson29060642009-01-31 22:14:21 +000013949 return PyUnicode_FromEncodedObject(x, encoding, errors);
Tim Peters6d6c1a32001-08-02 04:15:00 +000013950}
13951
Guido van Rossume023fe02001-08-30 03:12:59 +000013952static PyObject *
13953unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13954{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013955 PyObject *unicode, *self;
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013956 Py_ssize_t length, char_size;
13957 int share_wstr, share_utf8;
13958 unsigned int kind;
13959 void *data;
Guido van Rossume023fe02001-08-30 03:12:59 +000013960
Benjamin Peterson14339b62009-01-31 16:36:08 +000013961 assert(PyType_IsSubtype(type, &PyUnicode_Type));
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013962
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013963 unicode = unicode_new(&PyUnicode_Type, args, kwds);
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013964 if (unicode == NULL)
Benjamin Peterson14339b62009-01-31 16:36:08 +000013965 return NULL;
Victor Stinner910337b2011-10-03 03:20:16 +020013966 assert(_PyUnicode_CHECK(unicode));
Benjamin Petersonbac79492012-01-14 13:34:47 -050013967 if (PyUnicode_READY(unicode) == -1) {
Benjamin Peterson22a29702012-01-02 09:00:30 -060013968 Py_DECREF(unicode);
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013969 return NULL;
Benjamin Peterson22a29702012-01-02 09:00:30 -060013970 }
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013971
Victor Stinner9db1a8b2011-10-23 20:04:37 +020013972 self = type->tp_alloc(type, 0);
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013973 if (self == NULL) {
13974 Py_DECREF(unicode);
Benjamin Peterson14339b62009-01-31 16:36:08 +000013975 return NULL;
13976 }
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013977 kind = PyUnicode_KIND(unicode);
13978 length = PyUnicode_GET_LENGTH(unicode);
13979
13980 _PyUnicode_LENGTH(self) = length;
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +020013981#ifdef Py_DEBUG
13982 _PyUnicode_HASH(self) = -1;
13983#else
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013984 _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode);
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +020013985#endif
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013986 _PyUnicode_STATE(self).interned = 0;
13987 _PyUnicode_STATE(self).kind = kind;
13988 _PyUnicode_STATE(self).compact = 0;
Victor Stinner3cf46372011-10-03 14:42:15 +020013989 _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii;
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013990 _PyUnicode_STATE(self).ready = 1;
13991 _PyUnicode_WSTR(self) = NULL;
13992 _PyUnicode_UTF8_LENGTH(self) = 0;
13993 _PyUnicode_UTF8(self) = NULL;
13994 _PyUnicode_WSTR_LENGTH(self) = 0;
Victor Stinnerc3c74152011-10-02 20:39:55 +020013995 _PyUnicode_DATA_ANY(self) = NULL;
Victor Stinner07ac3eb2011-10-01 16:16:43 +020013996
13997 share_utf8 = 0;
13998 share_wstr = 0;
13999 if (kind == PyUnicode_1BYTE_KIND) {
14000 char_size = 1;
14001 if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
14002 share_utf8 = 1;
14003 }
14004 else if (kind == PyUnicode_2BYTE_KIND) {
14005 char_size = 2;
14006 if (sizeof(wchar_t) == 2)
14007 share_wstr = 1;
14008 }
14009 else {
14010 assert(kind == PyUnicode_4BYTE_KIND);
14011 char_size = 4;
14012 if (sizeof(wchar_t) == 4)
14013 share_wstr = 1;
14014 }
14015
14016 /* Ensure we won't overflow the length. */
14017 if (length > (PY_SSIZE_T_MAX / char_size - 1)) {
14018 PyErr_NoMemory();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014019 goto onError;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014020 }
Victor Stinner07ac3eb2011-10-01 16:16:43 +020014021 data = PyObject_MALLOC((length + 1) * char_size);
14022 if (data == NULL) {
14023 PyErr_NoMemory();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014024 goto onError;
14025 }
14026
Victor Stinnerc3c74152011-10-02 20:39:55 +020014027 _PyUnicode_DATA_ANY(self) = data;
Victor Stinner07ac3eb2011-10-01 16:16:43 +020014028 if (share_utf8) {
14029 _PyUnicode_UTF8_LENGTH(self) = length;
14030 _PyUnicode_UTF8(self) = data;
14031 }
14032 if (share_wstr) {
14033 _PyUnicode_WSTR_LENGTH(self) = length;
14034 _PyUnicode_WSTR(self) = (wchar_t *)data;
14035 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014036
Victor Stinner07ac3eb2011-10-01 16:16:43 +020014037 Py_MEMCPY(data, PyUnicode_DATA(unicode),
Martin v. Löwisc47adb02011-10-07 20:55:35 +020014038 kind * (length + 1));
Victor Stinnerbb10a1f2011-10-05 01:34:17 +020014039 assert(_PyUnicode_CheckConsistency(self, 1));
Victor Stinnerfb9ea8c2011-10-06 01:45:57 +020014040#ifdef Py_DEBUG
14041 _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode);
14042#endif
Victor Stinnerdd18d3a2011-10-22 11:08:10 +020014043 Py_DECREF(unicode);
Victor Stinner7931d9a2011-11-04 00:22:48 +010014044 return self;
Victor Stinner07ac3eb2011-10-01 16:16:43 +020014045
14046onError:
14047 Py_DECREF(unicode);
14048 Py_DECREF(self);
14049 return NULL;
Guido van Rossume023fe02001-08-30 03:12:59 +000014050}
14051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000014052PyDoc_STRVAR(unicode_doc,
Benjamin Peterson29060642009-01-31 22:14:21 +000014053 "str(string[, encoding[, errors]]) -> str\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +000014054\n\
Collin Winterd474ce82007-08-07 19:42:11 +000014055Create a new string object from the given encoded string.\n\
Skip Montanaro35b37a52002-07-26 16:22:46 +000014056encoding defaults to the current default string encoding.\n\
14057errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.");
Tim Peters6d6c1a32001-08-02 04:15:00 +000014058
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014059static PyObject *unicode_iter(PyObject *seq);
14060
Guido van Rossumd57fd912000-03-10 22:53:23 +000014061PyTypeObject PyUnicode_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000014062 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Benjamin Peterson14339b62009-01-31 16:36:08 +000014063 "str", /* tp_name */
14064 sizeof(PyUnicodeObject), /* tp_size */
14065 0, /* tp_itemsize */
Guido van Rossumd57fd912000-03-10 22:53:23 +000014066 /* Slots */
Benjamin Peterson14339b62009-01-31 16:36:08 +000014067 (destructor)unicode_dealloc, /* tp_dealloc */
14068 0, /* tp_print */
14069 0, /* tp_getattr */
14070 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +000014071 0, /* tp_reserved */
Benjamin Peterson14339b62009-01-31 16:36:08 +000014072 unicode_repr, /* tp_repr */
14073 &unicode_as_number, /* tp_as_number */
14074 &unicode_as_sequence, /* tp_as_sequence */
14075 &unicode_as_mapping, /* tp_as_mapping */
14076 (hashfunc) unicode_hash, /* tp_hash*/
14077 0, /* tp_call*/
14078 (reprfunc) unicode_str, /* tp_str */
14079 PyObject_GenericGetAttr, /* tp_getattro */
14080 0, /* tp_setattro */
14081 0, /* tp_as_buffer */
14082 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Benjamin Peterson29060642009-01-31 22:14:21 +000014083 Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */
Benjamin Peterson14339b62009-01-31 16:36:08 +000014084 unicode_doc, /* tp_doc */
14085 0, /* tp_traverse */
14086 0, /* tp_clear */
14087 PyUnicode_RichCompare, /* tp_richcompare */
14088 0, /* tp_weaklistoffset */
14089 unicode_iter, /* tp_iter */
14090 0, /* tp_iternext */
14091 unicode_methods, /* tp_methods */
14092 0, /* tp_members */
14093 0, /* tp_getset */
14094 &PyBaseObject_Type, /* tp_base */
14095 0, /* tp_dict */
14096 0, /* tp_descr_get */
14097 0, /* tp_descr_set */
14098 0, /* tp_dictoffset */
14099 0, /* tp_init */
14100 0, /* tp_alloc */
14101 unicode_new, /* tp_new */
14102 PyObject_Del, /* tp_free */
Guido van Rossumd57fd912000-03-10 22:53:23 +000014103};
14104
14105/* Initialize the Unicode implementation */
14106
Victor Stinner3a50e702011-10-18 21:21:00 +020014107int _PyUnicode_Init(void)
Guido van Rossumd57fd912000-03-10 22:53:23 +000014108{
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +000014109 int i;
14110
Thomas Wouters477c8d52006-05-27 19:21:47 +000014111 /* XXX - move this array to unicodectype.c ? */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014112 Py_UCS2 linebreak[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +000014113 0x000A, /* LINE FEED */
14114 0x000D, /* CARRIAGE RETURN */
14115 0x001C, /* FILE SEPARATOR */
14116 0x001D, /* GROUP SEPARATOR */
14117 0x001E, /* RECORD SEPARATOR */
14118 0x0085, /* NEXT LINE */
14119 0x2028, /* LINE SEPARATOR */
14120 0x2029, /* PARAGRAPH SEPARATOR */
14121 };
14122
Fred Drakee4315f52000-05-09 19:53:39 +000014123 /* Init the implementation */
Victor Stinnera464fc12011-10-02 20:39:30 +020014124 unicode_empty = PyUnicode_New(0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000014125 if (!unicode_empty)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014126 Py_FatalError("Can't create empty string");
Victor Stinnerd3df8ab2011-11-22 01:22:34 +010014127 assert(_PyUnicode_CheckConsistency(unicode_empty, 1));
Thomas Wouters0e3f5912006-08-11 14:57:12 +000014128
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +000014129 for (i = 0; i < 256; i++)
Benjamin Peterson29060642009-01-31 22:14:21 +000014130 unicode_latin1[i] = NULL;
Guido van Rossumcacfc072002-05-24 19:01:59 +000014131 if (PyType_Ready(&PyUnicode_Type) < 0)
Benjamin Peterson29060642009-01-31 22:14:21 +000014132 Py_FatalError("Can't initialize 'unicode'");
Thomas Wouters477c8d52006-05-27 19:21:47 +000014133
14134 /* initialize the linebreak bloom filter */
14135 bloom_linebreak = make_bloom_mask(
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014136 PyUnicode_2BYTE_KIND, linebreak,
Victor Stinner63941882011-09-29 00:42:28 +020014137 Py_ARRAY_LENGTH(linebreak));
Thomas Wouters0e3f5912006-08-11 14:57:12 +000014138
14139 PyType_Ready(&EncodingMapType);
Victor Stinner3a50e702011-10-18 21:21:00 +020014140
14141#ifdef HAVE_MBCS
14142 winver.dwOSVersionInfoSize = sizeof(winver);
14143 if (!GetVersionEx((OSVERSIONINFO*)&winver)) {
14144 PyErr_SetFromWindowsErr(0);
14145 return -1;
14146 }
14147#endif
14148 return 0;
Guido van Rossumd57fd912000-03-10 22:53:23 +000014149}
14150
14151/* Finalize the Unicode implementation */
14152
Christian Heimesa156e092008-02-16 07:38:31 +000014153int
14154PyUnicode_ClearFreeList(void)
14155{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014156 return 0;
Christian Heimesa156e092008-02-16 07:38:31 +000014157}
14158
Guido van Rossumd57fd912000-03-10 22:53:23 +000014159void
Thomas Wouters78890102000-07-22 19:25:51 +000014160_PyUnicode_Fini(void)
Guido van Rossumd57fd912000-03-10 22:53:23 +000014161{
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +000014162 int i;
Guido van Rossumd57fd912000-03-10 22:53:23 +000014163
Guido van Rossum4ae8ef82000-10-03 18:09:04 +000014164 Py_XDECREF(unicode_empty);
14165 unicode_empty = NULL;
Barry Warsaw5b4c2282000-10-03 20:45:26 +000014166
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +000014167 for (i = 0; i < 256; i++) {
Benjamin Peterson29060642009-01-31 22:14:21 +000014168 if (unicode_latin1[i]) {
14169 Py_DECREF(unicode_latin1[i]);
14170 unicode_latin1[i] = NULL;
14171 }
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +000014172 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020014173 _PyUnicode_ClearStaticStrings();
Christian Heimesa156e092008-02-16 07:38:31 +000014174 (void)PyUnicode_ClearFreeList();
Guido van Rossumd57fd912000-03-10 22:53:23 +000014175}
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +000014176
Walter Dörwald16807132007-05-25 13:52:07 +000014177void
14178PyUnicode_InternInPlace(PyObject **p)
14179{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014180 register PyObject *s = *p;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014181 PyObject *t;
Victor Stinner4fae54c2011-10-03 02:01:52 +020014182#ifdef Py_DEBUG
14183 assert(s != NULL);
14184 assert(_PyUnicode_CHECK(s));
14185#else
Benjamin Peterson14339b62009-01-31 16:36:08 +000014186 if (s == NULL || !PyUnicode_Check(s))
Victor Stinner4fae54c2011-10-03 02:01:52 +020014187 return;
14188#endif
Benjamin Peterson14339b62009-01-31 16:36:08 +000014189 /* If it's a subclass, we don't really know what putting
14190 it in the interned dict might do. */
14191 if (!PyUnicode_CheckExact(s))
14192 return;
14193 if (PyUnicode_CHECK_INTERNED(s))
14194 return;
14195 if (interned == NULL) {
14196 interned = PyDict_New();
14197 if (interned == NULL) {
14198 PyErr_Clear(); /* Don't leave an exception */
14199 return;
14200 }
14201 }
14202 /* It might be that the GetItem call fails even
14203 though the key is present in the dictionary,
14204 namely when this happens during a stack overflow. */
14205 Py_ALLOW_RECURSION
Victor Stinner7931d9a2011-11-04 00:22:48 +010014206 t = PyDict_GetItem(interned, s);
Benjamin Peterson14339b62009-01-31 16:36:08 +000014207 Py_END_ALLOW_RECURSION
Martin v. Löwis5b222132007-06-10 09:51:05 +000014208
Benjamin Peterson29060642009-01-31 22:14:21 +000014209 if (t) {
14210 Py_INCREF(t);
14211 Py_DECREF(*p);
14212 *p = t;
14213 return;
14214 }
Walter Dörwald16807132007-05-25 13:52:07 +000014215
Benjamin Peterson14339b62009-01-31 16:36:08 +000014216 PyThreadState_GET()->recursion_critical = 1;
Victor Stinner7931d9a2011-11-04 00:22:48 +010014217 if (PyDict_SetItem(interned, s, s) < 0) {
Benjamin Peterson14339b62009-01-31 16:36:08 +000014218 PyErr_Clear();
14219 PyThreadState_GET()->recursion_critical = 0;
14220 return;
14221 }
14222 PyThreadState_GET()->recursion_critical = 0;
14223 /* The two references in interned are not counted by refcnt.
14224 The deallocator will take care of this */
14225 Py_REFCNT(s) -= 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014226 _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL;
Walter Dörwald16807132007-05-25 13:52:07 +000014227}
14228
14229void
14230PyUnicode_InternImmortal(PyObject **p)
14231{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014232 PyUnicode_InternInPlace(p);
14233 if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
Victor Stinneraf9e4b82011-10-23 20:07:00 +020014234 _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014235 Py_INCREF(*p);
14236 }
Walter Dörwald16807132007-05-25 13:52:07 +000014237}
14238
14239PyObject *
14240PyUnicode_InternFromString(const char *cp)
14241{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014242 PyObject *s = PyUnicode_FromString(cp);
14243 if (s == NULL)
14244 return NULL;
14245 PyUnicode_InternInPlace(&s);
14246 return s;
Walter Dörwald16807132007-05-25 13:52:07 +000014247}
14248
Alexander Belopolsky40018472011-02-26 01:02:56 +000014249void
14250_Py_ReleaseInternedUnicodeStrings(void)
Walter Dörwald16807132007-05-25 13:52:07 +000014251{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014252 PyObject *keys;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014253 PyObject *s;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014254 Py_ssize_t i, n;
14255 Py_ssize_t immortal_size = 0, mortal_size = 0;
Walter Dörwald16807132007-05-25 13:52:07 +000014256
Benjamin Peterson14339b62009-01-31 16:36:08 +000014257 if (interned == NULL || !PyDict_Check(interned))
14258 return;
14259 keys = PyDict_Keys(interned);
14260 if (keys == NULL || !PyList_Check(keys)) {
14261 PyErr_Clear();
14262 return;
14263 }
Walter Dörwald16807132007-05-25 13:52:07 +000014264
Benjamin Peterson14339b62009-01-31 16:36:08 +000014265 /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak
14266 detector, interned unicode strings are not forcibly deallocated;
14267 rather, we give them their stolen references back, and then clear
14268 and DECREF the interned dict. */
Walter Dörwald16807132007-05-25 13:52:07 +000014269
Benjamin Peterson14339b62009-01-31 16:36:08 +000014270 n = PyList_GET_SIZE(keys);
14271 fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n",
Benjamin Peterson29060642009-01-31 22:14:21 +000014272 n);
Benjamin Peterson14339b62009-01-31 16:36:08 +000014273 for (i = 0; i < n; i++) {
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014274 s = PyList_GET_ITEM(keys, i);
Victor Stinner6b56a7f2011-10-04 20:04:52 +020014275 if (PyUnicode_READY(s) == -1) {
14276 assert(0 && "could not ready string");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014277 fprintf(stderr, "could not ready string\n");
Victor Stinner6b56a7f2011-10-04 20:04:52 +020014278 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014279 switch (PyUnicode_CHECK_INTERNED(s)) {
Benjamin Peterson14339b62009-01-31 16:36:08 +000014280 case SSTATE_NOT_INTERNED:
14281 /* XXX Shouldn't happen */
14282 break;
14283 case SSTATE_INTERNED_IMMORTAL:
14284 Py_REFCNT(s) += 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014285 immortal_size += PyUnicode_GET_LENGTH(s);
Benjamin Peterson14339b62009-01-31 16:36:08 +000014286 break;
14287 case SSTATE_INTERNED_MORTAL:
14288 Py_REFCNT(s) += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014289 mortal_size += PyUnicode_GET_LENGTH(s);
Benjamin Peterson14339b62009-01-31 16:36:08 +000014290 break;
14291 default:
14292 Py_FatalError("Inconsistent interned string state.");
14293 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014294 _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014295 }
14296 fprintf(stderr, "total size of all interned strings: "
14297 "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d "
14298 "mortal/immortal\n", mortal_size, immortal_size);
14299 Py_DECREF(keys);
14300 PyDict_Clear(interned);
14301 Py_DECREF(interned);
14302 interned = NULL;
Walter Dörwald16807132007-05-25 13:52:07 +000014303}
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014304
14305
14306/********************* Unicode Iterator **************************/
14307
14308typedef struct {
Benjamin Peterson14339b62009-01-31 16:36:08 +000014309 PyObject_HEAD
14310 Py_ssize_t it_index;
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014311 PyObject *it_seq; /* Set to NULL when iterator is exhausted */
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014312} unicodeiterobject;
14313
14314static void
14315unicodeiter_dealloc(unicodeiterobject *it)
14316{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014317 _PyObject_GC_UNTRACK(it);
14318 Py_XDECREF(it->it_seq);
14319 PyObject_GC_Del(it);
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014320}
14321
14322static int
14323unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg)
14324{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014325 Py_VISIT(it->it_seq);
14326 return 0;
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014327}
14328
14329static PyObject *
14330unicodeiter_next(unicodeiterobject *it)
14331{
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014332 PyObject *seq, *item;
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014333
Benjamin Peterson14339b62009-01-31 16:36:08 +000014334 assert(it != NULL);
14335 seq = it->it_seq;
14336 if (seq == NULL)
14337 return NULL;
Victor Stinner910337b2011-10-03 03:20:16 +020014338 assert(_PyUnicode_CHECK(seq));
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014339
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014340 if (it->it_index < PyUnicode_GET_LENGTH(seq)) {
14341 int kind = PyUnicode_KIND(seq);
14342 void *data = PyUnicode_DATA(seq);
14343 Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index);
14344 item = PyUnicode_FromOrdinal(chr);
Benjamin Peterson14339b62009-01-31 16:36:08 +000014345 if (item != NULL)
14346 ++it->it_index;
14347 return item;
14348 }
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014349
Benjamin Peterson14339b62009-01-31 16:36:08 +000014350 Py_DECREF(seq);
14351 it->it_seq = NULL;
14352 return NULL;
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014353}
14354
14355static PyObject *
14356unicodeiter_len(unicodeiterobject *it)
14357{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014358 Py_ssize_t len = 0;
14359 if (it->it_seq)
Victor Stinnerc4f281e2011-10-11 22:11:42 +020014360 len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014361 return PyLong_FromSsize_t(len);
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014362}
14363
14364PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
14365
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000014366static PyObject *
14367unicodeiter_reduce(unicodeiterobject *it)
14368{
14369 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +020014370 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000014371 it->it_seq, it->it_index);
14372 } else {
14373 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
14374 if (u == NULL)
14375 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +020014376 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000014377 }
14378}
14379
14380PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
14381
14382static PyObject *
14383unicodeiter_setstate(unicodeiterobject *it, PyObject *state)
14384{
14385 Py_ssize_t index = PyLong_AsSsize_t(state);
14386 if (index == -1 && PyErr_Occurred())
14387 return NULL;
14388 if (index < 0)
14389 index = 0;
14390 it->it_index = index;
14391 Py_RETURN_NONE;
14392}
14393
14394PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
14395
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014396static PyMethodDef unicodeiter_methods[] = {
Benjamin Peterson14339b62009-01-31 16:36:08 +000014397 {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS,
Benjamin Peterson29060642009-01-31 22:14:21 +000014398 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000014399 {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS,
14400 reduce_doc},
14401 {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O,
14402 setstate_doc},
Benjamin Peterson14339b62009-01-31 16:36:08 +000014403 {NULL, NULL} /* sentinel */
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014404};
14405
14406PyTypeObject PyUnicodeIter_Type = {
Benjamin Peterson14339b62009-01-31 16:36:08 +000014407 PyVarObject_HEAD_INIT(&PyType_Type, 0)
14408 "str_iterator", /* tp_name */
14409 sizeof(unicodeiterobject), /* tp_basicsize */
14410 0, /* tp_itemsize */
14411 /* methods */
14412 (destructor)unicodeiter_dealloc, /* tp_dealloc */
14413 0, /* tp_print */
14414 0, /* tp_getattr */
14415 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +000014416 0, /* tp_reserved */
Benjamin Peterson14339b62009-01-31 16:36:08 +000014417 0, /* tp_repr */
14418 0, /* tp_as_number */
14419 0, /* tp_as_sequence */
14420 0, /* tp_as_mapping */
14421 0, /* tp_hash */
14422 0, /* tp_call */
14423 0, /* tp_str */
14424 PyObject_GenericGetAttr, /* tp_getattro */
14425 0, /* tp_setattro */
14426 0, /* tp_as_buffer */
14427 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
14428 0, /* tp_doc */
14429 (traverseproc)unicodeiter_traverse, /* tp_traverse */
14430 0, /* tp_clear */
14431 0, /* tp_richcompare */
14432 0, /* tp_weaklistoffset */
14433 PyObject_SelfIter, /* tp_iter */
14434 (iternextfunc)unicodeiter_next, /* tp_iternext */
14435 unicodeiter_methods, /* tp_methods */
14436 0,
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014437};
14438
14439static PyObject *
14440unicode_iter(PyObject *seq)
14441{
Benjamin Peterson14339b62009-01-31 16:36:08 +000014442 unicodeiterobject *it;
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014443
Benjamin Peterson14339b62009-01-31 16:36:08 +000014444 if (!PyUnicode_Check(seq)) {
14445 PyErr_BadInternalCall();
14446 return NULL;
14447 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014448 if (PyUnicode_READY(seq) == -1)
14449 return NULL;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014450 it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type);
14451 if (it == NULL)
14452 return NULL;
14453 it->it_index = 0;
14454 Py_INCREF(seq);
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014455 it->it_seq = seq;
Benjamin Peterson14339b62009-01-31 16:36:08 +000014456 _PyObject_GC_TRACK(it);
14457 return (PyObject *)it;
Guido van Rossum50e9fb92006-08-17 05:42:55 +000014458}
14459
Martin v. Löwis0d3072e2011-10-31 08:40:56 +010014460
14461size_t
14462Py_UNICODE_strlen(const Py_UNICODE *u)
14463{
14464 int res = 0;
14465 while(*u++)
14466 res++;
14467 return res;
14468}
14469
14470Py_UNICODE*
14471Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2)
14472{
14473 Py_UNICODE *u = s1;
14474 while ((*u++ = *s2++));
14475 return s1;
14476}
14477
14478Py_UNICODE*
14479Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
14480{
14481 Py_UNICODE *u = s1;
14482 while ((*u++ = *s2++))
14483 if (n-- == 0)
14484 break;
14485 return s1;
14486}
14487
14488Py_UNICODE*
14489Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2)
14490{
14491 Py_UNICODE *u1 = s1;
14492 u1 += Py_UNICODE_strlen(u1);
14493 Py_UNICODE_strcpy(u1, s2);
14494 return s1;
14495}
14496
14497int
14498Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2)
14499{
14500 while (*s1 && *s2 && *s1 == *s2)
14501 s1++, s2++;
14502 if (*s1 && *s2)
14503 return (*s1 < *s2) ? -1 : +1;
14504 if (*s1)
14505 return 1;
14506 if (*s2)
14507 return -1;
14508 return 0;
14509}
14510
14511int
14512Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
14513{
14514 register Py_UNICODE u1, u2;
14515 for (; n != 0; n--) {
14516 u1 = *s1;
14517 u2 = *s2;
14518 if (u1 != u2)
14519 return (u1 < u2) ? -1 : +1;
14520 if (u1 == '\0')
14521 return 0;
14522 s1++;
14523 s2++;
14524 }
14525 return 0;
14526}
14527
14528Py_UNICODE*
14529Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c)
14530{
14531 const Py_UNICODE *p;
14532 for (p = s; *p; p++)
14533 if (*p == c)
14534 return (Py_UNICODE*)p;
14535 return NULL;
14536}
14537
14538Py_UNICODE*
14539Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c)
14540{
14541 const Py_UNICODE *p;
14542 p = s + Py_UNICODE_strlen(s);
14543 while (p != s) {
14544 p--;
14545 if (*p == c)
14546 return (Py_UNICODE*)p;
14547 }
14548 return NULL;
14549}
Victor Stinner331ea922010-08-10 16:37:20 +000014550
Victor Stinner71133ff2010-09-01 23:43:53 +000014551Py_UNICODE*
Victor Stinner9db1a8b2011-10-23 20:04:37 +020014552PyUnicode_AsUnicodeCopy(PyObject *unicode)
Victor Stinner71133ff2010-09-01 23:43:53 +000014553{
Victor Stinner577db2c2011-10-11 22:12:48 +020014554 Py_UNICODE *u, *copy;
Victor Stinner57ffa9d2011-10-23 20:10:08 +020014555 Py_ssize_t len, size;
Victor Stinner71133ff2010-09-01 23:43:53 +000014556
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020014557 if (!PyUnicode_Check(unicode)) {
14558 PyErr_BadArgument();
14559 return NULL;
14560 }
Victor Stinner57ffa9d2011-10-23 20:10:08 +020014561 u = PyUnicode_AsUnicodeAndSize(unicode, &len);
Victor Stinner577db2c2011-10-11 22:12:48 +020014562 if (u == NULL)
14563 return NULL;
Victor Stinner71133ff2010-09-01 23:43:53 +000014564 /* Ensure we won't overflow the size. */
Victor Stinner57ffa9d2011-10-23 20:10:08 +020014565 if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
Victor Stinner71133ff2010-09-01 23:43:53 +000014566 PyErr_NoMemory();
14567 return NULL;
14568 }
Victor Stinner57ffa9d2011-10-23 20:10:08 +020014569 size = len + 1; /* copy the null character */
Victor Stinner71133ff2010-09-01 23:43:53 +000014570 size *= sizeof(Py_UNICODE);
14571 copy = PyMem_Malloc(size);
14572 if (copy == NULL) {
14573 PyErr_NoMemory();
14574 return NULL;
14575 }
Victor Stinner577db2c2011-10-11 22:12:48 +020014576 memcpy(copy, u, size);
Victor Stinner71133ff2010-09-01 23:43:53 +000014577 return copy;
14578}
Martin v. Löwis5b222132007-06-10 09:51:05 +000014579
Georg Brandl66c221e2010-10-14 07:04:07 +000014580/* A _string module, to export formatter_parser and formatter_field_name_split
14581 to the string.Formatter class implemented in Python. */
14582
14583static PyMethodDef _string_methods[] = {
14584 {"formatter_field_name_split", (PyCFunction) formatter_field_name_split,
14585 METH_O, PyDoc_STR("split the argument as a field name")},
14586 {"formatter_parser", (PyCFunction) formatter_parser,
14587 METH_O, PyDoc_STR("parse the argument as a format string")},
14588 {NULL, NULL}
14589};
14590
14591static struct PyModuleDef _string_module = {
14592 PyModuleDef_HEAD_INIT,
14593 "_string",
14594 PyDoc_STR("string helper module"),
14595 0,
14596 _string_methods,
14597 NULL,
14598 NULL,
14599 NULL,
14600 NULL
14601};
14602
14603PyMODINIT_FUNC
14604PyInit__string(void)
14605{
14606 return PyModule_Create(&_string_module);
14607}
14608
14609
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014610#ifdef __cplusplus
14611}
14612#endif