Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik |
| 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- |
| 12 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB |
| 15 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its |
| 18 | associated documentation, you agree that you have read, understood, |
| 19 | and will comply with the following terms and conditions: |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | associated documentation for any purpose and without fee is hereby |
| 23 | granted, provided that the above copyright notice appears in all |
| 24 | copies, and that both that copyright notice and this permission notice |
| 25 | appear in supporting documentation, and that the name of Secret Labs |
| 26 | AB or the author not be used in advertising or publicity pertaining to |
| 27 | distribution of the software without specific, written prior |
| 28 | permission. |
| 29 | |
| 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | -------------------------------------------------------------------- |
| 38 | |
| 39 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 50 | /* 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é Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 58 | /* --- 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 Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 70 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 71 | #define MAX_UNICODE 0x10ffff |
| 72 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 73 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 74 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 75 | #else |
| 76 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 77 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 78 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 79 | #define _PyUnicode_UTF8(op) \ |
| 80 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 81 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 82 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 83 | assert(PyUnicode_IS_READY(op)), \ |
| 84 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 85 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 86 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 87 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 88 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 89 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 90 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 91 | assert(PyUnicode_IS_READY(op)), \ |
| 92 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 93 | ((PyASCIIObject*)(op))->length : \ |
| 94 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 95 | #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 Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 105 | #define _PyUnicode_KIND(op) \ |
| 106 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 107 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 108 | #define _PyUnicode_GET_LENGTH(op) \ |
| 109 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 110 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 111 | #define _PyUnicode_DATA_ANY(op) \ |
| 112 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 113 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 114 | #undef PyUnicode_READY |
| 115 | #define PyUnicode_READY(op) \ |
| 116 | (assert(_PyUnicode_CHECK(op)), \ |
| 117 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 118 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 119 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 120 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 121 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 122 | (assert(_PyUnicode_CHECK(op)), \ |
| 123 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 124 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 125 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 126 | (assert(_PyUnicode_CHECK(op)), \ |
| 127 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 128 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 129 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 130 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 132 | (assert(_PyUnicode_CHECK(op)), \ |
| 133 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 134 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 135 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 136 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 137 | /* true if the Unicode object has an allocated wstr memory block |
| 138 | (not shared with other data) */ |
| 139 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 140 | (assert(_PyUnicode_CHECK(op)), \ |
| 141 | (_PyUnicode_WSTR(op) && \ |
| 142 | (!PyUnicode_IS_READY(op) || \ |
| 143 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 144 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 145 | /* Generic helper macro to convert characters of different types. |
| 146 | from_type and to_type have to be valid type names, begin and end |
| 147 | are pointers to the source characters which should be of type |
| 148 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 149 | buffer where the result characters are written to. */ |
| 150 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 151 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 152 | to_type *_to = (to_type *) to; \ |
| 153 | const from_type *_iter = (begin); \ |
| 154 | const from_type *_end = (end); \ |
| 155 | Py_ssize_t n = (_end) - (_iter); \ |
| 156 | const from_type *_unrolled_end = \ |
| 157 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 158 | while (_iter < (_unrolled_end)) { \ |
| 159 | _to[0] = (to_type) _iter[0]; \ |
| 160 | _to[1] = (to_type) _iter[1]; \ |
| 161 | _to[2] = (to_type) _iter[2]; \ |
| 162 | _to[3] = (to_type) _iter[3]; \ |
| 163 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 164 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 165 | while (_iter < (_end)) \ |
| 166 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 167 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 168 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 169 | /* This dictionary holds all interned unicode strings. Note that references |
| 170 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 171 | When the interned string reaches a refcnt of 0 the string deallocation |
| 172 | function will delete the reference from this dictionary. |
| 173 | |
| 174 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 175 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 176 | */ |
| 177 | static PyObject *interned; |
| 178 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 179 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 180 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 181 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 182 | /* List of static strings. */ |
| 183 | static _Py_Identifier *static_strings; |
| 184 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 185 | /* Single character Unicode strings in the Latin-1 range are being |
| 186 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 187 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 188 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 189 | /* Fast detection of the most frequent whitespace characters */ |
| 190 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 191 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 192 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 193 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 194 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 195 | /* case 0x000C: * FORM FEED */ |
| 196 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 197 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 198 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 199 | /* case 0x001C: * FILE SEPARATOR */ |
| 200 | /* case 0x001D: * GROUP SEPARATOR */ |
| 201 | /* case 0x001E: * RECORD SEPARATOR */ |
| 202 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 203 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 204 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 207 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 209 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 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, |
| 214 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 215 | 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 220 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 221 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 222 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 223 | static void copy_characters( |
| 224 | PyObject *to, Py_ssize_t to_start, |
| 225 | PyObject *from, Py_ssize_t from_start, |
| 226 | Py_ssize_t how_many); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 227 | static int unicode_modifiable(PyObject *unicode); |
| 228 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 229 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 230 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 231 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 232 | static PyObject * |
| 233 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 234 | static PyObject * |
| 235 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 236 | static PyObject * |
| 237 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 238 | |
| 239 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 240 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 241 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 242 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 243 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 245 | static void |
| 246 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 247 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 248 | PyObject *unicode, |
| 249 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 250 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 251 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 252 | /* Same for linebreaks */ |
| 253 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 254 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 255 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 256 | /* 0x000B, * LINE TABULATION */ |
| 257 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 258 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 259 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 260 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 261 | /* 0x001C, * FILE SEPARATOR */ |
| 262 | /* 0x001D, * GROUP SEPARATOR */ |
| 263 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 264 | 0, 0, 0, 0, 1, 1, 1, 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, |
| 268 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 269 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 280 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 281 | This function is kept for backward compatibility with the old API. */ |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 282 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 283 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 284 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 285 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 286 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 287 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 288 | /* This is actually an illegal character, so it should |
| 289 | not be passed to unichr. */ |
| 290 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 291 | #endif |
| 292 | } |
| 293 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 294 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 295 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 296 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 297 | { |
| 298 | PyASCIIObject *ascii; |
| 299 | unsigned int kind; |
| 300 | |
| 301 | assert(PyUnicode_Check(op)); |
| 302 | |
| 303 | ascii = (PyASCIIObject *)op; |
| 304 | kind = ascii->state.kind; |
| 305 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 306 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 308 | assert(ascii->state.ready == 1); |
| 309 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 310 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 311 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 312 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 314 | if (ascii->state.compact == 1) { |
| 315 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 316 | assert(kind == PyUnicode_1BYTE_KIND |
| 317 | || kind == PyUnicode_2BYTE_KIND |
| 318 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 319 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 320 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 321 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 322 | } |
| 323 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 324 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 325 | |
| 326 | data = unicode->data.any; |
| 327 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 328 | assert(ascii->length == 0); |
| 329 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 330 | assert(ascii->state.compact == 0); |
| 331 | assert(ascii->state.ascii == 0); |
| 332 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 333 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | assert(ascii->wstr != NULL); |
| 335 | assert(data == NULL); |
| 336 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 337 | } |
| 338 | else { |
| 339 | assert(kind == PyUnicode_1BYTE_KIND |
| 340 | || kind == PyUnicode_2BYTE_KIND |
| 341 | || kind == PyUnicode_4BYTE_KIND); |
| 342 | assert(ascii->state.compact == 0); |
| 343 | assert(ascii->state.ready == 1); |
| 344 | assert(data != NULL); |
| 345 | if (ascii->state.ascii) { |
| 346 | assert (compact->utf8 == data); |
| 347 | assert (compact->utf8_length == ascii->length); |
| 348 | } |
| 349 | else |
| 350 | assert (compact->utf8 != data); |
| 351 | } |
| 352 | } |
| 353 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 354 | if ( |
| 355 | #if SIZEOF_WCHAR_T == 2 |
| 356 | kind == PyUnicode_2BYTE_KIND |
| 357 | #else |
| 358 | kind == PyUnicode_4BYTE_KIND |
| 359 | #endif |
| 360 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 361 | { |
| 362 | assert(ascii->wstr == data); |
| 363 | assert(compact->wstr_length == ascii->length); |
| 364 | } else |
| 365 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 366 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 367 | |
| 368 | if (compact->utf8 == NULL) |
| 369 | assert(compact->utf8_length == 0); |
| 370 | if (ascii->wstr == NULL) |
| 371 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 372 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 373 | /* check that the best kind is used */ |
| 374 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 375 | { |
| 376 | Py_ssize_t i; |
| 377 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 378 | void *data; |
| 379 | Py_UCS4 ch; |
| 380 | |
| 381 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 382 | for (i=0; i < ascii->length; i++) |
| 383 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 384 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 385 | if (ch > maxchar) |
| 386 | maxchar = ch; |
| 387 | } |
| 388 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 389 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 390 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 391 | assert(maxchar <= 255); |
| 392 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 393 | else |
| 394 | assert(maxchar < 128); |
| 395 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 396 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 397 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 398 | assert(maxchar <= 0xFFFF); |
| 399 | } |
| 400 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 402 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 403 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 404 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 405 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 406 | return 1; |
| 407 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 408 | #endif |
| 409 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 410 | static PyObject* |
| 411 | unicode_result_wchar(PyObject *unicode) |
| 412 | { |
| 413 | #ifndef Py_DEBUG |
| 414 | Py_ssize_t len; |
| 415 | |
| 416 | assert(Py_REFCNT(unicode) == 1); |
| 417 | |
| 418 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 419 | if (len == 0) { |
| 420 | Py_INCREF(unicode_empty); |
| 421 | Py_DECREF(unicode); |
| 422 | return unicode_empty; |
| 423 | } |
| 424 | |
| 425 | if (len == 1) { |
| 426 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 427 | if (ch < 256) { |
| 428 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 429 | Py_DECREF(unicode); |
| 430 | return latin1_char; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (_PyUnicode_Ready(unicode) < 0) { |
| 435 | Py_XDECREF(unicode); |
| 436 | return NULL; |
| 437 | } |
| 438 | #else |
| 439 | /* don't make the result ready in debug mode to ensure that the caller |
| 440 | makes the string ready before using it */ |
| 441 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 442 | #endif |
| 443 | return unicode; |
| 444 | } |
| 445 | |
| 446 | static PyObject* |
| 447 | unicode_result_ready(PyObject *unicode) |
| 448 | { |
| 449 | Py_ssize_t length; |
| 450 | |
| 451 | length = PyUnicode_GET_LENGTH(unicode); |
| 452 | if (length == 0) { |
| 453 | if (unicode != unicode_empty) { |
| 454 | Py_INCREF(unicode_empty); |
| 455 | Py_DECREF(unicode); |
| 456 | } |
| 457 | return unicode_empty; |
| 458 | } |
| 459 | |
| 460 | if (length == 1) { |
| 461 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 462 | if (ch < 256) { |
| 463 | PyObject *latin1_char = unicode_latin1[ch]; |
| 464 | if (latin1_char != NULL) { |
| 465 | if (unicode != latin1_char) { |
| 466 | Py_INCREF(latin1_char); |
| 467 | Py_DECREF(unicode); |
| 468 | } |
| 469 | return latin1_char; |
| 470 | } |
| 471 | else { |
| 472 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 473 | Py_INCREF(unicode); |
| 474 | unicode_latin1[ch] = unicode; |
| 475 | return unicode; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 481 | return unicode; |
| 482 | } |
| 483 | |
| 484 | static PyObject* |
| 485 | unicode_result(PyObject *unicode) |
| 486 | { |
| 487 | assert(_PyUnicode_CHECK(unicode)); |
| 488 | if (PyUnicode_IS_READY(unicode)) |
| 489 | return unicode_result_ready(unicode); |
| 490 | else |
| 491 | return unicode_result_wchar(unicode); |
| 492 | } |
| 493 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 494 | static PyObject* |
| 495 | unicode_result_unchanged(PyObject *unicode) |
| 496 | { |
| 497 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 498 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 499 | return NULL; |
| 500 | Py_INCREF(unicode); |
| 501 | return unicode; |
| 502 | } |
| 503 | else |
| 504 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 505 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 508 | #ifdef HAVE_MBCS |
| 509 | static OSVERSIONINFOEX winver; |
| 510 | #endif |
| 511 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 512 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 513 | |
| 514 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 515 | to keep things simple, we use a single bitmask, using the least 5 |
| 516 | bits from each unicode characters as the bit index. */ |
| 517 | |
| 518 | /* the linebreak mask is set up by Unicode_Init below */ |
| 519 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 520 | #if LONG_BIT >= 128 |
| 521 | #define BLOOM_WIDTH 128 |
| 522 | #elif LONG_BIT >= 64 |
| 523 | #define BLOOM_WIDTH 64 |
| 524 | #elif LONG_BIT >= 32 |
| 525 | #define BLOOM_WIDTH 32 |
| 526 | #else |
| 527 | #error "LONG_BIT is smaller than 32" |
| 528 | #endif |
| 529 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 530 | #define BLOOM_MASK unsigned long |
| 531 | |
| 532 | static BLOOM_MASK bloom_linebreak; |
| 533 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 534 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 535 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 536 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 537 | #define BLOOM_LINEBREAK(ch) \ |
| 538 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 539 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 540 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 541 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 542 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | { |
| 544 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 545 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 546 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 547 | Py_ssize_t i; |
| 548 | |
| 549 | mask = 0; |
| 550 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 551 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 552 | |
| 553 | return mask; |
| 554 | } |
| 555 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 556 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 557 | (BLOOM(mask, chr) \ |
| 558 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 560 | /* Compilation of templated routines */ |
| 561 | |
| 562 | #include "stringlib/asciilib.h" |
| 563 | #include "stringlib/fastsearch.h" |
| 564 | #include "stringlib/partition.h" |
| 565 | #include "stringlib/split.h" |
| 566 | #include "stringlib/count.h" |
| 567 | #include "stringlib/find.h" |
| 568 | #include "stringlib/find_max_char.h" |
| 569 | #include "stringlib/localeutil.h" |
| 570 | #include "stringlib/undef.h" |
| 571 | |
| 572 | #include "stringlib/ucs1lib.h" |
| 573 | #include "stringlib/fastsearch.h" |
| 574 | #include "stringlib/partition.h" |
| 575 | #include "stringlib/split.h" |
| 576 | #include "stringlib/count.h" |
| 577 | #include "stringlib/find.h" |
| 578 | #include "stringlib/find_max_char.h" |
| 579 | #include "stringlib/localeutil.h" |
| 580 | #include "stringlib/undef.h" |
| 581 | |
| 582 | #include "stringlib/ucs2lib.h" |
| 583 | #include "stringlib/fastsearch.h" |
| 584 | #include "stringlib/partition.h" |
| 585 | #include "stringlib/split.h" |
| 586 | #include "stringlib/count.h" |
| 587 | #include "stringlib/find.h" |
| 588 | #include "stringlib/find_max_char.h" |
| 589 | #include "stringlib/localeutil.h" |
| 590 | #include "stringlib/undef.h" |
| 591 | |
| 592 | #include "stringlib/ucs4lib.h" |
| 593 | #include "stringlib/fastsearch.h" |
| 594 | #include "stringlib/partition.h" |
| 595 | #include "stringlib/split.h" |
| 596 | #include "stringlib/count.h" |
| 597 | #include "stringlib/find.h" |
| 598 | #include "stringlib/find_max_char.h" |
| 599 | #include "stringlib/localeutil.h" |
| 600 | #include "stringlib/undef.h" |
| 601 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 602 | #include "stringlib/unicodedefs.h" |
| 603 | #include "stringlib/fastsearch.h" |
| 604 | #include "stringlib/count.h" |
| 605 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 606 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 608 | /* --- Unicode Object ----------------------------------------------------- */ |
| 609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 610 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 611 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 612 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 613 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 614 | Py_ssize_t size, Py_UCS4 ch, |
| 615 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 616 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 617 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 618 | |
| 619 | switch (kind) { |
| 620 | case PyUnicode_1BYTE_KIND: |
| 621 | { |
| 622 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 623 | if (ch1 == ch) |
| 624 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 625 | else |
| 626 | return -1; |
| 627 | } |
| 628 | case PyUnicode_2BYTE_KIND: |
| 629 | { |
| 630 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 631 | if (ch2 == ch) |
| 632 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 633 | else |
| 634 | return -1; |
| 635 | } |
| 636 | case PyUnicode_4BYTE_KIND: |
| 637 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 638 | default: |
| 639 | assert(0); |
| 640 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 641 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 644 | static PyObject* |
| 645 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 646 | { |
| 647 | Py_ssize_t char_size; |
| 648 | Py_ssize_t struct_size; |
| 649 | Py_ssize_t new_size; |
| 650 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 651 | PyObject *new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 652 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 653 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 654 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 655 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 656 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | struct_size = sizeof(PyASCIIObject); |
| 658 | else |
| 659 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 660 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 661 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 662 | 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 Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 668 | _Py_DEC_REFTOTAL; |
| 669 | _Py_ForgetReference(unicode); |
| 670 | |
| 671 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 672 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 673 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 674 | PyErr_NoMemory(); |
| 675 | return NULL; |
| 676 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 677 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 678 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 679 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 680 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 681 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 682 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 683 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 684 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 685 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 686 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 687 | length, 0); |
| 688 | return unicode; |
| 689 | } |
| 690 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 691 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 692 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 693 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 694 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 695 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 696 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 697 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 698 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 699 | if (PyUnicode_IS_READY(unicode)) { |
| 700 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 701 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 702 | void *data; |
| 703 | |
| 704 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 705 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 706 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 707 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 708 | |
| 709 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 710 | PyErr_NoMemory(); |
| 711 | return -1; |
| 712 | } |
| 713 | new_size = (length + 1) * char_size; |
| 714 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 715 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 716 | { |
| 717 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 718 | _PyUnicode_UTF8(unicode) = NULL; |
| 719 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 720 | } |
| 721 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 722 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 723 | if (data == NULL) { |
| 724 | PyErr_NoMemory(); |
| 725 | return -1; |
| 726 | } |
| 727 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 728 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 729 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 730 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 731 | } |
| 732 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 733 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 734 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 735 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | _PyUnicode_LENGTH(unicode) = length; |
| 737 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 738 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 739 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 740 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 742 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 743 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 744 | |
| 745 | /* check for integer overflow */ |
| 746 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 747 | PyErr_NoMemory(); |
| 748 | return -1; |
| 749 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 750 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 751 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 752 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 753 | if (!wstr) { |
| 754 | PyErr_NoMemory(); |
| 755 | return -1; |
| 756 | } |
| 757 | _PyUnicode_WSTR(unicode) = wstr; |
| 758 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 759 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 760 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 764 | static PyObject* |
| 765 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 766 | { |
| 767 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 768 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 769 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 770 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 771 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 772 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 773 | |
| 774 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 775 | if (copy == NULL) |
| 776 | return NULL; |
| 777 | |
| 778 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 779 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 780 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 781 | } |
| 782 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 783 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 784 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 785 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 786 | if (w == NULL) |
| 787 | return NULL; |
| 788 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 789 | copy_length = Py_MIN(copy_length, length); |
| 790 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 791 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 792 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 796 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 797 | Ux0000 terminated; some code (e.g. new_identifier) |
| 798 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 799 | |
| 800 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 801 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 802 | |
| 803 | */ |
| 804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 805 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 806 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 807 | #endif |
| 808 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 809 | static PyUnicodeObject * |
| 810 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 811 | { |
| 812 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 813 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 814 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 815 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 816 | if (length == 0 && unicode_empty != NULL) { |
| 817 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 818 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 821 | /* Ensure we won't overflow the size. */ |
| 822 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 823 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 824 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 825 | if (length < 0) { |
| 826 | PyErr_SetString(PyExc_SystemError, |
| 827 | "Negative size passed to _PyUnicode_New"); |
| 828 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 831 | #ifdef Py_DEBUG |
| 832 | ++unicode_old_new_calls; |
| 833 | #endif |
| 834 | |
| 835 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 836 | if (unicode == NULL) |
| 837 | return NULL; |
| 838 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 839 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 840 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 841 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 842 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 843 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 844 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 845 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 846 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 847 | * the caller fails before initializing str -- unicode_resize() |
| 848 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 849 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 850 | * We don't want unicode_resize to read uninitialized memory in |
| 851 | * that case. |
| 852 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 853 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 854 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 855 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 856 | _PyUnicode_HASH(unicode) = -1; |
| 857 | _PyUnicode_STATE(unicode).interned = 0; |
| 858 | _PyUnicode_STATE(unicode).kind = 0; |
| 859 | _PyUnicode_STATE(unicode).compact = 0; |
| 860 | _PyUnicode_STATE(unicode).ready = 0; |
| 861 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 862 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 863 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 864 | _PyUnicode_UTF8(unicode) = NULL; |
| 865 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 866 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 867 | return unicode; |
| 868 | } |
| 869 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 870 | static const char* |
| 871 | unicode_kind_name(PyObject *unicode) |
| 872 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 873 | /* don't check consistency: unicode_kind_name() is called from |
| 874 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 875 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 876 | { |
| 877 | if (!PyUnicode_IS_READY(unicode)) |
| 878 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 879 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 880 | { |
| 881 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 882 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 883 | return "legacy ascii"; |
| 884 | else |
| 885 | return "legacy latin1"; |
| 886 | case PyUnicode_2BYTE_KIND: |
| 887 | return "legacy UCS2"; |
| 888 | case PyUnicode_4BYTE_KIND: |
| 889 | return "legacy UCS4"; |
| 890 | default: |
| 891 | return "<legacy invalid kind>"; |
| 892 | } |
| 893 | } |
| 894 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 895 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 896 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 897 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 898 | return "ascii"; |
| 899 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 900 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 901 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 902 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 903 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 904 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 905 | default: |
| 906 | return "<invalid compact kind>"; |
| 907 | } |
| 908 | } |
| 909 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 910 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 911 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | |
| 913 | /* Functions wrapping macros for use in debugger */ |
| 914 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 915 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | void *_PyUnicode_compact_data(void *unicode) { |
| 919 | return _PyUnicode_COMPACT_DATA(unicode); |
| 920 | } |
| 921 | void *_PyUnicode_data(void *unicode){ |
| 922 | printf("obj %p\n", unicode); |
| 923 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 924 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 925 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 926 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 927 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 928 | return PyUnicode_DATA(unicode); |
| 929 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 930 | |
| 931 | void |
| 932 | _PyUnicode_Dump(PyObject *op) |
| 933 | { |
| 934 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 935 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 936 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 937 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 938 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 939 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 940 | { |
| 941 | if (ascii->state.ascii) |
| 942 | data = (ascii + 1); |
| 943 | else |
| 944 | data = (compact + 1); |
| 945 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 946 | else |
| 947 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 948 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 949 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 950 | if (ascii->wstr == data) |
| 951 | printf("shared "); |
| 952 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 953 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 954 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 955 | printf(" (%zu), ", compact->wstr_length); |
| 956 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 957 | printf("shared "); |
| 958 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 959 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 960 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 962 | #endif |
| 963 | |
| 964 | PyObject * |
| 965 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 966 | { |
| 967 | PyObject *obj; |
| 968 | PyCompactUnicodeObject *unicode; |
| 969 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 970 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 971 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 972 | Py_ssize_t char_size; |
| 973 | Py_ssize_t struct_size; |
| 974 | |
| 975 | /* Optimization for empty strings */ |
| 976 | if (size == 0 && unicode_empty != NULL) { |
| 977 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 978 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | #ifdef Py_DEBUG |
| 982 | ++unicode_new_new_calls; |
| 983 | #endif |
| 984 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 985 | is_ascii = 0; |
| 986 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | struct_size = sizeof(PyCompactUnicodeObject); |
| 988 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 989 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 990 | char_size = 1; |
| 991 | is_ascii = 1; |
| 992 | struct_size = sizeof(PyASCIIObject); |
| 993 | } |
| 994 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 995 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 996 | char_size = 1; |
| 997 | } |
| 998 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 999 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1000 | char_size = 2; |
| 1001 | if (sizeof(wchar_t) == 2) |
| 1002 | is_sharing = 1; |
| 1003 | } |
| 1004 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1005 | if (maxchar > MAX_UNICODE) { |
| 1006 | PyErr_SetString(PyExc_SystemError, |
| 1007 | "invalid maximum character passed to PyUnicode_New"); |
| 1008 | return NULL; |
| 1009 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1010 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1011 | char_size = 4; |
| 1012 | if (sizeof(wchar_t) == 4) |
| 1013 | is_sharing = 1; |
| 1014 | } |
| 1015 | |
| 1016 | /* Ensure we won't overflow the size. */ |
| 1017 | if (size < 0) { |
| 1018 | PyErr_SetString(PyExc_SystemError, |
| 1019 | "Negative size passed to PyUnicode_New"); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1023 | return PyErr_NoMemory(); |
| 1024 | |
| 1025 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1026 | * PyObject_New() so we are able to allocate space for the object and |
| 1027 | * it's data buffer. |
| 1028 | */ |
| 1029 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1030 | if (obj == NULL) |
| 1031 | return PyErr_NoMemory(); |
| 1032 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1033 | if (obj == NULL) |
| 1034 | return NULL; |
| 1035 | |
| 1036 | unicode = (PyCompactUnicodeObject *)obj; |
| 1037 | if (is_ascii) |
| 1038 | data = ((PyASCIIObject*)obj) + 1; |
| 1039 | else |
| 1040 | data = unicode + 1; |
| 1041 | _PyUnicode_LENGTH(unicode) = size; |
| 1042 | _PyUnicode_HASH(unicode) = -1; |
| 1043 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1044 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | _PyUnicode_STATE(unicode).compact = 1; |
| 1046 | _PyUnicode_STATE(unicode).ready = 1; |
| 1047 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1048 | if (is_ascii) { |
| 1049 | ((char*)data)[size] = 0; |
| 1050 | _PyUnicode_WSTR(unicode) = NULL; |
| 1051 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1052 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | ((char*)data)[size] = 0; |
| 1054 | _PyUnicode_WSTR(unicode) = NULL; |
| 1055 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1057 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1058 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1059 | else { |
| 1060 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1061 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1062 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1063 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1064 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1065 | ((Py_UCS4*)data)[size] = 0; |
| 1066 | if (is_sharing) { |
| 1067 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1068 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1069 | } |
| 1070 | else { |
| 1071 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1072 | _PyUnicode_WSTR(unicode) = NULL; |
| 1073 | } |
| 1074 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1075 | #ifdef Py_DEBUG |
| 1076 | /* Fill the data with invalid characters to detect bugs earlier. |
| 1077 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, |
| 1078 | at least for ASCII and UCS-4 strings. U+00FF is invalid in ASCII |
| 1079 | and U+FFFFFFFF is an invalid character in Unicode 6.0. */ |
| 1080 | memset(data, 0xff, size * kind); |
| 1081 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1082 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1083 | return obj; |
| 1084 | } |
| 1085 | |
| 1086 | #if SIZEOF_WCHAR_T == 2 |
| 1087 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1088 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1089 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | |
| 1091 | This function assumes that unicode can hold one more code point than wstr |
| 1092 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1093 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1094 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1095 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1096 | { |
| 1097 | const wchar_t *iter; |
| 1098 | Py_UCS4 *ucs4_out; |
| 1099 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1100 | assert(unicode != NULL); |
| 1101 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1102 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1103 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1104 | |
| 1105 | for (iter = begin; iter < end; ) { |
| 1106 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1107 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1108 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1109 | && (iter+1) < end |
| 1110 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1111 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1112 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1113 | iter += 2; |
| 1114 | } |
| 1115 | else { |
| 1116 | *ucs4_out++ = *iter; |
| 1117 | iter++; |
| 1118 | } |
| 1119 | } |
| 1120 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1121 | _PyUnicode_GET_LENGTH(unicode))); |
| 1122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1123 | } |
| 1124 | #endif |
| 1125 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1126 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1127 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1128 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1129 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1130 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1131 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1137 | static int |
| 1138 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1139 | PyObject *from, Py_ssize_t from_start, |
| 1140 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1141 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1142 | unsigned int from_kind, to_kind; |
| 1143 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1144 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1145 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1146 | assert(PyUnicode_Check(from)); |
| 1147 | assert(PyUnicode_Check(to)); |
| 1148 | assert(PyUnicode_IS_READY(from)); |
| 1149 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1151 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1152 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1153 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1154 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1155 | if (how_many == 0) |
| 1156 | return 0; |
| 1157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1158 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1159 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1160 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1161 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1162 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1163 | #ifdef Py_DEBUG |
| 1164 | if (!check_maxchar |
| 1165 | && (from_kind > to_kind |
| 1166 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1167 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1168 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1169 | Py_UCS4 ch; |
| 1170 | Py_ssize_t i; |
| 1171 | for (i=0; i < how_many; i++) { |
| 1172 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1173 | assert(ch <= to_maxchar); |
| 1174 | } |
| 1175 | } |
| 1176 | #endif |
| 1177 | fast = (from_kind == to_kind); |
| 1178 | if (check_maxchar |
| 1179 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1180 | { |
| 1181 | /* deny latin1 => ascii */ |
| 1182 | fast = 0; |
| 1183 | } |
| 1184 | |
| 1185 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1186 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1187 | (char*)from_data + from_kind * from_start, |
| 1188 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1189 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1190 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1191 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1192 | { |
| 1193 | _PyUnicode_CONVERT_BYTES( |
| 1194 | Py_UCS1, Py_UCS2, |
| 1195 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1196 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1197 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1198 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1199 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1200 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1201 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1202 | { |
| 1203 | _PyUnicode_CONVERT_BYTES( |
| 1204 | Py_UCS1, Py_UCS4, |
| 1205 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1206 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1207 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1208 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1209 | } |
| 1210 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1211 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1212 | { |
| 1213 | _PyUnicode_CONVERT_BYTES( |
| 1214 | Py_UCS2, Py_UCS4, |
| 1215 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1216 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1217 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1218 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1219 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1220 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1221 | /* check if max_char(from substring) <= max_char(to) */ |
| 1222 | if (from_kind > to_kind |
| 1223 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1224 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1225 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1226 | /* slow path to check for character overflow */ |
| 1227 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1228 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1229 | Py_ssize_t i; |
| 1230 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1231 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1232 | for (i=0; i < how_many; i++) { |
| 1233 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1234 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1235 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1236 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1237 | #else |
| 1238 | if (!check_maxchar) { |
| 1239 | for (i=0; i < how_many; i++) { |
| 1240 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1241 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1242 | } |
| 1243 | } |
| 1244 | else { |
| 1245 | for (i=0; i < how_many; i++) { |
| 1246 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1247 | if (ch > to_maxchar) |
| 1248 | return 1; |
| 1249 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1250 | } |
| 1251 | } |
| 1252 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1253 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1254 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1255 | assert(0 && "inconsistent state"); |
| 1256 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1257 | } |
| 1258 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | static void |
| 1263 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1264 | PyObject *from, Py_ssize_t from_start, |
| 1265 | Py_ssize_t how_many) |
| 1266 | { |
| 1267 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1268 | } |
| 1269 | |
| 1270 | Py_ssize_t |
| 1271 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1272 | PyObject *from, Py_ssize_t from_start, |
| 1273 | Py_ssize_t how_many) |
| 1274 | { |
| 1275 | int err; |
| 1276 | |
| 1277 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1278 | PyErr_BadInternalCall(); |
| 1279 | return -1; |
| 1280 | } |
| 1281 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1282 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1283 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1284 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1285 | return -1; |
| 1286 | |
| 1287 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1288 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1289 | PyErr_Format(PyExc_SystemError, |
| 1290 | "Cannot write %zi characters at %zi " |
| 1291 | "in a string of %zi characters", |
| 1292 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1293 | return -1; |
| 1294 | } |
| 1295 | |
| 1296 | if (how_many == 0) |
| 1297 | return 0; |
| 1298 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1299 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1300 | return -1; |
| 1301 | |
| 1302 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1303 | if (err) { |
| 1304 | PyErr_Format(PyExc_SystemError, |
| 1305 | "Cannot copy %s characters " |
| 1306 | "into a string of %s characters", |
| 1307 | unicode_kind_name(from), |
| 1308 | unicode_kind_name(to)); |
| 1309 | return -1; |
| 1310 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1311 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1312 | } |
| 1313 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1314 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1315 | correct string length can be computed before converting a string to UCS4. |
| 1316 | This function counts single surrogates as a character and not as a pair. |
| 1317 | |
| 1318 | Return 0 on success, or -1 on error. */ |
| 1319 | static int |
| 1320 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1321 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | { |
| 1323 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1324 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1325 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1326 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1327 | *num_surrogates = 0; |
| 1328 | *maxchar = 0; |
| 1329 | |
| 1330 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1331 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1332 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1333 | && (iter+1) < end |
| 1334 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1335 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1336 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1337 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | iter += 2; |
| 1339 | } |
| 1340 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1341 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1342 | { |
| 1343 | ch = *iter; |
| 1344 | iter++; |
| 1345 | } |
| 1346 | if (ch > *maxchar) { |
| 1347 | *maxchar = ch; |
| 1348 | if (*maxchar > MAX_UNICODE) { |
| 1349 | PyErr_Format(PyExc_ValueError, |
| 1350 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1351 | ch); |
| 1352 | return -1; |
| 1353 | } |
| 1354 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1355 | } |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1360 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1361 | #endif |
| 1362 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1363 | int |
| 1364 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1365 | { |
| 1366 | wchar_t *end; |
| 1367 | Py_UCS4 maxchar = 0; |
| 1368 | Py_ssize_t num_surrogates; |
| 1369 | #if SIZEOF_WCHAR_T == 2 |
| 1370 | Py_ssize_t length_wo_surrogates; |
| 1371 | #endif |
| 1372 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1373 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1374 | strings were created using _PyObject_New() and where no canonical |
| 1375 | representation (the str field) has been set yet aka strings |
| 1376 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1377 | assert(_PyUnicode_CHECK(unicode)); |
| 1378 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1379 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1380 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1381 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1382 | /* Actually, it should neither be interned nor be anything else: */ |
| 1383 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1384 | |
| 1385 | #ifdef Py_DEBUG |
| 1386 | ++unicode_ready_calls; |
| 1387 | #endif |
| 1388 | |
| 1389 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1390 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1391 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1392 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | |
| 1394 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1395 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1396 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | PyErr_NoMemory(); |
| 1398 | return -1; |
| 1399 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1400 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | _PyUnicode_WSTR(unicode), end, |
| 1402 | PyUnicode_1BYTE_DATA(unicode)); |
| 1403 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1404 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1405 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1406 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1407 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1408 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1409 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | } |
| 1411 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1412 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1413 | _PyUnicode_UTF8(unicode) = NULL; |
| 1414 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | } |
| 1416 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1417 | _PyUnicode_WSTR(unicode) = NULL; |
| 1418 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1419 | } |
| 1420 | /* In this case we might have to convert down from 4-byte native |
| 1421 | wchar_t to 2-byte unicode. */ |
| 1422 | else if (maxchar < 65536) { |
| 1423 | assert(num_surrogates == 0 && |
| 1424 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1425 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1426 | #if SIZEOF_WCHAR_T == 2 |
| 1427 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1428 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1429 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1430 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1431 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1432 | _PyUnicode_UTF8(unicode) = NULL; |
| 1433 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1434 | #else |
| 1435 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1436 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1437 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1438 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1439 | PyErr_NoMemory(); |
| 1440 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1441 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1442 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1443 | _PyUnicode_WSTR(unicode), end, |
| 1444 | PyUnicode_2BYTE_DATA(unicode)); |
| 1445 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1446 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1447 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1448 | _PyUnicode_UTF8(unicode) = NULL; |
| 1449 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1450 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1451 | _PyUnicode_WSTR(unicode) = NULL; |
| 1452 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1453 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1454 | } |
| 1455 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1456 | else { |
| 1457 | #if SIZEOF_WCHAR_T == 2 |
| 1458 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1459 | new normalized 4-byte version. */ |
| 1460 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1461 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1462 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | PyErr_NoMemory(); |
| 1464 | return -1; |
| 1465 | } |
| 1466 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1467 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1468 | _PyUnicode_UTF8(unicode) = NULL; |
| 1469 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1470 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1471 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1472 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1473 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1474 | _PyUnicode_WSTR(unicode) = NULL; |
| 1475 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1476 | #else |
| 1477 | assert(num_surrogates == 0); |
| 1478 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1479 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1481 | _PyUnicode_UTF8(unicode) = NULL; |
| 1482 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1483 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1484 | #endif |
| 1485 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1486 | } |
| 1487 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1488 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1492 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1493 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1494 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1495 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1496 | case SSTATE_NOT_INTERNED: |
| 1497 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1498 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1499 | case SSTATE_INTERNED_MORTAL: |
| 1500 | /* revive dead object temporarily for DelItem */ |
| 1501 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1502 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1503 | Py_FatalError( |
| 1504 | "deletion of interned string failed"); |
| 1505 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1506 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1507 | case SSTATE_INTERNED_IMMORTAL: |
| 1508 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1509 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1510 | default: |
| 1511 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1514 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1515 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1516 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1517 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1518 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1519 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1520 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1521 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1524 | #ifdef Py_DEBUG |
| 1525 | static int |
| 1526 | unicode_is_singleton(PyObject *unicode) |
| 1527 | { |
| 1528 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1529 | if (unicode == unicode_empty) |
| 1530 | return 1; |
| 1531 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1532 | { |
| 1533 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1534 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1535 | return 1; |
| 1536 | } |
| 1537 | return 0; |
| 1538 | } |
| 1539 | #endif |
| 1540 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1541 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1542 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1543 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1544 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1545 | if (Py_REFCNT(unicode) != 1) |
| 1546 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1547 | if (_PyUnicode_HASH(unicode) != -1) |
| 1548 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1549 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1550 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1551 | if (!PyUnicode_CheckExact(unicode)) |
| 1552 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1553 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1554 | /* singleton refcount is greater than 1 */ |
| 1555 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1556 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1557 | return 1; |
| 1558 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1559 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1560 | static int |
| 1561 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1562 | { |
| 1563 | PyObject *unicode; |
| 1564 | Py_ssize_t old_length; |
| 1565 | |
| 1566 | assert(p_unicode != NULL); |
| 1567 | unicode = *p_unicode; |
| 1568 | |
| 1569 | assert(unicode != NULL); |
| 1570 | assert(PyUnicode_Check(unicode)); |
| 1571 | assert(0 <= length); |
| 1572 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1573 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1574 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1575 | else |
| 1576 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1577 | if (old_length == length) |
| 1578 | return 0; |
| 1579 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1580 | if (length == 0) { |
| 1581 | Py_DECREF(*p_unicode); |
| 1582 | *p_unicode = unicode_empty; |
| 1583 | Py_INCREF(*p_unicode); |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1587 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1588 | PyObject *copy = resize_copy(unicode, length); |
| 1589 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1590 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1591 | Py_DECREF(*p_unicode); |
| 1592 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1593 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1596 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1597 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1598 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1599 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1600 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1601 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1602 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1603 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1604 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1607 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1608 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1609 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1610 | PyObject *unicode; |
| 1611 | if (p_unicode == NULL) { |
| 1612 | PyErr_BadInternalCall(); |
| 1613 | return -1; |
| 1614 | } |
| 1615 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1616 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1617 | { |
| 1618 | PyErr_BadInternalCall(); |
| 1619 | return -1; |
| 1620 | } |
| 1621 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1622 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1623 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1624 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1625 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1626 | { |
| 1627 | PyObject *result; |
| 1628 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1629 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1630 | return 0; |
| 1631 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1632 | maxchar); |
| 1633 | if (result == NULL) |
| 1634 | return -1; |
| 1635 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1636 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1637 | Py_DECREF(*p_unicode); |
| 1638 | *p_unicode = result; |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | static int |
| 1643 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1644 | Py_UCS4 ch) |
| 1645 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1646 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1647 | if (unicode_widen(p_unicode, ch) < 0) |
| 1648 | return -1; |
| 1649 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1650 | PyUnicode_DATA(*p_unicode), |
| 1651 | (*pos)++, ch); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1655 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
| 1656 | Return the length of the input string. |
| 1657 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1658 | WARNING: The function doesn't copy the terminating null character and |
| 1659 | doesn't check the maximum character (may write a latin1 character in an |
| 1660 | ASCII string). */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1661 | static Py_ssize_t |
| 1662 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str) |
| 1663 | { |
| 1664 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1665 | void *data = PyUnicode_DATA(unicode); |
| 1666 | |
| 1667 | switch (kind) { |
| 1668 | case PyUnicode_1BYTE_KIND: { |
| 1669 | Py_ssize_t len = strlen(str); |
| 1670 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1671 | memcpy((char *) data + index, str, len); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1672 | return len; |
| 1673 | } |
| 1674 | case PyUnicode_2BYTE_KIND: { |
| 1675 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1676 | Py_UCS2 *ucs2 = start; |
| 1677 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1678 | |
| 1679 | for (; *str; ++ucs2, ++str) |
| 1680 | *ucs2 = (Py_UCS2)*str; |
| 1681 | |
| 1682 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1683 | return ucs2 - start; |
| 1684 | } |
| 1685 | default: { |
| 1686 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1687 | Py_UCS4 *ucs4 = start; |
| 1688 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1689 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1690 | |
| 1691 | for (; *str; ++ucs4, ++str) |
| 1692 | *ucs4 = (Py_UCS4)*str; |
| 1693 | |
| 1694 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1695 | return ucs4 - start; |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1701 | static PyObject* |
| 1702 | get_latin1_char(unsigned char ch) |
| 1703 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1704 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1705 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1706 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1707 | if (!unicode) |
| 1708 | return NULL; |
| 1709 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1710 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1711 | unicode_latin1[ch] = unicode; |
| 1712 | } |
| 1713 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1714 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1717 | PyObject * |
| 1718 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1719 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1720 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1721 | Py_UCS4 maxchar = 0; |
| 1722 | Py_ssize_t num_surrogates; |
| 1723 | |
| 1724 | if (u == NULL) |
| 1725 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1727 | /* If the Unicode data is known at construction time, we can apply |
| 1728 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1730 | /* Optimization for empty strings */ |
| 1731 | if (size == 0 && unicode_empty != NULL) { |
| 1732 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1733 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1734 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1735 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1736 | /* Single character Unicode objects in the Latin-1 range are |
| 1737 | shared when using this constructor */ |
| 1738 | if (size == 1 && *u < 256) |
| 1739 | return get_latin1_char((unsigned char)*u); |
| 1740 | |
| 1741 | /* If not empty and not single character, copy the Unicode data |
| 1742 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1743 | if (find_maxchar_surrogates(u, u + size, |
| 1744 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | return NULL; |
| 1746 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1747 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1748 | if (!unicode) |
| 1749 | return NULL; |
| 1750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1751 | switch (PyUnicode_KIND(unicode)) { |
| 1752 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1753 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1755 | break; |
| 1756 | case PyUnicode_2BYTE_KIND: |
| 1757 | #if Py_UNICODE_SIZE == 2 |
| 1758 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1759 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1760 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1762 | #endif |
| 1763 | break; |
| 1764 | case PyUnicode_4BYTE_KIND: |
| 1765 | #if SIZEOF_WCHAR_T == 2 |
| 1766 | /* This is the only case which has to process surrogates, thus |
| 1767 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1768 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1769 | #else |
| 1770 | assert(num_surrogates == 0); |
| 1771 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1772 | #endif |
| 1773 | break; |
| 1774 | default: |
| 1775 | assert(0 && "Impossible state"); |
| 1776 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1777 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1778 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1781 | PyObject * |
| 1782 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1783 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1784 | if (size < 0) { |
| 1785 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1786 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1787 | return NULL; |
| 1788 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1789 | if (u != NULL) |
| 1790 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1791 | else |
| 1792 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1795 | PyObject * |
| 1796 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1797 | { |
| 1798 | size_t size = strlen(u); |
| 1799 | if (size > PY_SSIZE_T_MAX) { |
| 1800 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1801 | return NULL; |
| 1802 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1803 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1806 | PyObject * |
| 1807 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1808 | { |
| 1809 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1810 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1811 | strlen(id->string), |
| 1812 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1813 | if (!id->object) |
| 1814 | return NULL; |
| 1815 | PyUnicode_InternInPlace(&id->object); |
| 1816 | assert(!id->next); |
| 1817 | id->next = static_strings; |
| 1818 | static_strings = id; |
| 1819 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1820 | return id->object; |
| 1821 | } |
| 1822 | |
| 1823 | void |
| 1824 | _PyUnicode_ClearStaticStrings() |
| 1825 | { |
| 1826 | _Py_Identifier *i; |
| 1827 | for (i = static_strings; i; i = i->next) { |
| 1828 | Py_DECREF(i->object); |
| 1829 | i->object = NULL; |
| 1830 | i->next = NULL; |
| 1831 | } |
| 1832 | } |
| 1833 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1834 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1835 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1836 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1837 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1838 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1839 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1840 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1841 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1842 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1843 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1844 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1845 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1846 | unicode = PyUnicode_New(size, 127); |
| 1847 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1848 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1849 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1850 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1851 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1854 | static Py_UCS4 |
| 1855 | kind_maxchar_limit(unsigned int kind) |
| 1856 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1857 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1858 | case PyUnicode_1BYTE_KIND: |
| 1859 | return 0x80; |
| 1860 | case PyUnicode_2BYTE_KIND: |
| 1861 | return 0x100; |
| 1862 | case PyUnicode_4BYTE_KIND: |
| 1863 | return 0x10000; |
| 1864 | default: |
| 1865 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1866 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1870 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1871 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1872 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1873 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1874 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1875 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1876 | if (size == 0) { |
| 1877 | Py_INCREF(unicode_empty); |
| 1878 | return unicode_empty; |
| 1879 | } |
| 1880 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1881 | if (size == 1) |
| 1882 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1883 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1884 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1885 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1886 | if (!res) |
| 1887 | return NULL; |
| 1888 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1889 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1890 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1893 | static PyObject* |
| 1894 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1895 | { |
| 1896 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1897 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1898 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1899 | if (size == 0) { |
| 1900 | Py_INCREF(unicode_empty); |
| 1901 | return unicode_empty; |
| 1902 | } |
| 1903 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1904 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1905 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1906 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1907 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1908 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1909 | if (!res) |
| 1910 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1911 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1912 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1913 | else { |
| 1914 | _PyUnicode_CONVERT_BYTES( |
| 1915 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1916 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1917 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1918 | return res; |
| 1919 | } |
| 1920 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1921 | static PyObject* |
| 1922 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1923 | { |
| 1924 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1925 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1926 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1927 | if (size == 0) { |
| 1928 | Py_INCREF(unicode_empty); |
| 1929 | return unicode_empty; |
| 1930 | } |
| 1931 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1932 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1933 | return get_latin1_char((unsigned char)u[0]); |
| 1934 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1935 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1936 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1937 | if (!res) |
| 1938 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1939 | if (max_char < 256) |
| 1940 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1941 | PyUnicode_1BYTE_DATA(res)); |
| 1942 | else if (max_char < 0x10000) |
| 1943 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1944 | PyUnicode_2BYTE_DATA(res)); |
| 1945 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1947 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1948 | return res; |
| 1949 | } |
| 1950 | |
| 1951 | PyObject* |
| 1952 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1953 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1954 | if (size < 0) { |
| 1955 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1956 | return NULL; |
| 1957 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1958 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1959 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1960 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1961 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1962 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1963 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1964 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1965 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1966 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1967 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1968 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 1971 | Py_UCS4 |
| 1972 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 1973 | { |
| 1974 | enum PyUnicode_Kind kind; |
| 1975 | void *startptr, *endptr; |
| 1976 | |
| 1977 | assert(PyUnicode_IS_READY(unicode)); |
| 1978 | assert(0 <= start); |
| 1979 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 1980 | assert(start <= end); |
| 1981 | |
| 1982 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 1983 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 1984 | |
| 1985 | if (start == end) |
| 1986 | return 127; |
| 1987 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 1988 | if (PyUnicode_IS_ASCII(unicode)) |
| 1989 | return 127; |
| 1990 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 1991 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 1992 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 1993 | endptr = (char *)startptr + end * kind; |
| 1994 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 1995 | switch(kind) { |
| 1996 | case PyUnicode_1BYTE_KIND: |
| 1997 | return ucs1lib_find_max_char(startptr, endptr); |
| 1998 | case PyUnicode_2BYTE_KIND: |
| 1999 | return ucs2lib_find_max_char(startptr, endptr); |
| 2000 | case PyUnicode_4BYTE_KIND: |
| 2001 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2002 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2003 | assert(0); |
| 2004 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2008 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2009 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2010 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2011 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2012 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2013 | { |
| 2014 | PyObject *unicode, *copy; |
| 2015 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2016 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2017 | unsigned int kind; |
| 2018 | |
| 2019 | assert(p_unicode != NULL); |
| 2020 | unicode = *p_unicode; |
| 2021 | assert(PyUnicode_IS_READY(unicode)); |
| 2022 | if (PyUnicode_IS_ASCII(unicode)) |
| 2023 | return; |
| 2024 | |
| 2025 | len = PyUnicode_GET_LENGTH(unicode); |
| 2026 | kind = PyUnicode_KIND(unicode); |
| 2027 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2028 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2029 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2030 | if (max_char >= 128) |
| 2031 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2032 | } |
| 2033 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2034 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2035 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2036 | if (max_char >= 256) |
| 2037 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2038 | } |
| 2039 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2040 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2041 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2042 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2043 | if (max_char >= 0x10000) |
| 2044 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2045 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2046 | copy = PyUnicode_New(len, max_char); |
| 2047 | copy_characters(copy, 0, unicode, 0, len); |
| 2048 | Py_DECREF(unicode); |
| 2049 | *p_unicode = copy; |
| 2050 | } |
| 2051 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2052 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2053 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2054 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2055 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2056 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2057 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2058 | if (!PyUnicode_Check(unicode)) { |
| 2059 | PyErr_BadInternalCall(); |
| 2060 | return NULL; |
| 2061 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2062 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2063 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2064 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2065 | length = PyUnicode_GET_LENGTH(unicode); |
| 2066 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2067 | if (!copy) |
| 2068 | return NULL; |
| 2069 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2070 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2071 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2072 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2073 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2074 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2077 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2078 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2079 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2080 | |
| 2081 | void* |
| 2082 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2083 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2084 | Py_ssize_t len; |
| 2085 | void *result; |
| 2086 | unsigned int skind; |
| 2087 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2088 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2089 | return NULL; |
| 2090 | |
| 2091 | len = PyUnicode_GET_LENGTH(s); |
| 2092 | skind = PyUnicode_KIND(s); |
| 2093 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2094 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2095 | return NULL; |
| 2096 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2097 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2098 | case PyUnicode_2BYTE_KIND: |
| 2099 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2100 | if (!result) |
| 2101 | return PyErr_NoMemory(); |
| 2102 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2103 | _PyUnicode_CONVERT_BYTES( |
| 2104 | Py_UCS1, Py_UCS2, |
| 2105 | PyUnicode_1BYTE_DATA(s), |
| 2106 | PyUnicode_1BYTE_DATA(s) + len, |
| 2107 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2108 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2109 | case PyUnicode_4BYTE_KIND: |
| 2110 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2111 | if (!result) |
| 2112 | return PyErr_NoMemory(); |
| 2113 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2114 | _PyUnicode_CONVERT_BYTES( |
| 2115 | Py_UCS2, Py_UCS4, |
| 2116 | PyUnicode_2BYTE_DATA(s), |
| 2117 | PyUnicode_2BYTE_DATA(s) + len, |
| 2118 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2119 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2120 | else { |
| 2121 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2122 | _PyUnicode_CONVERT_BYTES( |
| 2123 | Py_UCS1, Py_UCS4, |
| 2124 | PyUnicode_1BYTE_DATA(s), |
| 2125 | PyUnicode_1BYTE_DATA(s) + len, |
| 2126 | result); |
| 2127 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2128 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2129 | default: |
| 2130 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2131 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2132 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2133 | return NULL; |
| 2134 | } |
| 2135 | |
| 2136 | static Py_UCS4* |
| 2137 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2138 | int copy_null) |
| 2139 | { |
| 2140 | int kind; |
| 2141 | void *data; |
| 2142 | Py_ssize_t len, targetlen; |
| 2143 | if (PyUnicode_READY(string) == -1) |
| 2144 | return NULL; |
| 2145 | kind = PyUnicode_KIND(string); |
| 2146 | data = PyUnicode_DATA(string); |
| 2147 | len = PyUnicode_GET_LENGTH(string); |
| 2148 | targetlen = len; |
| 2149 | if (copy_null) |
| 2150 | targetlen++; |
| 2151 | if (!target) { |
| 2152 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2153 | PyErr_NoMemory(); |
| 2154 | return NULL; |
| 2155 | } |
| 2156 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2157 | if (!target) { |
| 2158 | PyErr_NoMemory(); |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | } |
| 2162 | else { |
| 2163 | if (targetsize < targetlen) { |
| 2164 | PyErr_Format(PyExc_SystemError, |
| 2165 | "string is longer than the buffer"); |
| 2166 | if (copy_null && 0 < targetsize) |
| 2167 | target[0] = 0; |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2171 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2172 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2173 | _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2175 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2176 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2177 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2178 | } |
| 2179 | else { |
| 2180 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2181 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2182 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2183 | if (copy_null) |
| 2184 | target[len] = 0; |
| 2185 | return target; |
| 2186 | } |
| 2187 | |
| 2188 | Py_UCS4* |
| 2189 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2190 | int copy_null) |
| 2191 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2192 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2193 | PyErr_BadInternalCall(); |
| 2194 | return NULL; |
| 2195 | } |
| 2196 | return as_ucs4(string, target, targetsize, copy_null); |
| 2197 | } |
| 2198 | |
| 2199 | Py_UCS4* |
| 2200 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2201 | { |
| 2202 | return as_ucs4(string, NULL, 0, 1); |
| 2203 | } |
| 2204 | |
| 2205 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2206 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2207 | PyObject * |
| 2208 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2209 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2210 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2211 | if (size == 0) { |
| 2212 | Py_INCREF(unicode_empty); |
| 2213 | return unicode_empty; |
| 2214 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2215 | PyErr_BadInternalCall(); |
| 2216 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2219 | if (size == -1) { |
| 2220 | size = wcslen(w); |
| 2221 | } |
| 2222 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2223 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2226 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2227 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2228 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2229 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2230 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2231 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2232 | *fmt++ = '%'; |
| 2233 | if (width) { |
| 2234 | if (zeropad) |
| 2235 | *fmt++ = '0'; |
| 2236 | fmt += sprintf(fmt, "%d", width); |
| 2237 | } |
| 2238 | if (precision) |
| 2239 | fmt += sprintf(fmt, ".%d", precision); |
| 2240 | if (longflag) |
| 2241 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2242 | else if (longlongflag) { |
| 2243 | /* longlongflag should only ever be nonzero on machines with |
| 2244 | HAVE_LONG_LONG defined */ |
| 2245 | #ifdef HAVE_LONG_LONG |
| 2246 | char *f = PY_FORMAT_LONG_LONG; |
| 2247 | while (*f) |
| 2248 | *fmt++ = *f++; |
| 2249 | #else |
| 2250 | /* we shouldn't ever get here */ |
| 2251 | assert(0); |
| 2252 | *fmt++ = 'l'; |
| 2253 | #endif |
| 2254 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2255 | else if (size_tflag) { |
| 2256 | char *f = PY_FORMAT_SIZE_T; |
| 2257 | while (*f) |
| 2258 | *fmt++ = *f++; |
| 2259 | } |
| 2260 | *fmt++ = c; |
| 2261 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2264 | /* helper for PyUnicode_FromFormatV() */ |
| 2265 | |
| 2266 | static const char* |
| 2267 | parse_format_flags(const char *f, |
| 2268 | int *p_width, int *p_precision, |
| 2269 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2270 | { |
| 2271 | int width, precision, longflag, longlongflag, size_tflag; |
| 2272 | |
| 2273 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2274 | f++; |
| 2275 | width = 0; |
| 2276 | while (Py_ISDIGIT((unsigned)*f)) |
| 2277 | width = (width*10) + *f++ - '0'; |
| 2278 | precision = 0; |
| 2279 | if (*f == '.') { |
| 2280 | f++; |
| 2281 | while (Py_ISDIGIT((unsigned)*f)) |
| 2282 | precision = (precision*10) + *f++ - '0'; |
| 2283 | if (*f == '%') { |
| 2284 | /* "%.3%s" => f points to "3" */ |
| 2285 | f--; |
| 2286 | } |
| 2287 | } |
| 2288 | if (*f == '\0') { |
| 2289 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2290 | f--; |
| 2291 | } |
| 2292 | if (p_width != NULL) |
| 2293 | *p_width = width; |
| 2294 | if (p_precision != NULL) |
| 2295 | *p_precision = precision; |
| 2296 | |
| 2297 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2298 | longflag = 0; |
| 2299 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2300 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2301 | |
| 2302 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2303 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2304 | longflag = 1; |
| 2305 | ++f; |
| 2306 | } |
| 2307 | #ifdef HAVE_LONG_LONG |
| 2308 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2309 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2310 | longlongflag = 1; |
| 2311 | f += 2; |
| 2312 | } |
| 2313 | #endif |
| 2314 | } |
| 2315 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2316 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2317 | size_tflag = 1; |
| 2318 | ++f; |
| 2319 | } |
| 2320 | if (p_longflag != NULL) |
| 2321 | *p_longflag = longflag; |
| 2322 | if (p_longlongflag != NULL) |
| 2323 | *p_longlongflag = longlongflag; |
| 2324 | if (p_size_tflag != NULL) |
| 2325 | *p_size_tflag = size_tflag; |
| 2326 | return f; |
| 2327 | } |
| 2328 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2329 | /* maximum number of characters required for output of %ld. 21 characters |
| 2330 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2331 | #define MAX_LONG_CHARS 21 |
| 2332 | /* maximum number of characters required for output of %lld. |
| 2333 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2334 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2335 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2336 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2337 | PyObject * |
| 2338 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2339 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2340 | va_list count; |
| 2341 | Py_ssize_t callcount = 0; |
| 2342 | PyObject **callresults = NULL; |
| 2343 | PyObject **callresult = NULL; |
| 2344 | Py_ssize_t n = 0; |
| 2345 | int width = 0; |
| 2346 | int precision = 0; |
| 2347 | int zeropad; |
| 2348 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2349 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2350 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2351 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2352 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2353 | Py_UCS4 argmaxchar; |
| 2354 | Py_ssize_t numbersize = 0; |
| 2355 | char *numberresults = NULL; |
| 2356 | char *numberresult = NULL; |
| 2357 | Py_ssize_t i; |
| 2358 | int kind; |
| 2359 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2360 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2361 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2362 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2363 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2364 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2365 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2366 | * also estimate a upper bound for all the number formats in the string, |
| 2367 | * numbers will be formatted in step 3 and be kept in a '\0'-separated |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2368 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2369 | for (f = format; *f; f++) { |
| 2370 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2371 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2372 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2373 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2374 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2375 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2376 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2377 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2378 | #ifdef HAVE_LONG_LONG |
| 2379 | if (longlongflag) { |
| 2380 | if (width < MAX_LONG_LONG_CHARS) |
| 2381 | width = MAX_LONG_LONG_CHARS; |
| 2382 | } |
| 2383 | else |
| 2384 | #endif |
| 2385 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2386 | including sign. Decimal takes the most space. This |
| 2387 | isn't enough for octal. If a width is specified we |
| 2388 | need more (which we allocate later). */ |
| 2389 | if (width < MAX_LONG_CHARS) |
| 2390 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2391 | |
| 2392 | /* account for the size + '\0' to separate numbers |
| 2393 | inside of the numberresults buffer */ |
| 2394 | numbersize += (width + 1); |
| 2395 | } |
| 2396 | } |
| 2397 | else if ((unsigned char)*f > 127) { |
| 2398 | PyErr_Format(PyExc_ValueError, |
| 2399 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2400 | "string, got a non-ASCII byte: 0x%02x", |
| 2401 | (unsigned char)*f); |
| 2402 | return NULL; |
| 2403 | } |
| 2404 | } |
| 2405 | /* step 2: allocate memory for the results of |
| 2406 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2407 | if (callcount) { |
| 2408 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2409 | if (!callresults) { |
| 2410 | PyErr_NoMemory(); |
| 2411 | return NULL; |
| 2412 | } |
| 2413 | callresult = callresults; |
| 2414 | } |
| 2415 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2416 | if (numbersize) { |
| 2417 | numberresults = PyObject_Malloc(numbersize); |
| 2418 | if (!numberresults) { |
| 2419 | PyErr_NoMemory(); |
| 2420 | goto fail; |
| 2421 | } |
| 2422 | numberresult = numberresults; |
| 2423 | } |
| 2424 | |
| 2425 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2426 | for (f = format; *f; f++) { |
| 2427 | if (*f == '%') { |
| 2428 | const char* p; |
| 2429 | int longflag; |
| 2430 | int longlongflag; |
| 2431 | int size_tflag; |
| 2432 | int numprinted; |
| 2433 | |
| 2434 | p = f; |
| 2435 | zeropad = (f[1] == '0'); |
| 2436 | f = parse_format_flags(f, &width, &precision, |
| 2437 | &longflag, &longlongflag, &size_tflag); |
| 2438 | switch (*f) { |
| 2439 | case 'c': |
| 2440 | { |
| 2441 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2442 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2443 | n++; |
| 2444 | break; |
| 2445 | } |
| 2446 | case '%': |
| 2447 | n++; |
| 2448 | break; |
| 2449 | case 'i': |
| 2450 | case 'd': |
| 2451 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2452 | width, precision, *f); |
| 2453 | if (longflag) |
| 2454 | numprinted = sprintf(numberresult, fmt, |
| 2455 | va_arg(count, long)); |
| 2456 | #ifdef HAVE_LONG_LONG |
| 2457 | else if (longlongflag) |
| 2458 | numprinted = sprintf(numberresult, fmt, |
| 2459 | va_arg(count, PY_LONG_LONG)); |
| 2460 | #endif |
| 2461 | else if (size_tflag) |
| 2462 | numprinted = sprintf(numberresult, fmt, |
| 2463 | va_arg(count, Py_ssize_t)); |
| 2464 | else |
| 2465 | numprinted = sprintf(numberresult, fmt, |
| 2466 | va_arg(count, int)); |
| 2467 | n += numprinted; |
| 2468 | /* advance by +1 to skip over the '\0' */ |
| 2469 | numberresult += (numprinted + 1); |
| 2470 | assert(*(numberresult - 1) == '\0'); |
| 2471 | assert(*(numberresult - 2) != '\0'); |
| 2472 | assert(numprinted >= 0); |
| 2473 | assert(numberresult <= numberresults + numbersize); |
| 2474 | break; |
| 2475 | case 'u': |
| 2476 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2477 | width, precision, 'u'); |
| 2478 | if (longflag) |
| 2479 | numprinted = sprintf(numberresult, fmt, |
| 2480 | va_arg(count, unsigned long)); |
| 2481 | #ifdef HAVE_LONG_LONG |
| 2482 | else if (longlongflag) |
| 2483 | numprinted = sprintf(numberresult, fmt, |
| 2484 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2485 | #endif |
| 2486 | else if (size_tflag) |
| 2487 | numprinted = sprintf(numberresult, fmt, |
| 2488 | va_arg(count, size_t)); |
| 2489 | else |
| 2490 | numprinted = sprintf(numberresult, fmt, |
| 2491 | va_arg(count, unsigned int)); |
| 2492 | n += numprinted; |
| 2493 | numberresult += (numprinted + 1); |
| 2494 | assert(*(numberresult - 1) == '\0'); |
| 2495 | assert(*(numberresult - 2) != '\0'); |
| 2496 | assert(numprinted >= 0); |
| 2497 | assert(numberresult <= numberresults + numbersize); |
| 2498 | break; |
| 2499 | case 'x': |
| 2500 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2501 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2502 | n += numprinted; |
| 2503 | numberresult += (numprinted + 1); |
| 2504 | assert(*(numberresult - 1) == '\0'); |
| 2505 | assert(*(numberresult - 2) != '\0'); |
| 2506 | assert(numprinted >= 0); |
| 2507 | assert(numberresult <= numberresults + numbersize); |
| 2508 | break; |
| 2509 | case 'p': |
| 2510 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2511 | /* %p is ill-defined: ensure leading 0x. */ |
| 2512 | if (numberresult[1] == 'X') |
| 2513 | numberresult[1] = 'x'; |
| 2514 | else if (numberresult[1] != 'x') { |
| 2515 | memmove(numberresult + 2, numberresult, |
| 2516 | strlen(numberresult) + 1); |
| 2517 | numberresult[0] = '0'; |
| 2518 | numberresult[1] = 'x'; |
| 2519 | numprinted += 2; |
| 2520 | } |
| 2521 | n += numprinted; |
| 2522 | numberresult += (numprinted + 1); |
| 2523 | assert(*(numberresult - 1) == '\0'); |
| 2524 | assert(*(numberresult - 2) != '\0'); |
| 2525 | assert(numprinted >= 0); |
| 2526 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2527 | break; |
| 2528 | case 's': |
| 2529 | { |
| 2530 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2531 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2532 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2533 | if (!str) |
| 2534 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2535 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2536 | unicode objects, there is no need to call ready on them */ |
| 2537 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2538 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2539 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2540 | /* Remember the str and switch to the next slot */ |
| 2541 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2542 | break; |
| 2543 | } |
| 2544 | case 'U': |
| 2545 | { |
| 2546 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2547 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2548 | if (PyUnicode_READY(obj) == -1) |
| 2549 | goto fail; |
| 2550 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2551 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2552 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2553 | break; |
| 2554 | } |
| 2555 | case 'V': |
| 2556 | { |
| 2557 | PyObject *obj = va_arg(count, PyObject *); |
| 2558 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2559 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2560 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2561 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2562 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2563 | if (PyUnicode_READY(obj) == -1) |
| 2564 | goto fail; |
| 2565 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2566 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2567 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2568 | *callresult++ = NULL; |
| 2569 | } |
| 2570 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2571 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2572 | if (!str_obj) |
| 2573 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2574 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2575 | Py_DECREF(str_obj); |
| 2576 | goto fail; |
| 2577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2578 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2579 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2580 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2581 | *callresult++ = str_obj; |
| 2582 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | } |
| 2585 | case 'S': |
| 2586 | { |
| 2587 | PyObject *obj = va_arg(count, PyObject *); |
| 2588 | PyObject *str; |
| 2589 | assert(obj); |
| 2590 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2591 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2592 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2593 | if (PyUnicode_READY(str) == -1) { |
| 2594 | Py_DECREF(str); |
| 2595 | goto fail; |
| 2596 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2597 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2598 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2599 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2600 | /* Remember the str and switch to the next slot */ |
| 2601 | *callresult++ = str; |
| 2602 | break; |
| 2603 | } |
| 2604 | case 'R': |
| 2605 | { |
| 2606 | PyObject *obj = va_arg(count, PyObject *); |
| 2607 | PyObject *repr; |
| 2608 | assert(obj); |
| 2609 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2610 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2611 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2612 | if (PyUnicode_READY(repr) == -1) { |
| 2613 | Py_DECREF(repr); |
| 2614 | goto fail; |
| 2615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2617 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2619 | /* Remember the repr and switch to the next slot */ |
| 2620 | *callresult++ = repr; |
| 2621 | break; |
| 2622 | } |
| 2623 | case 'A': |
| 2624 | { |
| 2625 | PyObject *obj = va_arg(count, PyObject *); |
| 2626 | PyObject *ascii; |
| 2627 | assert(obj); |
| 2628 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2629 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2630 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2631 | if (PyUnicode_READY(ascii) == -1) { |
| 2632 | Py_DECREF(ascii); |
| 2633 | goto fail; |
| 2634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2636 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | /* Remember the repr and switch to the next slot */ |
| 2639 | *callresult++ = ascii; |
| 2640 | break; |
| 2641 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2642 | default: |
| 2643 | /* if we stumble upon an unknown |
| 2644 | formatting code, copy the rest of |
| 2645 | the format string to the output |
| 2646 | string. (we cannot just skip the |
| 2647 | code, since there's no way to know |
| 2648 | what's in the argument list) */ |
| 2649 | n += strlen(p); |
| 2650 | goto expand; |
| 2651 | } |
| 2652 | } else |
| 2653 | n++; |
| 2654 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2655 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2656 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2658 | we don't have to resize the string. |
| 2659 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2660 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2661 | if (!string) |
| 2662 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2663 | kind = PyUnicode_KIND(string); |
| 2664 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2665 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2666 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2667 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2668 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2669 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2670 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2671 | |
| 2672 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2674 | /* checking for == because the last argument could be a empty |
| 2675 | string, which causes i to point to end, the assert at the end of |
| 2676 | the loop */ |
| 2677 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2678 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2679 | switch (*f) { |
| 2680 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2681 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2682 | const int ordinal = va_arg(vargs, int); |
| 2683 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2684 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2685 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2686 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2687 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2688 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2689 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2690 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2691 | { |
| 2692 | Py_ssize_t written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2693 | /* unused, since we already have the result */ |
| 2694 | if (*f == 'p') |
| 2695 | (void) va_arg(vargs, void *); |
| 2696 | else |
| 2697 | (void) va_arg(vargs, int); |
| 2698 | /* extract the result from numberresults and append. */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2699 | written = unicode_write_cstr(string, i, numberresult); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2700 | /* skip over the separating '\0' */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2701 | i += written; |
| 2702 | numberresult += written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | assert(*numberresult == '\0'); |
| 2704 | numberresult++; |
| 2705 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2706 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2707 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2708 | case 's': |
| 2709 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2710 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2711 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2712 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2713 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2714 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2715 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2716 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2717 | /* We're done with the unicode()/repr() => forget it */ |
| 2718 | Py_DECREF(*callresult); |
| 2719 | /* switch to next unicode()/repr() result */ |
| 2720 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | } |
| 2723 | case 'U': |
| 2724 | { |
| 2725 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2726 | Py_ssize_t size; |
| 2727 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2728 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2729 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2730 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | } |
| 2733 | case 'V': |
| 2734 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2735 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2736 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2737 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2738 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2739 | size = PyUnicode_GET_LENGTH(obj); |
| 2740 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2741 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2743 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2744 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2745 | assert(PyUnicode_KIND(*callresult) <= |
| 2746 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2747 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2748 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2749 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2750 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2751 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2752 | break; |
| 2753 | } |
| 2754 | case 'S': |
| 2755 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2756 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2757 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2758 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2759 | /* unused, since we already have the result */ |
| 2760 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2761 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2762 | copy_characters(string, i, *callresult, 0, size); |
| 2763 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2764 | /* We're done with the unicode()/repr() => forget it */ |
| 2765 | Py_DECREF(*callresult); |
| 2766 | /* switch to next unicode()/repr() result */ |
| 2767 | ++callresult; |
| 2768 | break; |
| 2769 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2770 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2771 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2772 | break; |
| 2773 | default: |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2774 | i += unicode_write_cstr(string, i, p); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2775 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2776 | goto end; |
| 2777 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2778 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2779 | else { |
| 2780 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2781 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2782 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2783 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2784 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2785 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2786 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2787 | if (callresults) |
| 2788 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2789 | if (numberresults) |
| 2790 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2791 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2792 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2793 | if (callresults) { |
| 2794 | PyObject **callresult2 = callresults; |
| 2795 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2796 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2797 | ++callresult2; |
| 2798 | } |
| 2799 | PyObject_Free(callresults); |
| 2800 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2801 | if (numberresults) |
| 2802 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2803 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2806 | PyObject * |
| 2807 | PyUnicode_FromFormat(const char *format, ...) |
| 2808 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2809 | PyObject* ret; |
| 2810 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2811 | |
| 2812 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2813 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2814 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2815 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2816 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2817 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2818 | va_end(vargs); |
| 2819 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 | #ifdef HAVE_WCHAR_H |
| 2823 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2824 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2825 | convert a Unicode object to a wide character string. |
| 2826 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2827 | - If w is NULL: return the number of wide characters (including the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2828 | character) required to convert the unicode object. Ignore size argument. |
| 2829 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2830 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2831 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2832 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2833 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2834 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2835 | wchar_t *w, |
| 2836 | Py_ssize_t size) |
| 2837 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2838 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2839 | const wchar_t *wstr; |
| 2840 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2841 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2842 | if (wstr == NULL) |
| 2843 | return -1; |
| 2844 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2845 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2846 | if (size > res) |
| 2847 | size = res + 1; |
| 2848 | else |
| 2849 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2850 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2851 | return res; |
| 2852 | } |
| 2853 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2854 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2858 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2859 | wchar_t *w, |
| 2860 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2861 | { |
| 2862 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2863 | PyErr_BadInternalCall(); |
| 2864 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2865 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2866 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2869 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2870 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2871 | Py_ssize_t *size) |
| 2872 | { |
| 2873 | wchar_t* buffer; |
| 2874 | Py_ssize_t buflen; |
| 2875 | |
| 2876 | if (unicode == NULL) { |
| 2877 | PyErr_BadInternalCall(); |
| 2878 | return NULL; |
| 2879 | } |
| 2880 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2881 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2882 | if (buflen == -1) |
| 2883 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2884 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2885 | PyErr_NoMemory(); |
| 2886 | return NULL; |
| 2887 | } |
| 2888 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2889 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2890 | if (buffer == NULL) { |
| 2891 | PyErr_NoMemory(); |
| 2892 | return NULL; |
| 2893 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2894 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2895 | if (buflen == -1) |
| 2896 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2897 | if (size != NULL) |
| 2898 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2899 | return buffer; |
| 2900 | } |
| 2901 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2902 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2904 | PyObject * |
| 2905 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2906 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2907 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2908 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2909 | PyErr_SetString(PyExc_ValueError, |
| 2910 | "chr() arg not in range(0x110000)"); |
| 2911 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2912 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2913 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2914 | if (ordinal < 256) |
| 2915 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2917 | v = PyUnicode_New(1, ordinal); |
| 2918 | if (v == NULL) |
| 2919 | return NULL; |
| 2920 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2921 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2922 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2925 | PyObject * |
| 2926 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2927 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2928 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2929 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2930 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2931 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2932 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2933 | Py_INCREF(obj); |
| 2934 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2935 | } |
| 2936 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2937 | /* For a Unicode subtype that's not a Unicode object, |
| 2938 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2939 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2940 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2941 | PyErr_Format(PyExc_TypeError, |
| 2942 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2943 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2944 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2947 | PyObject * |
| 2948 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2949 | const char *encoding, |
| 2950 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2951 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2952 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2953 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2955 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2956 | PyErr_BadInternalCall(); |
| 2957 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2958 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2959 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2960 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2961 | if (PyBytes_Check(obj)) { |
| 2962 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2963 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2964 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2965 | } |
| 2966 | else { |
| 2967 | v = PyUnicode_Decode( |
| 2968 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2969 | encoding, errors); |
| 2970 | } |
| 2971 | return v; |
| 2972 | } |
| 2973 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2974 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2975 | PyErr_SetString(PyExc_TypeError, |
| 2976 | "decoding str is not supported"); |
| 2977 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2978 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2979 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2980 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2981 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2982 | PyErr_Format(PyExc_TypeError, |
| 2983 | "coercing to str: need bytes, bytearray " |
| 2984 | "or buffer-like object, %.80s found", |
| 2985 | Py_TYPE(obj)->tp_name); |
| 2986 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2987 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2988 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2989 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2990 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2991 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2992 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2993 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2994 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2995 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2996 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2997 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3000 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3001 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3002 | 1 on success. */ |
| 3003 | static int |
| 3004 | normalize_encoding(const char *encoding, |
| 3005 | char *lower, |
| 3006 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3007 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3008 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3009 | char *l; |
| 3010 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3011 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3012 | if (encoding == NULL) { |
| 3013 | strcpy(lower, "utf-8"); |
| 3014 | return 1; |
| 3015 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3016 | e = encoding; |
| 3017 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3018 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3019 | while (*e) { |
| 3020 | if (l == l_end) |
| 3021 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3022 | if (Py_ISUPPER(*e)) { |
| 3023 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3024 | } |
| 3025 | else if (*e == '_') { |
| 3026 | *l++ = '-'; |
| 3027 | e++; |
| 3028 | } |
| 3029 | else { |
| 3030 | *l++ = *e++; |
| 3031 | } |
| 3032 | } |
| 3033 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3034 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3037 | PyObject * |
| 3038 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3039 | Py_ssize_t size, |
| 3040 | const char *encoding, |
| 3041 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3042 | { |
| 3043 | PyObject *buffer = NULL, *unicode; |
| 3044 | Py_buffer info; |
| 3045 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3046 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3047 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3048 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3049 | if ((strcmp(lower, "utf-8") == 0) || |
| 3050 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3051 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3052 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3053 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3054 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3055 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3056 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3057 | else if (strcmp(lower, "mbcs") == 0) |
| 3058 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3059 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3060 | else if (strcmp(lower, "ascii") == 0) |
| 3061 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3062 | else if (strcmp(lower, "utf-16") == 0) |
| 3063 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3064 | else if (strcmp(lower, "utf-32") == 0) |
| 3065 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3066 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | |
| 3068 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3069 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3070 | if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0) |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3071 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3072 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3073 | if (buffer == NULL) |
| 3074 | goto onError; |
| 3075 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3076 | if (unicode == NULL) |
| 3077 | goto onError; |
| 3078 | if (!PyUnicode_Check(unicode)) { |
| 3079 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3080 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3081 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3082 | Py_DECREF(unicode); |
| 3083 | goto onError; |
| 3084 | } |
| 3085 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3086 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3088 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | Py_XDECREF(buffer); |
| 3090 | return NULL; |
| 3091 | } |
| 3092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3093 | PyObject * |
| 3094 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3095 | const char *encoding, |
| 3096 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3097 | { |
| 3098 | PyObject *v; |
| 3099 | |
| 3100 | if (!PyUnicode_Check(unicode)) { |
| 3101 | PyErr_BadArgument(); |
| 3102 | goto onError; |
| 3103 | } |
| 3104 | |
| 3105 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3106 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3107 | |
| 3108 | /* Decode via the codec registry */ |
| 3109 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3110 | if (v == NULL) |
| 3111 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3112 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3113 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3114 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3115 | return NULL; |
| 3116 | } |
| 3117 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3118 | PyObject * |
| 3119 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3120 | const char *encoding, |
| 3121 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3122 | { |
| 3123 | PyObject *v; |
| 3124 | |
| 3125 | if (!PyUnicode_Check(unicode)) { |
| 3126 | PyErr_BadArgument(); |
| 3127 | goto onError; |
| 3128 | } |
| 3129 | |
| 3130 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3131 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3132 | |
| 3133 | /* Decode via the codec registry */ |
| 3134 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3135 | if (v == NULL) |
| 3136 | goto onError; |
| 3137 | if (!PyUnicode_Check(v)) { |
| 3138 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3139 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3140 | Py_TYPE(v)->tp_name); |
| 3141 | Py_DECREF(v); |
| 3142 | goto onError; |
| 3143 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3144 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3145 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3146 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3147 | return NULL; |
| 3148 | } |
| 3149 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3150 | PyObject * |
| 3151 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3152 | Py_ssize_t size, |
| 3153 | const char *encoding, |
| 3154 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | { |
| 3156 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3157 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3158 | unicode = PyUnicode_FromUnicode(s, size); |
| 3159 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3160 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3162 | Py_DECREF(unicode); |
| 3163 | return v; |
| 3164 | } |
| 3165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3166 | PyObject * |
| 3167 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3168 | const char *encoding, |
| 3169 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3170 | { |
| 3171 | PyObject *v; |
| 3172 | |
| 3173 | if (!PyUnicode_Check(unicode)) { |
| 3174 | PyErr_BadArgument(); |
| 3175 | goto onError; |
| 3176 | } |
| 3177 | |
| 3178 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3179 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3180 | |
| 3181 | /* Encode via the codec registry */ |
| 3182 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3183 | if (v == NULL) |
| 3184 | goto onError; |
| 3185 | return v; |
| 3186 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3187 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3188 | return NULL; |
| 3189 | } |
| 3190 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3191 | static size_t |
| 3192 | wcstombs_errorpos(const wchar_t *wstr) |
| 3193 | { |
| 3194 | size_t len; |
| 3195 | #if SIZEOF_WCHAR_T == 2 |
| 3196 | wchar_t buf[3]; |
| 3197 | #else |
| 3198 | wchar_t buf[2]; |
| 3199 | #endif |
| 3200 | char outbuf[MB_LEN_MAX]; |
| 3201 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3202 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3203 | #if SIZEOF_WCHAR_T == 2 |
| 3204 | buf[2] = 0; |
| 3205 | #else |
| 3206 | buf[1] = 0; |
| 3207 | #endif |
| 3208 | start = wstr; |
| 3209 | while (*wstr != L'\0') |
| 3210 | { |
| 3211 | previous = wstr; |
| 3212 | #if SIZEOF_WCHAR_T == 2 |
| 3213 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3214 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3215 | { |
| 3216 | buf[0] = wstr[0]; |
| 3217 | buf[1] = wstr[1]; |
| 3218 | wstr += 2; |
| 3219 | } |
| 3220 | else { |
| 3221 | buf[0] = *wstr; |
| 3222 | buf[1] = 0; |
| 3223 | wstr++; |
| 3224 | } |
| 3225 | #else |
| 3226 | buf[0] = *wstr; |
| 3227 | wstr++; |
| 3228 | #endif |
| 3229 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3230 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3231 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
| 3237 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3238 | static int |
| 3239 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3240 | { |
| 3241 | if (errors == NULL) { |
| 3242 | *surrogateescape = 0; |
| 3243 | return 0; |
| 3244 | } |
| 3245 | |
| 3246 | if (strcmp(errors, "strict") == 0) { |
| 3247 | *surrogateescape = 0; |
| 3248 | return 0; |
| 3249 | } |
| 3250 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3251 | *surrogateescape = 1; |
| 3252 | return 0; |
| 3253 | } |
| 3254 | PyErr_Format(PyExc_ValueError, |
| 3255 | "only 'strict' and 'surrogateescape' error handlers " |
| 3256 | "are supported, not '%s'", |
| 3257 | errors); |
| 3258 | return -1; |
| 3259 | } |
| 3260 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3261 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3262 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3263 | { |
| 3264 | Py_ssize_t wlen, wlen2; |
| 3265 | wchar_t *wstr; |
| 3266 | PyObject *bytes = NULL; |
| 3267 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3268 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3269 | PyObject *exc; |
| 3270 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3271 | int surrogateescape; |
| 3272 | |
| 3273 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3274 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3275 | |
| 3276 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3277 | if (wstr == NULL) |
| 3278 | return NULL; |
| 3279 | |
| 3280 | wlen2 = wcslen(wstr); |
| 3281 | if (wlen2 != wlen) { |
| 3282 | PyMem_Free(wstr); |
| 3283 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3284 | return NULL; |
| 3285 | } |
| 3286 | |
| 3287 | if (surrogateescape) { |
| 3288 | /* locale encoding with surrogateescape */ |
| 3289 | char *str; |
| 3290 | |
| 3291 | str = _Py_wchar2char(wstr, &error_pos); |
| 3292 | if (str == NULL) { |
| 3293 | if (error_pos == (size_t)-1) { |
| 3294 | PyErr_NoMemory(); |
| 3295 | PyMem_Free(wstr); |
| 3296 | return NULL; |
| 3297 | } |
| 3298 | else { |
| 3299 | goto encode_error; |
| 3300 | } |
| 3301 | } |
| 3302 | PyMem_Free(wstr); |
| 3303 | |
| 3304 | bytes = PyBytes_FromString(str); |
| 3305 | PyMem_Free(str); |
| 3306 | } |
| 3307 | else { |
| 3308 | size_t len, len2; |
| 3309 | |
| 3310 | len = wcstombs(NULL, wstr, 0); |
| 3311 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3312 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3313 | goto encode_error; |
| 3314 | } |
| 3315 | |
| 3316 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3317 | if (bytes == NULL) { |
| 3318 | PyMem_Free(wstr); |
| 3319 | return NULL; |
| 3320 | } |
| 3321 | |
| 3322 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3323 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3324 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3325 | goto encode_error; |
| 3326 | } |
| 3327 | PyMem_Free(wstr); |
| 3328 | } |
| 3329 | return bytes; |
| 3330 | |
| 3331 | encode_error: |
| 3332 | errmsg = strerror(errno); |
| 3333 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3334 | |
| 3335 | if (error_pos == (size_t)-1) |
| 3336 | error_pos = wcstombs_errorpos(wstr); |
| 3337 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3338 | PyMem_Free(wstr); |
| 3339 | Py_XDECREF(bytes); |
| 3340 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3341 | if (errmsg != NULL) { |
| 3342 | size_t errlen; |
| 3343 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3344 | if (wstr != NULL) { |
| 3345 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3346 | PyMem_Free(wstr); |
| 3347 | } else |
| 3348 | errmsg = NULL; |
| 3349 | } |
| 3350 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3351 | reason = PyUnicode_FromString( |
| 3352 | "wcstombs() encountered an unencodable " |
| 3353 | "wide character"); |
| 3354 | if (reason == NULL) |
| 3355 | return NULL; |
| 3356 | |
| 3357 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3358 | "locale", unicode, |
| 3359 | (Py_ssize_t)error_pos, |
| 3360 | (Py_ssize_t)(error_pos+1), |
| 3361 | reason); |
| 3362 | Py_DECREF(reason); |
| 3363 | if (exc != NULL) { |
| 3364 | PyCodec_StrictErrors(exc); |
| 3365 | Py_XDECREF(exc); |
| 3366 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3367 | return NULL; |
| 3368 | } |
| 3369 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3370 | PyObject * |
| 3371 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3372 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3373 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3374 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3375 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3376 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3377 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3378 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3379 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3380 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3381 | the Python codec requires to encode at least its own filename. Use the C |
| 3382 | version of the locale codec until the codec registry is initialized and |
| 3383 | the Python codec is loaded. |
| 3384 | |
| 3385 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3386 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3387 | subinterpreters. */ |
| 3388 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3389 | return PyUnicode_AsEncodedString(unicode, |
| 3390 | Py_FileSystemDefaultEncoding, |
| 3391 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3392 | } |
| 3393 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3394 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3395 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3396 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3399 | PyObject * |
| 3400 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3401 | const char *encoding, |
| 3402 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3403 | { |
| 3404 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3405 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | if (!PyUnicode_Check(unicode)) { |
| 3408 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3409 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3410 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3411 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3412 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3413 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3414 | if ((strcmp(lower, "utf-8") == 0) || |
| 3415 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3416 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3417 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3418 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3419 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3420 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3421 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3422 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3423 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3424 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3425 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3426 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3427 | else if (strcmp(lower, "mbcs") == 0) |
| 3428 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3429 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3430 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3431 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3432 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3433 | |
| 3434 | /* Encode via the codec registry */ |
| 3435 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3436 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3437 | return NULL; |
| 3438 | |
| 3439 | /* The normal path */ |
| 3440 | if (PyBytes_Check(v)) |
| 3441 | return v; |
| 3442 | |
| 3443 | /* If the codec returns a buffer, raise a warning and convert to bytes */ |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3444 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3445 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3446 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3447 | |
| 3448 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3449 | "encoder %s returned bytearray instead of bytes", |
| 3450 | encoding); |
| 3451 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3452 | Py_DECREF(v); |
| 3453 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3454 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3455 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3456 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3457 | Py_DECREF(v); |
| 3458 | return b; |
| 3459 | } |
| 3460 | |
| 3461 | PyErr_Format(PyExc_TypeError, |
| 3462 | "encoder did not return a bytes object (type=%.400s)", |
| 3463 | Py_TYPE(v)->tp_name); |
| 3464 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3465 | return NULL; |
| 3466 | } |
| 3467 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3468 | PyObject * |
| 3469 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3470 | const char *encoding, |
| 3471 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3472 | { |
| 3473 | PyObject *v; |
| 3474 | |
| 3475 | if (!PyUnicode_Check(unicode)) { |
| 3476 | PyErr_BadArgument(); |
| 3477 | goto onError; |
| 3478 | } |
| 3479 | |
| 3480 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3481 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3482 | |
| 3483 | /* Encode via the codec registry */ |
| 3484 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3485 | if (v == NULL) |
| 3486 | goto onError; |
| 3487 | if (!PyUnicode_Check(v)) { |
| 3488 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3489 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3490 | Py_TYPE(v)->tp_name); |
| 3491 | Py_DECREF(v); |
| 3492 | goto onError; |
| 3493 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3494 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3495 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3496 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3497 | return NULL; |
| 3498 | } |
| 3499 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3500 | static size_t |
| 3501 | mbstowcs_errorpos(const char *str, size_t len) |
| 3502 | { |
| 3503 | #ifdef HAVE_MBRTOWC |
| 3504 | const char *start = str; |
| 3505 | mbstate_t mbs; |
| 3506 | size_t converted; |
| 3507 | wchar_t ch; |
| 3508 | |
| 3509 | memset(&mbs, 0, sizeof mbs); |
| 3510 | while (len) |
| 3511 | { |
| 3512 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3513 | if (converted == 0) |
| 3514 | /* Reached end of string */ |
| 3515 | break; |
| 3516 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3517 | /* Conversion error or incomplete character */ |
| 3518 | return str - start; |
| 3519 | } |
| 3520 | else { |
| 3521 | str += converted; |
| 3522 | len -= converted; |
| 3523 | } |
| 3524 | } |
| 3525 | /* failed to find the undecodable byte sequence */ |
| 3526 | return 0; |
| 3527 | #endif |
| 3528 | return 0; |
| 3529 | } |
| 3530 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3531 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3532 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3533 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3534 | { |
| 3535 | wchar_t smallbuf[256]; |
| 3536 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3537 | wchar_t *wstr; |
| 3538 | size_t wlen, wlen2; |
| 3539 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3540 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3541 | size_t error_pos; |
| 3542 | char *errmsg; |
| 3543 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3544 | |
| 3545 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3546 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3547 | |
| 3548 | if (str[len] != '\0' || len != strlen(str)) { |
| 3549 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3550 | return NULL; |
| 3551 | } |
| 3552 | |
| 3553 | if (surrogateescape) |
| 3554 | { |
| 3555 | wstr = _Py_char2wchar(str, &wlen); |
| 3556 | if (wstr == NULL) { |
| 3557 | if (wlen == (size_t)-1) |
| 3558 | PyErr_NoMemory(); |
| 3559 | else |
| 3560 | PyErr_SetFromErrno(PyExc_OSError); |
| 3561 | return NULL; |
| 3562 | } |
| 3563 | |
| 3564 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3565 | PyMem_Free(wstr); |
| 3566 | } |
| 3567 | else { |
| 3568 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3569 | wlen = mbstowcs(NULL, str, 0); |
| 3570 | #else |
| 3571 | wlen = len; |
| 3572 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3573 | if (wlen == (size_t)-1) |
| 3574 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3575 | if (wlen+1 <= smallbuf_len) { |
| 3576 | wstr = smallbuf; |
| 3577 | } |
| 3578 | else { |
| 3579 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3580 | return PyErr_NoMemory(); |
| 3581 | |
| 3582 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3583 | if (!wstr) |
| 3584 | return PyErr_NoMemory(); |
| 3585 | } |
| 3586 | |
| 3587 | /* This shouldn't fail now */ |
| 3588 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3589 | if (wlen2 == (size_t)-1) { |
| 3590 | if (wstr != smallbuf) |
| 3591 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3592 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3593 | } |
| 3594 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3595 | assert(wlen2 == wlen); |
| 3596 | #endif |
| 3597 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3598 | if (wstr != smallbuf) |
| 3599 | PyMem_Free(wstr); |
| 3600 | } |
| 3601 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3602 | |
| 3603 | decode_error: |
| 3604 | errmsg = strerror(errno); |
| 3605 | assert(errmsg != NULL); |
| 3606 | |
| 3607 | error_pos = mbstowcs_errorpos(str, len); |
| 3608 | if (errmsg != NULL) { |
| 3609 | size_t errlen; |
| 3610 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3611 | if (wstr != NULL) { |
| 3612 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3613 | PyMem_Free(wstr); |
| 3614 | } else |
| 3615 | errmsg = NULL; |
| 3616 | } |
| 3617 | if (errmsg == NULL) |
| 3618 | reason = PyUnicode_FromString( |
| 3619 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3620 | if (reason == NULL) |
| 3621 | return NULL; |
| 3622 | |
| 3623 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3624 | "locale", str, len, |
| 3625 | (Py_ssize_t)error_pos, |
| 3626 | (Py_ssize_t)(error_pos+1), |
| 3627 | reason); |
| 3628 | Py_DECREF(reason); |
| 3629 | if (exc != NULL) { |
| 3630 | PyCodec_StrictErrors(exc); |
| 3631 | Py_XDECREF(exc); |
| 3632 | } |
| 3633 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3637 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3638 | { |
| 3639 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3640 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | |
| 3644 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3645 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3646 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3647 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3648 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3649 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3650 | PyObject* |
| 3651 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3652 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3653 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3654 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3655 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3656 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3657 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3658 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3659 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3660 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3661 | the Python codec requires to encode at least its own filename. Use the C |
| 3662 | version of the locale codec until the codec registry is initialized and |
| 3663 | the Python codec is loaded. |
| 3664 | |
| 3665 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3666 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3667 | subinterpreters. */ |
| 3668 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3669 | return PyUnicode_Decode(s, size, |
| 3670 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3671 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3672 | } |
| 3673 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3674 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3675 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3676 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3679 | |
| 3680 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3681 | _PyUnicode_HasNULChars(PyObject* s) |
| 3682 | { |
| 3683 | static PyObject *nul = NULL; |
| 3684 | |
| 3685 | if (nul == NULL) |
| 3686 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3687 | if (nul == NULL) |
| 3688 | return -1; |
| 3689 | return PyUnicode_Contains(s, nul); |
| 3690 | } |
| 3691 | |
| 3692 | |
| 3693 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3694 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3695 | { |
| 3696 | PyObject *output = NULL; |
| 3697 | Py_ssize_t size; |
| 3698 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3699 | if (arg == NULL) { |
| 3700 | Py_DECREF(*(PyObject**)addr); |
| 3701 | return 1; |
| 3702 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3703 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3704 | output = arg; |
| 3705 | Py_INCREF(output); |
| 3706 | } |
| 3707 | else { |
| 3708 | arg = PyUnicode_FromObject(arg); |
| 3709 | if (!arg) |
| 3710 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3711 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3712 | Py_DECREF(arg); |
| 3713 | if (!output) |
| 3714 | return 0; |
| 3715 | if (!PyBytes_Check(output)) { |
| 3716 | Py_DECREF(output); |
| 3717 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3718 | return 0; |
| 3719 | } |
| 3720 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3721 | size = PyBytes_GET_SIZE(output); |
| 3722 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3723 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3724 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3725 | Py_DECREF(output); |
| 3726 | return 0; |
| 3727 | } |
| 3728 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3729 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3733 | int |
| 3734 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3735 | { |
| 3736 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3737 | if (arg == NULL) { |
| 3738 | Py_DECREF(*(PyObject**)addr); |
| 3739 | return 1; |
| 3740 | } |
| 3741 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3742 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3743 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3744 | output = arg; |
| 3745 | Py_INCREF(output); |
| 3746 | } |
| 3747 | else { |
| 3748 | arg = PyBytes_FromObject(arg); |
| 3749 | if (!arg) |
| 3750 | return 0; |
| 3751 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3752 | PyBytes_GET_SIZE(arg)); |
| 3753 | Py_DECREF(arg); |
| 3754 | if (!output) |
| 3755 | return 0; |
| 3756 | if (!PyUnicode_Check(output)) { |
| 3757 | Py_DECREF(output); |
| 3758 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3759 | return 0; |
| 3760 | } |
| 3761 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3762 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3763 | Py_DECREF(output); |
| 3764 | return 0; |
| 3765 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3767 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3768 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3769 | Py_DECREF(output); |
| 3770 | return 0; |
| 3771 | } |
| 3772 | *(PyObject**)addr = output; |
| 3773 | return Py_CLEANUP_SUPPORTED; |
| 3774 | } |
| 3775 | |
| 3776 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3777 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3778 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3779 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3780 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3781 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3782 | if (!PyUnicode_Check(unicode)) { |
| 3783 | PyErr_BadArgument(); |
| 3784 | return NULL; |
| 3785 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3786 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3787 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3789 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3790 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3791 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3792 | if (bytes == NULL) |
| 3793 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3794 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3795 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3796 | Py_DECREF(bytes); |
| 3797 | return NULL; |
| 3798 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3799 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3800 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3801 | PyBytes_AS_STRING(bytes), |
| 3802 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3803 | Py_DECREF(bytes); |
| 3804 | } |
| 3805 | |
| 3806 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3807 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3808 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3813 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3814 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3815 | } |
| 3816 | |
| 3817 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3818 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3819 | #endif |
| 3820 | |
| 3821 | |
| 3822 | Py_UNICODE * |
| 3823 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3824 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3825 | const unsigned char *one_byte; |
| 3826 | #if SIZEOF_WCHAR_T == 4 |
| 3827 | const Py_UCS2 *two_bytes; |
| 3828 | #else |
| 3829 | const Py_UCS4 *four_bytes; |
| 3830 | const Py_UCS4 *ucs4_end; |
| 3831 | Py_ssize_t num_surrogates; |
| 3832 | #endif |
| 3833 | wchar_t *w; |
| 3834 | wchar_t *wchar_end; |
| 3835 | |
| 3836 | if (!PyUnicode_Check(unicode)) { |
| 3837 | PyErr_BadArgument(); |
| 3838 | return NULL; |
| 3839 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3840 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3842 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3843 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | |
| 3845 | #ifdef Py_DEBUG |
| 3846 | ++unicode_as_unicode_calls; |
| 3847 | #endif |
| 3848 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3849 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3850 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3851 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3852 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3853 | num_surrogates = 0; |
| 3854 | |
| 3855 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3856 | if (*four_bytes > 0xFFFF) |
| 3857 | ++num_surrogates; |
| 3858 | } |
| 3859 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3860 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3861 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3862 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3863 | PyErr_NoMemory(); |
| 3864 | return NULL; |
| 3865 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3866 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3867 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3868 | w = _PyUnicode_WSTR(unicode); |
| 3869 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3870 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3872 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3873 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3875 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3876 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3877 | } |
| 3878 | else |
| 3879 | *w = *four_bytes; |
| 3880 | |
| 3881 | if (w > wchar_end) { |
| 3882 | assert(0 && "Miscalculated string end"); |
| 3883 | } |
| 3884 | } |
| 3885 | *w = 0; |
| 3886 | #else |
| 3887 | /* sizeof(wchar_t) == 4 */ |
| 3888 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3889 | "should share memory already."); |
| 3890 | return NULL; |
| 3891 | #endif |
| 3892 | } |
| 3893 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3894 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3895 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3896 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3897 | PyErr_NoMemory(); |
| 3898 | return NULL; |
| 3899 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3900 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3901 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3902 | w = _PyUnicode_WSTR(unicode); |
| 3903 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3904 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3905 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3906 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3907 | for (; w < wchar_end; ++one_byte, ++w) |
| 3908 | *w = *one_byte; |
| 3909 | /* null-terminate the wstr */ |
| 3910 | *w = 0; |
| 3911 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3912 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3913 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3914 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3915 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3916 | *w = *two_bytes; |
| 3917 | /* null-terminate the wstr */ |
| 3918 | *w = 0; |
| 3919 | #else |
| 3920 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3921 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3922 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3923 | Py_FatalError("Impossible unicode object state, wstr " |
| 3924 | "and str should share memory already."); |
| 3925 | return NULL; |
| 3926 | #endif |
| 3927 | } |
| 3928 | else { |
| 3929 | assert(0 && "This should never happen."); |
| 3930 | } |
| 3931 | } |
| 3932 | } |
| 3933 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3934 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3935 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3938 | Py_UNICODE * |
| 3939 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3940 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3941 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3944 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3945 | Py_ssize_t |
| 3946 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3947 | { |
| 3948 | if (!PyUnicode_Check(unicode)) { |
| 3949 | PyErr_BadArgument(); |
| 3950 | goto onError; |
| 3951 | } |
| 3952 | return PyUnicode_GET_SIZE(unicode); |
| 3953 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3954 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | return -1; |
| 3956 | } |
| 3957 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3958 | Py_ssize_t |
| 3959 | PyUnicode_GetLength(PyObject *unicode) |
| 3960 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3961 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3962 | PyErr_BadArgument(); |
| 3963 | return -1; |
| 3964 | } |
| 3965 | |
| 3966 | return PyUnicode_GET_LENGTH(unicode); |
| 3967 | } |
| 3968 | |
| 3969 | Py_UCS4 |
| 3970 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3971 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3972 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3973 | PyErr_BadArgument(); |
| 3974 | return (Py_UCS4)-1; |
| 3975 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3976 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3977 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3978 | return (Py_UCS4)-1; |
| 3979 | } |
| 3980 | return PyUnicode_READ_CHAR(unicode, index); |
| 3981 | } |
| 3982 | |
| 3983 | int |
| 3984 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3985 | { |
| 3986 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3987 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3988 | return -1; |
| 3989 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3990 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3991 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3992 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3993 | return -1; |
| 3994 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3995 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3996 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3997 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3998 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3999 | return -1; |
| 4000 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4001 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4002 | index, ch); |
| 4003 | return 0; |
| 4004 | } |
| 4005 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4006 | const char * |
| 4007 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4008 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4009 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4012 | /* create or adjust a UnicodeDecodeError */ |
| 4013 | static void |
| 4014 | make_decode_exception(PyObject **exceptionObject, |
| 4015 | const char *encoding, |
| 4016 | const char *input, Py_ssize_t length, |
| 4017 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4018 | const char *reason) |
| 4019 | { |
| 4020 | if (*exceptionObject == NULL) { |
| 4021 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4022 | encoding, input, length, startpos, endpos, reason); |
| 4023 | } |
| 4024 | else { |
| 4025 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4026 | goto onError; |
| 4027 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4028 | goto onError; |
| 4029 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4030 | goto onError; |
| 4031 | } |
| 4032 | return; |
| 4033 | |
| 4034 | onError: |
| 4035 | Py_DECREF(*exceptionObject); |
| 4036 | *exceptionObject = NULL; |
| 4037 | } |
| 4038 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | /* error handling callback helper: |
| 4040 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4041 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4042 | and adjust various state variables. |
| 4043 | return 0 on success, -1 on error |
| 4044 | */ |
| 4045 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4046 | static int |
| 4047 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4048 | const char *encoding, const char *reason, |
| 4049 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4050 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4051 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4052 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4053 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4054 | |
| 4055 | PyObject *restuple = NULL; |
| 4056 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4057 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4058 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4059 | Py_ssize_t requiredsize; |
| 4060 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4061 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4062 | int res = -1; |
| 4063 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4064 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4065 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4066 | else |
| 4067 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4068 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4069 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4070 | *errorHandler = PyCodec_LookupError(errors); |
| 4071 | if (*errorHandler == NULL) |
| 4072 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4075 | make_decode_exception(exceptionObject, |
| 4076 | encoding, |
| 4077 | *input, *inend - *input, |
| 4078 | *startinpos, *endinpos, |
| 4079 | reason); |
| 4080 | if (*exceptionObject == NULL) |
| 4081 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4082 | |
| 4083 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4084 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4085 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4086 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4087 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4088 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | } |
| 4090 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4091 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4092 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4093 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4094 | |
| 4095 | /* Copy back the bytes variables, which might have been modified by the |
| 4096 | callback */ |
| 4097 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4098 | if (!inputobj) |
| 4099 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4100 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4101 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4102 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4103 | *input = PyBytes_AS_STRING(inputobj); |
| 4104 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4105 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4106 | /* we can DECREF safely, as the exception has another reference, |
| 4107 | so the object won't go away. */ |
| 4108 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4109 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4111 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4112 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4113 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4114 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4115 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4116 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4117 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4118 | /* need more space? (at least enough for what we |
| 4119 | have+the replacement+the rest of the string (starting |
| 4120 | at the new input position), so we won't have to check space |
| 4121 | when there are no errors in the rest of the string) */ |
| 4122 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4123 | requiredsize = *outpos + replen + insize-newpos; |
| 4124 | if (requiredsize > outsize) { |
| 4125 | if (requiredsize<2*outsize) |
| 4126 | requiredsize = 2*outsize; |
| 4127 | if (unicode_resize(output, requiredsize) < 0) |
| 4128 | goto onError; |
| 4129 | } |
| 4130 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4131 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4132 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4133 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4134 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4135 | else { |
| 4136 | wchar_t *repwstr; |
| 4137 | Py_ssize_t repwlen; |
| 4138 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4139 | if (repwstr == NULL) |
| 4140 | goto onError; |
| 4141 | /* need more space? (at least enough for what we |
| 4142 | have+the replacement+the rest of the string (starting |
| 4143 | at the new input position), so we won't have to check space |
| 4144 | when there are no errors in the rest of the string) */ |
| 4145 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4146 | if (requiredsize > outsize) { |
| 4147 | if (requiredsize < 2*outsize) |
| 4148 | requiredsize = 2*outsize; |
| 4149 | if (unicode_resize(output, requiredsize) < 0) |
| 4150 | goto onError; |
| 4151 | } |
| 4152 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4153 | *outpos += repwlen; |
| 4154 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4155 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4156 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4157 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4158 | /* we made it! */ |
| 4159 | res = 0; |
| 4160 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4161 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4162 | Py_XDECREF(restuple); |
| 4163 | return res; |
| 4164 | } |
| 4165 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4166 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4167 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4168 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4169 | |
| 4170 | /* Three simple macros defining base-64. */ |
| 4171 | |
| 4172 | /* Is c a base-64 character? */ |
| 4173 | |
| 4174 | #define IS_BASE64(c) \ |
| 4175 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4176 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4177 | ((c) >= '0' && (c) <= '9') || \ |
| 4178 | (c) == '+' || (c) == '/') |
| 4179 | |
| 4180 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4181 | |
| 4182 | #define FROM_BASE64(c) \ |
| 4183 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4184 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4185 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4186 | (c) == '+' ? 62 : 63) |
| 4187 | |
| 4188 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4189 | |
| 4190 | #define TO_BASE64(n) \ |
| 4191 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4192 | |
| 4193 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4194 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4195 | * byte not decoding to itself is the + which begins a base64 |
| 4196 | * string. */ |
| 4197 | |
| 4198 | #define DECODE_DIRECT(c) \ |
| 4199 | ((c) <= 127 && (c) != '+') |
| 4200 | |
| 4201 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4202 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4203 | * the above). See RFC2152. This array identifies these different |
| 4204 | * sets: |
| 4205 | * 0 : "Set D" |
| 4206 | * alphanumeric and '(),-./:? |
| 4207 | * 1 : "Set O" |
| 4208 | * !"#$%&*;<=>@[]^_`{|} |
| 4209 | * 2 : "whitespace" |
| 4210 | * ht nl cr sp |
| 4211 | * 3 : special (must be base64 encoded) |
| 4212 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4213 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4214 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4215 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4216 | char utf7_category[128] = { |
| 4217 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4218 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4219 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4220 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4221 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4222 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4223 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4224 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4225 | /* @ A B C D E F G H I J K L M N O */ |
| 4226 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4227 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4228 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4229 | /* ` a b c d e f g h i j k l m n o */ |
| 4230 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4231 | /* p q r s t u v w x y z { | } ~ del */ |
| 4232 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4233 | }; |
| 4234 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4235 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4236 | * answer depends on whether we are encoding set O as itself, and also |
| 4237 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4238 | * clear that the answers to these questions vary between |
| 4239 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4240 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4241 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4242 | ((c) < 128 && (c) > 0 && \ |
| 4243 | ((utf7_category[(c)] == 0) || \ |
| 4244 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4245 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4246 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4247 | PyObject * |
| 4248 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4249 | Py_ssize_t size, |
| 4250 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4251 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4252 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4253 | } |
| 4254 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4255 | /* The decoder. The only state we preserve is our read position, |
| 4256 | * i.e. how many characters we have consumed. So if we end in the |
| 4257 | * middle of a shift sequence we have to back off the read position |
| 4258 | * and the output to the beginning of the sequence, otherwise we lose |
| 4259 | * all the shift state (seen bits, number of bits seen, high |
| 4260 | * surrogate). */ |
| 4261 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4262 | PyObject * |
| 4263 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4264 | Py_ssize_t size, |
| 4265 | const char *errors, |
| 4266 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4267 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4268 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4269 | Py_ssize_t startinpos; |
| 4270 | Py_ssize_t endinpos; |
| 4271 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4272 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4273 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4274 | const char *errmsg = ""; |
| 4275 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4276 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4277 | unsigned int base64bits = 0; |
| 4278 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4279 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4280 | PyObject *errorHandler = NULL; |
| 4281 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4282 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4283 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4284 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4285 | if (!unicode) |
| 4286 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4287 | if (size == 0) { |
| 4288 | if (consumed) |
| 4289 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4290 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4291 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4292 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4293 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4294 | e = s + size; |
| 4295 | |
| 4296 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4297 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4298 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4299 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4300 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4301 | if (inShift) { /* in a base-64 section */ |
| 4302 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4303 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4304 | base64bits += 6; |
| 4305 | s++; |
| 4306 | if (base64bits >= 16) { |
| 4307 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4308 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4309 | base64bits -= 16; |
| 4310 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4311 | if (surrogate) { |
| 4312 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4313 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4314 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4315 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4316 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4317 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4318 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4319 | } |
| 4320 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4321 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4322 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4323 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4324 | } |
| 4325 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4326 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4327 | /* first surrogate */ |
| 4328 | surrogate = outCh; |
| 4329 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4330 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4331 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4332 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4337 | inShift = 0; |
| 4338 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4339 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4340 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4341 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4342 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4343 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4344 | if (base64bits > 0) { /* left-over bits */ |
| 4345 | if (base64bits >= 6) { |
| 4346 | /* We've seen at least one base-64 character */ |
| 4347 | errmsg = "partial character in shift sequence"; |
| 4348 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4349 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4350 | else { |
| 4351 | /* Some bits remain; they should be zero */ |
| 4352 | if (base64buffer != 0) { |
| 4353 | errmsg = "non-zero padding bits in shift sequence"; |
| 4354 | goto utf7Error; |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | if (ch != '-') { |
| 4359 | /* '-' is absorbed; other terminating |
| 4360 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4361 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4362 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4363 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4367 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4368 | s++; /* consume '+' */ |
| 4369 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4370 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4371 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4372 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4373 | } |
| 4374 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4375 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4376 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4377 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4378 | } |
| 4379 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4381 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4382 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4383 | s++; |
| 4384 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4385 | else { |
| 4386 | startinpos = s-starts; |
| 4387 | s++; |
| 4388 | errmsg = "unexpected special character"; |
| 4389 | goto utf7Error; |
| 4390 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4391 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4392 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4393 | endinpos = s-starts; |
| 4394 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4395 | errors, &errorHandler, |
| 4396 | "utf7", errmsg, |
| 4397 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4398 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4399 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4402 | /* end of string */ |
| 4403 | |
| 4404 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4405 | /* if we're in an inconsistent state, that's an error */ |
| 4406 | if (surrogate || |
| 4407 | (base64bits >= 6) || |
| 4408 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4409 | endinpos = size; |
| 4410 | if (unicode_decode_call_errorhandler( |
| 4411 | errors, &errorHandler, |
| 4412 | "utf7", "unterminated shift sequence", |
| 4413 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4414 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | goto onError; |
| 4416 | if (s < e) |
| 4417 | goto restart; |
| 4418 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4420 | |
| 4421 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4422 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4423 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4424 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4425 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4426 | } |
| 4427 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4428 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4429 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4430 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4431 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4432 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4433 | goto onError; |
| 4434 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | Py_XDECREF(errorHandler); |
| 4436 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4437 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4439 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4440 | Py_XDECREF(errorHandler); |
| 4441 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4442 | Py_DECREF(unicode); |
| 4443 | return NULL; |
| 4444 | } |
| 4445 | |
| 4446 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4447 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4448 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4449 | int base64SetO, |
| 4450 | int base64WhiteSpace, |
| 4451 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4452 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4453 | int kind; |
| 4454 | void *data; |
| 4455 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4456 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4457 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4458 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4459 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4460 | unsigned int base64bits = 0; |
| 4461 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4462 | char * out; |
| 4463 | char * start; |
| 4464 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4465 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4466 | return NULL; |
| 4467 | kind = PyUnicode_KIND(str); |
| 4468 | data = PyUnicode_DATA(str); |
| 4469 | len = PyUnicode_GET_LENGTH(str); |
| 4470 | |
| 4471 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4472 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4473 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4474 | /* It might be possible to tighten this worst case */ |
| 4475 | allocated = 8 * len; |
| 4476 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4477 | return PyErr_NoMemory(); |
| 4478 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4479 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4480 | if (v == NULL) |
| 4481 | return NULL; |
| 4482 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4483 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4484 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4485 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4486 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4487 | if (inShift) { |
| 4488 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4489 | /* shifting out */ |
| 4490 | if (base64bits) { /* output remaining bits */ |
| 4491 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4492 | base64buffer = 0; |
| 4493 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4494 | } |
| 4495 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4496 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4497 | so no '-' is required, except if the character is itself a '-' */ |
| 4498 | if (IS_BASE64(ch) || ch == '-') { |
| 4499 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4500 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4501 | *out++ = (char) ch; |
| 4502 | } |
| 4503 | else { |
| 4504 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4505 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4506 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4507 | else { /* not in a shift sequence */ |
| 4508 | if (ch == '+') { |
| 4509 | *out++ = '+'; |
| 4510 | *out++ = '-'; |
| 4511 | } |
| 4512 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4513 | *out++ = (char) ch; |
| 4514 | } |
| 4515 | else { |
| 4516 | *out++ = '+'; |
| 4517 | inShift = 1; |
| 4518 | goto encode_char; |
| 4519 | } |
| 4520 | } |
| 4521 | continue; |
| 4522 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4523 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4524 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4525 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4526 | /* code first surrogate */ |
| 4527 | base64bits += 16; |
| 4528 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4529 | while (base64bits >= 6) { |
| 4530 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4531 | base64bits -= 6; |
| 4532 | } |
| 4533 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4534 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4535 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4536 | base64bits += 16; |
| 4537 | base64buffer = (base64buffer << 16) | ch; |
| 4538 | while (base64bits >= 6) { |
| 4539 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4540 | base64bits -= 6; |
| 4541 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4542 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4543 | if (base64bits) |
| 4544 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4545 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4546 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4547 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4548 | return NULL; |
| 4549 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4550 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4551 | PyObject * |
| 4552 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4553 | Py_ssize_t size, |
| 4554 | int base64SetO, |
| 4555 | int base64WhiteSpace, |
| 4556 | const char *errors) |
| 4557 | { |
| 4558 | PyObject *result; |
| 4559 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4560 | if (tmp == NULL) |
| 4561 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4562 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4563 | base64WhiteSpace, errors); |
| 4564 | Py_DECREF(tmp); |
| 4565 | return result; |
| 4566 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4567 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4568 | #undef IS_BASE64 |
| 4569 | #undef FROM_BASE64 |
| 4570 | #undef TO_BASE64 |
| 4571 | #undef DECODE_DIRECT |
| 4572 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4574 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4575 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4576 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4577 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4578 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4579 | illegal prefix. See RFC 3629 for details */ |
| 4580 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4581 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4582 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4583 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4584 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4585 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4586 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4587 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4588 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4589 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4590 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4591 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4592 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4593 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4594 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4595 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4596 | }; |
| 4597 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4598 | PyObject * |
| 4599 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4600 | Py_ssize_t size, |
| 4601 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4603 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4604 | } |
| 4605 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4606 | #include "stringlib/ucs1lib.h" |
| 4607 | #include "stringlib/codecs.h" |
| 4608 | #include "stringlib/undef.h" |
| 4609 | |
| 4610 | #include "stringlib/ucs2lib.h" |
| 4611 | #include "stringlib/codecs.h" |
| 4612 | #include "stringlib/undef.h" |
| 4613 | |
| 4614 | #include "stringlib/ucs4lib.h" |
| 4615 | #include "stringlib/codecs.h" |
| 4616 | #include "stringlib/undef.h" |
| 4617 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4618 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4619 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4620 | |
| 4621 | /* Mask to quickly check whether a C 'long' contains a |
| 4622 | non-ASCII, UTF8-encoded char. */ |
| 4623 | #if (SIZEOF_LONG == 8) |
| 4624 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4625 | #elif (SIZEOF_LONG == 4) |
| 4626 | # define ASCII_CHAR_MASK 0x80808080L |
| 4627 | #else |
| 4628 | # error C 'long' size should be either 4 or 8! |
| 4629 | #endif |
| 4630 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4631 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4632 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4633 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4634 | This function doesn't check for errors, these checks are performed in |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4635 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4636 | */ |
| 4637 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4638 | utf8_scanner(const unsigned char *p, Py_ssize_t string_size, Py_ssize_t *unicode_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4639 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4640 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4641 | const unsigned char *end = p + string_size; |
| 4642 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4643 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4644 | assert(unicode_size != NULL); |
| 4645 | |
| 4646 | /* By having a cascade of independent loops which fallback onto each |
| 4647 | other, we minimize the amount of work done in the average loop |
| 4648 | iteration, and we also maximize the CPU's ability to predict |
| 4649 | branches correctly (because a given condition will have always the |
| 4650 | same boolean outcome except perhaps in the last iteration of the |
| 4651 | corresponding loop). |
| 4652 | In the general case this brings us rather close to decoding |
| 4653 | performance pre-PEP 393, despite the two-pass decoding. |
| 4654 | |
| 4655 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4656 | character has been encountered. It is actually a pessimization (by |
| 4657 | a significant factor) to use this loop on text with many non-ASCII |
| 4658 | characters, and it is important to avoid bad performance on valid |
| 4659 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4660 | */ |
| 4661 | |
| 4662 | /* ASCII */ |
| 4663 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4664 | /* Only check value if it's not a ASCII char... */ |
| 4665 | if (*p < 0x80) { |
| 4666 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4667 | an explanation. */ |
| 4668 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4669 | /* Help register allocation */ |
| 4670 | register const unsigned char *_p = p; |
| 4671 | while (_p < aligned_end) { |
| 4672 | unsigned long value = *(unsigned long *) _p; |
| 4673 | if (value & ASCII_CHAR_MASK) |
| 4674 | break; |
| 4675 | _p += SIZEOF_LONG; |
| 4676 | char_count += SIZEOF_LONG; |
| 4677 | } |
| 4678 | p = _p; |
| 4679 | if (p == end) |
| 4680 | break; |
| 4681 | } |
| 4682 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4683 | if (*p < 0x80) |
| 4684 | ++char_count; |
| 4685 | else |
| 4686 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4687 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4688 | *unicode_size = char_count; |
| 4689 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4690 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4691 | _ucs1loop: |
| 4692 | for (; p < end; ++p) { |
| 4693 | if (*p < 0xc4) |
| 4694 | char_count += ((*p & 0xc0) != 0x80); |
| 4695 | else |
| 4696 | goto _ucs2loop; |
| 4697 | } |
| 4698 | *unicode_size = char_count; |
| 4699 | return 255; |
| 4700 | |
| 4701 | _ucs2loop: |
| 4702 | for (; p < end; ++p) { |
| 4703 | if (*p < 0xf0) |
| 4704 | char_count += ((*p & 0xc0) != 0x80); |
| 4705 | else |
| 4706 | goto _ucs4loop; |
| 4707 | } |
| 4708 | *unicode_size = char_count; |
| 4709 | return 65535; |
| 4710 | |
| 4711 | _ucs4loop: |
| 4712 | for (; p < end; ++p) { |
| 4713 | char_count += ((*p & 0xc0) != 0x80); |
| 4714 | } |
| 4715 | *unicode_size = char_count; |
| 4716 | return 65537; |
| 4717 | } |
| 4718 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4719 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4720 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4721 | Potential resizing overallocates, so the result needs to shrink at the end. |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4722 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4723 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4724 | do { \ |
| 4725 | Py_ssize_t pos = index; \ |
| 4726 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4727 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4728 | goto onError; \ |
| 4729 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4730 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4731 | } while (0) |
| 4732 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4733 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4734 | decode_utf8_errors(const char *starts, |
| 4735 | Py_ssize_t size, |
| 4736 | const char *errors, |
| 4737 | Py_ssize_t *consumed, |
| 4738 | const char *s, |
| 4739 | PyObject *unicode, |
| 4740 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4741 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4743 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4744 | Py_ssize_t startinpos; |
| 4745 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4746 | const char *e = starts + size; |
| 4747 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4748 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4749 | PyObject *errorHandler = NULL; |
| 4750 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4751 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4752 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4753 | |
| 4754 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4755 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4756 | |
| 4757 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4758 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4759 | input will consist of an overwhelming majority of ASCII |
| 4760 | characters, we try to optimize for this case by checking |
| 4761 | as many characters as a C 'long' can contain. |
| 4762 | First, check if we can do an aligned read, as most CPUs have |
| 4763 | a penalty for unaligned reads. |
| 4764 | */ |
| 4765 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4766 | /* Help register allocation */ |
| 4767 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4768 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4769 | while (_s < aligned_end) { |
| 4770 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4771 | and do a fast unrolled copy if it only contains ASCII |
| 4772 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4773 | unsigned long value = *(unsigned long *) _s; |
| 4774 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4775 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4776 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4777 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4778 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4779 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4780 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4781 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4782 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4783 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4784 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4785 | #endif |
| 4786 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4787 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4788 | } |
| 4789 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4790 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4791 | if (s == e) |
| 4792 | break; |
| 4793 | ch = (unsigned char)*s; |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4798 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4799 | s++; |
| 4800 | continue; |
| 4801 | } |
| 4802 | |
| 4803 | n = utf8_code_length[ch]; |
| 4804 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4805 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4806 | if (consumed) |
| 4807 | break; |
| 4808 | else { |
| 4809 | errmsg = "unexpected end of data"; |
| 4810 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4811 | endinpos = startinpos+1; |
| 4812 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4813 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4814 | goto utf8Error; |
| 4815 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4816 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | |
| 4818 | switch (n) { |
| 4819 | |
| 4820 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4821 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4822 | startinpos = s-starts; |
| 4823 | endinpos = startinpos+1; |
| 4824 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | |
| 4826 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4827 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4828 | startinpos = s-starts; |
| 4829 | endinpos = startinpos+1; |
| 4830 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4831 | |
| 4832 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4833 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4834 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4835 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4836 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4837 | goto utf8Error; |
| 4838 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4839 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4840 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4841 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4842 | break; |
| 4843 | |
| 4844 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4845 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4846 | will result in surrogates in range d800-dfff. Surrogates are |
| 4847 | not valid UTF-8 so they are rejected. |
| 4848 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4849 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4850 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4851 | (s[2] & 0xc0) != 0x80 || |
| 4852 | ((unsigned char)s[0] == 0xE0 && |
| 4853 | (unsigned char)s[1] < 0xA0) || |
| 4854 | ((unsigned char)s[0] == 0xED && |
| 4855 | (unsigned char)s[1] > 0x9F)) { |
| 4856 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4857 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4858 | endinpos = startinpos + 1; |
| 4859 | |
| 4860 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4861 | continuation byte is s[2], so increment endinpos by 1, |
| 4862 | if not, s[1] is invalid and endinpos doesn't need to |
| 4863 | be incremented. */ |
| 4864 | if ((s[1] & 0xC0) == 0x80) |
| 4865 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4866 | goto utf8Error; |
| 4867 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4868 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4869 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4870 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4871 | break; |
| 4872 | |
| 4873 | case 4: |
| 4874 | if ((s[1] & 0xc0) != 0x80 || |
| 4875 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4876 | (s[3] & 0xc0) != 0x80 || |
| 4877 | ((unsigned char)s[0] == 0xF0 && |
| 4878 | (unsigned char)s[1] < 0x90) || |
| 4879 | ((unsigned char)s[0] == 0xF4 && |
| 4880 | (unsigned char)s[1] > 0x8F)) { |
| 4881 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4882 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4883 | endinpos = startinpos + 1; |
| 4884 | if ((s[1] & 0xC0) == 0x80) { |
| 4885 | endinpos++; |
| 4886 | if ((s[2] & 0xC0) == 0x80) |
| 4887 | endinpos++; |
| 4888 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4889 | goto utf8Error; |
| 4890 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4891 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4892 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4893 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4894 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4895 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4896 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4897 | } |
| 4898 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4899 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4901 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4902 | if (unicode_decode_call_errorhandler( |
| 4903 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4904 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4905 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4906 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4908 | /* Update data because unicode_decode_call_errorhandler might have |
| 4909 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4910 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4911 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4912 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4913 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4915 | /* Adjust length and ready string when it contained errors and |
| 4916 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4917 | if (unicode_resize(&unicode, i) < 0) |
| 4918 | goto onError; |
| 4919 | unicode_adjust_maxchar(&unicode); |
| 4920 | if (unicode == NULL) |
| 4921 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4922 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4923 | Py_XDECREF(errorHandler); |
| 4924 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4925 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4926 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4928 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4929 | Py_XDECREF(errorHandler); |
| 4930 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4931 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4932 | return NULL; |
| 4933 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4934 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4935 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4936 | PyObject * |
| 4937 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4938 | Py_ssize_t size, |
| 4939 | const char *errors, |
| 4940 | Py_ssize_t *consumed) |
| 4941 | { |
| 4942 | Py_UCS4 maxchar = 0; |
| 4943 | Py_ssize_t unicode_size; |
| 4944 | int has_errors = 0; |
| 4945 | PyObject *unicode; |
| 4946 | int kind; |
| 4947 | void *data; |
| 4948 | const char *starts = s; |
| 4949 | const char *e; |
| 4950 | Py_ssize_t i; |
| 4951 | |
| 4952 | if (size == 0) { |
| 4953 | if (consumed) |
| 4954 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4955 | Py_INCREF(unicode_empty); |
| 4956 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4957 | } |
| 4958 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4959 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4960 | |
| 4961 | /* When the string is ASCII only, just use memcpy and return. |
| 4962 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4963 | sequence at the end of the ASCII block. */ |
| 4964 | if (maxchar < 128 && size == unicode_size) { |
| 4965 | if (consumed) |
| 4966 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 4967 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4971 | if (!unicode) |
| 4972 | return NULL; |
| 4973 | kind = PyUnicode_KIND(unicode); |
| 4974 | data = PyUnicode_DATA(unicode); |
| 4975 | |
| 4976 | /* Unpack UTF-8 encoded data */ |
| 4977 | i = 0; |
| 4978 | e = starts + size; |
| 4979 | switch (kind) { |
| 4980 | case PyUnicode_1BYTE_KIND: |
| 4981 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4982 | break; |
| 4983 | case PyUnicode_2BYTE_KIND: |
| 4984 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4985 | break; |
| 4986 | case PyUnicode_4BYTE_KIND: |
| 4987 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4988 | break; |
| 4989 | } |
| 4990 | if (!has_errors) { |
| 4991 | /* Ensure the unicode size calculation was correct */ |
| 4992 | assert(i == unicode_size); |
| 4993 | assert(s == e); |
| 4994 | if (consumed) |
| 4995 | *consumed = size; |
| 4996 | return unicode; |
| 4997 | } |
| 4998 | |
| 4999 | /* In case of errors, maxchar and size computation might be incorrect; |
| 5000 | code below refits and resizes as necessary. */ |
| 5001 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 5002 | } |
| 5003 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5004 | #ifdef __APPLE__ |
| 5005 | |
| 5006 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 5007 | used to decode the command line arguments on Mac OS X. */ |
| 5008 | |
| 5009 | wchar_t* |
| 5010 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 5011 | { |
| 5012 | int n; |
| 5013 | const char *e; |
| 5014 | wchar_t *unicode, *p; |
| 5015 | |
| 5016 | /* Note: size will always be longer than the resulting Unicode |
| 5017 | character count */ |
| 5018 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 5019 | PyErr_NoMemory(); |
| 5020 | return NULL; |
| 5021 | } |
| 5022 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 5023 | if (!unicode) |
| 5024 | return NULL; |
| 5025 | |
| 5026 | /* Unpack UTF-8 encoded data */ |
| 5027 | p = unicode; |
| 5028 | e = s + size; |
| 5029 | while (s < e) { |
| 5030 | Py_UCS4 ch = (unsigned char)*s; |
| 5031 | |
| 5032 | if (ch < 0x80) { |
| 5033 | *p++ = (wchar_t)ch; |
| 5034 | s++; |
| 5035 | continue; |
| 5036 | } |
| 5037 | |
| 5038 | n = utf8_code_length[ch]; |
| 5039 | if (s + n > e) { |
| 5040 | goto surrogateescape; |
| 5041 | } |
| 5042 | |
| 5043 | switch (n) { |
| 5044 | case 0: |
| 5045 | case 1: |
| 5046 | goto surrogateescape; |
| 5047 | |
| 5048 | case 2: |
| 5049 | if ((s[1] & 0xc0) != 0x80) |
| 5050 | goto surrogateescape; |
| 5051 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 5052 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 5053 | *p++ = (wchar_t)ch; |
| 5054 | break; |
| 5055 | |
| 5056 | case 3: |
| 5057 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 5058 | will result in surrogates in range d800-dfff. Surrogates are |
| 5059 | not valid UTF-8 so they are rejected. |
| 5060 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 5061 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 5062 | if ((s[1] & 0xc0) != 0x80 || |
| 5063 | (s[2] & 0xc0) != 0x80 || |
| 5064 | ((unsigned char)s[0] == 0xE0 && |
| 5065 | (unsigned char)s[1] < 0xA0) || |
| 5066 | ((unsigned char)s[0] == 0xED && |
| 5067 | (unsigned char)s[1] > 0x9F)) { |
| 5068 | |
| 5069 | goto surrogateescape; |
| 5070 | } |
| 5071 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 5072 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5073 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5074 | break; |
| 5075 | |
| 5076 | case 4: |
| 5077 | if ((s[1] & 0xc0) != 0x80 || |
| 5078 | (s[2] & 0xc0) != 0x80 || |
| 5079 | (s[3] & 0xc0) != 0x80 || |
| 5080 | ((unsigned char)s[0] == 0xF0 && |
| 5081 | (unsigned char)s[1] < 0x90) || |
| 5082 | ((unsigned char)s[0] == 0xF4 && |
| 5083 | (unsigned char)s[1] > 0x8F)) { |
| 5084 | goto surrogateescape; |
| 5085 | } |
| 5086 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 5087 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5088 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5089 | |
| 5090 | #if SIZEOF_WCHAR_T == 4 |
| 5091 | *p++ = (wchar_t)ch; |
| 5092 | #else |
| 5093 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5094 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5095 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5096 | #endif |
| 5097 | break; |
| 5098 | } |
| 5099 | s += n; |
| 5100 | continue; |
| 5101 | |
| 5102 | surrogateescape: |
| 5103 | *p++ = 0xDC00 + ch; |
| 5104 | s++; |
| 5105 | } |
| 5106 | *p = L'\0'; |
| 5107 | return unicode; |
| 5108 | } |
| 5109 | |
| 5110 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5111 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5112 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5113 | |
| 5114 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5115 | and allocate exactly as much space needed at the end. Else allocate the |
| 5116 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5117 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5118 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5119 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5120 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5121 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5122 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5123 | void *data; |
| 5124 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5126 | if (!PyUnicode_Check(unicode)) { |
| 5127 | PyErr_BadArgument(); |
| 5128 | return NULL; |
| 5129 | } |
| 5130 | |
| 5131 | if (PyUnicode_READY(unicode) == -1) |
| 5132 | return NULL; |
| 5133 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5134 | if (PyUnicode_UTF8(unicode)) |
| 5135 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5136 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5137 | |
| 5138 | kind = PyUnicode_KIND(unicode); |
| 5139 | data = PyUnicode_DATA(unicode); |
| 5140 | size = PyUnicode_GET_LENGTH(unicode); |
| 5141 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5142 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5143 | default: |
| 5144 | assert(0); |
| 5145 | case PyUnicode_1BYTE_KIND: |
| 5146 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5147 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5148 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5149 | case PyUnicode_2BYTE_KIND: |
| 5150 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5151 | case PyUnicode_4BYTE_KIND: |
| 5152 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5153 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5156 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5157 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5158 | Py_ssize_t size, |
| 5159 | const char *errors) |
| 5160 | { |
| 5161 | PyObject *v, *unicode; |
| 5162 | |
| 5163 | unicode = PyUnicode_FromUnicode(s, size); |
| 5164 | if (unicode == NULL) |
| 5165 | return NULL; |
| 5166 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5167 | Py_DECREF(unicode); |
| 5168 | return v; |
| 5169 | } |
| 5170 | |
| 5171 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5172 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5173 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5174 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5177 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5178 | |
| 5179 | PyObject * |
| 5180 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | Py_ssize_t size, |
| 5182 | const char *errors, |
| 5183 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5184 | { |
| 5185 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5186 | } |
| 5187 | |
| 5188 | PyObject * |
| 5189 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5190 | Py_ssize_t size, |
| 5191 | const char *errors, |
| 5192 | int *byteorder, |
| 5193 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5194 | { |
| 5195 | const char *starts = s; |
| 5196 | Py_ssize_t startinpos; |
| 5197 | Py_ssize_t endinpos; |
| 5198 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5199 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5200 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5201 | int bo = 0; /* assume native ordering by default */ |
| 5202 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5203 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5204 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5205 | int iorder[] = {0, 1, 2, 3}; |
| 5206 | #else |
| 5207 | int iorder[] = {3, 2, 1, 0}; |
| 5208 | #endif |
| 5209 | PyObject *errorHandler = NULL; |
| 5210 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5211 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5212 | q = (unsigned char *)s; |
| 5213 | e = q + size; |
| 5214 | |
| 5215 | if (byteorder) |
| 5216 | bo = *byteorder; |
| 5217 | |
| 5218 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5219 | byte order setting accordingly. In native mode, the leading BOM |
| 5220 | mark is skipped, in all other modes, it is copied to the output |
| 5221 | stream as-is (giving a ZWNBSP character). */ |
| 5222 | if (bo == 0) { |
| 5223 | if (size >= 4) { |
| 5224 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5225 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5226 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5227 | if (bom == 0x0000FEFF) { |
| 5228 | q += 4; |
| 5229 | bo = -1; |
| 5230 | } |
| 5231 | else if (bom == 0xFFFE0000) { |
| 5232 | q += 4; |
| 5233 | bo = 1; |
| 5234 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5235 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | if (bom == 0x0000FEFF) { |
| 5237 | q += 4; |
| 5238 | bo = 1; |
| 5239 | } |
| 5240 | else if (bom == 0xFFFE0000) { |
| 5241 | q += 4; |
| 5242 | bo = -1; |
| 5243 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5244 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5245 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | if (bo == -1) { |
| 5249 | /* force LE */ |
| 5250 | iorder[0] = 0; |
| 5251 | iorder[1] = 1; |
| 5252 | iorder[2] = 2; |
| 5253 | iorder[3] = 3; |
| 5254 | } |
| 5255 | else if (bo == 1) { |
| 5256 | /* force BE */ |
| 5257 | iorder[0] = 3; |
| 5258 | iorder[1] = 2; |
| 5259 | iorder[2] = 1; |
| 5260 | iorder[3] = 0; |
| 5261 | } |
| 5262 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5263 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5264 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5265 | if (!unicode) |
| 5266 | return NULL; |
| 5267 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5268 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5269 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5270 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5271 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5272 | Py_UCS4 ch; |
| 5273 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5274 | if (e-q<4) { |
| 5275 | if (consumed) |
| 5276 | break; |
| 5277 | errmsg = "truncated data"; |
| 5278 | startinpos = ((const char *)q)-starts; |
| 5279 | endinpos = ((const char *)e)-starts; |
| 5280 | goto utf32Error; |
| 5281 | /* The remaining input chars are ignored if the callback |
| 5282 | chooses to skip the input */ |
| 5283 | } |
| 5284 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5285 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5286 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5287 | if (ch >= 0x110000) |
| 5288 | { |
| 5289 | errmsg = "codepoint not in range(0x110000)"; |
| 5290 | startinpos = ((const char *)q)-starts; |
| 5291 | endinpos = startinpos+4; |
| 5292 | goto utf32Error; |
| 5293 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5294 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5295 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5296 | q += 4; |
| 5297 | continue; |
| 5298 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5299 | if (unicode_decode_call_errorhandler( |
| 5300 | errors, &errorHandler, |
| 5301 | "utf32", errmsg, |
| 5302 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5303 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5304 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | if (byteorder) |
| 5308 | *byteorder = bo; |
| 5309 | |
| 5310 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5312 | |
| 5313 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5314 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5315 | goto onError; |
| 5316 | |
| 5317 | Py_XDECREF(errorHandler); |
| 5318 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5319 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5320 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5321 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5322 | Py_DECREF(unicode); |
| 5323 | Py_XDECREF(errorHandler); |
| 5324 | Py_XDECREF(exc); |
| 5325 | return NULL; |
| 5326 | } |
| 5327 | |
| 5328 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5329 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5330 | const char *errors, |
| 5331 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5332 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5333 | int kind; |
| 5334 | void *data; |
| 5335 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5336 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5337 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5338 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5339 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5340 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5341 | int iorder[] = {0, 1, 2, 3}; |
| 5342 | #else |
| 5343 | int iorder[] = {3, 2, 1, 0}; |
| 5344 | #endif |
| 5345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5346 | #define STORECHAR(CH) \ |
| 5347 | do { \ |
| 5348 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5349 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5350 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5351 | p[iorder[0]] = (CH) & 0xff; \ |
| 5352 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5353 | } while(0) |
| 5354 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5355 | if (!PyUnicode_Check(str)) { |
| 5356 | PyErr_BadArgument(); |
| 5357 | return NULL; |
| 5358 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5359 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5360 | return NULL; |
| 5361 | kind = PyUnicode_KIND(str); |
| 5362 | data = PyUnicode_DATA(str); |
| 5363 | len = PyUnicode_GET_LENGTH(str); |
| 5364 | |
| 5365 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5366 | bytesize = nsize * 4; |
| 5367 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5368 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5369 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5370 | if (v == NULL) |
| 5371 | return NULL; |
| 5372 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5373 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5374 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5375 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5376 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5377 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5378 | |
| 5379 | if (byteorder == -1) { |
| 5380 | /* force LE */ |
| 5381 | iorder[0] = 0; |
| 5382 | iorder[1] = 1; |
| 5383 | iorder[2] = 2; |
| 5384 | iorder[3] = 3; |
| 5385 | } |
| 5386 | else if (byteorder == 1) { |
| 5387 | /* force BE */ |
| 5388 | iorder[0] = 3; |
| 5389 | iorder[1] = 2; |
| 5390 | iorder[2] = 1; |
| 5391 | iorder[3] = 0; |
| 5392 | } |
| 5393 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5394 | for (i = 0; i < len; i++) |
| 5395 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5396 | |
| 5397 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5398 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5399 | #undef STORECHAR |
| 5400 | } |
| 5401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5402 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5403 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5404 | Py_ssize_t size, |
| 5405 | const char *errors, |
| 5406 | int byteorder) |
| 5407 | { |
| 5408 | PyObject *result; |
| 5409 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5410 | if (tmp == NULL) |
| 5411 | return NULL; |
| 5412 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5413 | Py_DECREF(tmp); |
| 5414 | return result; |
| 5415 | } |
| 5416 | |
| 5417 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5418 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5419 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5420 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5424 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5425 | PyObject * |
| 5426 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5427 | Py_ssize_t size, |
| 5428 | const char *errors, |
| 5429 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5431 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5432 | } |
| 5433 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5434 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5435 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5436 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5437 | rare in most input. |
| 5438 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5439 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5440 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5441 | #if (SIZEOF_LONG == 8) |
| 5442 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5443 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5444 | # define STRIPPED_MASK 0x00FF00FF00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5445 | #elif (SIZEOF_LONG == 4) |
| 5446 | # define FAST_CHAR_MASK 0x80008000L |
| 5447 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5448 | # define STRIPPED_MASK 0x00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5449 | #else |
| 5450 | # error C 'long' size should be either 4 or 8! |
| 5451 | #endif |
| 5452 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5453 | PyObject * |
| 5454 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5455 | Py_ssize_t size, |
| 5456 | const char *errors, |
| 5457 | int *byteorder, |
| 5458 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5459 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5460 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5461 | Py_ssize_t startinpos; |
| 5462 | Py_ssize_t endinpos; |
| 5463 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5464 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5465 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5466 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5467 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5468 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5469 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5470 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5471 | int ihi = 1, ilo = 0; |
| 5472 | #else |
| 5473 | int ihi = 0, ilo = 1; |
| 5474 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5475 | PyObject *errorHandler = NULL; |
| 5476 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 | |
| 5478 | /* Note: size will always be longer than the resulting Unicode |
| 5479 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5480 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 | if (!unicode) |
| 5482 | return NULL; |
| 5483 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5484 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5485 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5486 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5487 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5488 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | |
| 5490 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5491 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5493 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5494 | byte order setting accordingly. In native mode, the leading BOM |
| 5495 | mark is skipped, in all other modes, it is copied to the output |
| 5496 | stream as-is (giving a ZWNBSP character). */ |
| 5497 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5498 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5499 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5500 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | if (bom == 0xFEFF) { |
| 5502 | q += 2; |
| 5503 | bo = -1; |
| 5504 | } |
| 5505 | else if (bom == 0xFFFE) { |
| 5506 | q += 2; |
| 5507 | bo = 1; |
| 5508 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5509 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5510 | if (bom == 0xFEFF) { |
| 5511 | q += 2; |
| 5512 | bo = 1; |
| 5513 | } |
| 5514 | else if (bom == 0xFFFE) { |
| 5515 | q += 2; |
| 5516 | bo = -1; |
| 5517 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5518 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5519 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5520 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5522 | if (bo == -1) { |
| 5523 | /* force LE */ |
| 5524 | ihi = 1; |
| 5525 | ilo = 0; |
| 5526 | } |
| 5527 | else if (bo == 1) { |
| 5528 | /* force BE */ |
| 5529 | ihi = 0; |
| 5530 | ilo = 1; |
| 5531 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5532 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5533 | native_ordering = ilo < ihi; |
| 5534 | #else |
| 5535 | native_ordering = ilo > ihi; |
| 5536 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5537 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5538 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5539 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5540 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5541 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5542 | reads are more expensive, better to defer to another iteration. */ |
| 5543 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5544 | /* Fast path for runs of non-surrogate chars. */ |
| 5545 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5546 | int kind = PyUnicode_KIND(unicode); |
| 5547 | void *data = PyUnicode_DATA(unicode); |
| 5548 | while (_q < aligned_end) { |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5549 | unsigned long block = * (unsigned long *) _q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5550 | Py_UCS4 maxch; |
| 5551 | if (native_ordering) { |
| 5552 | /* Can use buffer directly */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5553 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5554 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5555 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5556 | else { |
| 5557 | /* Need to byte-swap */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5558 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5559 | break; |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5560 | block = ((block >> 8) & STRIPPED_MASK) | |
| 5561 | ((block & STRIPPED_MASK) << 8); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5562 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5563 | maxch = (Py_UCS2)(block & 0xFFFF); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5564 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5565 | ch = (Py_UCS2)((block >> 16) & 0xFFFF); |
| 5566 | maxch = Py_MAX(maxch, ch); |
| 5567 | ch = (Py_UCS2)((block >> 32) & 0xFFFF); |
| 5568 | maxch = Py_MAX(maxch, ch); |
| 5569 | ch = (Py_UCS2)(block >> 48); |
| 5570 | maxch = Py_MAX(maxch, ch); |
| 5571 | #else |
| 5572 | ch = (Py_UCS2)(block >> 16); |
| 5573 | maxch = Py_MAX(maxch, ch); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5574 | #endif |
| 5575 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5576 | if (unicode_widen(&unicode, maxch) < 0) |
| 5577 | goto onError; |
| 5578 | kind = PyUnicode_KIND(unicode); |
| 5579 | data = PyUnicode_DATA(unicode); |
| 5580 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5581 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5582 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5583 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5584 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5585 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5586 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5587 | #else |
| 5588 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5589 | #endif |
| 5590 | #else |
| 5591 | #if SIZEOF_LONG == 8 |
| 5592 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5593 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5594 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5595 | #else |
| 5596 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5597 | #endif |
| 5598 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5599 | #endif |
| 5600 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5601 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5602 | q = _q; |
| 5603 | if (q >= e) |
| 5604 | break; |
| 5605 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5606 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5607 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5608 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5609 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5610 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5611 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5612 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5613 | continue; |
| 5614 | } |
| 5615 | |
| 5616 | /* UTF-16 code pair: */ |
| 5617 | if (q > e) { |
| 5618 | errmsg = "unexpected end of data"; |
| 5619 | startinpos = (((const char *)q) - 2) - starts; |
| 5620 | endinpos = ((const char *)e) + 1 - starts; |
| 5621 | goto utf16Error; |
| 5622 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5623 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5624 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5625 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5626 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5627 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5628 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5629 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5630 | continue; |
| 5631 | } |
| 5632 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5633 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5634 | startinpos = (((const char *)q)-4)-starts; |
| 5635 | endinpos = startinpos+2; |
| 5636 | goto utf16Error; |
| 5637 | } |
| 5638 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5639 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5640 | errmsg = "illegal encoding"; |
| 5641 | startinpos = (((const char *)q)-2)-starts; |
| 5642 | endinpos = startinpos+2; |
| 5643 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5644 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5645 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5646 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5647 | errors, |
| 5648 | &errorHandler, |
| 5649 | "utf16", errmsg, |
| 5650 | &starts, |
| 5651 | (const char **)&e, |
| 5652 | &startinpos, |
| 5653 | &endinpos, |
| 5654 | &exc, |
| 5655 | (const char **)&q, |
| 5656 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5657 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5658 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5660 | /* remaining byte at the end? (size should be even) */ |
| 5661 | if (e == q) { |
| 5662 | if (!consumed) { |
| 5663 | errmsg = "truncated data"; |
| 5664 | startinpos = ((const char *)q) - starts; |
| 5665 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5666 | if (unicode_decode_call_errorhandler( |
| 5667 | errors, |
| 5668 | &errorHandler, |
| 5669 | "utf16", errmsg, |
| 5670 | &starts, |
| 5671 | (const char **)&e, |
| 5672 | &startinpos, |
| 5673 | &endinpos, |
| 5674 | &exc, |
| 5675 | (const char **)&q, |
| 5676 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5677 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5678 | goto onError; |
| 5679 | /* The remaining input chars are ignored if the callback |
| 5680 | chooses to skip the input */ |
| 5681 | } |
| 5682 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | |
| 5684 | if (byteorder) |
| 5685 | *byteorder = bo; |
| 5686 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5687 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5688 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5691 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | goto onError; |
| 5693 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5694 | Py_XDECREF(errorHandler); |
| 5695 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5696 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5698 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5700 | Py_XDECREF(errorHandler); |
| 5701 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | return NULL; |
| 5703 | } |
| 5704 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5705 | #undef FAST_CHAR_MASK |
| 5706 | #undef SWAPPED_FAST_CHAR_MASK |
| 5707 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5708 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5709 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5710 | const char *errors, |
| 5711 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5713 | int kind; |
| 5714 | void *data; |
| 5715 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5716 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5717 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5718 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5719 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5720 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5721 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5722 | int ihi = 1, ilo = 0; |
| 5723 | #else |
| 5724 | int ihi = 0, ilo = 1; |
| 5725 | #endif |
| 5726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5727 | #define STORECHAR(CH) \ |
| 5728 | do { \ |
| 5729 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5730 | p[ilo] = (CH) & 0xff; \ |
| 5731 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5732 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5734 | if (!PyUnicode_Check(str)) { |
| 5735 | PyErr_BadArgument(); |
| 5736 | return NULL; |
| 5737 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5738 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5739 | return NULL; |
| 5740 | kind = PyUnicode_KIND(str); |
| 5741 | data = PyUnicode_DATA(str); |
| 5742 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5743 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5744 | pairs = 0; |
| 5745 | if (kind == PyUnicode_4BYTE_KIND) |
| 5746 | for (i = 0; i < len; i++) |
| 5747 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5748 | pairs++; |
| 5749 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5750 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5751 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5752 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5753 | bytesize = nsize * 2; |
| 5754 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5756 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | if (v == NULL) |
| 5758 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5760 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5762 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5763 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5764 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5765 | |
| 5766 | if (byteorder == -1) { |
| 5767 | /* force LE */ |
| 5768 | ihi = 1; |
| 5769 | ilo = 0; |
| 5770 | } |
| 5771 | else if (byteorder == 1) { |
| 5772 | /* force BE */ |
| 5773 | ihi = 0; |
| 5774 | ilo = 1; |
| 5775 | } |
| 5776 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5777 | for (i = 0; i < len; i++) { |
| 5778 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5779 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5780 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5781 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5782 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5783 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5784 | STORECHAR(ch); |
| 5785 | if (ch2) |
| 5786 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5787 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5788 | |
| 5789 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5790 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5791 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5794 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5795 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5796 | Py_ssize_t size, |
| 5797 | const char *errors, |
| 5798 | int byteorder) |
| 5799 | { |
| 5800 | PyObject *result; |
| 5801 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5802 | if (tmp == NULL) |
| 5803 | return NULL; |
| 5804 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5805 | Py_DECREF(tmp); |
| 5806 | return result; |
| 5807 | } |
| 5808 | |
| 5809 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5810 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5812 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5816 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5817 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5818 | if all the escapes in the string make it still a valid ASCII string. |
| 5819 | Returns -1 if any escapes were found which cause the string to |
| 5820 | pop out of ASCII range. Otherwise returns the length of the |
| 5821 | required buffer to hold the string. |
| 5822 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5823 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5824 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5825 | { |
| 5826 | const unsigned char *p = (const unsigned char *)s; |
| 5827 | const unsigned char *end = p + size; |
| 5828 | Py_ssize_t length = 0; |
| 5829 | |
| 5830 | if (size < 0) |
| 5831 | return -1; |
| 5832 | |
| 5833 | for (; p < end; ++p) { |
| 5834 | if (*p > 127) { |
| 5835 | /* Non-ASCII */ |
| 5836 | return -1; |
| 5837 | } |
| 5838 | else if (*p != '\\') { |
| 5839 | /* Normal character */ |
| 5840 | ++length; |
| 5841 | } |
| 5842 | else { |
| 5843 | /* Backslash-escape, check next char */ |
| 5844 | ++p; |
| 5845 | /* Escape sequence reaches till end of string or |
| 5846 | non-ASCII follow-up. */ |
| 5847 | if (p >= end || *p > 127) |
| 5848 | return -1; |
| 5849 | switch (*p) { |
| 5850 | case '\n': |
| 5851 | /* backslash + \n result in zero characters */ |
| 5852 | break; |
| 5853 | case '\\': case '\'': case '\"': |
| 5854 | case 'b': case 'f': case 't': |
| 5855 | case 'n': case 'r': case 'v': case 'a': |
| 5856 | ++length; |
| 5857 | break; |
| 5858 | case '0': case '1': case '2': case '3': |
| 5859 | case '4': case '5': case '6': case '7': |
| 5860 | case 'x': case 'u': case 'U': case 'N': |
| 5861 | /* these do not guarantee ASCII characters */ |
| 5862 | return -1; |
| 5863 | default: |
| 5864 | /* count the backslash + the other character */ |
| 5865 | length += 2; |
| 5866 | } |
| 5867 | } |
| 5868 | } |
| 5869 | return length; |
| 5870 | } |
| 5871 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5872 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5874 | PyObject * |
| 5875 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5876 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5877 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5878 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5880 | Py_ssize_t startinpos; |
| 5881 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5882 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5883 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5885 | char* message; |
| 5886 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5887 | PyObject *errorHandler = NULL; |
| 5888 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5889 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5890 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5891 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5892 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5893 | |
| 5894 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5895 | either the string is pure ASCII with named escapes like \n, etc. |
| 5896 | and we determined it's exact size (common case) |
| 5897 | or it contains \x, \u, ... escape sequences. then we create a |
| 5898 | legacy wchar string and resize it at the end of this function. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5899 | if (len >= 0) { |
| 5900 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5901 | if (!v) |
| 5902 | goto onError; |
| 5903 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5904 | } |
| 5905 | else { |
| 5906 | /* Escaped strings will always be longer than the resulting |
| 5907 | Unicode string, so we start with size here and then reduce the |
| 5908 | length after conversion to the true value. |
| 5909 | (but if the error callback returns a long replacement string |
| 5910 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5911 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5912 | if (!v) |
| 5913 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5914 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5915 | } |
| 5916 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5918 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5919 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | while (s < end) { |
| 5923 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5924 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5925 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5927 | /* The only case in which i == ascii_length is a backslash |
| 5928 | followed by a newline. */ |
| 5929 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5932 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5933 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5934 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | continue; |
| 5936 | } |
| 5937 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5938 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | /* \ - Escapes */ |
| 5940 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5941 | c = *s++; |
| 5942 | if (s > end) |
| 5943 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5944 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5945 | /* The only case in which i == ascii_length is a backslash |
| 5946 | followed by a newline. */ |
| 5947 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5948 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5949 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5952 | #define WRITECHAR(ch) \ |
| 5953 | do { \ |
| 5954 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5955 | goto onError; \ |
| 5956 | }while(0) |
| 5957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5959 | case '\\': WRITECHAR('\\'); break; |
| 5960 | case '\'': WRITECHAR('\''); break; |
| 5961 | case '\"': WRITECHAR('\"'); break; |
| 5962 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5963 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5964 | case 'f': WRITECHAR('\014'); break; |
| 5965 | case 't': WRITECHAR('\t'); break; |
| 5966 | case 'n': WRITECHAR('\n'); break; |
| 5967 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5968 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5969 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5970 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5971 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5973 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | case '0': case '1': case '2': case '3': |
| 5975 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5976 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5977 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5978 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5979 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5980 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5982 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | break; |
| 5984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | /* hex escapes */ |
| 5986 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5988 | digits = 2; |
| 5989 | message = "truncated \\xXX escape"; |
| 5990 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5992 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5994 | digits = 4; |
| 5995 | message = "truncated \\uXXXX escape"; |
| 5996 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5998 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5999 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6000 | digits = 8; |
| 6001 | message = "truncated \\UXXXXXXXX escape"; |
| 6002 | hexescape: |
| 6003 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6004 | if (s+digits>end) { |
| 6005 | endinpos = size; |
| 6006 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | errors, &errorHandler, |
| 6008 | "unicodeescape", "end of string in escape sequence", |
| 6009 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6010 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6011 | goto onError; |
| 6012 | goto nextByte; |
| 6013 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6014 | for (j = 0; j < digits; ++j) { |
| 6015 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6016 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6017 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6018 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | errors, &errorHandler, |
| 6020 | "unicodeescape", message, |
| 6021 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6022 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6023 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6024 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6025 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6026 | } |
| 6027 | chr = (chr<<4) & ~0xF; |
| 6028 | if (c >= '0' && c <= '9') |
| 6029 | chr += c - '0'; |
| 6030 | else if (c >= 'a' && c <= 'f') |
| 6031 | chr += 10 + c - 'a'; |
| 6032 | else |
| 6033 | chr += 10 + c - 'A'; |
| 6034 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6035 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 6036 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6037 | /* _decoding_error will have already written into the |
| 6038 | target buffer. */ |
| 6039 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6040 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6041 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6042 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6043 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6044 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6045 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6046 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | errors, &errorHandler, |
| 6048 | "unicodeescape", "illegal Unicode character", |
| 6049 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6050 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6051 | goto onError; |
| 6052 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6053 | break; |
| 6054 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6055 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6056 | case 'N': |
| 6057 | message = "malformed \\N character escape"; |
| 6058 | if (ucnhash_CAPI == NULL) { |
| 6059 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6060 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 6061 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6062 | if (ucnhash_CAPI == NULL) |
| 6063 | goto ucnhashError; |
| 6064 | } |
| 6065 | if (*s == '{') { |
| 6066 | const char *start = s+1; |
| 6067 | /* look for the closing brace */ |
| 6068 | while (*s != '}' && s < end) |
| 6069 | s++; |
| 6070 | if (s > start && s < end && *s == '}') { |
| 6071 | /* found a name. look it up in the unicode database */ |
| 6072 | message = "unknown Unicode character name"; |
| 6073 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6074 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6075 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6076 | goto store; |
| 6077 | } |
| 6078 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6079 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6080 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6081 | errors, &errorHandler, |
| 6082 | "unicodeescape", message, |
| 6083 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6084 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6085 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6086 | break; |
| 6087 | |
| 6088 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6089 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6090 | message = "\\ at end of string"; |
| 6091 | s--; |
| 6092 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6093 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6094 | errors, &errorHandler, |
| 6095 | "unicodeescape", message, |
| 6096 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6097 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6098 | goto onError; |
| 6099 | } |
| 6100 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6101 | WRITECHAR('\\'); |
| 6102 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6103 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6104 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6107 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6109 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6110 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6111 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6112 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6113 | Py_XDECREF(errorHandler); |
| 6114 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6115 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6116 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6117 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6118 | PyErr_SetString( |
| 6119 | PyExc_UnicodeError, |
| 6120 | "\\N escapes not supported (can't load unicodedata module)" |
| 6121 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6122 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6123 | Py_XDECREF(errorHandler); |
| 6124 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6125 | return NULL; |
| 6126 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6128 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6129 | Py_XDECREF(errorHandler); |
| 6130 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | return NULL; |
| 6132 | } |
| 6133 | |
| 6134 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6135 | |
| 6136 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6137 | appropriate. |
| 6138 | |
| 6139 | */ |
| 6140 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6141 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6142 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6144 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6145 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6147 | int kind; |
| 6148 | void *data; |
| 6149 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6151 | /* Initial allocation is based on the longest-possible unichr |
| 6152 | escape. |
| 6153 | |
| 6154 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6155 | unichr, so in this case it's the longest unichr escape. In |
| 6156 | narrow (UTF-16) builds this is five chars per source unichr |
| 6157 | since there are two unichrs in the surrogate pair, so in narrow |
| 6158 | (UTF-16) builds it's not the longest unichr escape. |
| 6159 | |
| 6160 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6161 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6162 | escape. |
| 6163 | */ |
| 6164 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6165 | if (!PyUnicode_Check(unicode)) { |
| 6166 | PyErr_BadArgument(); |
| 6167 | return NULL; |
| 6168 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6169 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6170 | return NULL; |
| 6171 | len = PyUnicode_GET_LENGTH(unicode); |
| 6172 | kind = PyUnicode_KIND(unicode); |
| 6173 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6174 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6175 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6176 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6177 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6178 | } |
| 6179 | |
| 6180 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6181 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6182 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6183 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6184 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6185 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6186 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6187 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6188 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6189 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | if (repr == NULL) |
| 6191 | return NULL; |
| 6192 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6193 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6195 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6196 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6197 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6198 | /* Escape backslashes */ |
| 6199 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | *p++ = '\\'; |
| 6201 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6202 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6203 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6204 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6205 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6206 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6207 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6208 | *p++ = '\\'; |
| 6209 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6210 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6211 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6212 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6213 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6214 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6215 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6216 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6217 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6218 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6219 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6222 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | *p++ = '\\'; |
| 6224 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6225 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6226 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6227 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6228 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6230 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6231 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6232 | else if (ch == '\t') { |
| 6233 | *p++ = '\\'; |
| 6234 | *p++ = 't'; |
| 6235 | } |
| 6236 | else if (ch == '\n') { |
| 6237 | *p++ = '\\'; |
| 6238 | *p++ = 'n'; |
| 6239 | } |
| 6240 | else if (ch == '\r') { |
| 6241 | *p++ = '\\'; |
| 6242 | *p++ = 'r'; |
| 6243 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6244 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6245 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6246 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6248 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6249 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6250 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6251 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6253 | /* Copy everything else as-is */ |
| 6254 | else |
| 6255 | *p++ = (char) ch; |
| 6256 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6257 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6258 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6259 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6260 | return NULL; |
| 6261 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6264 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6265 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6266 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6268 | PyObject *result; |
| 6269 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6270 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6272 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6273 | Py_DECREF(tmp); |
| 6274 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6278 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6279 | PyObject * |
| 6280 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6281 | Py_ssize_t size, |
| 6282 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6283 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6284 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6285 | Py_ssize_t startinpos; |
| 6286 | Py_ssize_t endinpos; |
| 6287 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6288 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6289 | const char *end; |
| 6290 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6291 | PyObject *errorHandler = NULL; |
| 6292 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6293 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | /* Escaped strings will always be longer than the resulting |
| 6295 | Unicode string, so we start with size here and then reduce the |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6296 | length after conversion to the true value. (But decoding error |
| 6297 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6298 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6301 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6302 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6303 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | end = s + size; |
| 6305 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | unsigned char c; |
| 6307 | Py_UCS4 x; |
| 6308 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6309 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6311 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6312 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6313 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6314 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6315 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6316 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | startinpos = s-starts; |
| 6318 | |
| 6319 | /* \u-escapes are only interpreted iff the number of leading |
| 6320 | backslashes if odd */ |
| 6321 | bs = s; |
| 6322 | for (;s < end;) { |
| 6323 | if (*s != '\\') |
| 6324 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6325 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6326 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6327 | } |
| 6328 | if (((s - bs) & 1) == 0 || |
| 6329 | s >= end || |
| 6330 | (*s != 'u' && *s != 'U')) { |
| 6331 | continue; |
| 6332 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6333 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6334 | count = *s=='u' ? 4 : 8; |
| 6335 | s++; |
| 6336 | |
| 6337 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6339 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6340 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6341 | endinpos = s-starts; |
| 6342 | if (unicode_decode_call_errorhandler( |
| 6343 | errors, &errorHandler, |
| 6344 | "rawunicodeescape", "truncated \\uXXXX", |
| 6345 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6346 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6347 | goto onError; |
| 6348 | goto nextByte; |
| 6349 | } |
| 6350 | x = (x<<4) & ~0xF; |
| 6351 | if (c >= '0' && c <= '9') |
| 6352 | x += c - '0'; |
| 6353 | else if (c >= 'a' && c <= 'f') |
| 6354 | x += 10 + c - 'a'; |
| 6355 | else |
| 6356 | x += 10 + c - 'A'; |
| 6357 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6358 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6359 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6360 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6361 | } else { |
| 6362 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6363 | if (unicode_decode_call_errorhandler( |
| 6364 | errors, &errorHandler, |
| 6365 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6366 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6367 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6369 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6370 | nextByte: |
| 6371 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6372 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6373 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6375 | Py_XDECREF(errorHandler); |
| 6376 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6377 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6378 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6380 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6381 | Py_XDECREF(errorHandler); |
| 6382 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | return NULL; |
| 6384 | } |
| 6385 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6386 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6387 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6388 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6390 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | char *p; |
| 6392 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6393 | Py_ssize_t expandsize, pos; |
| 6394 | int kind; |
| 6395 | void *data; |
| 6396 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6398 | if (!PyUnicode_Check(unicode)) { |
| 6399 | PyErr_BadArgument(); |
| 6400 | return NULL; |
| 6401 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6402 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6403 | return NULL; |
| 6404 | kind = PyUnicode_KIND(unicode); |
| 6405 | data = PyUnicode_DATA(unicode); |
| 6406 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6407 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6408 | bytes, and 1 byte characters 4. */ |
| 6409 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6410 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6411 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6413 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6414 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | if (repr == NULL) |
| 6416 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6417 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6418 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6419 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6420 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6421 | for (pos = 0; pos < len; pos++) { |
| 6422 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6424 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6425 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6426 | *p++ = '\\'; |
| 6427 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6428 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6429 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6430 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6431 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6432 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6433 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6434 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6435 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6436 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6437 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6438 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | *p++ = '\\'; |
| 6440 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6441 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6442 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6443 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6444 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6445 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | /* Copy everything else as-is */ |
| 6447 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | *p++ = (char) ch; |
| 6449 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6450 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6451 | assert(p > q); |
| 6452 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6453 | return NULL; |
| 6454 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | } |
| 6456 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6457 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6458 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6459 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6461 | PyObject *result; |
| 6462 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6463 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6464 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6465 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6466 | Py_DECREF(tmp); |
| 6467 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6470 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6471 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6472 | PyObject * |
| 6473 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6474 | Py_ssize_t size, |
| 6475 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6476 | { |
| 6477 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6478 | Py_ssize_t startinpos; |
| 6479 | Py_ssize_t endinpos; |
| 6480 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6481 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6482 | const char *end; |
| 6483 | const char *reason; |
| 6484 | PyObject *errorHandler = NULL; |
| 6485 | PyObject *exc = NULL; |
| 6486 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6487 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6488 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6489 | 1)) |
| 6490 | return NULL; |
| 6491 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6492 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6493 | v = PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE, 127); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6494 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6495 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6496 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6497 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6498 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6499 | end = s + size; |
| 6500 | |
| 6501 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6502 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6503 | Py_UCS4 ch; |
| 6504 | /* We copy the raw representation one byte at a time because the |
| 6505 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6506 | ((char *) &uch)[0] = s[0]; |
| 6507 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6508 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6509 | ((char *) &uch)[2] = s[2]; |
| 6510 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6511 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6512 | ch = uch; |
| 6513 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6514 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6515 | some malformed UCS-4 data. */ |
| 6516 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6517 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6518 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6519 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6520 | end-s < Py_UNICODE_SIZE |
| 6521 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6523 | startinpos = s - starts; |
| 6524 | if (end-s < Py_UNICODE_SIZE) { |
| 6525 | endinpos = end-starts; |
| 6526 | reason = "truncated input"; |
| 6527 | } |
| 6528 | else { |
| 6529 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6530 | reason = "illegal code point (> 0x10FFFF)"; |
| 6531 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6532 | if (unicode_decode_call_errorhandler( |
| 6533 | errors, &errorHandler, |
| 6534 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6535 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6536 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6537 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6538 | continue; |
| 6539 | } |
| 6540 | |
| 6541 | s += Py_UNICODE_SIZE; |
| 6542 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6543 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6544 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6545 | Py_UNICODE uch2; |
| 6546 | ((char *) &uch2)[0] = s[0]; |
| 6547 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6548 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6549 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6550 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6551 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6552 | } |
| 6553 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6554 | #endif |
| 6555 | |
| 6556 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6557 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6558 | } |
| 6559 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6560 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6561 | goto onError; |
| 6562 | Py_XDECREF(errorHandler); |
| 6563 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6564 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6565 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6567 | Py_XDECREF(v); |
| 6568 | Py_XDECREF(errorHandler); |
| 6569 | Py_XDECREF(exc); |
| 6570 | return NULL; |
| 6571 | } |
| 6572 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6574 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6575 | PyObject * |
| 6576 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6577 | Py_ssize_t size, |
| 6578 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6580 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6581 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | } |
| 6583 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6584 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6585 | static void |
| 6586 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6587 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6588 | PyObject *unicode, |
| 6589 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6590 | const char *reason) |
| 6591 | { |
| 6592 | if (*exceptionObject == NULL) { |
| 6593 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6594 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6595 | encoding, unicode, startpos, endpos, reason); |
| 6596 | } |
| 6597 | else { |
| 6598 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6599 | goto onError; |
| 6600 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6601 | goto onError; |
| 6602 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6603 | goto onError; |
| 6604 | return; |
| 6605 | onError: |
| 6606 | Py_DECREF(*exceptionObject); |
| 6607 | *exceptionObject = NULL; |
| 6608 | } |
| 6609 | } |
| 6610 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6611 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6612 | static void |
| 6613 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6614 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6615 | PyObject *unicode, |
| 6616 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6617 | const char *reason) |
| 6618 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6619 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6620 | encoding, unicode, startpos, endpos, reason); |
| 6621 | if (*exceptionObject != NULL) |
| 6622 | PyCodec_StrictErrors(*exceptionObject); |
| 6623 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6624 | |
| 6625 | /* error handling callback helper: |
| 6626 | build arguments, call the callback and check the arguments, |
| 6627 | put the result into newpos and return the replacement string, which |
| 6628 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6629 | static PyObject * |
| 6630 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6631 | PyObject **errorHandler, |
| 6632 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6633 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6634 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6635 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6636 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6637 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6638 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6639 | PyObject *restuple; |
| 6640 | PyObject *resunicode; |
| 6641 | |
| 6642 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6643 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6644 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6645 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6648 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6649 | return NULL; |
| 6650 | len = PyUnicode_GET_LENGTH(unicode); |
| 6651 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6652 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6653 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6654 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6656 | |
| 6657 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6658 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6659 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6660 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6661 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6662 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6663 | Py_DECREF(restuple); |
| 6664 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6665 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6666 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | &resunicode, newpos)) { |
| 6668 | Py_DECREF(restuple); |
| 6669 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6670 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6671 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6672 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6673 | Py_DECREF(restuple); |
| 6674 | return NULL; |
| 6675 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6676 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6677 | *newpos = len + *newpos; |
| 6678 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6679 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6680 | Py_DECREF(restuple); |
| 6681 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6682 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6683 | Py_INCREF(resunicode); |
| 6684 | Py_DECREF(restuple); |
| 6685 | return resunicode; |
| 6686 | } |
| 6687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6688 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6689 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6690 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6691 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6692 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6693 | /* input state */ |
| 6694 | Py_ssize_t pos=0, size; |
| 6695 | int kind; |
| 6696 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6697 | /* output object */ |
| 6698 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6699 | /* pointer into the output */ |
| 6700 | char *str; |
| 6701 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6702 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6703 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6704 | const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6705 | PyObject *errorHandler = NULL; |
| 6706 | PyObject *exc = NULL; |
| 6707 | /* the following variable is used for caching string comparisons |
| 6708 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6709 | int known_errorHandler = -1; |
| 6710 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6711 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6712 | return NULL; |
| 6713 | size = PyUnicode_GET_LENGTH(unicode); |
| 6714 | kind = PyUnicode_KIND(unicode); |
| 6715 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6716 | /* allocate enough for a simple encoding without |
| 6717 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6718 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6719 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6720 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6721 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6722 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6723 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6724 | ressize = size; |
| 6725 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6726 | while (pos < size) { |
| 6727 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6728 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6729 | /* can we encode this? */ |
| 6730 | if (c<limit) { |
| 6731 | /* no overflow check, because we know that the space is enough */ |
| 6732 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6733 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6734 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6735 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | Py_ssize_t requiredsize; |
| 6737 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6738 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6739 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6740 | Py_ssize_t collstart = pos; |
| 6741 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6743 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6744 | ++collend; |
| 6745 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6746 | if (known_errorHandler==-1) { |
| 6747 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6748 | known_errorHandler = 1; |
| 6749 | else if (!strcmp(errors, "replace")) |
| 6750 | known_errorHandler = 2; |
| 6751 | else if (!strcmp(errors, "ignore")) |
| 6752 | known_errorHandler = 3; |
| 6753 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6754 | known_errorHandler = 4; |
| 6755 | else |
| 6756 | known_errorHandler = 0; |
| 6757 | } |
| 6758 | switch (known_errorHandler) { |
| 6759 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6760 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6761 | goto onError; |
| 6762 | case 2: /* replace */ |
| 6763 | while (collstart++<collend) |
| 6764 | *str++ = '?'; /* fall through */ |
| 6765 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6766 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | break; |
| 6768 | case 4: /* xmlcharrefreplace */ |
| 6769 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6770 | /* determine replacement size */ |
| 6771 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6772 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6773 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6775 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6777 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6778 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6779 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6781 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6783 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6784 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6785 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6786 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6787 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6788 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6789 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6790 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6791 | if (requiredsize > ressize) { |
| 6792 | if (requiredsize<2*ressize) |
| 6793 | requiredsize = 2*ressize; |
| 6794 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6795 | goto onError; |
| 6796 | str = PyBytes_AS_STRING(res) + respos; |
| 6797 | ressize = requiredsize; |
| 6798 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6799 | /* generate replacement */ |
| 6800 | for (i = collstart; i < collend; ++i) { |
| 6801 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6802 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6803 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6804 | break; |
| 6805 | default: |
| 6806 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6807 | encoding, reason, unicode, &exc, |
| 6808 | collstart, collend, &newpos); |
| 6809 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6810 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6811 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6812 | if (PyBytes_Check(repunicode)) { |
| 6813 | /* Directly copy bytes result to output. */ |
| 6814 | repsize = PyBytes_Size(repunicode); |
| 6815 | if (repsize > 1) { |
| 6816 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6817 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6818 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6819 | Py_DECREF(repunicode); |
| 6820 | goto onError; |
| 6821 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6822 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6823 | ressize += repsize-1; |
| 6824 | } |
| 6825 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6826 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6827 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6828 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6829 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6830 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6831 | /* need more space? (at least enough for what we |
| 6832 | have+the replacement+the rest of the string, so |
| 6833 | we won't have to check space for encodable characters) */ |
| 6834 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6835 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6836 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6837 | if (requiredsize > ressize) { |
| 6838 | if (requiredsize<2*ressize) |
| 6839 | requiredsize = 2*ressize; |
| 6840 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6841 | Py_DECREF(repunicode); |
| 6842 | goto onError; |
| 6843 | } |
| 6844 | str = PyBytes_AS_STRING(res) + respos; |
| 6845 | ressize = requiredsize; |
| 6846 | } |
| 6847 | /* check if there is anything unencodable in the replacement |
| 6848 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6849 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6850 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6851 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6852 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6853 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6854 | Py_DECREF(repunicode); |
| 6855 | goto onError; |
| 6856 | } |
| 6857 | *str = (char)c; |
| 6858 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6859 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6860 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6861 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6862 | } |
| 6863 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6864 | /* Resize if we allocated to much */ |
| 6865 | size = str - PyBytes_AS_STRING(res); |
| 6866 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6867 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6868 | if (_PyBytes_Resize(&res, size) < 0) |
| 6869 | goto onError; |
| 6870 | } |
| 6871 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6872 | Py_XDECREF(errorHandler); |
| 6873 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6874 | return res; |
| 6875 | |
| 6876 | onError: |
| 6877 | Py_XDECREF(res); |
| 6878 | Py_XDECREF(errorHandler); |
| 6879 | Py_XDECREF(exc); |
| 6880 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6883 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6884 | PyObject * |
| 6885 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6886 | Py_ssize_t size, |
| 6887 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6889 | PyObject *result; |
| 6890 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6891 | if (unicode == NULL) |
| 6892 | return NULL; |
| 6893 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6894 | Py_DECREF(unicode); |
| 6895 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6898 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6899 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | { |
| 6901 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6902 | PyErr_BadArgument(); |
| 6903 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6904 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6905 | if (PyUnicode_READY(unicode) == -1) |
| 6906 | return NULL; |
| 6907 | /* Fast path: if it is a one-byte string, construct |
| 6908 | bytes object directly. */ |
| 6909 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6910 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6911 | PyUnicode_GET_LENGTH(unicode)); |
| 6912 | /* Non-Latin-1 characters present. Defer to above function to |
| 6913 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6914 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6915 | } |
| 6916 | |
| 6917 | PyObject* |
| 6918 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6919 | { |
| 6920 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6925 | PyObject * |
| 6926 | PyUnicode_DecodeASCII(const char *s, |
| 6927 | Py_ssize_t size, |
| 6928 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6930 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6931 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6932 | int kind; |
| 6933 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6934 | Py_ssize_t startinpos; |
| 6935 | Py_ssize_t endinpos; |
| 6936 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6937 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6938 | int has_error; |
| 6939 | const unsigned char *p = (const unsigned char *)s; |
| 6940 | const unsigned char *end = p + size; |
| 6941 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6942 | PyObject *errorHandler = NULL; |
| 6943 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6944 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6945 | if (size == 0) { |
| 6946 | Py_INCREF(unicode_empty); |
| 6947 | return unicode_empty; |
| 6948 | } |
| 6949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6950 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6951 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6952 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6953 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6954 | has_error = 0; |
| 6955 | while (p < end && !has_error) { |
| 6956 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6957 | an explanation. */ |
| 6958 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6959 | /* Help register allocation */ |
| 6960 | register const unsigned char *_p = p; |
| 6961 | while (_p < aligned_end) { |
| 6962 | unsigned long value = *(unsigned long *) _p; |
| 6963 | if (value & ASCII_CHAR_MASK) { |
| 6964 | has_error = 1; |
| 6965 | break; |
| 6966 | } |
| 6967 | _p += SIZEOF_LONG; |
| 6968 | } |
| 6969 | if (_p == end) |
| 6970 | break; |
| 6971 | if (has_error) |
| 6972 | break; |
| 6973 | p = _p; |
| 6974 | } |
| 6975 | if (*p & 0x80) { |
| 6976 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6977 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6978 | } |
| 6979 | else { |
| 6980 | ++p; |
| 6981 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6982 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6983 | if (!has_error) |
| 6984 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6985 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6986 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6988 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6989 | kind = PyUnicode_KIND(v); |
| 6990 | data = PyUnicode_DATA(v); |
| 6991 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6992 | e = s + size; |
| 6993 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6994 | register unsigned char c = (unsigned char)*s; |
| 6995 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6996 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6997 | ++s; |
| 6998 | } |
| 6999 | else { |
| 7000 | startinpos = s-starts; |
| 7001 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7002 | if (unicode_decode_call_errorhandler( |
| 7003 | errors, &errorHandler, |
| 7004 | "ascii", "ordinal not in range(128)", |
| 7005 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7006 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7007 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7008 | kind = PyUnicode_KIND(v); |
| 7009 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7010 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7012 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7013 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7014 | Py_XDECREF(errorHandler); |
| 7015 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7016 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7017 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7018 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7019 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7020 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7021 | Py_XDECREF(errorHandler); |
| 7022 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | return NULL; |
| 7024 | } |
| 7025 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7026 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7027 | PyObject * |
| 7028 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 7029 | Py_ssize_t size, |
| 7030 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7031 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7032 | PyObject *result; |
| 7033 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7034 | if (unicode == NULL) |
| 7035 | return NULL; |
| 7036 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 7037 | Py_DECREF(unicode); |
| 7038 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7041 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7042 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | { |
| 7044 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7045 | PyErr_BadArgument(); |
| 7046 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7048 | if (PyUnicode_READY(unicode) == -1) |
| 7049 | return NULL; |
| 7050 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 7051 | directly. Else defer to above function to raise the exception. */ |
| 7052 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 7053 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 7054 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7055 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | PyObject * |
| 7059 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 7060 | { |
| 7061 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7064 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7065 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7066 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7067 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 7068 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7069 | #define NEED_RETRY |
| 7070 | #endif |
| 7071 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7072 | #ifndef WC_ERR_INVALID_CHARS |
| 7073 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 7074 | #endif |
| 7075 | |
| 7076 | static char* |
| 7077 | code_page_name(UINT code_page, PyObject **obj) |
| 7078 | { |
| 7079 | *obj = NULL; |
| 7080 | if (code_page == CP_ACP) |
| 7081 | return "mbcs"; |
| 7082 | if (code_page == CP_UTF7) |
| 7083 | return "CP_UTF7"; |
| 7084 | if (code_page == CP_UTF8) |
| 7085 | return "CP_UTF8"; |
| 7086 | |
| 7087 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7088 | if (*obj == NULL) |
| 7089 | return NULL; |
| 7090 | return PyBytes_AS_STRING(*obj); |
| 7091 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7093 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7094 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7095 | { |
| 7096 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7097 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7098 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7099 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 7100 | return 0; |
| 7101 | |
| 7102 | prev = CharPrevExA(code_page, s, curr, 0); |
| 7103 | if (prev == curr) |
| 7104 | return 1; |
| 7105 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 7106 | as it assumes an incomplete character consists of a single |
| 7107 | byte. */ |
| 7108 | if (curr - prev == 2) |
| 7109 | return 1; |
| 7110 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 7111 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7112 | return 0; |
| 7113 | } |
| 7114 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7115 | static DWORD |
| 7116 | decode_code_page_flags(UINT code_page) |
| 7117 | { |
| 7118 | if (code_page == CP_UTF7) { |
| 7119 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7120 | return 0; |
| 7121 | } |
| 7122 | else |
| 7123 | return MB_ERR_INVALID_CHARS; |
| 7124 | } |
| 7125 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7126 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7127 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7128 | * mode. |
| 7129 | * |
| 7130 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7131 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7132 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7133 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7134 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7135 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7136 | const char *in, |
| 7137 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7138 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7139 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7140 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7141 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7142 | |
| 7143 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7144 | assert(insize > 0); |
| 7145 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7146 | if (outsize <= 0) |
| 7147 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7148 | |
| 7149 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7150 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7151 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7152 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7153 | if (*v == NULL) |
| 7154 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7155 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7156 | } |
| 7157 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7158 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7159 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7160 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7161 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7162 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7163 | } |
| 7164 | |
| 7165 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7166 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7167 | if (outsize <= 0) |
| 7168 | goto error; |
| 7169 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7170 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7171 | error: |
| 7172 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7173 | return -2; |
| 7174 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7175 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7178 | /* |
| 7179 | * Decode a byte string from a code page into unicode object with an error |
| 7180 | * handler. |
| 7181 | * |
| 7182 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7183 | * UnicodeDecodeError exception and returns -1 on error. |
| 7184 | */ |
| 7185 | static int |
| 7186 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7187 | PyObject **v, |
| 7188 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7189 | const char *errors) |
| 7190 | { |
| 7191 | const char *startin = in; |
| 7192 | const char *endin = in + size; |
| 7193 | const DWORD flags = decode_code_page_flags(code_page); |
| 7194 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7195 | 2000 English version of the message. */ |
| 7196 | const char *reason = "No mapping for the Unicode character exists " |
| 7197 | "in the target code page."; |
| 7198 | /* each step cannot decode more than 1 character, but a character can be |
| 7199 | represented as a surrogate pair */ |
| 7200 | wchar_t buffer[2], *startout, *out; |
| 7201 | int insize, outsize; |
| 7202 | PyObject *errorHandler = NULL; |
| 7203 | PyObject *exc = NULL; |
| 7204 | PyObject *encoding_obj = NULL; |
| 7205 | char *encoding; |
| 7206 | DWORD err; |
| 7207 | int ret = -1; |
| 7208 | |
| 7209 | assert(size > 0); |
| 7210 | |
| 7211 | encoding = code_page_name(code_page, &encoding_obj); |
| 7212 | if (encoding == NULL) |
| 7213 | return -1; |
| 7214 | |
| 7215 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7216 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7217 | UnicodeDecodeError. */ |
| 7218 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7219 | if (exc != NULL) { |
| 7220 | PyCodec_StrictErrors(exc); |
| 7221 | Py_CLEAR(exc); |
| 7222 | } |
| 7223 | goto error; |
| 7224 | } |
| 7225 | |
| 7226 | if (*v == NULL) { |
| 7227 | /* Create unicode object */ |
| 7228 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7229 | PyErr_NoMemory(); |
| 7230 | goto error; |
| 7231 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7232 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7233 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7234 | if (*v == NULL) |
| 7235 | goto error; |
| 7236 | startout = PyUnicode_AS_UNICODE(*v); |
| 7237 | } |
| 7238 | else { |
| 7239 | /* Extend unicode object */ |
| 7240 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7241 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7242 | PyErr_NoMemory(); |
| 7243 | goto error; |
| 7244 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7245 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7246 | goto error; |
| 7247 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7248 | } |
| 7249 | |
| 7250 | /* Decode the byte string character per character */ |
| 7251 | out = startout; |
| 7252 | while (in < endin) |
| 7253 | { |
| 7254 | /* Decode a character */ |
| 7255 | insize = 1; |
| 7256 | do |
| 7257 | { |
| 7258 | outsize = MultiByteToWideChar(code_page, flags, |
| 7259 | in, insize, |
| 7260 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7261 | if (outsize > 0) |
| 7262 | break; |
| 7263 | err = GetLastError(); |
| 7264 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7265 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7266 | { |
| 7267 | PyErr_SetFromWindowsErr(0); |
| 7268 | goto error; |
| 7269 | } |
| 7270 | insize++; |
| 7271 | } |
| 7272 | /* 4=maximum length of a UTF-8 sequence */ |
| 7273 | while (insize <= 4 && (in + insize) <= endin); |
| 7274 | |
| 7275 | if (outsize <= 0) { |
| 7276 | Py_ssize_t startinpos, endinpos, outpos; |
| 7277 | |
| 7278 | startinpos = in - startin; |
| 7279 | endinpos = startinpos + 1; |
| 7280 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7281 | if (unicode_decode_call_errorhandler( |
| 7282 | errors, &errorHandler, |
| 7283 | encoding, reason, |
| 7284 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7285 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7286 | { |
| 7287 | goto error; |
| 7288 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7289 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7290 | } |
| 7291 | else { |
| 7292 | in += insize; |
| 7293 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7294 | out += outsize; |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | /* write a NUL character at the end */ |
| 7299 | *out = 0; |
| 7300 | |
| 7301 | /* Extend unicode object */ |
| 7302 | outsize = out - startout; |
| 7303 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7304 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7305 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7306 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7307 | |
| 7308 | error: |
| 7309 | Py_XDECREF(encoding_obj); |
| 7310 | Py_XDECREF(errorHandler); |
| 7311 | Py_XDECREF(exc); |
| 7312 | return ret; |
| 7313 | } |
| 7314 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7315 | static PyObject * |
| 7316 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7317 | const char *s, Py_ssize_t size, |
| 7318 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7319 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7320 | PyObject *v = NULL; |
| 7321 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7322 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7323 | if (code_page < 0) { |
| 7324 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7325 | return NULL; |
| 7326 | } |
| 7327 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7328 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7329 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7330 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7331 | do |
| 7332 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7333 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7334 | if (size > INT_MAX) { |
| 7335 | chunk_size = INT_MAX; |
| 7336 | final = 0; |
| 7337 | done = 0; |
| 7338 | } |
| 7339 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7340 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7341 | { |
| 7342 | chunk_size = (int)size; |
| 7343 | final = (consumed == NULL); |
| 7344 | done = 1; |
| 7345 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7346 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7347 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7348 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7349 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7350 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7351 | if (chunk_size == 0 && done) { |
| 7352 | if (v != NULL) |
| 7353 | break; |
| 7354 | Py_INCREF(unicode_empty); |
| 7355 | return unicode_empty; |
| 7356 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7357 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7358 | |
| 7359 | converted = decode_code_page_strict(code_page, &v, |
| 7360 | s, chunk_size); |
| 7361 | if (converted == -2) |
| 7362 | converted = decode_code_page_errors(code_page, &v, |
| 7363 | s, chunk_size, |
| 7364 | errors); |
| 7365 | assert(converted != 0); |
| 7366 | |
| 7367 | if (converted < 0) { |
| 7368 | Py_XDECREF(v); |
| 7369 | return NULL; |
| 7370 | } |
| 7371 | |
| 7372 | if (consumed) |
| 7373 | *consumed += converted; |
| 7374 | |
| 7375 | s += converted; |
| 7376 | size -= converted; |
| 7377 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7378 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7379 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7380 | } |
| 7381 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7382 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7383 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7384 | const char *s, |
| 7385 | Py_ssize_t size, |
| 7386 | const char *errors, |
| 7387 | Py_ssize_t *consumed) |
| 7388 | { |
| 7389 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7390 | } |
| 7391 | |
| 7392 | PyObject * |
| 7393 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7394 | Py_ssize_t size, |
| 7395 | const char *errors, |
| 7396 | Py_ssize_t *consumed) |
| 7397 | { |
| 7398 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7399 | } |
| 7400 | |
| 7401 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7402 | PyUnicode_DecodeMBCS(const char *s, |
| 7403 | Py_ssize_t size, |
| 7404 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7405 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7406 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7407 | } |
| 7408 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7409 | static DWORD |
| 7410 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7411 | { |
| 7412 | if (code_page == CP_UTF8) { |
| 7413 | if (winver.dwMajorVersion >= 6) |
| 7414 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7415 | and later */ |
| 7416 | return WC_ERR_INVALID_CHARS; |
| 7417 | else |
| 7418 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7419 | return 0; |
| 7420 | } |
| 7421 | else if (code_page == CP_UTF7) { |
| 7422 | /* CP_UTF7 only supports flags=0 */ |
| 7423 | return 0; |
| 7424 | } |
| 7425 | else { |
| 7426 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7427 | return 0; |
| 7428 | else |
| 7429 | return WC_NO_BEST_FIT_CHARS; |
| 7430 | } |
| 7431 | } |
| 7432 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7433 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7434 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7435 | * mode. |
| 7436 | * |
| 7437 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7438 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7439 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7440 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7441 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7442 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7443 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7444 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7445 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7446 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7447 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7448 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7449 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7450 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7451 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7452 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7453 | /* Create a substring so that we can get the UTF-16 representation |
| 7454 | of just the slice under consideration. */ |
| 7455 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7456 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7457 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7458 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7459 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7460 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7461 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7462 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7463 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7464 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7465 | if (substring == NULL) |
| 7466 | return -1; |
| 7467 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7468 | if (p == NULL) { |
| 7469 | Py_DECREF(substring); |
| 7470 | return -1; |
| 7471 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7472 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7473 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7474 | outsize = WideCharToMultiByte(code_page, flags, |
| 7475 | p, size, |
| 7476 | NULL, 0, |
| 7477 | NULL, pusedDefaultChar); |
| 7478 | if (outsize <= 0) |
| 7479 | goto error; |
| 7480 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7481 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7482 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7483 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7484 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7485 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7486 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7488 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7489 | if (*outbytes == NULL) { |
| 7490 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7491 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7492 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7493 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7494 | } |
| 7495 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7496 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7497 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7498 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7499 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7500 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7501 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7502 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7503 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7504 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7505 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7506 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7507 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
| 7510 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7511 | outsize = WideCharToMultiByte(code_page, flags, |
| 7512 | p, size, |
| 7513 | out, outsize, |
| 7514 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7515 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7516 | if (outsize <= 0) |
| 7517 | goto error; |
| 7518 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7519 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7520 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7521 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7522 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7523 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7524 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7525 | return -2; |
| 7526 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7527 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7530 | /* |
| 7531 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7532 | * error handler. |
| 7533 | * |
| 7534 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7535 | * -1 on other error. |
| 7536 | */ |
| 7537 | static int |
| 7538 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7539 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7540 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7541 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7542 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7543 | Py_ssize_t pos = unicode_offset; |
| 7544 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7545 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7546 | 2000 English version of the message. */ |
| 7547 | const char *reason = "invalid character"; |
| 7548 | /* 4=maximum length of a UTF-8 sequence */ |
| 7549 | char buffer[4]; |
| 7550 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7551 | Py_ssize_t outsize; |
| 7552 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7553 | PyObject *errorHandler = NULL; |
| 7554 | PyObject *exc = NULL; |
| 7555 | PyObject *encoding_obj = NULL; |
| 7556 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7557 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7558 | PyObject *rep; |
| 7559 | int ret = -1; |
| 7560 | |
| 7561 | assert(insize > 0); |
| 7562 | |
| 7563 | encoding = code_page_name(code_page, &encoding_obj); |
| 7564 | if (encoding == NULL) |
| 7565 | return -1; |
| 7566 | |
| 7567 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7568 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7569 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7570 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7571 | if (exc != NULL) { |
| 7572 | PyCodec_StrictErrors(exc); |
| 7573 | Py_DECREF(exc); |
| 7574 | } |
| 7575 | Py_XDECREF(encoding_obj); |
| 7576 | return -1; |
| 7577 | } |
| 7578 | |
| 7579 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7580 | pusedDefaultChar = &usedDefaultChar; |
| 7581 | else |
| 7582 | pusedDefaultChar = NULL; |
| 7583 | |
| 7584 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7585 | PyErr_NoMemory(); |
| 7586 | goto error; |
| 7587 | } |
| 7588 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7589 | |
| 7590 | if (*outbytes == NULL) { |
| 7591 | /* Create string object */ |
| 7592 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7593 | if (*outbytes == NULL) |
| 7594 | goto error; |
| 7595 | out = PyBytes_AS_STRING(*outbytes); |
| 7596 | } |
| 7597 | else { |
| 7598 | /* Extend string object */ |
| 7599 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7600 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7601 | PyErr_NoMemory(); |
| 7602 | goto error; |
| 7603 | } |
| 7604 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7605 | goto error; |
| 7606 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7607 | } |
| 7608 | |
| 7609 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7610 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7611 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7612 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7613 | wchar_t chars[2]; |
| 7614 | int charsize; |
| 7615 | if (ch < 0x10000) { |
| 7616 | chars[0] = (wchar_t)ch; |
| 7617 | charsize = 1; |
| 7618 | } |
| 7619 | else { |
| 7620 | ch -= 0x10000; |
| 7621 | chars[0] = 0xd800 + (ch >> 10); |
| 7622 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7623 | charsize = 2; |
| 7624 | } |
| 7625 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7626 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7627 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7628 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7629 | NULL, pusedDefaultChar); |
| 7630 | if (outsize > 0) { |
| 7631 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7632 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7633 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7634 | memcpy(out, buffer, outsize); |
| 7635 | out += outsize; |
| 7636 | continue; |
| 7637 | } |
| 7638 | } |
| 7639 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7640 | PyErr_SetFromWindowsErr(0); |
| 7641 | goto error; |
| 7642 | } |
| 7643 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7644 | rep = unicode_encode_call_errorhandler( |
| 7645 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7646 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7647 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7648 | if (rep == NULL) |
| 7649 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7650 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7651 | |
| 7652 | if (PyBytes_Check(rep)) { |
| 7653 | outsize = PyBytes_GET_SIZE(rep); |
| 7654 | if (outsize != 1) { |
| 7655 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7656 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7657 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7658 | Py_DECREF(rep); |
| 7659 | goto error; |
| 7660 | } |
| 7661 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7662 | } |
| 7663 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7664 | out += outsize; |
| 7665 | } |
| 7666 | else { |
| 7667 | Py_ssize_t i; |
| 7668 | enum PyUnicode_Kind kind; |
| 7669 | void *data; |
| 7670 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7671 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7672 | Py_DECREF(rep); |
| 7673 | goto error; |
| 7674 | } |
| 7675 | |
| 7676 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7677 | if (outsize != 1) { |
| 7678 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7679 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7680 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7681 | Py_DECREF(rep); |
| 7682 | goto error; |
| 7683 | } |
| 7684 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7685 | } |
| 7686 | kind = PyUnicode_KIND(rep); |
| 7687 | data = PyUnicode_DATA(rep); |
| 7688 | for (i=0; i < outsize; i++) { |
| 7689 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7690 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7691 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7692 | encoding, unicode, |
| 7693 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7694 | "unable to encode error handler result to ASCII"); |
| 7695 | Py_DECREF(rep); |
| 7696 | goto error; |
| 7697 | } |
| 7698 | *out = (unsigned char)ch; |
| 7699 | out++; |
| 7700 | } |
| 7701 | } |
| 7702 | Py_DECREF(rep); |
| 7703 | } |
| 7704 | /* write a NUL byte */ |
| 7705 | *out = 0; |
| 7706 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7707 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7708 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7709 | goto error; |
| 7710 | ret = 0; |
| 7711 | |
| 7712 | error: |
| 7713 | Py_XDECREF(encoding_obj); |
| 7714 | Py_XDECREF(errorHandler); |
| 7715 | Py_XDECREF(exc); |
| 7716 | return ret; |
| 7717 | } |
| 7718 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7719 | static PyObject * |
| 7720 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7721 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7722 | const char *errors) |
| 7723 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7724 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7725 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7726 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7727 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7728 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7729 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7730 | return NULL; |
| 7731 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7732 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7733 | if (code_page < 0) { |
| 7734 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7735 | return NULL; |
| 7736 | } |
| 7737 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7738 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7739 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7740 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7741 | offset = 0; |
| 7742 | do |
| 7743 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7744 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7745 | /* UTF-16 encoding may double the size, so use only INT_MAX/2 |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7746 | chunks. */ |
| 7747 | if (len > INT_MAX/2) { |
| 7748 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7749 | done = 0; |
| 7750 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7751 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7752 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7753 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7754 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7755 | done = 1; |
| 7756 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7757 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7758 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7759 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7760 | errors); |
| 7761 | if (ret == -2) |
| 7762 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7763 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7764 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7765 | if (ret < 0) { |
| 7766 | Py_XDECREF(outbytes); |
| 7767 | return NULL; |
| 7768 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7769 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7770 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7771 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7772 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7773 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7774 | return outbytes; |
| 7775 | } |
| 7776 | |
| 7777 | PyObject * |
| 7778 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7779 | Py_ssize_t size, |
| 7780 | const char *errors) |
| 7781 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7782 | PyObject *unicode, *res; |
| 7783 | unicode = PyUnicode_FromUnicode(p, size); |
| 7784 | if (unicode == NULL) |
| 7785 | return NULL; |
| 7786 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7787 | Py_DECREF(unicode); |
| 7788 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | PyObject * |
| 7792 | PyUnicode_EncodeCodePage(int code_page, |
| 7793 | PyObject *unicode, |
| 7794 | const char *errors) |
| 7795 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7796 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7797 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7798 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7799 | PyObject * |
| 7800 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7801 | { |
| 7802 | if (!PyUnicode_Check(unicode)) { |
| 7803 | PyErr_BadArgument(); |
| 7804 | return NULL; |
| 7805 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7806 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7807 | } |
| 7808 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7809 | #undef NEED_RETRY |
| 7810 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7811 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7814 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7815 | PyObject * |
| 7816 | PyUnicode_DecodeCharmap(const char *s, |
| 7817 | Py_ssize_t size, |
| 7818 | PyObject *mapping, |
| 7819 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7820 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7821 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7822 | Py_ssize_t startinpos; |
| 7823 | Py_ssize_t endinpos; |
| 7824 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7825 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7826 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7827 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7828 | PyObject *errorHandler = NULL; |
| 7829 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | /* Default to Latin-1 */ |
| 7832 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7835 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7836 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7837 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7839 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7840 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7841 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7842 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7843 | Py_ssize_t maplen; |
| 7844 | enum PyUnicode_Kind kind; |
| 7845 | void *data; |
| 7846 | Py_UCS4 x; |
| 7847 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7848 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7849 | return NULL; |
| 7850 | |
| 7851 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7852 | data = PyUnicode_DATA(mapping); |
| 7853 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7854 | while (s < e) { |
| 7855 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7856 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7857 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7858 | x = PyUnicode_READ(kind, data, ch); |
| 7859 | else |
| 7860 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7862 | if (x == 0xfffe) |
| 7863 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7864 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7865 | startinpos = s-starts; |
| 7866 | endinpos = startinpos+1; |
| 7867 | if (unicode_decode_call_errorhandler( |
| 7868 | errors, &errorHandler, |
| 7869 | "charmap", "character maps to <undefined>", |
| 7870 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7871 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7872 | goto onError; |
| 7873 | } |
| 7874 | continue; |
| 7875 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7876 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7877 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7878 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7880 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7881 | } |
| 7882 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7883 | while (s < e) { |
| 7884 | unsigned char ch = *s; |
| 7885 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7886 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7887 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7888 | w = PyLong_FromLong((long)ch); |
| 7889 | if (w == NULL) |
| 7890 | goto onError; |
| 7891 | x = PyObject_GetItem(mapping, w); |
| 7892 | Py_DECREF(w); |
| 7893 | if (x == NULL) { |
| 7894 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7895 | /* No mapping found means: mapping is undefined. */ |
| 7896 | PyErr_Clear(); |
| 7897 | x = Py_None; |
| 7898 | Py_INCREF(x); |
| 7899 | } else |
| 7900 | goto onError; |
| 7901 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | /* Apply mapping */ |
| 7904 | if (PyLong_Check(x)) { |
| 7905 | long value = PyLong_AS_LONG(x); |
| 7906 | if (value < 0 || value > 65535) { |
| 7907 | PyErr_SetString(PyExc_TypeError, |
| 7908 | "character mapping must be in range(65536)"); |
| 7909 | Py_DECREF(x); |
| 7910 | goto onError; |
| 7911 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7912 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7913 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7914 | } |
| 7915 | else if (x == Py_None) { |
| 7916 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7917 | startinpos = s-starts; |
| 7918 | endinpos = startinpos+1; |
| 7919 | if (unicode_decode_call_errorhandler( |
| 7920 | errors, &errorHandler, |
| 7921 | "charmap", "character maps to <undefined>", |
| 7922 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7923 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7924 | Py_DECREF(x); |
| 7925 | goto onError; |
| 7926 | } |
| 7927 | Py_DECREF(x); |
| 7928 | continue; |
| 7929 | } |
| 7930 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7931 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7932 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7933 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7934 | goto onError; |
| 7935 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7936 | |
| 7937 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7938 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7939 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7940 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7941 | goto onError; |
| 7942 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | else if (targetsize > 1) { |
| 7944 | /* 1-n mapping */ |
| 7945 | if (targetsize > extrachars) { |
| 7946 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7947 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7948 | (targetsize << 2); |
| 7949 | extrachars += needed; |
| 7950 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7951 | if (unicode_resize(&v, |
| 7952 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7953 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7954 | Py_DECREF(x); |
| 7955 | goto onError; |
| 7956 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7957 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7958 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7959 | goto onError; |
| 7960 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7961 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7962 | extrachars -= targetsize; |
| 7963 | } |
| 7964 | /* 1-0 mapping: skip the character */ |
| 7965 | } |
| 7966 | else { |
| 7967 | /* wrong return value */ |
| 7968 | PyErr_SetString(PyExc_TypeError, |
| 7969 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7970 | Py_DECREF(x); |
| 7971 | goto onError; |
| 7972 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7973 | Py_DECREF(x); |
| 7974 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7975 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7976 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7977 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7978 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7979 | Py_XDECREF(errorHandler); |
| 7980 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7981 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7982 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7984 | Py_XDECREF(errorHandler); |
| 7985 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7986 | Py_XDECREF(v); |
| 7987 | return NULL; |
| 7988 | } |
| 7989 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7990 | /* Charmap encoding: the lookup table */ |
| 7991 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7992 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | PyObject_HEAD |
| 7994 | unsigned char level1[32]; |
| 7995 | int count2, count3; |
| 7996 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7997 | }; |
| 7998 | |
| 7999 | static PyObject* |
| 8000 | encoding_map_size(PyObject *obj, PyObject* args) |
| 8001 | { |
| 8002 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8003 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8004 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
| 8007 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8008 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8009 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 8010 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8011 | }; |
| 8012 | |
| 8013 | static void |
| 8014 | encoding_map_dealloc(PyObject* o) |
| 8015 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8016 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8017 | } |
| 8018 | |
| 8019 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8020 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 | "EncodingMap", /*tp_name*/ |
| 8022 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 8023 | 0, /*tp_itemsize*/ |
| 8024 | /* methods */ |
| 8025 | encoding_map_dealloc, /*tp_dealloc*/ |
| 8026 | 0, /*tp_print*/ |
| 8027 | 0, /*tp_getattr*/ |
| 8028 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 8029 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | 0, /*tp_repr*/ |
| 8031 | 0, /*tp_as_number*/ |
| 8032 | 0, /*tp_as_sequence*/ |
| 8033 | 0, /*tp_as_mapping*/ |
| 8034 | 0, /*tp_hash*/ |
| 8035 | 0, /*tp_call*/ |
| 8036 | 0, /*tp_str*/ |
| 8037 | 0, /*tp_getattro*/ |
| 8038 | 0, /*tp_setattro*/ |
| 8039 | 0, /*tp_as_buffer*/ |
| 8040 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 8041 | 0, /*tp_doc*/ |
| 8042 | 0, /*tp_traverse*/ |
| 8043 | 0, /*tp_clear*/ |
| 8044 | 0, /*tp_richcompare*/ |
| 8045 | 0, /*tp_weaklistoffset*/ |
| 8046 | 0, /*tp_iter*/ |
| 8047 | 0, /*tp_iternext*/ |
| 8048 | encoding_map_methods, /*tp_methods*/ |
| 8049 | 0, /*tp_members*/ |
| 8050 | 0, /*tp_getset*/ |
| 8051 | 0, /*tp_base*/ |
| 8052 | 0, /*tp_dict*/ |
| 8053 | 0, /*tp_descr_get*/ |
| 8054 | 0, /*tp_descr_set*/ |
| 8055 | 0, /*tp_dictoffset*/ |
| 8056 | 0, /*tp_init*/ |
| 8057 | 0, /*tp_alloc*/ |
| 8058 | 0, /*tp_new*/ |
| 8059 | 0, /*tp_free*/ |
| 8060 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8061 | }; |
| 8062 | |
| 8063 | PyObject* |
| 8064 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 8065 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8066 | PyObject *result; |
| 8067 | struct encoding_map *mresult; |
| 8068 | int i; |
| 8069 | int need_dict = 0; |
| 8070 | unsigned char level1[32]; |
| 8071 | unsigned char level2[512]; |
| 8072 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8073 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8074 | int kind; |
| 8075 | void *data; |
| 8076 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8078 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8079 | PyErr_BadArgument(); |
| 8080 | return NULL; |
| 8081 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8082 | kind = PyUnicode_KIND(string); |
| 8083 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8084 | memset(level1, 0xFF, sizeof level1); |
| 8085 | memset(level2, 0xFF, sizeof level2); |
| 8086 | |
| 8087 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8088 | or if there are non-BMP characters, we need to use |
| 8089 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8090 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8091 | need_dict = 1; |
| 8092 | for (i = 1; i < 256; i++) { |
| 8093 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8094 | ch = PyUnicode_READ(kind, data, i); |
| 8095 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8096 | need_dict = 1; |
| 8097 | break; |
| 8098 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8099 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8100 | /* unmapped character */ |
| 8101 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8102 | l1 = ch >> 11; |
| 8103 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8104 | if (level1[l1] == 0xFF) |
| 8105 | level1[l1] = count2++; |
| 8106 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8107 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8108 | } |
| 8109 | |
| 8110 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8111 | need_dict = 1; |
| 8112 | |
| 8113 | if (need_dict) { |
| 8114 | PyObject *result = PyDict_New(); |
| 8115 | PyObject *key, *value; |
| 8116 | if (!result) |
| 8117 | return NULL; |
| 8118 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8120 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8121 | if (!key || !value) |
| 8122 | goto failed1; |
| 8123 | if (PyDict_SetItem(result, key, value) == -1) |
| 8124 | goto failed1; |
| 8125 | Py_DECREF(key); |
| 8126 | Py_DECREF(value); |
| 8127 | } |
| 8128 | return result; |
| 8129 | failed1: |
| 8130 | Py_XDECREF(key); |
| 8131 | Py_XDECREF(value); |
| 8132 | Py_DECREF(result); |
| 8133 | return NULL; |
| 8134 | } |
| 8135 | |
| 8136 | /* Create a three-level trie */ |
| 8137 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8138 | 16*count2 + 128*count3 - 1); |
| 8139 | if (!result) |
| 8140 | return PyErr_NoMemory(); |
| 8141 | PyObject_Init(result, &EncodingMapType); |
| 8142 | mresult = (struct encoding_map*)result; |
| 8143 | mresult->count2 = count2; |
| 8144 | mresult->count3 = count3; |
| 8145 | mlevel1 = mresult->level1; |
| 8146 | mlevel2 = mresult->level23; |
| 8147 | mlevel3 = mresult->level23 + 16*count2; |
| 8148 | memcpy(mlevel1, level1, 32); |
| 8149 | memset(mlevel2, 0xFF, 16*count2); |
| 8150 | memset(mlevel3, 0, 128*count3); |
| 8151 | count3 = 0; |
| 8152 | for (i = 1; i < 256; i++) { |
| 8153 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8154 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8155 | /* unmapped character */ |
| 8156 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8157 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8158 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8159 | i2 = 16*mlevel1[o1] + o2; |
| 8160 | if (mlevel2[i2] == 0xFF) |
| 8161 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8162 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8163 | i3 = 128*mlevel2[i2] + o3; |
| 8164 | mlevel3[i3] = i; |
| 8165 | } |
| 8166 | return result; |
| 8167 | } |
| 8168 | |
| 8169 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8170 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8171 | { |
| 8172 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8173 | int l1 = c>>11; |
| 8174 | int l2 = (c>>7) & 0xF; |
| 8175 | int l3 = c & 0x7F; |
| 8176 | int i; |
| 8177 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8178 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8179 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8180 | if (c == 0) |
| 8181 | return 0; |
| 8182 | /* level 1*/ |
| 8183 | i = map->level1[l1]; |
| 8184 | if (i == 0xFF) { |
| 8185 | return -1; |
| 8186 | } |
| 8187 | /* level 2*/ |
| 8188 | i = map->level23[16*i+l2]; |
| 8189 | if (i == 0xFF) { |
| 8190 | return -1; |
| 8191 | } |
| 8192 | /* level 3 */ |
| 8193 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8194 | if (i == 0) { |
| 8195 | return -1; |
| 8196 | } |
| 8197 | return i; |
| 8198 | } |
| 8199 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8200 | /* Lookup the character ch in the mapping. If the character |
| 8201 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8202 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8203 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8204 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8205 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8206 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8207 | PyObject *x; |
| 8208 | |
| 8209 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | x = PyObject_GetItem(mapping, w); |
| 8212 | Py_DECREF(w); |
| 8213 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8214 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8215 | /* No mapping found means: mapping is undefined. */ |
| 8216 | PyErr_Clear(); |
| 8217 | x = Py_None; |
| 8218 | Py_INCREF(x); |
| 8219 | return x; |
| 8220 | } else |
| 8221 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8222 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8223 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8224 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8225 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8226 | long value = PyLong_AS_LONG(x); |
| 8227 | if (value < 0 || value > 255) { |
| 8228 | PyErr_SetString(PyExc_TypeError, |
| 8229 | "character mapping must be in range(256)"); |
| 8230 | Py_DECREF(x); |
| 8231 | return NULL; |
| 8232 | } |
| 8233 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8234 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8235 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8236 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 | /* wrong return value */ |
| 8239 | PyErr_Format(PyExc_TypeError, |
| 8240 | "character mapping must return integer, bytes or None, not %.400s", |
| 8241 | x->ob_type->tp_name); |
| 8242 | Py_DECREF(x); |
| 8243 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8244 | } |
| 8245 | } |
| 8246 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8247 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8248 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8249 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8250 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8251 | /* exponentially overallocate to minimize reallocations */ |
| 8252 | if (requiredsize < 2*outsize) |
| 8253 | requiredsize = 2*outsize; |
| 8254 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8255 | return -1; |
| 8256 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8257 | } |
| 8258 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8259 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8261 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8262 | /* lookup the character, put the result in the output string and adjust |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 8263 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8264 | space is available. Return a new reference to the object that |
| 8265 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8266 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8267 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8268 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8269 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8270 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8271 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8272 | PyObject *rep; |
| 8273 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8274 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8276 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8277 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8279 | if (res == -1) |
| 8280 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8281 | if (outsize<requiredsize) |
| 8282 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8283 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8284 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | outstart[(*outpos)++] = (char)res; |
| 8286 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8287 | } |
| 8288 | |
| 8289 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8290 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8291 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8292 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8293 | Py_DECREF(rep); |
| 8294 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8295 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8296 | if (PyLong_Check(rep)) { |
| 8297 | Py_ssize_t requiredsize = *outpos+1; |
| 8298 | if (outsize<requiredsize) |
| 8299 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8300 | Py_DECREF(rep); |
| 8301 | return enc_EXCEPTION; |
| 8302 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8303 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8305 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8306 | else { |
| 8307 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8308 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8309 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8310 | if (outsize<requiredsize) |
| 8311 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8312 | Py_DECREF(rep); |
| 8313 | return enc_EXCEPTION; |
| 8314 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8315 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8316 | memcpy(outstart + *outpos, repchars, repsize); |
| 8317 | *outpos += repsize; |
| 8318 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8319 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8320 | Py_DECREF(rep); |
| 8321 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8322 | } |
| 8323 | |
| 8324 | /* handle an error in PyUnicode_EncodeCharmap |
| 8325 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8326 | static int |
| 8327 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8328 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8330 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8331 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8332 | { |
| 8333 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8334 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8335 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8336 | enum PyUnicode_Kind kind; |
| 8337 | void *data; |
| 8338 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8339 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8340 | Py_ssize_t collstartpos = *inpos; |
| 8341 | Py_ssize_t collendpos = *inpos+1; |
| 8342 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8343 | char *encoding = "charmap"; |
| 8344 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8345 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8346 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8347 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8348 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8349 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8350 | return -1; |
| 8351 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | /* find all unencodable characters */ |
| 8353 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8354 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8355 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8356 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8357 | val = encoding_map_lookup(ch, mapping); |
| 8358 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8359 | break; |
| 8360 | ++collendpos; |
| 8361 | continue; |
| 8362 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8363 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8364 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8365 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | if (rep==NULL) |
| 8367 | return -1; |
| 8368 | else if (rep!=Py_None) { |
| 8369 | Py_DECREF(rep); |
| 8370 | break; |
| 8371 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8372 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8373 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8374 | } |
| 8375 | /* cache callback name lookup |
| 8376 | * (if not done yet, i.e. it's the first error) */ |
| 8377 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8379 | *known_errorHandler = 1; |
| 8380 | else if (!strcmp(errors, "replace")) |
| 8381 | *known_errorHandler = 2; |
| 8382 | else if (!strcmp(errors, "ignore")) |
| 8383 | *known_errorHandler = 3; |
| 8384 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8385 | *known_errorHandler = 4; |
| 8386 | else |
| 8387 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8388 | } |
| 8389 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8390 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8391 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8392 | return -1; |
| 8393 | case 2: /* replace */ |
| 8394 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8395 | x = charmapencode_output('?', mapping, res, respos); |
| 8396 | if (x==enc_EXCEPTION) { |
| 8397 | return -1; |
| 8398 | } |
| 8399 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8400 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | return -1; |
| 8402 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8403 | } |
| 8404 | /* fall through */ |
| 8405 | case 3: /* ignore */ |
| 8406 | *inpos = collendpos; |
| 8407 | break; |
| 8408 | case 4: /* xmlcharrefreplace */ |
| 8409 | /* generate replacement (temporarily (mis)uses p) */ |
| 8410 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8411 | char buffer[2+29+1+1]; |
| 8412 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8413 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | for (cp = buffer; *cp; ++cp) { |
| 8415 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8416 | if (x==enc_EXCEPTION) |
| 8417 | return -1; |
| 8418 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8419 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8420 | return -1; |
| 8421 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8422 | } |
| 8423 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8424 | *inpos = collendpos; |
| 8425 | break; |
| 8426 | default: |
| 8427 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8428 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8429 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8430 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8432 | if (PyBytes_Check(repunicode)) { |
| 8433 | /* Directly copy bytes result to output. */ |
| 8434 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8435 | Py_ssize_t requiredsize; |
| 8436 | repsize = PyBytes_Size(repunicode); |
| 8437 | requiredsize = *respos + repsize; |
| 8438 | if (requiredsize > outsize) |
| 8439 | /* Make room for all additional bytes. */ |
| 8440 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8441 | Py_DECREF(repunicode); |
| 8442 | return -1; |
| 8443 | } |
| 8444 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8445 | PyBytes_AsString(repunicode), repsize); |
| 8446 | *respos += repsize; |
| 8447 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8448 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8449 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8450 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8451 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8452 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8453 | Py_DECREF(repunicode); |
| 8454 | return -1; |
| 8455 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8456 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8457 | data = PyUnicode_DATA(repunicode); |
| 8458 | kind = PyUnicode_KIND(repunicode); |
| 8459 | for (index = 0; index < repsize; index++) { |
| 8460 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8461 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8462 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8463 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8464 | return -1; |
| 8465 | } |
| 8466 | else if (x==enc_FAILED) { |
| 8467 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8468 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | return -1; |
| 8470 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8471 | } |
| 8472 | *inpos = newpos; |
| 8473 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8474 | } |
| 8475 | return 0; |
| 8476 | } |
| 8477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8478 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8479 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8480 | PyObject *mapping, |
| 8481 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8482 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8483 | /* output object */ |
| 8484 | PyObject *res = NULL; |
| 8485 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8486 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8487 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8488 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8489 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8490 | PyObject *errorHandler = NULL; |
| 8491 | PyObject *exc = NULL; |
| 8492 | /* the following variable is used for caching string comparisons |
| 8493 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8494 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8495 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8496 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8497 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8498 | return NULL; |
| 8499 | size = PyUnicode_GET_LENGTH(unicode); |
| 8500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8501 | /* Default to Latin-1 */ |
| 8502 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8503 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8505 | /* allocate enough for a simple encoding without |
| 8506 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8507 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8508 | if (res == NULL) |
| 8509 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8510 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8511 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8512 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8513 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8514 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8516 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | if (x==enc_EXCEPTION) /* error */ |
| 8518 | goto onError; |
| 8519 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8520 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8521 | &exc, |
| 8522 | &known_errorHandler, &errorHandler, errors, |
| 8523 | &res, &respos)) { |
| 8524 | goto onError; |
| 8525 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8526 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | else |
| 8528 | /* done with this character => adjust input position */ |
| 8529 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8530 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8531 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8532 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8533 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8534 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8535 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8536 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8537 | Py_XDECREF(exc); |
| 8538 | Py_XDECREF(errorHandler); |
| 8539 | return res; |
| 8540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8541 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8542 | Py_XDECREF(res); |
| 8543 | Py_XDECREF(exc); |
| 8544 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8545 | return NULL; |
| 8546 | } |
| 8547 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8548 | /* Deprecated */ |
| 8549 | PyObject * |
| 8550 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8551 | Py_ssize_t size, |
| 8552 | PyObject *mapping, |
| 8553 | const char *errors) |
| 8554 | { |
| 8555 | PyObject *result; |
| 8556 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8557 | if (unicode == NULL) |
| 8558 | return NULL; |
| 8559 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8560 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8561 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8562 | } |
| 8563 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8564 | PyObject * |
| 8565 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8566 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8567 | { |
| 8568 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | PyErr_BadArgument(); |
| 8570 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8571 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8572 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8573 | } |
| 8574 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8575 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8576 | static void |
| 8577 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8579 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8580 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8581 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8582 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8584 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | } |
| 8586 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8587 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8588 | goto onError; |
| 8589 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8590 | goto onError; |
| 8591 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8592 | goto onError; |
| 8593 | return; |
| 8594 | onError: |
| 8595 | Py_DECREF(*exceptionObject); |
| 8596 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8597 | } |
| 8598 | } |
| 8599 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8600 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8601 | static void |
| 8602 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8603 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8604 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8605 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8606 | { |
| 8607 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8609 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
| 8613 | /* error handling callback helper: |
| 8614 | build arguments, call the callback and check the arguments, |
| 8615 | put the result into newpos and return the replacement string, which |
| 8616 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8617 | static PyObject * |
| 8618 | unicode_translate_call_errorhandler(const char *errors, |
| 8619 | PyObject **errorHandler, |
| 8620 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8621 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8622 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8623 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8624 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8625 | static char *argparse = "O!n;translating error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8626 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8627 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8628 | PyObject *restuple; |
| 8629 | PyObject *resunicode; |
| 8630 | |
| 8631 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8633 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8634 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8635 | } |
| 8636 | |
| 8637 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8638 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8639 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8640 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8641 | |
| 8642 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8643 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8644 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8645 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8646 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8647 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8648 | Py_DECREF(restuple); |
| 8649 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8650 | } |
| 8651 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8652 | &resunicode, &i_newpos)) { |
| 8653 | Py_DECREF(restuple); |
| 8654 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8655 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8656 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8658 | else |
| 8659 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8660 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8661 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8662 | Py_DECREF(restuple); |
| 8663 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8664 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8665 | Py_INCREF(resunicode); |
| 8666 | Py_DECREF(restuple); |
| 8667 | return resunicode; |
| 8668 | } |
| 8669 | |
| 8670 | /* Lookup the character ch in the mapping and put the result in result, |
| 8671 | which must be decrefed by the caller. |
| 8672 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8673 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8674 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8675 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8676 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8677 | PyObject *x; |
| 8678 | |
| 8679 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8680 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8681 | x = PyObject_GetItem(mapping, w); |
| 8682 | Py_DECREF(w); |
| 8683 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8684 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8685 | /* No mapping found means: use 1:1 mapping. */ |
| 8686 | PyErr_Clear(); |
| 8687 | *result = NULL; |
| 8688 | return 0; |
| 8689 | } else |
| 8690 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8691 | } |
| 8692 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | *result = x; |
| 8694 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8695 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8696 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8697 | long value = PyLong_AS_LONG(x); |
| 8698 | long max = PyUnicode_GetMax(); |
| 8699 | if (value < 0 || value > max) { |
| 8700 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8701 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8702 | Py_DECREF(x); |
| 8703 | return -1; |
| 8704 | } |
| 8705 | *result = x; |
| 8706 | return 0; |
| 8707 | } |
| 8708 | else if (PyUnicode_Check(x)) { |
| 8709 | *result = x; |
| 8710 | return 0; |
| 8711 | } |
| 8712 | else { |
| 8713 | /* wrong return value */ |
| 8714 | PyErr_SetString(PyExc_TypeError, |
| 8715 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8716 | Py_DECREF(x); |
| 8717 | return -1; |
| 8718 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8719 | } |
| 8720 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | if not reallocate and adjust various state variables. |
| 8722 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8723 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8724 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8725 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8726 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8727 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8728 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8729 | /* exponentially overallocate to minimize reallocations */ |
| 8730 | if (requiredsize < 2 * oldsize) |
| 8731 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8732 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8733 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8735 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8736 | } |
| 8737 | return 0; |
| 8738 | } |
| 8739 | /* lookup the character, put the result in the output string and adjust |
| 8740 | various state variables. Return a new reference to the object that |
| 8741 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8742 | undefined (in which case no character was written). |
| 8743 | The called must decref result. |
| 8744 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8745 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8747 | PyObject *mapping, Py_UCS4 **output, |
| 8748 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8749 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8750 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8751 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8752 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8753 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8754 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8755 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8756 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8757 | } |
| 8758 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8759 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8760 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8761 | /* no overflow check, because we know that the space is enough */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8762 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8763 | } |
| 8764 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | Py_ssize_t repsize; |
| 8766 | if (PyUnicode_READY(*res) == -1) |
| 8767 | return -1; |
| 8768 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8769 | if (repsize==1) { |
| 8770 | /* no overflow check, because we know that the space is enough */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8771 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8772 | } |
| 8773 | else if (repsize!=0) { |
| 8774 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8775 | Py_ssize_t requiredsize = *opos + |
| 8776 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8777 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8778 | Py_ssize_t i; |
| 8779 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8780 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8781 | for(i = 0; i < repsize; i++) |
| 8782 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8783 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8784 | } |
| 8785 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8786 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8787 | return 0; |
| 8788 | } |
| 8789 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8790 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8792 | PyObject *mapping, |
| 8793 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8794 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8795 | /* input object */ |
| 8796 | char *idata; |
| 8797 | Py_ssize_t size, i; |
| 8798 | int kind; |
| 8799 | /* output buffer */ |
| 8800 | Py_UCS4 *output = NULL; |
| 8801 | Py_ssize_t osize; |
| 8802 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8803 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8804 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8805 | char *reason = "character maps to <undefined>"; |
| 8806 | PyObject *errorHandler = NULL; |
| 8807 | PyObject *exc = NULL; |
| 8808 | /* the following variable is used for caching string comparisons |
| 8809 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8810 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8811 | int known_errorHandler = -1; |
| 8812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8813 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8814 | PyErr_BadArgument(); |
| 8815 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8816 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8818 | if (PyUnicode_READY(input) == -1) |
| 8819 | return NULL; |
| 8820 | idata = (char*)PyUnicode_DATA(input); |
| 8821 | kind = PyUnicode_KIND(input); |
| 8822 | size = PyUnicode_GET_LENGTH(input); |
| 8823 | i = 0; |
| 8824 | |
| 8825 | if (size == 0) { |
| 8826 | Py_INCREF(input); |
| 8827 | return input; |
| 8828 | } |
| 8829 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8830 | /* allocate enough for a simple 1:1 translation without |
| 8831 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | osize = size; |
| 8833 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8834 | opos = 0; |
| 8835 | if (output == NULL) { |
| 8836 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8837 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8838 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8840 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8841 | /* try to encode it */ |
| 8842 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8843 | if (charmaptranslate_output(input, i, mapping, |
| 8844 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8845 | Py_XDECREF(x); |
| 8846 | goto onError; |
| 8847 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8848 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8849 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8850 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8851 | else { /* untranslatable character */ |
| 8852 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8853 | Py_ssize_t repsize; |
| 8854 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8855 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8856 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8857 | Py_ssize_t collstart = i; |
| 8858 | Py_ssize_t collend = i+1; |
| 8859 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8861 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8862 | while (collend < size) { |
| 8863 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | goto onError; |
| 8865 | Py_XDECREF(x); |
| 8866 | if (x!=Py_None) |
| 8867 | break; |
| 8868 | ++collend; |
| 8869 | } |
| 8870 | /* cache callback name lookup |
| 8871 | * (if not done yet, i.e. it's the first error) */ |
| 8872 | if (known_errorHandler==-1) { |
| 8873 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8874 | known_errorHandler = 1; |
| 8875 | else if (!strcmp(errors, "replace")) |
| 8876 | known_errorHandler = 2; |
| 8877 | else if (!strcmp(errors, "ignore")) |
| 8878 | known_errorHandler = 3; |
| 8879 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8880 | known_errorHandler = 4; |
| 8881 | else |
| 8882 | known_errorHandler = 0; |
| 8883 | } |
| 8884 | switch (known_errorHandler) { |
| 8885 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8886 | raise_translate_exception(&exc, input, collstart, |
| 8887 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8888 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | case 2: /* replace */ |
| 8890 | /* No need to check for space, this is a 1:1 replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8891 | for (coll = collstart; coll<collend; coll++) |
| 8892 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8893 | /* fall through */ |
| 8894 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8895 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8896 | break; |
| 8897 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8898 | /* generate replacement (temporarily (mis)uses i) */ |
| 8899 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8900 | char buffer[2+29+1+1]; |
| 8901 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8902 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8903 | if (charmaptranslate_makespace(&output, &osize, |
| 8904 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8905 | goto onError; |
| 8906 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8907 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8908 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8909 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8910 | break; |
| 8911 | default: |
| 8912 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8913 | reason, input, &exc, |
| 8914 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8915 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8916 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8917 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8918 | Py_DECREF(repunicode); |
| 8919 | goto onError; |
| 8920 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8921 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8922 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8923 | if (charmaptranslate_makespace(&output, &osize, |
| 8924 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8925 | Py_DECREF(repunicode); |
| 8926 | goto onError; |
| 8927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8929 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8930 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8931 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8932 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8933 | } |
| 8934 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8935 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8936 | if (!res) |
| 8937 | goto onError; |
| 8938 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8939 | Py_XDECREF(exc); |
| 8940 | Py_XDECREF(errorHandler); |
| 8941 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8943 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8944 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8945 | Py_XDECREF(exc); |
| 8946 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8947 | return NULL; |
| 8948 | } |
| 8949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8950 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8951 | PyObject * |
| 8952 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8953 | Py_ssize_t size, |
| 8954 | PyObject *mapping, |
| 8955 | const char *errors) |
| 8956 | { |
| 8957 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8958 | if (!unicode) |
| 8959 | return NULL; |
| 8960 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8961 | } |
| 8962 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8963 | PyObject * |
| 8964 | PyUnicode_Translate(PyObject *str, |
| 8965 | PyObject *mapping, |
| 8966 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | { |
| 8968 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8970 | str = PyUnicode_FromObject(str); |
| 8971 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8972 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8974 | Py_DECREF(str); |
| 8975 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8976 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8977 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | Py_XDECREF(str); |
| 8979 | return NULL; |
| 8980 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8982 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8983 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8984 | { |
| 8985 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8986 | called as a callback from fixup() which does it already. */ |
| 8987 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8988 | const int kind = PyUnicode_KIND(self); |
| 8989 | void *data = PyUnicode_DATA(self); |
| 8990 | Py_UCS4 maxchar = 0, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8991 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | Py_ssize_t i; |
| 8993 | |
| 8994 | for (i = 0; i < len; ++i) { |
| 8995 | ch = PyUnicode_READ(kind, data, i); |
| 8996 | fixed = 0; |
| 8997 | if (ch > 127) { |
| 8998 | if (Py_UNICODE_ISSPACE(ch)) |
| 8999 | fixed = ' '; |
| 9000 | else { |
| 9001 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9002 | if (decimal >= 0) |
| 9003 | fixed = '0' + decimal; |
| 9004 | } |
| 9005 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9006 | modified = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9007 | if (fixed > maxchar) |
| 9008 | maxchar = fixed; |
| 9009 | PyUnicode_WRITE(kind, data, i, fixed); |
| 9010 | } |
| 9011 | else if (ch > maxchar) |
| 9012 | maxchar = ch; |
| 9013 | } |
| 9014 | else if (ch > maxchar) |
| 9015 | maxchar = ch; |
| 9016 | } |
| 9017 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9018 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9019 | } |
| 9020 | |
| 9021 | PyObject * |
| 9022 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 9023 | { |
| 9024 | if (!PyUnicode_Check(unicode)) { |
| 9025 | PyErr_BadInternalCall(); |
| 9026 | return NULL; |
| 9027 | } |
| 9028 | if (PyUnicode_READY(unicode) == -1) |
| 9029 | return NULL; |
| 9030 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 9031 | /* If the string is already ASCII, just return the same string */ |
| 9032 | Py_INCREF(unicode); |
| 9033 | return unicode; |
| 9034 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9035 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9036 | } |
| 9037 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9038 | PyObject * |
| 9039 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9040 | Py_ssize_t length) |
| 9041 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9042 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9043 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9044 | Py_UCS4 maxchar; |
| 9045 | enum PyUnicode_Kind kind; |
| 9046 | void *data; |
| 9047 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9048 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9049 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9050 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9051 | if (ch > 127) { |
| 9052 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9053 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9054 | ch = '0' + decimal; |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9055 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9056 | } |
| 9057 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9058 | |
| 9059 | /* Copy to a new string */ |
| 9060 | decimal = PyUnicode_New(length, maxchar); |
| 9061 | if (decimal == NULL) |
| 9062 | return decimal; |
| 9063 | kind = PyUnicode_KIND(decimal); |
| 9064 | data = PyUnicode_DATA(decimal); |
| 9065 | /* Iterate over code points */ |
| 9066 | for (i = 0; i < length; i++) { |
| 9067 | Py_UNICODE ch = s[i]; |
| 9068 | if (ch > 127) { |
| 9069 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9070 | if (decimal >= 0) |
| 9071 | ch = '0' + decimal; |
| 9072 | } |
| 9073 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9074 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9075 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9076 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9077 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9078 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9079 | int |
| 9080 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9081 | Py_ssize_t length, |
| 9082 | char *output, |
| 9083 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9084 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9085 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9086 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9087 | enum PyUnicode_Kind kind; |
| 9088 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9089 | |
| 9090 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9091 | PyErr_BadArgument(); |
| 9092 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9093 | } |
| 9094 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9095 | unicode = PyUnicode_FromUnicode(s, length); |
| 9096 | if (unicode == NULL) |
| 9097 | return -1; |
| 9098 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9099 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9100 | Py_DECREF(unicode); |
| 9101 | return -1; |
| 9102 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9103 | kind = PyUnicode_KIND(unicode); |
| 9104 | data = PyUnicode_DATA(unicode); |
| 9105 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9106 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9107 | PyObject *exc; |
| 9108 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9109 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9110 | Py_ssize_t startpos; |
| 9111 | |
| 9112 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9113 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9114 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9115 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9116 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9117 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9118 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9119 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9120 | if (decimal >= 0) { |
| 9121 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9122 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9123 | continue; |
| 9124 | } |
| 9125 | if (0 < ch && ch < 256) { |
| 9126 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9127 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9128 | continue; |
| 9129 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9130 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9131 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9132 | exc = NULL; |
| 9133 | raise_encode_exception(&exc, "decimal", unicode, |
| 9134 | startpos, startpos+1, |
| 9135 | "invalid decimal Unicode string"); |
| 9136 | Py_XDECREF(exc); |
| 9137 | Py_DECREF(unicode); |
| 9138 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9139 | } |
| 9140 | /* 0-terminate the output string */ |
| 9141 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9142 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9143 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9144 | } |
| 9145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | /* --- Helpers ------------------------------------------------------------ */ |
| 9147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9148 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9149 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9150 | Py_ssize_t start, |
| 9151 | Py_ssize_t end) |
| 9152 | { |
| 9153 | int kind1, kind2, kind; |
| 9154 | void *buf1, *buf2; |
| 9155 | Py_ssize_t len1, len2, result; |
| 9156 | |
| 9157 | kind1 = PyUnicode_KIND(s1); |
| 9158 | kind2 = PyUnicode_KIND(s2); |
| 9159 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9160 | buf1 = PyUnicode_DATA(s1); |
| 9161 | buf2 = PyUnicode_DATA(s2); |
| 9162 | if (kind1 != kind) |
| 9163 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9164 | if (!buf1) |
| 9165 | return -2; |
| 9166 | if (kind2 != kind) |
| 9167 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9168 | if (!buf2) { |
| 9169 | if (kind1 != kind) PyMem_Free(buf1); |
| 9170 | return -2; |
| 9171 | } |
| 9172 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9173 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9174 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9175 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9176 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9177 | case PyUnicode_1BYTE_KIND: |
| 9178 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9179 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9180 | else |
| 9181 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9182 | break; |
| 9183 | case PyUnicode_2BYTE_KIND: |
| 9184 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9185 | break; |
| 9186 | case PyUnicode_4BYTE_KIND: |
| 9187 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9188 | break; |
| 9189 | default: |
| 9190 | assert(0); result = -2; |
| 9191 | } |
| 9192 | } |
| 9193 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9194 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9195 | case PyUnicode_1BYTE_KIND: |
| 9196 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9197 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9198 | else |
| 9199 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9200 | break; |
| 9201 | case PyUnicode_2BYTE_KIND: |
| 9202 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9203 | break; |
| 9204 | case PyUnicode_4BYTE_KIND: |
| 9205 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9206 | break; |
| 9207 | default: |
| 9208 | assert(0); result = -2; |
| 9209 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9210 | } |
| 9211 | |
| 9212 | if (kind1 != kind) |
| 9213 | PyMem_Free(buf1); |
| 9214 | if (kind2 != kind) |
| 9215 | PyMem_Free(buf2); |
| 9216 | |
| 9217 | return result; |
| 9218 | } |
| 9219 | |
| 9220 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9221 | _PyUnicode_InsertThousandsGrouping( |
| 9222 | PyObject *unicode, Py_ssize_t index, |
| 9223 | Py_ssize_t n_buffer, |
| 9224 | void *digits, Py_ssize_t n_digits, |
| 9225 | Py_ssize_t min_width, |
| 9226 | const char *grouping, PyObject *thousands_sep, |
| 9227 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9228 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9229 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9230 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9231 | Py_ssize_t thousands_sep_len; |
| 9232 | Py_ssize_t len; |
| 9233 | |
| 9234 | if (unicode != NULL) { |
| 9235 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9236 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9237 | } |
| 9238 | else { |
| 9239 | kind = PyUnicode_1BYTE_KIND; |
| 9240 | data = NULL; |
| 9241 | } |
| 9242 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9243 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9244 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9245 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9246 | if (thousands_sep_kind < kind) { |
| 9247 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9248 | if (!thousands_sep_data) |
| 9249 | return -1; |
| 9250 | } |
| 9251 | else { |
| 9252 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9253 | if (!data) |
| 9254 | return -1; |
| 9255 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9256 | } |
| 9257 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9258 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9259 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9260 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9261 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9262 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9263 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9264 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9265 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9266 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9267 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9268 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9269 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9270 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9271 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9272 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9273 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9274 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9275 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9276 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9278 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9279 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9280 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9281 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9282 | break; |
| 9283 | default: |
| 9284 | assert(0); |
| 9285 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9286 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9287 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9288 | if (thousands_sep_kind < kind) |
| 9289 | PyMem_Free(thousands_sep_data); |
| 9290 | else |
| 9291 | PyMem_Free(data); |
| 9292 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9293 | if (unicode == NULL) { |
| 9294 | *maxchar = 127; |
| 9295 | if (len != n_digits) { |
| 9296 | *maxchar = Py_MAX(*maxchar, |
| 9297 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
| 9298 | } |
| 9299 | } |
| 9300 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9301 | } |
| 9302 | |
| 9303 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9304 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9305 | #define ADJUST_INDICES(start, end, len) \ |
| 9306 | if (end > len) \ |
| 9307 | end = len; \ |
| 9308 | else if (end < 0) { \ |
| 9309 | end += len; \ |
| 9310 | if (end < 0) \ |
| 9311 | end = 0; \ |
| 9312 | } \ |
| 9313 | if (start < 0) { \ |
| 9314 | start += len; \ |
| 9315 | if (start < 0) \ |
| 9316 | start = 0; \ |
| 9317 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9318 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9319 | Py_ssize_t |
| 9320 | PyUnicode_Count(PyObject *str, |
| 9321 | PyObject *substr, |
| 9322 | Py_ssize_t start, |
| 9323 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9324 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9325 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9326 | PyObject* str_obj; |
| 9327 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | int kind1, kind2, kind; |
| 9329 | void *buf1 = NULL, *buf2 = NULL; |
| 9330 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9331 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9332 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9333 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9334 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9335 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9336 | if (!sub_obj) { |
| 9337 | Py_DECREF(str_obj); |
| 9338 | return -1; |
| 9339 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9340 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9341 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9342 | Py_DECREF(str_obj); |
| 9343 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9344 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9346 | kind1 = PyUnicode_KIND(str_obj); |
| 9347 | kind2 = PyUnicode_KIND(sub_obj); |
| 9348 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9349 | buf1 = PyUnicode_DATA(str_obj); |
| 9350 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9351 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9352 | if (!buf1) |
| 9353 | goto onError; |
| 9354 | buf2 = PyUnicode_DATA(sub_obj); |
| 9355 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9356 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9357 | if (!buf2) |
| 9358 | goto onError; |
| 9359 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9360 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9361 | |
| 9362 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9363 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9364 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9365 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9366 | result = asciilib_count( |
| 9367 | ((Py_UCS1*)buf1) + start, end - start, |
| 9368 | buf2, len2, PY_SSIZE_T_MAX |
| 9369 | ); |
| 9370 | else |
| 9371 | result = ucs1lib_count( |
| 9372 | ((Py_UCS1*)buf1) + start, end - start, |
| 9373 | buf2, len2, PY_SSIZE_T_MAX |
| 9374 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9375 | break; |
| 9376 | case PyUnicode_2BYTE_KIND: |
| 9377 | result = ucs2lib_count( |
| 9378 | ((Py_UCS2*)buf1) + start, end - start, |
| 9379 | buf2, len2, PY_SSIZE_T_MAX |
| 9380 | ); |
| 9381 | break; |
| 9382 | case PyUnicode_4BYTE_KIND: |
| 9383 | result = ucs4lib_count( |
| 9384 | ((Py_UCS4*)buf1) + start, end - start, |
| 9385 | buf2, len2, PY_SSIZE_T_MAX |
| 9386 | ); |
| 9387 | break; |
| 9388 | default: |
| 9389 | assert(0); result = 0; |
| 9390 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9391 | |
| 9392 | Py_DECREF(sub_obj); |
| 9393 | Py_DECREF(str_obj); |
| 9394 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9395 | if (kind1 != kind) |
| 9396 | PyMem_Free(buf1); |
| 9397 | if (kind2 != kind) |
| 9398 | PyMem_Free(buf2); |
| 9399 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9401 | onError: |
| 9402 | Py_DECREF(sub_obj); |
| 9403 | Py_DECREF(str_obj); |
| 9404 | if (kind1 != kind && buf1) |
| 9405 | PyMem_Free(buf1); |
| 9406 | if (kind2 != kind && buf2) |
| 9407 | PyMem_Free(buf2); |
| 9408 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9409 | } |
| 9410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9411 | Py_ssize_t |
| 9412 | PyUnicode_Find(PyObject *str, |
| 9413 | PyObject *sub, |
| 9414 | Py_ssize_t start, |
| 9415 | Py_ssize_t end, |
| 9416 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9417 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9418 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9420 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9421 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9422 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9423 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9424 | if (!sub) { |
| 9425 | Py_DECREF(str); |
| 9426 | return -2; |
| 9427 | } |
| 9428 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9429 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9430 | Py_DECREF(str); |
| 9431 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9432 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9433 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9434 | result = any_find_slice(direction, |
| 9435 | str, sub, start, end |
| 9436 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9438 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9439 | Py_DECREF(sub); |
| 9440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9441 | return result; |
| 9442 | } |
| 9443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9444 | Py_ssize_t |
| 9445 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9446 | Py_ssize_t start, Py_ssize_t end, |
| 9447 | int direction) |
| 9448 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9449 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9450 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9451 | if (PyUnicode_READY(str) == -1) |
| 9452 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9453 | if (start < 0 || end < 0) { |
| 9454 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9455 | return -2; |
| 9456 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9457 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9458 | end = PyUnicode_GET_LENGTH(str); |
| 9459 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9460 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9461 | kind, end-start, ch, direction); |
| 9462 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9464 | else |
| 9465 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9466 | } |
| 9467 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9468 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9469 | tailmatch(PyObject *self, |
| 9470 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9471 | Py_ssize_t start, |
| 9472 | Py_ssize_t end, |
| 9473 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9474 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | int kind_self; |
| 9476 | int kind_sub; |
| 9477 | void *data_self; |
| 9478 | void *data_sub; |
| 9479 | Py_ssize_t offset; |
| 9480 | Py_ssize_t i; |
| 9481 | Py_ssize_t end_sub; |
| 9482 | |
| 9483 | if (PyUnicode_READY(self) == -1 || |
| 9484 | PyUnicode_READY(substring) == -1) |
| 9485 | return 0; |
| 9486 | |
| 9487 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9488 | return 1; |
| 9489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9490 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9491 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9493 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | kind_self = PyUnicode_KIND(self); |
| 9496 | data_self = PyUnicode_DATA(self); |
| 9497 | kind_sub = PyUnicode_KIND(substring); |
| 9498 | data_sub = PyUnicode_DATA(substring); |
| 9499 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9500 | |
| 9501 | if (direction > 0) |
| 9502 | offset = end; |
| 9503 | else |
| 9504 | offset = start; |
| 9505 | |
| 9506 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9507 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9508 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9509 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9510 | /* If both are of the same kind, memcmp is sufficient */ |
| 9511 | if (kind_self == kind_sub) { |
| 9512 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9513 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9514 | data_sub, |
| 9515 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9516 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9517 | } |
| 9518 | /* otherwise we have to compare each character by first accesing it */ |
| 9519 | else { |
| 9520 | /* We do not need to compare 0 and len(substring)-1 because |
| 9521 | the if statement above ensured already that they are equal |
| 9522 | when we end up here. */ |
| 9523 | // TODO: honor direction and do a forward or backwards search |
| 9524 | for (i = 1; i < end_sub; ++i) { |
| 9525 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9526 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9527 | return 0; |
| 9528 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9529 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
| 9533 | return 0; |
| 9534 | } |
| 9535 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9536 | Py_ssize_t |
| 9537 | PyUnicode_Tailmatch(PyObject *str, |
| 9538 | PyObject *substr, |
| 9539 | Py_ssize_t start, |
| 9540 | Py_ssize_t end, |
| 9541 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9542 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9543 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9545 | str = PyUnicode_FromObject(str); |
| 9546 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9547 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | substr = PyUnicode_FromObject(substr); |
| 9549 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9550 | Py_DECREF(str); |
| 9551 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9552 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9553 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9554 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9555 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9556 | Py_DECREF(str); |
| 9557 | Py_DECREF(substr); |
| 9558 | return result; |
| 9559 | } |
| 9560 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9561 | /* Apply fixfct filter to the Unicode object self and return a |
| 9562 | reference to the modified object */ |
| 9563 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9564 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9565 | fixup(PyObject *self, |
| 9566 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9567 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9568 | PyObject *u; |
| 9569 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9570 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9571 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9572 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9573 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9574 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9575 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9576 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9577 | /* fix functions return the new maximum character in a string, |
| 9578 | if the kind of the resulting unicode object does not change, |
| 9579 | everything is fine. Otherwise we need to change the string kind |
| 9580 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9581 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9582 | |
| 9583 | if (maxchar_new == 0) { |
| 9584 | /* no changes */; |
| 9585 | if (PyUnicode_CheckExact(self)) { |
| 9586 | Py_DECREF(u); |
| 9587 | Py_INCREF(self); |
| 9588 | return self; |
| 9589 | } |
| 9590 | else |
| 9591 | return u; |
| 9592 | } |
| 9593 | |
| 9594 | if (maxchar_new <= 127) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9595 | maxchar_new = 127; |
| 9596 | else if (maxchar_new <= 255) |
| 9597 | maxchar_new = 255; |
| 9598 | else if (maxchar_new <= 65535) |
| 9599 | maxchar_new = 65535; |
| 9600 | else |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 9601 | maxchar_new = MAX_UNICODE; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9602 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9603 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9604 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9605 | |
| 9606 | /* In case the maximum character changed, we need to |
| 9607 | convert the string to the new category. */ |
| 9608 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9609 | if (v == NULL) { |
| 9610 | Py_DECREF(u); |
| 9611 | return NULL; |
| 9612 | } |
| 9613 | if (maxchar_new > maxchar_old) { |
| 9614 | /* If the maxchar increased so that the kind changed, not all |
| 9615 | characters are representable anymore and we need to fix the |
| 9616 | string again. This only happens in very few cases. */ |
| 9617 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9618 | maxchar_old = fixfct(v); |
| 9619 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9620 | } |
| 9621 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9622 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9623 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9624 | Py_DECREF(u); |
| 9625 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9626 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9627 | } |
| 9628 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9629 | static PyObject * |
| 9630 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9632 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9633 | char *resdata, *data = PyUnicode_DATA(self); |
| 9634 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9635 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9636 | res = PyUnicode_New(len, 127); |
| 9637 | if (res == NULL) |
| 9638 | return NULL; |
| 9639 | resdata = PyUnicode_DATA(res); |
| 9640 | if (lower) |
| 9641 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9642 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9643 | _Py_bytes_upper(resdata, data, len); |
| 9644 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9647 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9648 | handle_capital_sigma(int kind, void *data, Py_ssize_t length, Py_ssize_t i) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9649 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9650 | Py_ssize_t j; |
| 9651 | int final_sigma; |
| 9652 | Py_UCS4 c; |
| 9653 | /* U+03A3 is in the Final_Sigma context when, it is found like this: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9654 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9655 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9656 | |
| 9657 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9658 | */ |
| 9659 | for (j = i - 1; j >= 0; j--) { |
| 9660 | c = PyUnicode_READ(kind, data, j); |
| 9661 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9662 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9663 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9664 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9665 | if (final_sigma) { |
| 9666 | for (j = i + 1; j < length; j++) { |
| 9667 | c = PyUnicode_READ(kind, data, j); |
| 9668 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9669 | break; |
| 9670 | } |
| 9671 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9672 | } |
| 9673 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9674 | } |
| 9675 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9676 | static int |
| 9677 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9678 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9679 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9680 | /* Obscure special case. */ |
| 9681 | if (c == 0x3A3) { |
| 9682 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9683 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9684 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9685 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9686 | } |
| 9687 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9688 | static Py_ssize_t |
| 9689 | do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9690 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9691 | Py_ssize_t i, k = 0; |
| 9692 | int n_res, j; |
| 9693 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9694 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9695 | c = PyUnicode_READ(kind, data, 0); |
| 9696 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9697 | for (j = 0; j < n_res; j++) { |
| 9698 | if (mapped[j] > *maxchar) |
| 9699 | *maxchar = mapped[j]; |
| 9700 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9701 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9702 | for (i = 1; i < length; i++) { |
| 9703 | c = PyUnicode_READ(kind, data, i); |
| 9704 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9705 | for (j = 0; j < n_res; j++) { |
| 9706 | if (mapped[j] > *maxchar) |
| 9707 | *maxchar = mapped[j]; |
| 9708 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9709 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9710 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9711 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9714 | static Py_ssize_t |
| 9715 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9716 | Py_ssize_t i, k = 0; |
| 9717 | |
| 9718 | for (i = 0; i < length; i++) { |
| 9719 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9720 | int n_res, j; |
| 9721 | if (Py_UNICODE_ISUPPER(c)) { |
| 9722 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9723 | } |
| 9724 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9725 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9726 | } |
| 9727 | else { |
| 9728 | n_res = 1; |
| 9729 | mapped[0] = c; |
| 9730 | } |
| 9731 | for (j = 0; j < n_res; j++) { |
| 9732 | if (mapped[j] > *maxchar) |
| 9733 | *maxchar = mapped[j]; |
| 9734 | res[k++] = mapped[j]; |
| 9735 | } |
| 9736 | } |
| 9737 | return k; |
| 9738 | } |
| 9739 | |
| 9740 | static Py_ssize_t |
| 9741 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9742 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9743 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9744 | Py_ssize_t i, k = 0; |
| 9745 | |
| 9746 | for (i = 0; i < length; i++) { |
| 9747 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9748 | int n_res, j; |
| 9749 | if (lower) |
| 9750 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9751 | else |
| 9752 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9753 | for (j = 0; j < n_res; j++) { |
| 9754 | if (mapped[j] > *maxchar) |
| 9755 | *maxchar = mapped[j]; |
| 9756 | res[k++] = mapped[j]; |
| 9757 | } |
| 9758 | } |
| 9759 | return k; |
| 9760 | } |
| 9761 | |
| 9762 | static Py_ssize_t |
| 9763 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9764 | { |
| 9765 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9766 | } |
| 9767 | |
| 9768 | static Py_ssize_t |
| 9769 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9770 | { |
| 9771 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9772 | } |
| 9773 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9774 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9775 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9776 | { |
| 9777 | Py_ssize_t i, k = 0; |
| 9778 | |
| 9779 | for (i = 0; i < length; i++) { |
| 9780 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9781 | Py_UCS4 mapped[3]; |
| 9782 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9783 | for (j = 0; j < n_res; j++) { |
| 9784 | if (mapped[j] > *maxchar) |
| 9785 | *maxchar = mapped[j]; |
| 9786 | res[k++] = mapped[j]; |
| 9787 | } |
| 9788 | } |
| 9789 | return k; |
| 9790 | } |
| 9791 | |
| 9792 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9793 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9794 | { |
| 9795 | Py_ssize_t i, k = 0; |
| 9796 | int previous_is_cased; |
| 9797 | |
| 9798 | previous_is_cased = 0; |
| 9799 | for (i = 0; i < length; i++) { |
| 9800 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9801 | Py_UCS4 mapped[3]; |
| 9802 | int n_res, j; |
| 9803 | |
| 9804 | if (previous_is_cased) |
| 9805 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9806 | else |
| 9807 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9808 | |
| 9809 | for (j = 0; j < n_res; j++) { |
| 9810 | if (mapped[j] > *maxchar) |
| 9811 | *maxchar = mapped[j]; |
| 9812 | res[k++] = mapped[j]; |
| 9813 | } |
| 9814 | |
| 9815 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9816 | } |
| 9817 | return k; |
| 9818 | } |
| 9819 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9820 | static PyObject * |
| 9821 | case_operation(PyObject *self, |
| 9822 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9823 | { |
| 9824 | PyObject *res = NULL; |
| 9825 | Py_ssize_t length, newlength = 0; |
| 9826 | int kind, outkind; |
| 9827 | void *data, *outdata; |
| 9828 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9829 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9830 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9831 | |
| 9832 | kind = PyUnicode_KIND(self); |
| 9833 | data = PyUnicode_DATA(self); |
| 9834 | length = PyUnicode_GET_LENGTH(self); |
| 9835 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9836 | if (tmp == NULL) |
| 9837 | return PyErr_NoMemory(); |
| 9838 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9839 | res = PyUnicode_New(newlength, maxchar); |
| 9840 | if (res == NULL) |
| 9841 | goto leave; |
| 9842 | tmpend = tmp + newlength; |
| 9843 | outdata = PyUnicode_DATA(res); |
| 9844 | outkind = PyUnicode_KIND(res); |
| 9845 | switch (outkind) { |
| 9846 | case PyUnicode_1BYTE_KIND: |
| 9847 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9848 | break; |
| 9849 | case PyUnicode_2BYTE_KIND: |
| 9850 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9851 | break; |
| 9852 | case PyUnicode_4BYTE_KIND: |
| 9853 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9854 | break; |
| 9855 | default: |
| 9856 | assert(0); |
| 9857 | break; |
| 9858 | } |
| 9859 | leave: |
| 9860 | PyMem_FREE(tmp); |
| 9861 | return res; |
| 9862 | } |
| 9863 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9864 | PyObject * |
| 9865 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9866 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9868 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9870 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9871 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9872 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9873 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9874 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9875 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9876 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9877 | int use_memcpy; |
| 9878 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9879 | PyObject *last_obj; |
| 9880 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9881 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9882 | fseq = PySequence_Fast(seq, ""); |
| 9883 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9884 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9885 | } |
| 9886 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9887 | /* NOTE: the following code can't call back into Python code, |
| 9888 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9889 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9890 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9891 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9892 | /* If empty sequence, return u"". */ |
| 9893 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9894 | Py_DECREF(fseq); |
| 9895 | Py_INCREF(unicode_empty); |
| 9896 | res = unicode_empty; |
| 9897 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9898 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9899 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9900 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9901 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9902 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9903 | if (seqlen == 1) { |
| 9904 | if (PyUnicode_CheckExact(items[0])) { |
| 9905 | res = items[0]; |
| 9906 | Py_INCREF(res); |
| 9907 | Py_DECREF(fseq); |
| 9908 | return res; |
| 9909 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9910 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9911 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9912 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9913 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9914 | /* Set up sep and seplen */ |
| 9915 | if (separator == NULL) { |
| 9916 | /* fall back to a blank space separator */ |
| 9917 | sep = PyUnicode_FromOrdinal(' '); |
| 9918 | if (!sep) |
| 9919 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9920 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9921 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9922 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9923 | else { |
| 9924 | if (!PyUnicode_Check(separator)) { |
| 9925 | PyErr_Format(PyExc_TypeError, |
| 9926 | "separator: expected str instance," |
| 9927 | " %.80s found", |
| 9928 | Py_TYPE(separator)->tp_name); |
| 9929 | goto onError; |
| 9930 | } |
| 9931 | if (PyUnicode_READY(separator)) |
| 9932 | goto onError; |
| 9933 | sep = separator; |
| 9934 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9935 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9936 | /* inc refcount to keep this code path symmetric with the |
| 9937 | above case of a blank separator */ |
| 9938 | Py_INCREF(sep); |
| 9939 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9940 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9941 | } |
| 9942 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9943 | /* There are at least two things to join, or else we have a subclass |
| 9944 | * of str in the sequence. |
| 9945 | * Do a pre-pass to figure out the total amount of space we'll |
| 9946 | * need (sz), and see whether all argument are strings. |
| 9947 | */ |
| 9948 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9949 | #ifdef Py_DEBUG |
| 9950 | use_memcpy = 0; |
| 9951 | #else |
| 9952 | use_memcpy = 1; |
| 9953 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9954 | for (i = 0; i < seqlen; i++) { |
| 9955 | const Py_ssize_t old_sz = sz; |
| 9956 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9957 | if (!PyUnicode_Check(item)) { |
| 9958 | PyErr_Format(PyExc_TypeError, |
| 9959 | "sequence item %zd: expected str instance," |
| 9960 | " %.80s found", |
| 9961 | i, Py_TYPE(item)->tp_name); |
| 9962 | goto onError; |
| 9963 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9964 | if (PyUnicode_READY(item) == -1) |
| 9965 | goto onError; |
| 9966 | sz += PyUnicode_GET_LENGTH(item); |
| 9967 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9968 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9969 | if (i != 0) |
| 9970 | sz += seplen; |
| 9971 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9972 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9973 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9974 | goto onError; |
| 9975 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9976 | if (use_memcpy && last_obj != NULL) { |
| 9977 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9978 | use_memcpy = 0; |
| 9979 | } |
| 9980 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9981 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9984 | if (res == NULL) |
| 9985 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9986 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9987 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9988 | #ifdef Py_DEBUG |
| 9989 | use_memcpy = 0; |
| 9990 | #else |
| 9991 | if (use_memcpy) { |
| 9992 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9993 | kind = PyUnicode_KIND(res); |
| 9994 | if (seplen != 0) |
| 9995 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9996 | } |
| 9997 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9999 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10000 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10001 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10002 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10003 | if (use_memcpy) { |
| 10004 | Py_MEMCPY(res_data, |
| 10005 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10006 | kind * seplen); |
| 10007 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10008 | } |
| 10009 | else { |
| 10010 | copy_characters(res, res_offset, sep, 0, seplen); |
| 10011 | res_offset += seplen; |
| 10012 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10013 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10014 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10015 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10016 | if (use_memcpy) { |
| 10017 | Py_MEMCPY(res_data, |
| 10018 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10019 | kind * itemlen); |
| 10020 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10021 | } |
| 10022 | else { |
| 10023 | copy_characters(res, res_offset, item, 0, itemlen); |
| 10024 | res_offset += itemlen; |
| 10025 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10026 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10027 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10028 | if (use_memcpy) |
| 10029 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10030 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10031 | else |
| 10032 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10033 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10034 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10035 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10036 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10037 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10038 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10039 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10040 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10042 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | return NULL; |
| 10044 | } |
| 10045 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10046 | #define FILL(kind, data, value, start, length) \ |
| 10047 | do { \ |
| 10048 | Py_ssize_t i_ = 0; \ |
| 10049 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10050 | switch ((kind)) { \ |
| 10051 | case PyUnicode_1BYTE_KIND: { \ |
| 10052 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 10053 | memset(to_, (unsigned char)value, length); \ |
| 10054 | break; \ |
| 10055 | } \ |
| 10056 | case PyUnicode_2BYTE_KIND: { \ |
| 10057 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10058 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10059 | break; \ |
| 10060 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10061 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10063 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10064 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10065 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | } \ |
| 10067 | } \ |
| 10068 | } while (0) |
| 10069 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10070 | Py_ssize_t |
| 10071 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10072 | Py_UCS4 fill_char) |
| 10073 | { |
| 10074 | Py_ssize_t maxlen; |
| 10075 | enum PyUnicode_Kind kind; |
| 10076 | void *data; |
| 10077 | |
| 10078 | if (!PyUnicode_Check(unicode)) { |
| 10079 | PyErr_BadInternalCall(); |
| 10080 | return -1; |
| 10081 | } |
| 10082 | if (PyUnicode_READY(unicode) == -1) |
| 10083 | return -1; |
| 10084 | if (unicode_check_modifiable(unicode)) |
| 10085 | return -1; |
| 10086 | |
| 10087 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10088 | PyErr_SetString(PyExc_ValueError, |
| 10089 | "fill character is bigger than " |
| 10090 | "the string maximum character"); |
| 10091 | return -1; |
| 10092 | } |
| 10093 | |
| 10094 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10095 | length = Py_MIN(maxlen, length); |
| 10096 | if (length <= 0) |
| 10097 | return 0; |
| 10098 | |
| 10099 | kind = PyUnicode_KIND(unicode); |
| 10100 | data = PyUnicode_DATA(unicode); |
| 10101 | FILL(kind, data, fill_char, start, length); |
| 10102 | return length; |
| 10103 | } |
| 10104 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10105 | static PyObject * |
| 10106 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10107 | Py_ssize_t left, |
| 10108 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10110 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10111 | PyObject *u; |
| 10112 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10113 | int kind; |
| 10114 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10115 | |
| 10116 | if (left < 0) |
| 10117 | left = 0; |
| 10118 | if (right < 0) |
| 10119 | right = 0; |
| 10120 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10121 | if (left == 0 && right == 0) |
| 10122 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10123 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10124 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10125 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10126 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10127 | return NULL; |
| 10128 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10129 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10130 | if (fill > maxchar) |
| 10131 | maxchar = fill; |
| 10132 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10133 | if (!u) |
| 10134 | return NULL; |
| 10135 | |
| 10136 | kind = PyUnicode_KIND(u); |
| 10137 | data = PyUnicode_DATA(u); |
| 10138 | if (left) |
| 10139 | FILL(kind, data, fill, 0, left); |
| 10140 | if (right) |
| 10141 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10142 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10143 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10144 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10145 | } |
| 10146 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10147 | PyObject * |
| 10148 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10149 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10151 | |
| 10152 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10153 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10154 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10155 | if (PyUnicode_READY(string) == -1) { |
| 10156 | Py_DECREF(string); |
| 10157 | return NULL; |
| 10158 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10159 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10160 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10162 | if (PyUnicode_IS_ASCII(string)) |
| 10163 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10164 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10165 | PyUnicode_GET_LENGTH(string), keepends); |
| 10166 | else |
| 10167 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10168 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10169 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | break; |
| 10171 | case PyUnicode_2BYTE_KIND: |
| 10172 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10173 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | PyUnicode_GET_LENGTH(string), keepends); |
| 10175 | break; |
| 10176 | case PyUnicode_4BYTE_KIND: |
| 10177 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10178 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | PyUnicode_GET_LENGTH(string), keepends); |
| 10180 | break; |
| 10181 | default: |
| 10182 | assert(0); |
| 10183 | list = 0; |
| 10184 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10185 | Py_DECREF(string); |
| 10186 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10187 | } |
| 10188 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10189 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10190 | split(PyObject *self, |
| 10191 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10192 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10193 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10194 | int kind1, kind2, kind; |
| 10195 | void *buf1, *buf2; |
| 10196 | Py_ssize_t len1, len2; |
| 10197 | PyObject* out; |
| 10198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10199 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10200 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10201 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | if (PyUnicode_READY(self) == -1) |
| 10203 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10204 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10205 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10206 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10207 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10208 | if (PyUnicode_IS_ASCII(self)) |
| 10209 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10210 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10211 | PyUnicode_GET_LENGTH(self), maxcount |
| 10212 | ); |
| 10213 | else |
| 10214 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10215 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10216 | PyUnicode_GET_LENGTH(self), maxcount |
| 10217 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | case PyUnicode_2BYTE_KIND: |
| 10219 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10220 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10221 | PyUnicode_GET_LENGTH(self), maxcount |
| 10222 | ); |
| 10223 | case PyUnicode_4BYTE_KIND: |
| 10224 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10225 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10226 | PyUnicode_GET_LENGTH(self), maxcount |
| 10227 | ); |
| 10228 | default: |
| 10229 | assert(0); |
| 10230 | return NULL; |
| 10231 | } |
| 10232 | |
| 10233 | if (PyUnicode_READY(substring) == -1) |
| 10234 | return NULL; |
| 10235 | |
| 10236 | kind1 = PyUnicode_KIND(self); |
| 10237 | kind2 = PyUnicode_KIND(substring); |
| 10238 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10239 | buf1 = PyUnicode_DATA(self); |
| 10240 | buf2 = PyUnicode_DATA(substring); |
| 10241 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10242 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | if (!buf1) |
| 10244 | return NULL; |
| 10245 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10246 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | if (!buf2) { |
| 10248 | if (kind1 != kind) PyMem_Free(buf1); |
| 10249 | return NULL; |
| 10250 | } |
| 10251 | len1 = PyUnicode_GET_LENGTH(self); |
| 10252 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10253 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10254 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10256 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10257 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10258 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10259 | else |
| 10260 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10261 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10262 | break; |
| 10263 | case PyUnicode_2BYTE_KIND: |
| 10264 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10265 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | break; |
| 10267 | case PyUnicode_4BYTE_KIND: |
| 10268 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10269 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10270 | break; |
| 10271 | default: |
| 10272 | out = NULL; |
| 10273 | } |
| 10274 | if (kind1 != kind) |
| 10275 | PyMem_Free(buf1); |
| 10276 | if (kind2 != kind) |
| 10277 | PyMem_Free(buf2); |
| 10278 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 | } |
| 10280 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10281 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10282 | rsplit(PyObject *self, |
| 10283 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10284 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10285 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | int kind1, kind2, kind; |
| 10287 | void *buf1, *buf2; |
| 10288 | Py_ssize_t len1, len2; |
| 10289 | PyObject* out; |
| 10290 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10291 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10292 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10293 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10294 | if (PyUnicode_READY(self) == -1) |
| 10295 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10297 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10298 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10299 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10300 | if (PyUnicode_IS_ASCII(self)) |
| 10301 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10302 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10303 | PyUnicode_GET_LENGTH(self), maxcount |
| 10304 | ); |
| 10305 | else |
| 10306 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10307 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10308 | PyUnicode_GET_LENGTH(self), maxcount |
| 10309 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10310 | case PyUnicode_2BYTE_KIND: |
| 10311 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10312 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10313 | PyUnicode_GET_LENGTH(self), maxcount |
| 10314 | ); |
| 10315 | case PyUnicode_4BYTE_KIND: |
| 10316 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10317 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10318 | PyUnicode_GET_LENGTH(self), maxcount |
| 10319 | ); |
| 10320 | default: |
| 10321 | assert(0); |
| 10322 | return NULL; |
| 10323 | } |
| 10324 | |
| 10325 | if (PyUnicode_READY(substring) == -1) |
| 10326 | return NULL; |
| 10327 | |
| 10328 | kind1 = PyUnicode_KIND(self); |
| 10329 | kind2 = PyUnicode_KIND(substring); |
| 10330 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10331 | buf1 = PyUnicode_DATA(self); |
| 10332 | buf2 = PyUnicode_DATA(substring); |
| 10333 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10334 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10335 | if (!buf1) |
| 10336 | return NULL; |
| 10337 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10338 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | if (!buf2) { |
| 10340 | if (kind1 != kind) PyMem_Free(buf1); |
| 10341 | return NULL; |
| 10342 | } |
| 10343 | len1 = PyUnicode_GET_LENGTH(self); |
| 10344 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10345 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10346 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10347 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10348 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10349 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10350 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10351 | else |
| 10352 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10353 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10354 | break; |
| 10355 | case PyUnicode_2BYTE_KIND: |
| 10356 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10357 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10358 | break; |
| 10359 | case PyUnicode_4BYTE_KIND: |
| 10360 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10361 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10362 | break; |
| 10363 | default: |
| 10364 | out = NULL; |
| 10365 | } |
| 10366 | if (kind1 != kind) |
| 10367 | PyMem_Free(buf1); |
| 10368 | if (kind2 != kind) |
| 10369 | PyMem_Free(buf2); |
| 10370 | return out; |
| 10371 | } |
| 10372 | |
| 10373 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10374 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10375 | PyObject *str2, void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10376 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10377 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10379 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10380 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10381 | else |
| 10382 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | case PyUnicode_2BYTE_KIND: |
| 10384 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10385 | case PyUnicode_4BYTE_KIND: |
| 10386 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10387 | } |
| 10388 | assert(0); |
| 10389 | return -1; |
| 10390 | } |
| 10391 | |
| 10392 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10393 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10394 | PyObject *str1, void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10396 | switch (kind) { |
| 10397 | case PyUnicode_1BYTE_KIND: |
| 10398 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10399 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10400 | else |
| 10401 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10402 | case PyUnicode_2BYTE_KIND: |
| 10403 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10404 | case PyUnicode_4BYTE_KIND: |
| 10405 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10406 | } |
| 10407 | assert(0); |
| 10408 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10409 | } |
| 10410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10411 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | replace(PyObject *self, PyObject *str1, |
| 10413 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10414 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10415 | PyObject *u; |
| 10416 | char *sbuf = PyUnicode_DATA(self); |
| 10417 | char *buf1 = PyUnicode_DATA(str1); |
| 10418 | char *buf2 = PyUnicode_DATA(str2); |
| 10419 | int srelease = 0, release1 = 0, release2 = 0; |
| 10420 | int skind = PyUnicode_KIND(self); |
| 10421 | int kind1 = PyUnicode_KIND(str1); |
| 10422 | int kind2 = PyUnicode_KIND(str2); |
| 10423 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10424 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10425 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10426 | int mayshrink; |
| 10427 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10428 | |
| 10429 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10430 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10432 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10433 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10434 | if (str1 == str2) |
| 10435 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | if (skind < kind1) |
| 10437 | /* substring too wide to be present */ |
| 10438 | goto nothing; |
| 10439 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10440 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10441 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10442 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10443 | result string. */ |
| 10444 | mayshrink = (maxchar_str2 < maxchar); |
| 10445 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10448 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10450 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10451 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10452 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10453 | Py_UCS4 u1, u2; |
| 10454 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10455 | Py_ssize_t index, pos; |
| 10456 | char *src; |
| 10457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10458 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10459 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10460 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10461 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10462 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10464 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10466 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10467 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10468 | |
| 10469 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10470 | index = 0; |
| 10471 | src = sbuf; |
| 10472 | while (--maxcount) |
| 10473 | { |
| 10474 | pos++; |
| 10475 | src += pos * PyUnicode_KIND(self); |
| 10476 | slen -= pos; |
| 10477 | index += pos; |
| 10478 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10479 | if (pos < 0) |
| 10480 | break; |
| 10481 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10482 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10483 | } |
| 10484 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10485 | int rkind = skind; |
| 10486 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10487 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | if (kind1 < rkind) { |
| 10490 | /* widen substring */ |
| 10491 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10492 | if (!buf1) goto error; |
| 10493 | release1 = 1; |
| 10494 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10495 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10496 | if (i < 0) |
| 10497 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | if (rkind > kind2) { |
| 10499 | /* widen replacement */ |
| 10500 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10501 | if (!buf2) goto error; |
| 10502 | release2 = 1; |
| 10503 | } |
| 10504 | else if (rkind < kind2) { |
| 10505 | /* widen self and buf1 */ |
| 10506 | rkind = kind2; |
| 10507 | if (release1) PyMem_Free(buf1); |
| 10508 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10509 | if (!sbuf) goto error; |
| 10510 | srelease = 1; |
| 10511 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10512 | if (!buf1) goto error; |
| 10513 | release1 = 1; |
| 10514 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10515 | u = PyUnicode_New(slen, maxchar); |
| 10516 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10517 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10518 | assert(PyUnicode_KIND(u) == rkind); |
| 10519 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10520 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10521 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10522 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10523 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10524 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10525 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10526 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10527 | |
| 10528 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10529 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10530 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10531 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10532 | if (i == -1) |
| 10533 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10534 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10535 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10536 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10537 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10539 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10540 | } |
| 10541 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10542 | Py_ssize_t n, i, j, ires; |
| 10543 | Py_ssize_t product, new_size; |
| 10544 | int rkind = skind; |
| 10545 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10546 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10547 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10548 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10550 | if (!buf1) goto error; |
| 10551 | release1 = 1; |
| 10552 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10553 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10554 | if (n == 0) |
| 10555 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10556 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10557 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10559 | if (!buf2) goto error; |
| 10560 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10561 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10562 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10563 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10564 | rkind = kind2; |
| 10565 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10566 | if (!sbuf) goto error; |
| 10567 | srelease = 1; |
| 10568 | if (release1) PyMem_Free(buf1); |
| 10569 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10570 | if (!buf1) goto error; |
| 10571 | release1 = 1; |
| 10572 | } |
| 10573 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10574 | PyUnicode_GET_LENGTH(str1))); */ |
| 10575 | product = n * (len2-len1); |
| 10576 | if ((product / (len2-len1)) != n) { |
| 10577 | PyErr_SetString(PyExc_OverflowError, |
| 10578 | "replace string is too long"); |
| 10579 | goto error; |
| 10580 | } |
| 10581 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10582 | if (new_size == 0) { |
| 10583 | Py_INCREF(unicode_empty); |
| 10584 | u = unicode_empty; |
| 10585 | goto done; |
| 10586 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10587 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10588 | PyErr_SetString(PyExc_OverflowError, |
| 10589 | "replace string is too long"); |
| 10590 | goto error; |
| 10591 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10592 | u = PyUnicode_New(new_size, maxchar); |
| 10593 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10594 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10595 | assert(PyUnicode_KIND(u) == rkind); |
| 10596 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10597 | ires = i = 0; |
| 10598 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10599 | while (n-- > 0) { |
| 10600 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10601 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10602 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10603 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10604 | if (j == -1) |
| 10605 | break; |
| 10606 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10607 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10608 | memcpy(res + rkind * ires, |
| 10609 | sbuf + rkind * i, |
| 10610 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10611 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10612 | } |
| 10613 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10615 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10616 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10617 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10619 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10620 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10621 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10622 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10623 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10624 | memcpy(res + rkind * ires, |
| 10625 | sbuf + rkind * i, |
| 10626 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10627 | } |
| 10628 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10629 | /* interleave */ |
| 10630 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10631 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10632 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10633 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10634 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10635 | if (--n <= 0) |
| 10636 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10637 | memcpy(res + rkind * ires, |
| 10638 | sbuf + rkind * i, |
| 10639 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10640 | ires++; |
| 10641 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10642 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10643 | memcpy(res + rkind * ires, |
| 10644 | sbuf + rkind * i, |
| 10645 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10646 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10647 | } |
| 10648 | |
| 10649 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10650 | unicode_adjust_maxchar(&u); |
| 10651 | if (u == NULL) |
| 10652 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10653 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10654 | |
| 10655 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10656 | if (srelease) |
| 10657 | PyMem_FREE(sbuf); |
| 10658 | if (release1) |
| 10659 | PyMem_FREE(buf1); |
| 10660 | if (release2) |
| 10661 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10662 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10663 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10664 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10665 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10666 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10667 | if (srelease) |
| 10668 | PyMem_FREE(sbuf); |
| 10669 | if (release1) |
| 10670 | PyMem_FREE(buf1); |
| 10671 | if (release2) |
| 10672 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10673 | return unicode_result_unchanged(self); |
| 10674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10675 | error: |
| 10676 | if (srelease && sbuf) |
| 10677 | PyMem_FREE(sbuf); |
| 10678 | if (release1 && buf1) |
| 10679 | PyMem_FREE(buf1); |
| 10680 | if (release2 && buf2) |
| 10681 | PyMem_FREE(buf2); |
| 10682 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10683 | } |
| 10684 | |
| 10685 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10687 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10688 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10689 | \n\ |
| 10690 | Return a titlecased version of S, i.e. words start with title case\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10691 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10692 | |
| 10693 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10694 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10695 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10696 | if (PyUnicode_READY(self) == -1) |
| 10697 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10698 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | } |
| 10700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10701 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10702 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10703 | \n\ |
| 10704 | Return a capitalized version of S, i.e. make the first character\n\ |
Senthil Kumaran | e51ee8a | 2010-07-05 12:00:56 +0000 | [diff] [blame] | 10705 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | |
| 10707 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10708 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10709 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10710 | if (PyUnicode_READY(self) == -1) |
| 10711 | return NULL; |
| 10712 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10713 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10714 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10715 | } |
| 10716 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10717 | PyDoc_STRVAR(casefold__doc__, |
| 10718 | "S.casefold() -> str\n\ |
| 10719 | \n\ |
| 10720 | Return a version of S suitable for caseless comparisons."); |
| 10721 | |
| 10722 | static PyObject * |
| 10723 | unicode_casefold(PyObject *self) |
| 10724 | { |
| 10725 | if (PyUnicode_READY(self) == -1) |
| 10726 | return NULL; |
| 10727 | if (PyUnicode_IS_ASCII(self)) |
| 10728 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10729 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10730 | } |
| 10731 | |
| 10732 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10733 | /* Argument converter. Coerces to a single unicode character */ |
| 10734 | |
| 10735 | static int |
| 10736 | convert_uc(PyObject *obj, void *addr) |
| 10737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10739 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10740 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10741 | uniobj = PyUnicode_FromObject(obj); |
| 10742 | if (uniobj == NULL) { |
| 10743 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10744 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10745 | return 0; |
| 10746 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10748 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10749 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10750 | Py_DECREF(uniobj); |
| 10751 | return 0; |
| 10752 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10753 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10754 | Py_DECREF(uniobj); |
| 10755 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10756 | } |
| 10757 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10758 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10759 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10761 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10762 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | |
| 10764 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10765 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10766 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10767 | Py_ssize_t marg, left; |
| 10768 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10769 | Py_UCS4 fillchar = ' '; |
| 10770 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10771 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10772 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10773 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10774 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10775 | return NULL; |
| 10776 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10777 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10778 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10779 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10780 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10781 | left = marg / 2 + (marg & width & 1); |
| 10782 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10783 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | } |
| 10785 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10786 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10787 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10788 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10789 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10790 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10791 | int kind1, kind2; |
| 10792 | void *data1, *data2; |
| 10793 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10794 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10795 | kind1 = PyUnicode_KIND(str1); |
| 10796 | kind2 = PyUnicode_KIND(str2); |
| 10797 | data1 = PyUnicode_DATA(str1); |
| 10798 | data2 = PyUnicode_DATA(str2); |
| 10799 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10800 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10801 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10802 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10803 | Py_UCS4 c1, c2; |
| 10804 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10805 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10806 | |
| 10807 | if (c1 != c2) |
| 10808 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10809 | } |
| 10810 | |
| 10811 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10812 | } |
| 10813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10814 | int |
| 10815 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10816 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10817 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10818 | if (PyUnicode_READY(left) == -1 || |
| 10819 | PyUnicode_READY(right) == -1) |
| 10820 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10821 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10822 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10823 | PyErr_Format(PyExc_TypeError, |
| 10824 | "Can't compare %.100s and %.100s", |
| 10825 | left->ob_type->tp_name, |
| 10826 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10827 | return -1; |
| 10828 | } |
| 10829 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10830 | int |
| 10831 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10832 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10833 | Py_ssize_t i; |
| 10834 | int kind; |
| 10835 | void *data; |
| 10836 | Py_UCS4 chr; |
| 10837 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10838 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10839 | if (PyUnicode_READY(uni) == -1) |
| 10840 | return -1; |
| 10841 | kind = PyUnicode_KIND(uni); |
| 10842 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10843 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10844 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10845 | if (chr != str[i]) |
| 10846 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10847 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10848 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10849 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10850 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10851 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10852 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10853 | return 0; |
| 10854 | } |
| 10855 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10856 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10857 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10858 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10859 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10860 | PyObject * |
| 10861 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10862 | { |
| 10863 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10864 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10865 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10866 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10867 | if (PyUnicode_READY(left) == -1 || |
| 10868 | PyUnicode_READY(right) == -1) |
| 10869 | return NULL; |
| 10870 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10871 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10872 | if (op == Py_EQ) { |
| 10873 | Py_INCREF(Py_False); |
| 10874 | return Py_False; |
| 10875 | } |
| 10876 | if (op == Py_NE) { |
| 10877 | Py_INCREF(Py_True); |
| 10878 | return Py_True; |
| 10879 | } |
| 10880 | } |
| 10881 | if (left == right) |
| 10882 | result = 0; |
| 10883 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10884 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10885 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10886 | /* Convert the return value to a Boolean */ |
| 10887 | switch (op) { |
| 10888 | case Py_EQ: |
| 10889 | v = TEST_COND(result == 0); |
| 10890 | break; |
| 10891 | case Py_NE: |
| 10892 | v = TEST_COND(result != 0); |
| 10893 | break; |
| 10894 | case Py_LE: |
| 10895 | v = TEST_COND(result <= 0); |
| 10896 | break; |
| 10897 | case Py_GE: |
| 10898 | v = TEST_COND(result >= 0); |
| 10899 | break; |
| 10900 | case Py_LT: |
| 10901 | v = TEST_COND(result == -1); |
| 10902 | break; |
| 10903 | case Py_GT: |
| 10904 | v = TEST_COND(result == 1); |
| 10905 | break; |
| 10906 | default: |
| 10907 | PyErr_BadArgument(); |
| 10908 | return NULL; |
| 10909 | } |
| 10910 | Py_INCREF(v); |
| 10911 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10912 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10913 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10914 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10915 | } |
| 10916 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10917 | int |
| 10918 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10919 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10920 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 | int kind1, kind2, kind; |
| 10922 | void *buf1, *buf2; |
| 10923 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10924 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10925 | |
| 10926 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10927 | sub = PyUnicode_FromObject(element); |
| 10928 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10929 | PyErr_Format(PyExc_TypeError, |
| 10930 | "'in <string>' requires string as left operand, not %s", |
| 10931 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10932 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10933 | } |
| 10934 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10935 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10936 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10937 | Py_DECREF(sub); |
| 10938 | return -1; |
| 10939 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10940 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10941 | Py_DECREF(sub); |
| 10942 | Py_DECREF(str); |
| 10943 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10944 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10945 | kind1 = PyUnicode_KIND(str); |
| 10946 | kind2 = PyUnicode_KIND(sub); |
| 10947 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10948 | buf1 = PyUnicode_DATA(str); |
| 10949 | buf2 = PyUnicode_DATA(sub); |
| 10950 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10951 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10952 | if (!buf1) { |
| 10953 | Py_DECREF(sub); |
| 10954 | return -1; |
| 10955 | } |
| 10956 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10957 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10958 | if (!buf2) { |
| 10959 | Py_DECREF(sub); |
| 10960 | if (kind1 != kind) PyMem_Free(buf1); |
| 10961 | return -1; |
| 10962 | } |
| 10963 | len1 = PyUnicode_GET_LENGTH(str); |
| 10964 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10965 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10966 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10967 | case PyUnicode_1BYTE_KIND: |
| 10968 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10969 | break; |
| 10970 | case PyUnicode_2BYTE_KIND: |
| 10971 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10972 | break; |
| 10973 | case PyUnicode_4BYTE_KIND: |
| 10974 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10975 | break; |
| 10976 | default: |
| 10977 | result = -1; |
| 10978 | assert(0); |
| 10979 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10980 | |
| 10981 | Py_DECREF(str); |
| 10982 | Py_DECREF(sub); |
| 10983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10984 | if (kind1 != kind) |
| 10985 | PyMem_Free(buf1); |
| 10986 | if (kind2 != kind) |
| 10987 | PyMem_Free(buf2); |
| 10988 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10989 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10990 | } |
| 10991 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10994 | PyObject * |
| 10995 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10996 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10997 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10998 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10999 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | |
| 11001 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11002 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11004 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11005 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11007 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | |
| 11009 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11010 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11011 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11012 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11014 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11015 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11016 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | } |
| 11018 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11019 | u_len = PyUnicode_GET_LENGTH(u); |
| 11020 | v_len = PyUnicode_GET_LENGTH(v); |
| 11021 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11022 | PyErr_SetString(PyExc_OverflowError, |
| 11023 | "strings are too large to concat"); |
| 11024 | goto onError; |
| 11025 | } |
| 11026 | new_len = u_len + v_len; |
| 11027 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11028 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11029 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 11030 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11032 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11033 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11035 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11036 | copy_characters(w, 0, u, 0, u_len); |
| 11037 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11038 | Py_DECREF(u); |
| 11039 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11040 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11041 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11042 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11043 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | Py_XDECREF(u); |
| 11045 | Py_XDECREF(v); |
| 11046 | return NULL; |
| 11047 | } |
| 11048 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11049 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11050 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11051 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11052 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11053 | Py_UCS4 maxchar, maxchar2; |
| 11054 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11055 | |
| 11056 | if (p_left == NULL) { |
| 11057 | if (!PyErr_Occurred()) |
| 11058 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11059 | return; |
| 11060 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11061 | left = *p_left; |
| 11062 | if (right == NULL || !PyUnicode_Check(left)) { |
| 11063 | if (!PyErr_Occurred()) |
| 11064 | PyErr_BadInternalCall(); |
| 11065 | goto error; |
| 11066 | } |
| 11067 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11068 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11069 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11070 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11071 | goto error; |
| 11072 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11073 | /* Shortcuts */ |
| 11074 | if (left == unicode_empty) { |
| 11075 | Py_DECREF(left); |
| 11076 | Py_INCREF(right); |
| 11077 | *p_left = right; |
| 11078 | return; |
| 11079 | } |
| 11080 | if (right == unicode_empty) |
| 11081 | return; |
| 11082 | |
| 11083 | left_len = PyUnicode_GET_LENGTH(left); |
| 11084 | right_len = PyUnicode_GET_LENGTH(right); |
| 11085 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11086 | PyErr_SetString(PyExc_OverflowError, |
| 11087 | "strings are too large to concat"); |
| 11088 | goto error; |
| 11089 | } |
| 11090 | new_len = left_len + right_len; |
| 11091 | |
| 11092 | if (unicode_modifiable(left) |
| 11093 | && PyUnicode_CheckExact(right) |
| 11094 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11095 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11096 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11097 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11098 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11099 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11100 | { |
| 11101 | /* append inplace */ |
| 11102 | if (unicode_resize(p_left, new_len) != 0) { |
| 11103 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 11104 | * deallocated so it cannot be put back into |
| 11105 | * 'variable'. The MemoryError is raised when there |
| 11106 | * is no value in 'variable', which might (very |
| 11107 | * remotely) be a cause of incompatibilities. |
| 11108 | */ |
| 11109 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11110 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11111 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11112 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11113 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11114 | else { |
| 11115 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11116 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
| 11117 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11118 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11119 | /* Concat the two Unicode strings */ |
| 11120 | res = PyUnicode_New(new_len, maxchar); |
| 11121 | if (res == NULL) |
| 11122 | goto error; |
| 11123 | copy_characters(res, 0, left, 0, left_len); |
| 11124 | copy_characters(res, left_len, right, 0, right_len); |
| 11125 | Py_DECREF(left); |
| 11126 | *p_left = res; |
| 11127 | } |
| 11128 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11129 | return; |
| 11130 | |
| 11131 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11132 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11133 | } |
| 11134 | |
| 11135 | void |
| 11136 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11137 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11138 | PyUnicode_Append(pleft, right); |
| 11139 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11140 | } |
| 11141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11142 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11143 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11144 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11145 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11146 | string S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11147 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | |
| 11149 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11150 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11151 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11152 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11153 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11154 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11155 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11156 | int kind1, kind2, kind; |
| 11157 | void *buf1, *buf2; |
| 11158 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11159 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11160 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11161 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11162 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11164 | kind1 = PyUnicode_KIND(self); |
| 11165 | kind2 = PyUnicode_KIND(substring); |
| 11166 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11167 | buf1 = PyUnicode_DATA(self); |
| 11168 | buf2 = PyUnicode_DATA(substring); |
| 11169 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11170 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11171 | if (!buf1) { |
| 11172 | Py_DECREF(substring); |
| 11173 | return NULL; |
| 11174 | } |
| 11175 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11176 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (!buf2) { |
| 11178 | Py_DECREF(substring); |
| 11179 | if (kind1 != kind) PyMem_Free(buf1); |
| 11180 | return NULL; |
| 11181 | } |
| 11182 | len1 = PyUnicode_GET_LENGTH(self); |
| 11183 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11184 | |
| 11185 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11186 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11187 | case PyUnicode_1BYTE_KIND: |
| 11188 | iresult = ucs1lib_count( |
| 11189 | ((Py_UCS1*)buf1) + start, end - start, |
| 11190 | buf2, len2, PY_SSIZE_T_MAX |
| 11191 | ); |
| 11192 | break; |
| 11193 | case PyUnicode_2BYTE_KIND: |
| 11194 | iresult = ucs2lib_count( |
| 11195 | ((Py_UCS2*)buf1) + start, end - start, |
| 11196 | buf2, len2, PY_SSIZE_T_MAX |
| 11197 | ); |
| 11198 | break; |
| 11199 | case PyUnicode_4BYTE_KIND: |
| 11200 | iresult = ucs4lib_count( |
| 11201 | ((Py_UCS4*)buf1) + start, end - start, |
| 11202 | buf2, len2, PY_SSIZE_T_MAX |
| 11203 | ); |
| 11204 | break; |
| 11205 | default: |
| 11206 | assert(0); iresult = 0; |
| 11207 | } |
| 11208 | |
| 11209 | result = PyLong_FromSsize_t(iresult); |
| 11210 | |
| 11211 | if (kind1 != kind) |
| 11212 | PyMem_Free(buf1); |
| 11213 | if (kind2 != kind) |
| 11214 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | |
| 11216 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11217 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | return result; |
| 11219 | } |
| 11220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11221 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11222 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11223 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11224 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11225 | is 'utf-8'. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 11226 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11227 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11228 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11229 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | |
| 11231 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11232 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11234 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | char *encoding = NULL; |
| 11236 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11237 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11238 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11239 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11241 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11242 | } |
| 11243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11244 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11245 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11246 | \n\ |
| 11247 | Return a copy of S where all tab characters are expanded using spaces.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11248 | If tabsize is not given, a tab size of 8 characters is assumed."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | |
| 11250 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11251 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11253 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11254 | Py_UCS4 ch; |
| 11255 | PyObject *u; |
| 11256 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11258 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11259 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 | |
| 11261 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11262 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11264 | if (PyUnicode_READY(self) == -1) |
| 11265 | return NULL; |
| 11266 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11267 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11268 | src_len = PyUnicode_GET_LENGTH(self); |
| 11269 | i = j = line_pos = 0; |
| 11270 | kind = PyUnicode_KIND(self); |
| 11271 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11272 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11273 | for (; i < src_len; i++) { |
| 11274 | ch = PyUnicode_READ(kind, src_data, i); |
| 11275 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11276 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11277 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11278 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11279 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11280 | goto overflow; |
| 11281 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11282 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11283 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11286 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11287 | goto overflow; |
| 11288 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11290 | if (ch == '\n' || ch == '\r') |
| 11291 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11293 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11294 | if (!found) |
| 11295 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11296 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11297 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11298 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11299 | if (!u) |
| 11300 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11301 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11303 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11304 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11305 | for (; i < src_len; i++) { |
| 11306 | ch = PyUnicode_READ(kind, src_data, i); |
| 11307 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11308 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11309 | incr = tabsize - (line_pos % tabsize); |
| 11310 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11311 | FILL(kind, dest_data, ' ', j, incr); |
| 11312 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11313 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11314 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11315 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11316 | line_pos++; |
| 11317 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11318 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11319 | if (ch == '\n' || ch == '\r') |
| 11320 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11322 | } |
| 11323 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11324 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11325 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11326 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11327 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11328 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | } |
| 11330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11331 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11332 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | \n\ |
| 11334 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11335 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11336 | arguments start and end are interpreted as in slice notation.\n\ |
| 11337 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11338 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | |
| 11340 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11343 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11344 | Py_ssize_t start; |
| 11345 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11346 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11347 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11348 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11349 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | if (PyUnicode_READY(self) == -1) |
| 11353 | return NULL; |
| 11354 | if (PyUnicode_READY(substring) == -1) |
| 11355 | return NULL; |
| 11356 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11357 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | |
| 11359 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11360 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11361 | if (result == -2) |
| 11362 | return NULL; |
| 11363 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11364 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | } |
| 11366 | |
| 11367 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11368 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11370 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11371 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11372 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11373 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11374 | } |
| 11375 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11376 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11377 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11378 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11379 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11380 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11381 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11382 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11383 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11384 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11385 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11386 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11387 | if (_PyUnicode_HASH(self) != -1) |
| 11388 | return _PyUnicode_HASH(self); |
| 11389 | if (PyUnicode_READY(self) == -1) |
| 11390 | return -1; |
| 11391 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11392 | /* |
| 11393 | We make the hash of the empty string be 0, rather than using |
| 11394 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11395 | */ |
| 11396 | if (len == 0) { |
| 11397 | _PyUnicode_HASH(self) = 0; |
| 11398 | return 0; |
| 11399 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11400 | |
| 11401 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11402 | #define HASH(P) \ |
| 11403 | x ^= (Py_uhash_t) *P << 7; \ |
| 11404 | while (--len >= 0) \ |
| 11405 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11406 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11407 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11408 | switch (PyUnicode_KIND(self)) { |
| 11409 | case PyUnicode_1BYTE_KIND: { |
| 11410 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11411 | HASH(c); |
| 11412 | break; |
| 11413 | } |
| 11414 | case PyUnicode_2BYTE_KIND: { |
| 11415 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11416 | HASH(s); |
| 11417 | break; |
| 11418 | } |
| 11419 | default: { |
| 11420 | Py_UCS4 *l; |
| 11421 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11422 | "Impossible switch case in unicode_hash"); |
| 11423 | l = PyUnicode_4BYTE_DATA(self); |
| 11424 | HASH(l); |
| 11425 | break; |
| 11426 | } |
| 11427 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11428 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11429 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11430 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11431 | if (x == -1) |
| 11432 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11433 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11434 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11438 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11441 | Like S.find() but raise ValueError when the substring is not found."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | |
| 11443 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11446 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11447 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11448 | Py_ssize_t start; |
| 11449 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11450 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11451 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11452 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11455 | if (PyUnicode_READY(self) == -1) |
| 11456 | return NULL; |
| 11457 | if (PyUnicode_READY(substring) == -1) |
| 11458 | return NULL; |
| 11459 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11460 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11461 | |
| 11462 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | if (result == -2) |
| 11465 | return NULL; |
| 11466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | if (result < 0) { |
| 11468 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11469 | return NULL; |
| 11470 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11471 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11472 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11473 | } |
| 11474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11475 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11476 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11478 | Return True if all cased characters in S are lowercase and there is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11479 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | |
| 11481 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11482 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11484 | Py_ssize_t i, length; |
| 11485 | int kind; |
| 11486 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | int cased; |
| 11488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 | if (PyUnicode_READY(self) == -1) |
| 11490 | return NULL; |
| 11491 | length = PyUnicode_GET_LENGTH(self); |
| 11492 | kind = PyUnicode_KIND(self); |
| 11493 | data = PyUnicode_DATA(self); |
| 11494 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11496 | if (length == 1) |
| 11497 | return PyBool_FromLong( |
| 11498 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11500 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11501 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11502 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | for (i = 0; i < length; i++) { |
| 11506 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11507 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11508 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11509 | return PyBool_FromLong(0); |
| 11510 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11511 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11513 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | } |
| 11515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11516 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11517 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11519 | Return True if all cased characters in S are uppercase and there is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11520 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | |
| 11522 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11523 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 | Py_ssize_t i, length; |
| 11526 | int kind; |
| 11527 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11528 | int cased; |
| 11529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11530 | if (PyUnicode_READY(self) == -1) |
| 11531 | return NULL; |
| 11532 | length = PyUnicode_GET_LENGTH(self); |
| 11533 | kind = PyUnicode_KIND(self); |
| 11534 | data = PyUnicode_DATA(self); |
| 11535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11536 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | if (length == 1) |
| 11538 | return PyBool_FromLong( |
| 11539 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11540 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11541 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11542 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11543 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11545 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11546 | for (i = 0; i < length; i++) { |
| 11547 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11548 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11549 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11550 | return PyBool_FromLong(0); |
| 11551 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11552 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11554 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | } |
| 11556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11557 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11558 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11560 | Return True if S is a titlecased string and there is at least one\n\ |
| 11561 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11562 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11563 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11564 | |
| 11565 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11566 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11568 | Py_ssize_t i, length; |
| 11569 | int kind; |
| 11570 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11571 | int cased, previous_is_cased; |
| 11572 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11573 | if (PyUnicode_READY(self) == -1) |
| 11574 | return NULL; |
| 11575 | length = PyUnicode_GET_LENGTH(self); |
| 11576 | kind = PyUnicode_KIND(self); |
| 11577 | data = PyUnicode_DATA(self); |
| 11578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11579 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11580 | if (length == 1) { |
| 11581 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11582 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11583 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11584 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11586 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11587 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11588 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11589 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11590 | cased = 0; |
| 11591 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11592 | for (i = 0; i < length; i++) { |
| 11593 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11595 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11596 | if (previous_is_cased) |
| 11597 | return PyBool_FromLong(0); |
| 11598 | previous_is_cased = 1; |
| 11599 | cased = 1; |
| 11600 | } |
| 11601 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11602 | if (!previous_is_cased) |
| 11603 | return PyBool_FromLong(0); |
| 11604 | previous_is_cased = 1; |
| 11605 | cased = 1; |
| 11606 | } |
| 11607 | else |
| 11608 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11609 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11610 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | } |
| 11612 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11613 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11614 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11616 | Return True if all characters in S are whitespace\n\ |
| 11617 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11618 | |
| 11619 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11620 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11621 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | Py_ssize_t i, length; |
| 11623 | int kind; |
| 11624 | void *data; |
| 11625 | |
| 11626 | if (PyUnicode_READY(self) == -1) |
| 11627 | return NULL; |
| 11628 | length = PyUnicode_GET_LENGTH(self); |
| 11629 | kind = PyUnicode_KIND(self); |
| 11630 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11632 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | if (length == 1) |
| 11634 | return PyBool_FromLong( |
| 11635 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11636 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11637 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11638 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11639 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | for (i = 0; i < length; i++) { |
| 11642 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11643 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11644 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11646 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11647 | } |
| 11648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11649 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11650 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11651 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11652 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11653 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11654 | |
| 11655 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11656 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11657 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11658 | Py_ssize_t i, length; |
| 11659 | int kind; |
| 11660 | void *data; |
| 11661 | |
| 11662 | if (PyUnicode_READY(self) == -1) |
| 11663 | return NULL; |
| 11664 | length = PyUnicode_GET_LENGTH(self); |
| 11665 | kind = PyUnicode_KIND(self); |
| 11666 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11667 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11668 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11669 | if (length == 1) |
| 11670 | return PyBool_FromLong( |
| 11671 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11672 | |
| 11673 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11674 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11675 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11676 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11677 | for (i = 0; i < length; i++) { |
| 11678 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11679 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11680 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11681 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11682 | } |
| 11683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11684 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11685 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11686 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11687 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11688 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11689 | |
| 11690 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11691 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11692 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11693 | int kind; |
| 11694 | void *data; |
| 11695 | Py_ssize_t len, i; |
| 11696 | |
| 11697 | if (PyUnicode_READY(self) == -1) |
| 11698 | return NULL; |
| 11699 | |
| 11700 | kind = PyUnicode_KIND(self); |
| 11701 | data = PyUnicode_DATA(self); |
| 11702 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11703 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11704 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11705 | if (len == 1) { |
| 11706 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11707 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11708 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11709 | |
| 11710 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11711 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11712 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | for (i = 0; i < len; i++) { |
| 11715 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11716 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11717 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11718 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11719 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11720 | } |
| 11721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11722 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11723 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11724 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11725 | Return True if there are only decimal characters in S,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11726 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11727 | |
| 11728 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11729 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11730 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11731 | Py_ssize_t i, length; |
| 11732 | int kind; |
| 11733 | void *data; |
| 11734 | |
| 11735 | if (PyUnicode_READY(self) == -1) |
| 11736 | return NULL; |
| 11737 | length = PyUnicode_GET_LENGTH(self); |
| 11738 | kind = PyUnicode_KIND(self); |
| 11739 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11741 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11742 | if (length == 1) |
| 11743 | return PyBool_FromLong( |
| 11744 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11746 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11747 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11748 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11750 | for (i = 0; i < length; i++) { |
| 11751 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11752 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11753 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11754 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | } |
| 11756 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11757 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11758 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11759 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11760 | Return True if all characters in S are digits\n\ |
| 11761 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11762 | |
| 11763 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11764 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | Py_ssize_t i, length; |
| 11767 | int kind; |
| 11768 | void *data; |
| 11769 | |
| 11770 | if (PyUnicode_READY(self) == -1) |
| 11771 | return NULL; |
| 11772 | length = PyUnicode_GET_LENGTH(self); |
| 11773 | kind = PyUnicode_KIND(self); |
| 11774 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11776 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11777 | if (length == 1) { |
| 11778 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11779 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11780 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11782 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11783 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11784 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11785 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11786 | for (i = 0; i < length; i++) { |
| 11787 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11788 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11789 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11790 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11791 | } |
| 11792 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11793 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11794 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11795 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11796 | Return True if there are only numeric characters in S,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11797 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11798 | |
| 11799 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11800 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11801 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11802 | Py_ssize_t i, length; |
| 11803 | int kind; |
| 11804 | void *data; |
| 11805 | |
| 11806 | if (PyUnicode_READY(self) == -1) |
| 11807 | return NULL; |
| 11808 | length = PyUnicode_GET_LENGTH(self); |
| 11809 | kind = PyUnicode_KIND(self); |
| 11810 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11812 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11813 | if (length == 1) |
| 11814 | return PyBool_FromLong( |
| 11815 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11817 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11818 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11819 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11820 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | for (i = 0; i < length; i++) { |
| 11822 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11823 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11824 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11825 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | } |
| 11827 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11828 | int |
| 11829 | PyUnicode_IsIdentifier(PyObject *self) |
| 11830 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11831 | int kind; |
| 11832 | void *data; |
| 11833 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11834 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11835 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11836 | if (PyUnicode_READY(self) == -1) { |
| 11837 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11838 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11839 | } |
| 11840 | |
| 11841 | /* Special case for empty strings */ |
| 11842 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11843 | return 0; |
| 11844 | kind = PyUnicode_KIND(self); |
| 11845 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11846 | |
| 11847 | /* PEP 3131 says that the first character must be in |
| 11848 | XID_Start and subsequent characters in XID_Continue, |
| 11849 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11850 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11851 | letters, digits, underscore). However, given the current |
| 11852 | definition of XID_Start and XID_Continue, it is sufficient |
| 11853 | to check just for these, except that _ must be allowed |
| 11854 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11855 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11856 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11857 | return 0; |
| 11858 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11859 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11860 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11861 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11862 | return 1; |
| 11863 | } |
| 11864 | |
| 11865 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11866 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11867 | \n\ |
| 11868 | Return True if S is a valid identifier according\n\ |
| 11869 | to the language definition."); |
| 11870 | |
| 11871 | static PyObject* |
| 11872 | unicode_isidentifier(PyObject *self) |
| 11873 | { |
| 11874 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11875 | } |
| 11876 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11877 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11878 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11879 | \n\ |
| 11880 | Return True if all characters in S are considered\n\ |
| 11881 | printable in repr() or S is empty, False otherwise."); |
| 11882 | |
| 11883 | static PyObject* |
| 11884 | unicode_isprintable(PyObject *self) |
| 11885 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | Py_ssize_t i, length; |
| 11887 | int kind; |
| 11888 | void *data; |
| 11889 | |
| 11890 | if (PyUnicode_READY(self) == -1) |
| 11891 | return NULL; |
| 11892 | length = PyUnicode_GET_LENGTH(self); |
| 11893 | kind = PyUnicode_KIND(self); |
| 11894 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11895 | |
| 11896 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11897 | if (length == 1) |
| 11898 | return PyBool_FromLong( |
| 11899 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11900 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11901 | for (i = 0; i < length; i++) { |
| 11902 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11903 | Py_RETURN_FALSE; |
| 11904 | } |
| 11905 | } |
| 11906 | Py_RETURN_TRUE; |
| 11907 | } |
| 11908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11909 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11910 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | \n\ |
| 11912 | Return a string which is the concatenation of the strings in the\n\ |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11913 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11914 | |
| 11915 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11916 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11918 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | } |
| 11920 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11921 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11922 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | if (PyUnicode_READY(self) == -1) |
| 11925 | return -1; |
| 11926 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | } |
| 11928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11929 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11930 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11931 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11932 | Return S left-justified in a Unicode string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11933 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | |
| 11935 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11936 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11937 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11938 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11939 | Py_UCS4 fillchar = ' '; |
| 11940 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11941 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | return NULL; |
| 11943 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11944 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11945 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11946 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11947 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11948 | return unicode_result_unchanged(self); |
| 11949 | |
| 11950 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | } |
| 11952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11953 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11954 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11955 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11956 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | |
| 11958 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11959 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11961 | if (PyUnicode_READY(self) == -1) |
| 11962 | return NULL; |
| 11963 | if (PyUnicode_IS_ASCII(self)) |
| 11964 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11965 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11966 | } |
| 11967 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11968 | #define LEFTSTRIP 0 |
| 11969 | #define RIGHTSTRIP 1 |
| 11970 | #define BOTHSTRIP 2 |
| 11971 | |
| 11972 | /* Arrays indexed by above */ |
| 11973 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11974 | |
| 11975 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11976 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11977 | /* externally visible for str.strip(unicode) */ |
| 11978 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11979 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11980 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11981 | void *data; |
| 11982 | int kind; |
| 11983 | Py_ssize_t i, j, len; |
| 11984 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11985 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11986 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11987 | return NULL; |
| 11988 | |
| 11989 | kind = PyUnicode_KIND(self); |
| 11990 | data = PyUnicode_DATA(self); |
| 11991 | len = PyUnicode_GET_LENGTH(self); |
| 11992 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11993 | PyUnicode_DATA(sepobj), |
| 11994 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11995 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11996 | i = 0; |
| 11997 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11998 | while (i < len && |
| 11999 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12000 | i++; |
| 12001 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12002 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12003 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12004 | j = len; |
| 12005 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12006 | do { |
| 12007 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12008 | } while (j >= i && |
| 12009 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12010 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12011 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12012 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12013 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12014 | } |
| 12015 | |
| 12016 | PyObject* |
| 12017 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12018 | { |
| 12019 | unsigned char *data; |
| 12020 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12021 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12022 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12023 | if (PyUnicode_READY(self) == -1) |
| 12024 | return NULL; |
| 12025 | |
| 12026 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 12027 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12028 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12029 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12031 | length = end - start; |
| 12032 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12033 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12035 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12036 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12037 | return NULL; |
| 12038 | } |
| 12039 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12040 | if (PyUnicode_IS_ASCII(self)) { |
| 12041 | kind = PyUnicode_KIND(self); |
| 12042 | data = PyUnicode_1BYTE_DATA(self); |
| 12043 | return unicode_fromascii(data + start, length); |
| 12044 | } |
| 12045 | else { |
| 12046 | kind = PyUnicode_KIND(self); |
| 12047 | data = PyUnicode_1BYTE_DATA(self); |
| 12048 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12049 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12050 | length); |
| 12051 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12052 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12053 | |
| 12054 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12055 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12056 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12057 | int kind; |
| 12058 | void *data; |
| 12059 | Py_ssize_t len, i, j; |
| 12060 | |
| 12061 | if (PyUnicode_READY(self) == -1) |
| 12062 | return NULL; |
| 12063 | |
| 12064 | kind = PyUnicode_KIND(self); |
| 12065 | data = PyUnicode_DATA(self); |
| 12066 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12067 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12068 | i = 0; |
| 12069 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12070 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12071 | i++; |
| 12072 | } |
| 12073 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12074 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12075 | j = len; |
| 12076 | if (striptype != LEFTSTRIP) { |
| 12077 | do { |
| 12078 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12079 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12080 | j++; |
| 12081 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12082 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12083 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12084 | } |
| 12085 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12086 | |
| 12087 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12088 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12089 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12090 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12091 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12092 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 12093 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12094 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12095 | if (sep != NULL && sep != Py_None) { |
| 12096 | if (PyUnicode_Check(sep)) |
| 12097 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12098 | else { |
| 12099 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12100 | "%s arg must be None or str", |
| 12101 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12102 | return NULL; |
| 12103 | } |
| 12104 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12105 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12106 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12107 | } |
| 12108 | |
| 12109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12110 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12111 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12112 | \n\ |
| 12113 | Return a copy of the string S with leading and trailing\n\ |
| 12114 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12115 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12116 | |
| 12117 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12118 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12119 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12120 | if (PyTuple_GET_SIZE(args) == 0) |
| 12121 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12122 | else |
| 12123 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12124 | } |
| 12125 | |
| 12126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12127 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12128 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12129 | \n\ |
| 12130 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12131 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12132 | |
| 12133 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12134 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12135 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12136 | if (PyTuple_GET_SIZE(args) == 0) |
| 12137 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12138 | else |
| 12139 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12140 | } |
| 12141 | |
| 12142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12143 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12144 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12145 | \n\ |
| 12146 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12147 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12148 | |
| 12149 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12150 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12151 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12152 | if (PyTuple_GET_SIZE(args) == 0) |
| 12153 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12154 | else |
| 12155 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12156 | } |
| 12157 | |
| 12158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12160 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12161 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12162 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12163 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12164 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12165 | if (len < 1) { |
| 12166 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12167 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12168 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12169 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12170 | /* no repeat, return original string */ |
| 12171 | if (len == 1) |
| 12172 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12173 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12174 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12175 | return NULL; |
| 12176 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12177 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12178 | PyErr_SetString(PyExc_OverflowError, |
| 12179 | "repeated string is too long"); |
| 12180 | return NULL; |
| 12181 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12183 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12184 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12185 | if (!u) |
| 12186 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12187 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12189 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12190 | const int kind = PyUnicode_KIND(str); |
| 12191 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12192 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12193 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12194 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12195 | } |
| 12196 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12197 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12198 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12199 | ucs2[n] = fill_char; |
| 12200 | } else { |
| 12201 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12202 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12203 | for (n = 0; n < len; ++n) |
| 12204 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12205 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12206 | } |
| 12207 | else { |
| 12208 | /* number of characters copied this far */ |
| 12209 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12210 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12211 | char *to = (char *) PyUnicode_DATA(u); |
| 12212 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12213 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12214 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12215 | n = (done <= nchars-done) ? done : nchars-done; |
| 12216 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12217 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12218 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | } |
| 12220 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12221 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12222 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12223 | } |
| 12224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12225 | PyObject * |
| 12226 | PyUnicode_Replace(PyObject *obj, |
| 12227 | PyObject *subobj, |
| 12228 | PyObject *replobj, |
| 12229 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | { |
| 12231 | PyObject *self; |
| 12232 | PyObject *str1; |
| 12233 | PyObject *str2; |
| 12234 | PyObject *result; |
| 12235 | |
| 12236 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12237 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12238 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12240 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12241 | Py_DECREF(self); |
| 12242 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12243 | } |
| 12244 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12245 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12246 | Py_DECREF(self); |
| 12247 | Py_DECREF(str1); |
| 12248 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12250 | if (PyUnicode_READY(self) == -1 || |
| 12251 | PyUnicode_READY(str1) == -1 || |
| 12252 | PyUnicode_READY(str2) == -1) |
| 12253 | result = NULL; |
| 12254 | else |
| 12255 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | Py_DECREF(self); |
| 12257 | Py_DECREF(str1); |
| 12258 | Py_DECREF(str2); |
| 12259 | return result; |
| 12260 | } |
| 12261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12262 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12263 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | \n\ |
| 12265 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12266 | old replaced by new. If the optional argument count is\n\ |
| 12267 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | |
| 12269 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12270 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12271 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | PyObject *str1; |
| 12273 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12274 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | PyObject *result; |
| 12276 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12277 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12279 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12281 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12282 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12283 | return NULL; |
| 12284 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12285 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12286 | Py_DECREF(str1); |
| 12287 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12288 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12289 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12290 | result = NULL; |
| 12291 | else |
| 12292 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 | |
| 12294 | Py_DECREF(str1); |
| 12295 | Py_DECREF(str2); |
| 12296 | return result; |
| 12297 | } |
| 12298 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12299 | static PyObject * |
| 12300 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12301 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12302 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | Py_ssize_t isize; |
| 12304 | Py_ssize_t osize, squote, dquote, i, o; |
| 12305 | Py_UCS4 max, quote; |
| 12306 | int ikind, okind; |
| 12307 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12308 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12309 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12310 | return NULL; |
| 12311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12313 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12314 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12315 | /* Compute length of output, quote characters, and |
| 12316 | maximum character */ |
| 12317 | osize = 2; /* quotes */ |
| 12318 | max = 127; |
| 12319 | squote = dquote = 0; |
| 12320 | ikind = PyUnicode_KIND(unicode); |
| 12321 | for (i = 0; i < isize; i++) { |
| 12322 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12323 | switch (ch) { |
| 12324 | case '\'': squote++; osize++; break; |
| 12325 | case '"': dquote++; osize++; break; |
| 12326 | case '\\': case '\t': case '\r': case '\n': |
| 12327 | osize += 2; break; |
| 12328 | default: |
| 12329 | /* Fast-path ASCII */ |
| 12330 | if (ch < ' ' || ch == 0x7f) |
| 12331 | osize += 4; /* \xHH */ |
| 12332 | else if (ch < 0x7f) |
| 12333 | osize++; |
| 12334 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12335 | osize++; |
| 12336 | max = ch > max ? ch : max; |
| 12337 | } |
| 12338 | else if (ch < 0x100) |
| 12339 | osize += 4; /* \xHH */ |
| 12340 | else if (ch < 0x10000) |
| 12341 | osize += 6; /* \uHHHH */ |
| 12342 | else |
| 12343 | osize += 10; /* \uHHHHHHHH */ |
| 12344 | } |
| 12345 | } |
| 12346 | |
| 12347 | quote = '\''; |
| 12348 | if (squote) { |
| 12349 | if (dquote) |
| 12350 | /* Both squote and dquote present. Use squote, |
| 12351 | and escape them */ |
| 12352 | osize += squote; |
| 12353 | else |
| 12354 | quote = '"'; |
| 12355 | } |
| 12356 | |
| 12357 | repr = PyUnicode_New(osize, max); |
| 12358 | if (repr == NULL) |
| 12359 | return NULL; |
| 12360 | okind = PyUnicode_KIND(repr); |
| 12361 | odata = PyUnicode_DATA(repr); |
| 12362 | |
| 12363 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12364 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12365 | |
| 12366 | for (i = 0, o = 1; i < isize; i++) { |
| 12367 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12368 | |
| 12369 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12370 | if ((ch == quote) || (ch == '\\')) { |
| 12371 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12372 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12373 | continue; |
| 12374 | } |
| 12375 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12376 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12377 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12378 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12379 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12380 | } |
| 12381 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12383 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12384 | } |
| 12385 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12386 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12387 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12388 | } |
| 12389 | |
| 12390 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12391 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12392 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12393 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12394 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12395 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12396 | } |
| 12397 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12398 | /* Copy ASCII characters as-is */ |
| 12399 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12401 | } |
| 12402 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12403 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12404 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12405 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12406 | (categories Z* and C* except ASCII space) |
| 12407 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12408 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12409 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12410 | if (ch <= 0xff) { |
| 12411 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12412 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12413 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12414 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12415 | } |
| 12416 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12417 | else if (ch >= 0x10000) { |
| 12418 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12419 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12420 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12421 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12422 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12423 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12424 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12425 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12426 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12427 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12428 | } |
| 12429 | /* Map 16-bit characters to '\uxxxx' */ |
| 12430 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12431 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12432 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12433 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12434 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12435 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12436 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12437 | } |
| 12438 | } |
| 12439 | /* Copy characters as-is */ |
| 12440 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12441 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12442 | } |
| 12443 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12446 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12447 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12448 | } |
| 12449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12450 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12451 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | \n\ |
| 12453 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12454 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12455 | arguments start and end are interpreted as in slice notation.\n\ |
| 12456 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12457 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12458 | |
| 12459 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12460 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12461 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12462 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12463 | Py_ssize_t start; |
| 12464 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12465 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12466 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12467 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12468 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12469 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12471 | if (PyUnicode_READY(self) == -1) |
| 12472 | return NULL; |
| 12473 | if (PyUnicode_READY(substring) == -1) |
| 12474 | return NULL; |
| 12475 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12476 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12477 | |
| 12478 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12480 | if (result == -2) |
| 12481 | return NULL; |
| 12482 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12483 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12484 | } |
| 12485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12486 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12487 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12488 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12489 | Like S.rfind() but raise ValueError when the substring is not found."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | |
| 12491 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12492 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12493 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12494 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12495 | Py_ssize_t start; |
| 12496 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12497 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12498 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12499 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12500 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12501 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12502 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12503 | if (PyUnicode_READY(self) == -1) |
| 12504 | return NULL; |
| 12505 | if (PyUnicode_READY(substring) == -1) |
| 12506 | return NULL; |
| 12507 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12508 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | |
| 12510 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | if (result == -2) |
| 12513 | return NULL; |
| 12514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | if (result < 0) { |
| 12516 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12517 | return NULL; |
| 12518 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12519 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12520 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12521 | } |
| 12522 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12523 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12524 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12526 | Return S right-justified in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12527 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12528 | |
| 12529 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12530 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12531 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12532 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12533 | Py_UCS4 fillchar = ' '; |
| 12534 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12535 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12536 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12537 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12538 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | return NULL; |
| 12540 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12541 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12542 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12544 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12545 | } |
| 12546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12547 | PyObject * |
| 12548 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | { |
| 12550 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12552 | s = PyUnicode_FromObject(s); |
| 12553 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12554 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12555 | if (sep != NULL) { |
| 12556 | sep = PyUnicode_FromObject(sep); |
| 12557 | if (sep == NULL) { |
| 12558 | Py_DECREF(s); |
| 12559 | return NULL; |
| 12560 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | } |
| 12562 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12563 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12564 | |
| 12565 | Py_DECREF(s); |
| 12566 | Py_XDECREF(sep); |
| 12567 | return result; |
| 12568 | } |
| 12569 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12570 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12571 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | \n\ |
| 12573 | Return a list of the words in S, using sep as the\n\ |
| 12574 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12575 | splits are done. If sep is not specified or is None, any\n\ |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 12576 | whitespace string is a separator and empty strings are\n\ |
| 12577 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12578 | |
| 12579 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12580 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12582 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12583 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12584 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12585 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12586 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12587 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12588 | return NULL; |
| 12589 | |
| 12590 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12591 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12592 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12593 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12594 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12595 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12596 | } |
| 12597 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12598 | PyObject * |
| 12599 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12600 | { |
| 12601 | PyObject* str_obj; |
| 12602 | PyObject* sep_obj; |
| 12603 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12604 | int kind1, kind2, kind; |
| 12605 | void *buf1 = NULL, *buf2 = NULL; |
| 12606 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12607 | |
| 12608 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12609 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12610 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12611 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12612 | if (!sep_obj) { |
| 12613 | Py_DECREF(str_obj); |
| 12614 | return NULL; |
| 12615 | } |
| 12616 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12617 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12618 | Py_DECREF(str_obj); |
| 12619 | return NULL; |
| 12620 | } |
| 12621 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12622 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12623 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12624 | kind = Py_MAX(kind1, kind2); |
| 12625 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12626 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12627 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12628 | if (!buf1) |
| 12629 | goto onError; |
| 12630 | buf2 = PyUnicode_DATA(sep_obj); |
| 12631 | if (kind2 != kind) |
| 12632 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12633 | if (!buf2) |
| 12634 | goto onError; |
| 12635 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12636 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12637 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12638 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12639 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12640 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12641 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12642 | else |
| 12643 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12644 | break; |
| 12645 | case PyUnicode_2BYTE_KIND: |
| 12646 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12647 | break; |
| 12648 | case PyUnicode_4BYTE_KIND: |
| 12649 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12650 | break; |
| 12651 | default: |
| 12652 | assert(0); |
| 12653 | out = 0; |
| 12654 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12655 | |
| 12656 | Py_DECREF(sep_obj); |
| 12657 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12658 | if (kind1 != kind) |
| 12659 | PyMem_Free(buf1); |
| 12660 | if (kind2 != kind) |
| 12661 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12662 | |
| 12663 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12664 | onError: |
| 12665 | Py_DECREF(sep_obj); |
| 12666 | Py_DECREF(str_obj); |
| 12667 | if (kind1 != kind && buf1) |
| 12668 | PyMem_Free(buf1); |
| 12669 | if (kind2 != kind && buf2) |
| 12670 | PyMem_Free(buf2); |
| 12671 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12672 | } |
| 12673 | |
| 12674 | |
| 12675 | PyObject * |
| 12676 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12677 | { |
| 12678 | PyObject* str_obj; |
| 12679 | PyObject* sep_obj; |
| 12680 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12681 | int kind1, kind2, kind; |
| 12682 | void *buf1 = NULL, *buf2 = NULL; |
| 12683 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12684 | |
| 12685 | str_obj = PyUnicode_FromObject(str_in); |
| 12686 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12687 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12688 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12689 | if (!sep_obj) { |
| 12690 | Py_DECREF(str_obj); |
| 12691 | return NULL; |
| 12692 | } |
| 12693 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12694 | kind1 = PyUnicode_KIND(str_in); |
| 12695 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12696 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12697 | buf1 = PyUnicode_DATA(str_in); |
| 12698 | if (kind1 != kind) |
| 12699 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12700 | if (!buf1) |
| 12701 | goto onError; |
| 12702 | buf2 = PyUnicode_DATA(sep_obj); |
| 12703 | if (kind2 != kind) |
| 12704 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12705 | if (!buf2) |
| 12706 | goto onError; |
| 12707 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12708 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12709 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12710 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12711 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12712 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12713 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12714 | else |
| 12715 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12716 | break; |
| 12717 | case PyUnicode_2BYTE_KIND: |
| 12718 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12719 | break; |
| 12720 | case PyUnicode_4BYTE_KIND: |
| 12721 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12722 | break; |
| 12723 | default: |
| 12724 | assert(0); |
| 12725 | out = 0; |
| 12726 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12727 | |
| 12728 | Py_DECREF(sep_obj); |
| 12729 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12730 | if (kind1 != kind) |
| 12731 | PyMem_Free(buf1); |
| 12732 | if (kind2 != kind) |
| 12733 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12734 | |
| 12735 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | onError: |
| 12737 | Py_DECREF(sep_obj); |
| 12738 | Py_DECREF(str_obj); |
| 12739 | if (kind1 != kind && buf1) |
| 12740 | PyMem_Free(buf1); |
| 12741 | if (kind2 != kind && buf2) |
| 12742 | PyMem_Free(buf2); |
| 12743 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12744 | } |
| 12745 | |
| 12746 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12747 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12748 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12749 | Search for the separator sep in S, and return the part before it,\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12750 | the separator itself, and the part after it. If the separator is not\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12751 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12752 | |
| 12753 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12754 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12755 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12756 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12757 | } |
| 12758 | |
| 12759 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12760 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12761 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12762 | Search for the separator sep in S, starting at the end of S, and return\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12763 | the part before it, the separator itself, and the part after it. If the\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12764 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12765 | |
| 12766 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12767 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12768 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12769 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12770 | } |
| 12771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12772 | PyObject * |
| 12773 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12774 | { |
| 12775 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12776 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12777 | s = PyUnicode_FromObject(s); |
| 12778 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12779 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12780 | if (sep != NULL) { |
| 12781 | sep = PyUnicode_FromObject(sep); |
| 12782 | if (sep == NULL) { |
| 12783 | Py_DECREF(s); |
| 12784 | return NULL; |
| 12785 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12786 | } |
| 12787 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12788 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12789 | |
| 12790 | Py_DECREF(s); |
| 12791 | Py_XDECREF(sep); |
| 12792 | return result; |
| 12793 | } |
| 12794 | |
| 12795 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12796 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12797 | \n\ |
| 12798 | Return a list of the words in S, using sep as the\n\ |
| 12799 | delimiter string, starting at the end of the string and\n\ |
| 12800 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12801 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12802 | is a separator."); |
| 12803 | |
| 12804 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12805 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12806 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12807 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12808 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12809 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12810 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12811 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12812 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12813 | return NULL; |
| 12814 | |
| 12815 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12816 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12817 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12818 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12819 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12820 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12821 | } |
| 12822 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12823 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12825 | \n\ |
| 12826 | Return a list of the lines in S, breaking at line boundaries.\n\ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12827 | Line breaks are not included in the resulting list unless keepends\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12828 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12829 | |
| 12830 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12831 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12833 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12834 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12835 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12836 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12837 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12838 | return NULL; |
| 12839 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12840 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 | } |
| 12842 | |
| 12843 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12844 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12846 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | } |
| 12848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12849 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12850 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12851 | \n\ |
| 12852 | Return a copy of S with uppercase characters converted to lowercase\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12853 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12854 | |
| 12855 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12856 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12857 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12858 | if (PyUnicode_READY(self) == -1) |
| 12859 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12860 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12861 | } |
| 12862 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12863 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12864 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12865 | \n\ |
| 12866 | Return a translation table usable for str.translate().\n\ |
| 12867 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12868 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12869 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12870 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12871 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12872 | character at the same position in y. If there is a third argument, it\n\ |
| 12873 | must be a string, whose characters will be mapped to None in the result."); |
| 12874 | |
| 12875 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12876 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12877 | { |
| 12878 | PyObject *x, *y = NULL, *z = NULL; |
| 12879 | PyObject *new = NULL, *key, *value; |
| 12880 | Py_ssize_t i = 0; |
| 12881 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12882 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12883 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12884 | return NULL; |
| 12885 | new = PyDict_New(); |
| 12886 | if (!new) |
| 12887 | return NULL; |
| 12888 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12889 | int x_kind, y_kind, z_kind; |
| 12890 | void *x_data, *y_data, *z_data; |
| 12891 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12892 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12893 | if (!PyUnicode_Check(x)) { |
| 12894 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12895 | "be a string if there is a second argument"); |
| 12896 | goto err; |
| 12897 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12898 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12899 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12900 | "arguments must have equal length"); |
| 12901 | goto err; |
| 12902 | } |
| 12903 | /* create entries for translating chars in x to those in y */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12904 | x_kind = PyUnicode_KIND(x); |
| 12905 | y_kind = PyUnicode_KIND(y); |
| 12906 | x_data = PyUnicode_DATA(x); |
| 12907 | y_data = PyUnicode_DATA(y); |
| 12908 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12909 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12910 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12911 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12912 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12913 | if (!value) { |
| 12914 | Py_DECREF(key); |
| 12915 | goto err; |
| 12916 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12917 | res = PyDict_SetItem(new, key, value); |
| 12918 | Py_DECREF(key); |
| 12919 | Py_DECREF(value); |
| 12920 | if (res < 0) |
| 12921 | goto err; |
| 12922 | } |
| 12923 | /* create entries for deleting chars in z */ |
| 12924 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12925 | z_kind = PyUnicode_KIND(z); |
| 12926 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12927 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12928 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12929 | if (!key) |
| 12930 | goto err; |
| 12931 | res = PyDict_SetItem(new, key, Py_None); |
| 12932 | Py_DECREF(key); |
| 12933 | if (res < 0) |
| 12934 | goto err; |
| 12935 | } |
| 12936 | } |
| 12937 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12938 | int kind; |
| 12939 | void *data; |
| 12940 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12941 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12942 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12943 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12944 | "to maketrans it must be a dict"); |
| 12945 | goto err; |
| 12946 | } |
| 12947 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12948 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12949 | if (PyUnicode_Check(key)) { |
| 12950 | /* convert string keys to integer keys */ |
| 12951 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12952 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12953 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12954 | "table must be of length 1"); |
| 12955 | goto err; |
| 12956 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12957 | kind = PyUnicode_KIND(key); |
| 12958 | data = PyUnicode_DATA(key); |
| 12959 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12960 | if (!newkey) |
| 12961 | goto err; |
| 12962 | res = PyDict_SetItem(new, newkey, value); |
| 12963 | Py_DECREF(newkey); |
| 12964 | if (res < 0) |
| 12965 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12966 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12967 | /* just keep integer keys */ |
| 12968 | if (PyDict_SetItem(new, key, value) < 0) |
| 12969 | goto err; |
| 12970 | } else { |
| 12971 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12972 | "be strings or integers"); |
| 12973 | goto err; |
| 12974 | } |
| 12975 | } |
| 12976 | } |
| 12977 | return new; |
| 12978 | err: |
| 12979 | Py_DECREF(new); |
| 12980 | return NULL; |
| 12981 | } |
| 12982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12983 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12984 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12985 | \n\ |
| 12986 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12987 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12988 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12989 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12990 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12991 | |
| 12992 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12993 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12994 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12995 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12996 | } |
| 12997 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12998 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12999 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13000 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13001 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13002 | |
| 13003 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13004 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13005 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13006 | if (PyUnicode_READY(self) == -1) |
| 13007 | return NULL; |
| 13008 | if (PyUnicode_IS_ASCII(self)) |
| 13009 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13010 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13011 | } |
| 13012 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13013 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13014 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13015 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13016 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13017 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13018 | |
| 13019 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13020 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13021 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13022 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13023 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13024 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13025 | int kind; |
| 13026 | void *data; |
| 13027 | Py_UCS4 chr; |
| 13028 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13029 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13030 | return NULL; |
| 13031 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13032 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13033 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13034 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13035 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13036 | return unicode_result_unchanged(self); |
| 13037 | |
| 13038 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13039 | |
| 13040 | u = pad(self, fill, 0, '0'); |
| 13041 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13042 | if (u == NULL) |
| 13043 | return NULL; |
| 13044 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13045 | kind = PyUnicode_KIND(u); |
| 13046 | data = PyUnicode_DATA(u); |
| 13047 | chr = PyUnicode_READ(kind, data, fill); |
| 13048 | |
| 13049 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13050 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13051 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13052 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13053 | } |
| 13054 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13055 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13056 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13057 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13058 | |
| 13059 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13060 | static PyObject * |
| 13061 | unicode__decimal2ascii(PyObject *self) |
| 13062 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13063 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13065 | #endif |
| 13066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13067 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13068 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13069 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13070 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13071 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13072 | With optional end, stop comparing S at that position.\n\ |
| 13073 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13074 | |
| 13075 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13076 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13077 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13078 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13079 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13080 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13081 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13082 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13083 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13084 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13085 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13086 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13087 | if (PyTuple_Check(subobj)) { |
| 13088 | Py_ssize_t i; |
| 13089 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13090 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13091 | if (substring == NULL) |
| 13092 | return NULL; |
| 13093 | result = tailmatch(self, substring, start, end, -1); |
| 13094 | Py_DECREF(substring); |
| 13095 | if (result) { |
| 13096 | Py_RETURN_TRUE; |
| 13097 | } |
| 13098 | } |
| 13099 | /* nothing matched */ |
| 13100 | Py_RETURN_FALSE; |
| 13101 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13102 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13103 | if (substring == NULL) { |
| 13104 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13105 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13106 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13107 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13108 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13109 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13111 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13112 | } |
| 13113 | |
| 13114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13115 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13116 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13117 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13118 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13119 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13120 | With optional end, stop comparing S at that position.\n\ |
| 13121 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13122 | |
| 13123 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13124 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13125 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13126 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13127 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13128 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13129 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13130 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13131 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13132 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13133 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13134 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13135 | if (PyTuple_Check(subobj)) { |
| 13136 | Py_ssize_t i; |
| 13137 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13138 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13139 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13140 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13141 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13142 | result = tailmatch(self, substring, start, end, +1); |
| 13143 | Py_DECREF(substring); |
| 13144 | if (result) { |
| 13145 | Py_RETURN_TRUE; |
| 13146 | } |
| 13147 | } |
| 13148 | Py_RETURN_FALSE; |
| 13149 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13150 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13151 | if (substring == NULL) { |
| 13152 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13153 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13154 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13155 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13156 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13157 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13158 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13159 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13160 | } |
| 13161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13162 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13163 | |
| 13164 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13165 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13166 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13167 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13168 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13169 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13170 | PyDoc_STRVAR(format_map__doc__, |
| 13171 | "S.format_map(mapping) -> str\n\ |
| 13172 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13173 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13174 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13175 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13176 | static PyObject * |
| 13177 | unicode__format__(PyObject* self, PyObject* args) |
| 13178 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13179 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13180 | |
| 13181 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13182 | return NULL; |
| 13183 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13184 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13185 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13186 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13187 | } |
| 13188 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13189 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13190 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13191 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13192 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13193 | |
| 13194 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13195 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13196 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13197 | Py_ssize_t size; |
| 13198 | |
| 13199 | /* If it's a compact object, account for base structure + |
| 13200 | character data. */ |
| 13201 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13202 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13203 | else if (PyUnicode_IS_COMPACT(v)) |
| 13204 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13205 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13206 | else { |
| 13207 | /* If it is a two-block object, account for base object, and |
| 13208 | for character block if present. */ |
| 13209 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13210 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13211 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13212 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13213 | } |
| 13214 | /* If the wstr pointer is present, account for it unless it is shared |
Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 13215 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13216 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13217 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13218 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13219 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13220 | |
| 13221 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13222 | } |
| 13223 | |
| 13224 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13225 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13226 | |
| 13227 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13228 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13229 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13230 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13231 | if (!copy) |
| 13232 | return NULL; |
| 13233 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13234 | } |
| 13235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13236 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13237 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13238 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13239 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13240 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13241 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13242 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13243 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13244 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13245 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13246 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13247 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13248 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13249 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13250 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13251 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13252 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13253 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13254 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13255 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13256 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13257 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13258 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13259 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13260 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13261 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13262 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13263 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13264 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13265 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13266 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13267 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13268 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13269 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13270 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13271 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13272 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13273 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13274 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13275 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13276 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13277 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13278 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13279 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13280 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13281 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13282 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13283 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13284 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13285 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13286 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13287 | #endif |
| 13288 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13289 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13290 | {NULL, NULL} |
| 13291 | }; |
| 13292 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13293 | static PyObject * |
| 13294 | unicode_mod(PyObject *v, PyObject *w) |
| 13295 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13296 | if (!PyUnicode_Check(v)) |
| 13297 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13298 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13299 | } |
| 13300 | |
| 13301 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13302 | 0, /*nb_add*/ |
| 13303 | 0, /*nb_subtract*/ |
| 13304 | 0, /*nb_multiply*/ |
| 13305 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13306 | }; |
| 13307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13308 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13309 | (lenfunc) unicode_length, /* sq_length */ |
| 13310 | PyUnicode_Concat, /* sq_concat */ |
| 13311 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13312 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13313 | 0, /* sq_slice */ |
| 13314 | 0, /* sq_ass_item */ |
| 13315 | 0, /* sq_ass_slice */ |
| 13316 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13317 | }; |
| 13318 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13319 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13320 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13321 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13322 | if (PyUnicode_READY(self) == -1) |
| 13323 | return NULL; |
| 13324 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13325 | if (PyIndex_Check(item)) { |
| 13326 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13327 | if (i == -1 && PyErr_Occurred()) |
| 13328 | return NULL; |
| 13329 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13330 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13331 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13332 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13333 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13334 | PyObject *result; |
| 13335 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13336 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13337 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13338 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13339 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13340 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13341 | return NULL; |
| 13342 | } |
| 13343 | |
| 13344 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13345 | Py_INCREF(unicode_empty); |
| 13346 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13347 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13348 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13349 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13350 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13351 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13352 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13353 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13354 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13355 | src_kind = PyUnicode_KIND(self); |
| 13356 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13357 | if (!PyUnicode_IS_ASCII(self)) { |
| 13358 | kind_limit = kind_maxchar_limit(src_kind); |
| 13359 | max_char = 0; |
| 13360 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13361 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13362 | if (ch > max_char) { |
| 13363 | max_char = ch; |
| 13364 | if (max_char >= kind_limit) |
| 13365 | break; |
| 13366 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13367 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13368 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13369 | else |
| 13370 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13371 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13372 | if (result == NULL) |
| 13373 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13374 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13375 | dest_data = PyUnicode_DATA(result); |
| 13376 | |
| 13377 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13378 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13379 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13380 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13381 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13382 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13383 | } else { |
| 13384 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13385 | return NULL; |
| 13386 | } |
| 13387 | } |
| 13388 | |
| 13389 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13390 | (lenfunc)unicode_length, /* mp_length */ |
| 13391 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13392 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13393 | }; |
| 13394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13396 | /* Helpers for PyUnicode_Format() */ |
| 13397 | |
| 13398 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13399 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13400 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13401 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13402 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13403 | (*p_argidx)++; |
| 13404 | if (arglen < 0) |
| 13405 | return args; |
| 13406 | else |
| 13407 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13408 | } |
| 13409 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13410 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13411 | return NULL; |
| 13412 | } |
| 13413 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13414 | /* Returns a new reference to a PyUnicode object, or NULL on failure. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13415 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13416 | static PyObject * |
| 13417 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13418 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13419 | char *p; |
| 13420 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13421 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13423 | x = PyFloat_AsDouble(v); |
| 13424 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13425 | return NULL; |
| 13426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13427 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13428 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13429 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13430 | p = PyOS_double_to_string(x, type, prec, |
| 13431 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13432 | if (p == NULL) |
| 13433 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13434 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13435 | PyMem_Free(p); |
| 13436 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13437 | } |
| 13438 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13439 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13440 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13441 | * Python's regular ints. |
| 13442 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13443 | * The output string is of the form |
| 13444 | * "-"? ("0x" | "0X")? digit+ |
| 13445 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13446 | * set in flags. The case of hex digits will be correct, |
| 13447 | * There will be at least prec digits, zero-filled on the left if |
| 13448 | * necessary to get that many. |
| 13449 | * val object to be converted |
| 13450 | * flags bitmask of format flags; only F_ALT is looked at |
| 13451 | * prec minimum number of digits; 0-fill on left if needed |
| 13452 | * type a character in [duoxX]; u acts the same as d |
| 13453 | * |
| 13454 | * CAUTION: o, x and X conversions on regular ints can never |
| 13455 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13456 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13457 | static PyObject* |
| 13458 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13459 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13460 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13461 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13462 | Py_ssize_t i; |
| 13463 | int sign; /* 1 if '-', else 0 */ |
| 13464 | int len; /* number of characters */ |
| 13465 | Py_ssize_t llen; |
| 13466 | int numdigits; /* len == numnondigits + numdigits */ |
| 13467 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13468 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13469 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13470 | if (prec > INT_MAX-3) { |
| 13471 | PyErr_SetString(PyExc_OverflowError, |
| 13472 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13473 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13474 | } |
| 13475 | |
| 13476 | assert(PyLong_Check(val)); |
| 13477 | |
| 13478 | switch (type) { |
| 13479 | case 'd': |
| 13480 | case 'u': |
| 13481 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13482 | if (PyBool_Check(val)) |
| 13483 | result = PyNumber_ToBase(val, 10); |
| 13484 | else |
| 13485 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13486 | break; |
| 13487 | case 'o': |
| 13488 | numnondigits = 2; |
| 13489 | result = PyNumber_ToBase(val, 8); |
| 13490 | break; |
| 13491 | case 'x': |
| 13492 | case 'X': |
| 13493 | numnondigits = 2; |
| 13494 | result = PyNumber_ToBase(val, 16); |
| 13495 | break; |
| 13496 | default: |
| 13497 | assert(!"'type' not in [duoxX]"); |
| 13498 | } |
| 13499 | if (!result) |
| 13500 | return NULL; |
| 13501 | |
| 13502 | assert(unicode_modifiable(result)); |
| 13503 | assert(PyUnicode_IS_READY(result)); |
| 13504 | assert(PyUnicode_IS_ASCII(result)); |
| 13505 | |
| 13506 | /* To modify the string in-place, there can only be one reference. */ |
| 13507 | if (Py_REFCNT(result) != 1) { |
| 13508 | PyErr_BadInternalCall(); |
| 13509 | return NULL; |
| 13510 | } |
| 13511 | buf = PyUnicode_DATA(result); |
| 13512 | llen = PyUnicode_GET_LENGTH(result); |
| 13513 | if (llen > INT_MAX) { |
| 13514 | PyErr_SetString(PyExc_ValueError, |
| 13515 | "string too large in _PyBytes_FormatLong"); |
| 13516 | return NULL; |
| 13517 | } |
| 13518 | len = (int)llen; |
| 13519 | sign = buf[0] == '-'; |
| 13520 | numnondigits += sign; |
| 13521 | numdigits = len - numnondigits; |
| 13522 | assert(numdigits > 0); |
| 13523 | |
| 13524 | /* Get rid of base marker unless F_ALT */ |
| 13525 | if (((flags & F_ALT) == 0 && |
| 13526 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13527 | assert(buf[sign] == '0'); |
| 13528 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13529 | buf[sign+1] == 'o'); |
| 13530 | numnondigits -= 2; |
| 13531 | buf += 2; |
| 13532 | len -= 2; |
| 13533 | if (sign) |
| 13534 | buf[0] = '-'; |
| 13535 | assert(len == numnondigits + numdigits); |
| 13536 | assert(numdigits > 0); |
| 13537 | } |
| 13538 | |
| 13539 | /* Fill with leading zeroes to meet minimum width. */ |
| 13540 | if (prec > numdigits) { |
| 13541 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13542 | numnondigits + prec); |
| 13543 | char *b1; |
| 13544 | if (!r1) { |
| 13545 | Py_DECREF(result); |
| 13546 | return NULL; |
| 13547 | } |
| 13548 | b1 = PyBytes_AS_STRING(r1); |
| 13549 | for (i = 0; i < numnondigits; ++i) |
| 13550 | *b1++ = *buf++; |
| 13551 | for (i = 0; i < prec - numdigits; i++) |
| 13552 | *b1++ = '0'; |
| 13553 | for (i = 0; i < numdigits; i++) |
| 13554 | *b1++ = *buf++; |
| 13555 | *b1 = '\0'; |
| 13556 | Py_DECREF(result); |
| 13557 | result = r1; |
| 13558 | buf = PyBytes_AS_STRING(result); |
| 13559 | len = numnondigits + prec; |
| 13560 | } |
| 13561 | |
| 13562 | /* Fix up case for hex conversions. */ |
| 13563 | if (type == 'X') { |
| 13564 | /* Need to convert all lower case letters to upper case. |
| 13565 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13566 | for (i = 0; i < len; i++) |
| 13567 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13568 | buf[i] -= 'a'-'A'; |
| 13569 | } |
| 13570 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13571 | PyObject *unicode; |
| 13572 | unicode = unicode_fromascii((unsigned char *)buf, len); |
| 13573 | Py_DECREF(result); |
| 13574 | result = unicode; |
| 13575 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13576 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13577 | } |
| 13578 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13579 | static Py_UCS4 |
| 13580 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13581 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13582 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13583 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13584 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13585 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13586 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13587 | goto onError; |
| 13588 | } |
| 13589 | else { |
| 13590 | /* Integer input truncated to a character */ |
| 13591 | long x; |
| 13592 | x = PyLong_AsLong(v); |
| 13593 | if (x == -1 && PyErr_Occurred()) |
| 13594 | goto onError; |
| 13595 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13596 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13597 | PyErr_SetString(PyExc_OverflowError, |
| 13598 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13599 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13600 | } |
| 13601 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13602 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13603 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13604 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13605 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13606 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13607 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13608 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13609 | } |
| 13610 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13611 | static int |
| 13612 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13613 | { |
| 13614 | int r; |
| 13615 | assert(count > 0); |
| 13616 | assert(PyUnicode_Check(obj)); |
| 13617 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13618 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13619 | if (repeated == NULL) |
| 13620 | return -1; |
| 13621 | r = _PyAccu_Accumulate(acc, repeated); |
| 13622 | Py_DECREF(repeated); |
| 13623 | return r; |
| 13624 | } |
| 13625 | else { |
| 13626 | do { |
| 13627 | if (_PyAccu_Accumulate(acc, obj)) |
| 13628 | return -1; |
| 13629 | } while (--count); |
| 13630 | return 0; |
| 13631 | } |
| 13632 | } |
| 13633 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13634 | PyObject * |
| 13635 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13636 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13637 | void *fmt; |
| 13638 | int fmtkind; |
| 13639 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13640 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13641 | int r; |
| 13642 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13643 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13644 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13645 | PyObject *temp = NULL; |
| 13646 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13647 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13648 | _PyAccu acc; |
| 13649 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13650 | |
| 13651 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13652 | return NULL; |
| 13653 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13654 | return NULL; |
| 13655 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13656 | return NULL; |
| 13657 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13658 | return NULL; |
| 13659 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13660 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13662 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13663 | PyErr_BadInternalCall(); |
| 13664 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13665 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13666 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13667 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13668 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13669 | if (PyUnicode_READY(uformat) == -1) |
| 13670 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13671 | if (_PyAccu_Init(&acc)) |
| 13672 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13673 | fmt = PyUnicode_DATA(uformat); |
| 13674 | fmtkind = PyUnicode_KIND(uformat); |
| 13675 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13676 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13677 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13678 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13679 | arglen = PyTuple_Size(args); |
| 13680 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13681 | } |
| 13682 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13683 | arglen = -1; |
| 13684 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13685 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13686 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13687 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13688 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13689 | |
| 13690 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13691 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13692 | PyObject *nonfmt; |
| 13693 | Py_ssize_t nonfmtpos; |
| 13694 | nonfmtpos = fmtpos++; |
| 13695 | while (fmtcnt >= 0 && |
| 13696 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13697 | fmtpos++; |
| 13698 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13699 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13700 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13701 | if (nonfmt == NULL) |
| 13702 | goto onError; |
| 13703 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13704 | Py_DECREF(nonfmt); |
| 13705 | if (r) |
| 13706 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13707 | } |
| 13708 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13709 | /* Got a format specifier */ |
| 13710 | int flags = 0; |
| 13711 | Py_ssize_t width = -1; |
| 13712 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13713 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13714 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13715 | int isnumok; |
| 13716 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13717 | void *pbuf = NULL; |
| 13718 | Py_ssize_t pindex, len; |
| 13719 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13721 | fmtpos++; |
| 13722 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13723 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13724 | Py_ssize_t keylen; |
| 13725 | PyObject *key; |
| 13726 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13728 | if (dict == NULL) { |
| 13729 | PyErr_SetString(PyExc_TypeError, |
| 13730 | "format requires a mapping"); |
| 13731 | goto onError; |
| 13732 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13733 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13734 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13735 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13736 | /* Skip over balanced parentheses */ |
| 13737 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13738 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13739 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13740 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13741 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13742 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13743 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13744 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13745 | if (fmtcnt < 0 || pcount > 0) { |
| 13746 | PyErr_SetString(PyExc_ValueError, |
| 13747 | "incomplete format key"); |
| 13748 | goto onError; |
| 13749 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13750 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13751 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13752 | if (key == NULL) |
| 13753 | goto onError; |
| 13754 | if (args_owned) { |
| 13755 | Py_DECREF(args); |
| 13756 | args_owned = 0; |
| 13757 | } |
| 13758 | args = PyObject_GetItem(dict, key); |
| 13759 | Py_DECREF(key); |
| 13760 | if (args == NULL) { |
| 13761 | goto onError; |
| 13762 | } |
| 13763 | args_owned = 1; |
| 13764 | arglen = -1; |
| 13765 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13766 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13767 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13768 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13769 | case '-': flags |= F_LJUST; continue; |
| 13770 | case '+': flags |= F_SIGN; continue; |
| 13771 | case ' ': flags |= F_BLANK; continue; |
| 13772 | case '#': flags |= F_ALT; continue; |
| 13773 | case '0': flags |= F_ZERO; continue; |
| 13774 | } |
| 13775 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13776 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13777 | if (c == '*') { |
| 13778 | v = getnextarg(args, arglen, &argidx); |
| 13779 | if (v == NULL) |
| 13780 | goto onError; |
| 13781 | if (!PyLong_Check(v)) { |
| 13782 | PyErr_SetString(PyExc_TypeError, |
| 13783 | "* wants int"); |
| 13784 | goto onError; |
| 13785 | } |
| 13786 | width = PyLong_AsLong(v); |
| 13787 | if (width == -1 && PyErr_Occurred()) |
| 13788 | goto onError; |
| 13789 | if (width < 0) { |
| 13790 | flags |= F_LJUST; |
| 13791 | width = -width; |
| 13792 | } |
| 13793 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13794 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13795 | } |
| 13796 | else if (c >= '0' && c <= '9') { |
| 13797 | width = c - '0'; |
| 13798 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13799 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13800 | if (c < '0' || c > '9') |
| 13801 | break; |
| 13802 | if ((width*10) / 10 != width) { |
| 13803 | PyErr_SetString(PyExc_ValueError, |
| 13804 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13805 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13806 | } |
| 13807 | width = width*10 + (c - '0'); |
| 13808 | } |
| 13809 | } |
| 13810 | if (c == '.') { |
| 13811 | prec = 0; |
| 13812 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13813 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13814 | if (c == '*') { |
| 13815 | v = getnextarg(args, arglen, &argidx); |
| 13816 | if (v == NULL) |
| 13817 | goto onError; |
| 13818 | if (!PyLong_Check(v)) { |
| 13819 | PyErr_SetString(PyExc_TypeError, |
| 13820 | "* wants int"); |
| 13821 | goto onError; |
| 13822 | } |
| 13823 | prec = PyLong_AsLong(v); |
| 13824 | if (prec == -1 && PyErr_Occurred()) |
| 13825 | goto onError; |
| 13826 | if (prec < 0) |
| 13827 | prec = 0; |
| 13828 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13829 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13830 | } |
| 13831 | else if (c >= '0' && c <= '9') { |
| 13832 | prec = c - '0'; |
| 13833 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13834 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13835 | if (c < '0' || c > '9') |
| 13836 | break; |
| 13837 | if ((prec*10) / 10 != prec) { |
| 13838 | PyErr_SetString(PyExc_ValueError, |
| 13839 | "prec too big"); |
| 13840 | goto onError; |
| 13841 | } |
| 13842 | prec = prec*10 + (c - '0'); |
| 13843 | } |
| 13844 | } |
| 13845 | } /* prec */ |
| 13846 | if (fmtcnt >= 0) { |
| 13847 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13848 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13849 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13850 | } |
| 13851 | } |
| 13852 | if (fmtcnt < 0) { |
| 13853 | PyErr_SetString(PyExc_ValueError, |
| 13854 | "incomplete format"); |
| 13855 | goto onError; |
| 13856 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame^] | 13857 | |
| 13858 | if (c == '%') { |
| 13859 | _PyAccu_Accumulate(&acc, percent); |
| 13860 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13861 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame^] | 13862 | |
| 13863 | |
| 13864 | v = getnextarg(args, arglen, &argidx); |
| 13865 | if (v == NULL) |
| 13866 | goto onError; |
| 13867 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13868 | sign = 0; |
| 13869 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13870 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13871 | switch (c) { |
| 13872 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13873 | case 's': |
| 13874 | case 'r': |
| 13875 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13876 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13877 | temp = v; |
| 13878 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13879 | } |
| 13880 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13881 | if (c == 's') |
| 13882 | temp = PyObject_Str(v); |
| 13883 | else if (c == 'r') |
| 13884 | temp = PyObject_Repr(v); |
| 13885 | else |
| 13886 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13887 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13888 | break; |
| 13889 | |
| 13890 | case 'i': |
| 13891 | case 'd': |
| 13892 | case 'u': |
| 13893 | case 'o': |
| 13894 | case 'x': |
| 13895 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13896 | isnumok = 0; |
| 13897 | if (PyNumber_Check(v)) { |
| 13898 | PyObject *iobj=NULL; |
| 13899 | |
| 13900 | if (PyLong_Check(v)) { |
| 13901 | iobj = v; |
| 13902 | Py_INCREF(iobj); |
| 13903 | } |
| 13904 | else { |
| 13905 | iobj = PyNumber_Long(v); |
| 13906 | } |
| 13907 | if (iobj!=NULL) { |
| 13908 | if (PyLong_Check(iobj)) { |
| 13909 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame^] | 13910 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13911 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13912 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13913 | } |
| 13914 | else { |
| 13915 | Py_DECREF(iobj); |
| 13916 | } |
| 13917 | } |
| 13918 | } |
| 13919 | if (!isnumok) { |
| 13920 | PyErr_Format(PyExc_TypeError, |
| 13921 | "%%%c format: a number is required, " |
| 13922 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13923 | goto onError; |
| 13924 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13925 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13926 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13927 | fillobj = zero; |
| 13928 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13929 | break; |
| 13930 | |
| 13931 | case 'e': |
| 13932 | case 'E': |
| 13933 | case 'f': |
| 13934 | case 'F': |
| 13935 | case 'g': |
| 13936 | case 'G': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13937 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13938 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13939 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13940 | fillobj = zero; |
| 13941 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame^] | 13942 | temp = formatfloat(v, flags, prec, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13943 | break; |
| 13944 | |
| 13945 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13946 | { |
| 13947 | Py_UCS4 ch = formatchar(v); |
| 13948 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13949 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13950 | temp = _PyUnicode_FromUCS4(&ch, 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13951 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13952 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13953 | |
| 13954 | default: |
| 13955 | PyErr_Format(PyExc_ValueError, |
| 13956 | "unsupported format character '%c' (0x%x) " |
| 13957 | "at index %zd", |
| 13958 | (31<=c && c<=126) ? (char)c : '?', |
| 13959 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13960 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13961 | goto onError; |
| 13962 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame^] | 13963 | if (temp == NULL) |
| 13964 | goto onError; |
| 13965 | assert (PyUnicode_Check(temp)); |
| 13966 | if (PyUnicode_READY(temp) == -1) { |
| 13967 | Py_CLEAR(temp); |
| 13968 | goto onError; |
| 13969 | } |
| 13970 | kind = PyUnicode_KIND(temp); |
| 13971 | pbuf = PyUnicode_DATA(temp); |
| 13972 | len = PyUnicode_GET_LENGTH(temp); |
| 13973 | |
| 13974 | if (c == 's' || c == 'r' || c == 'a') { |
| 13975 | if (prec >= 0 && len > prec) |
| 13976 | len = prec; |
| 13977 | } |
| 13978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13979 | /* pbuf is initialized here. */ |
| 13980 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13981 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13982 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13983 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13984 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13985 | pindex++; |
| 13986 | } |
| 13987 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13988 | signobj = plus; |
| 13989 | len--; |
| 13990 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13991 | } |
| 13992 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13993 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13994 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13995 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13996 | else |
| 13997 | sign = 0; |
| 13998 | } |
| 13999 | if (width < len) |
| 14000 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14001 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14002 | if (fill != ' ') { |
| 14003 | assert(signobj != NULL); |
| 14004 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 14005 | goto onError; |
| 14006 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14007 | if (width > len) |
| 14008 | width--; |
| 14009 | } |
| 14010 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14011 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14012 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14013 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14014 | second = get_latin1_char( |
| 14015 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 14016 | pindex += 2; |
| 14017 | if (second == NULL || |
| 14018 | _PyAccu_Accumulate(&acc, zero) || |
| 14019 | _PyAccu_Accumulate(&acc, second)) |
| 14020 | goto onError; |
| 14021 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14022 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14023 | width -= 2; |
| 14024 | if (width < 0) |
| 14025 | width = 0; |
| 14026 | len -= 2; |
| 14027 | } |
| 14028 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14029 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14030 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 14031 | goto onError; |
| 14032 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14033 | } |
| 14034 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14035 | if (sign) { |
| 14036 | assert(signobj != NULL); |
| 14037 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 14038 | goto onError; |
| 14039 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14040 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14041 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14042 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14043 | second = get_latin1_char( |
| 14044 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 14045 | pindex += 2; |
| 14046 | if (second == NULL || |
| 14047 | _PyAccu_Accumulate(&acc, zero) || |
| 14048 | _PyAccu_Accumulate(&acc, second)) |
| 14049 | goto onError; |
| 14050 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14051 | } |
| 14052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14053 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14054 | if (temp != NULL) { |
| 14055 | assert(pbuf == PyUnicode_DATA(temp)); |
| 14056 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14057 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14058 | else { |
| 14059 | const char *p = (const char *) pbuf; |
| 14060 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14061 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14062 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 14063 | } |
| 14064 | if (v == NULL) |
| 14065 | goto onError; |
| 14066 | r = _PyAccu_Accumulate(&acc, v); |
| 14067 | Py_DECREF(v); |
| 14068 | if (r) |
| 14069 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14070 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 14071 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14072 | if (dict && (argidx < arglen) && c != '%') { |
| 14073 | PyErr_SetString(PyExc_TypeError, |
| 14074 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14075 | goto onError; |
| 14076 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14077 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14078 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14079 | } /* until end */ |
| 14080 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14081 | PyErr_SetString(PyExc_TypeError, |
| 14082 | "not all arguments converted during string formatting"); |
| 14083 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14084 | } |
| 14085 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14086 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14087 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14088 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14089 | } |
| 14090 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14091 | Py_XDECREF(temp); |
| 14092 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14093 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14094 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14095 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14096 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14097 | Py_XDECREF(temp); |
| 14098 | Py_XDECREF(second); |
| 14099 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14100 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14101 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14102 | } |
| 14103 | return NULL; |
| 14104 | } |
| 14105 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14106 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14107 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14108 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14109 | static PyObject * |
| 14110 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14111 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14112 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14113 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14114 | char *encoding = NULL; |
| 14115 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14116 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14117 | if (type != &PyUnicode_Type) |
| 14118 | return unicode_subtype_new(type, args, kwds); |
| 14119 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14120 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14121 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 14122 | if (x == NULL) { |
| 14123 | Py_INCREF(unicode_empty); |
| 14124 | return unicode_empty; |
| 14125 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14126 | if (encoding == NULL && errors == NULL) |
| 14127 | return PyObject_Str(x); |
| 14128 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14129 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14130 | } |
| 14131 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14132 | static PyObject * |
| 14133 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14134 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14135 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14136 | Py_ssize_t length, char_size; |
| 14137 | int share_wstr, share_utf8; |
| 14138 | unsigned int kind; |
| 14139 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14140 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14141 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14142 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14143 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14144 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14145 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14146 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14147 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14148 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14149 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14150 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14151 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14152 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14153 | if (self == NULL) { |
| 14154 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14155 | return NULL; |
| 14156 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14157 | kind = PyUnicode_KIND(unicode); |
| 14158 | length = PyUnicode_GET_LENGTH(unicode); |
| 14159 | |
| 14160 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14161 | #ifdef Py_DEBUG |
| 14162 | _PyUnicode_HASH(self) = -1; |
| 14163 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14164 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14165 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14166 | _PyUnicode_STATE(self).interned = 0; |
| 14167 | _PyUnicode_STATE(self).kind = kind; |
| 14168 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14169 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14170 | _PyUnicode_STATE(self).ready = 1; |
| 14171 | _PyUnicode_WSTR(self) = NULL; |
| 14172 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14173 | _PyUnicode_UTF8(self) = NULL; |
| 14174 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14175 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14176 | |
| 14177 | share_utf8 = 0; |
| 14178 | share_wstr = 0; |
| 14179 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14180 | char_size = 1; |
| 14181 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14182 | share_utf8 = 1; |
| 14183 | } |
| 14184 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14185 | char_size = 2; |
| 14186 | if (sizeof(wchar_t) == 2) |
| 14187 | share_wstr = 1; |
| 14188 | } |
| 14189 | else { |
| 14190 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14191 | char_size = 4; |
| 14192 | if (sizeof(wchar_t) == 4) |
| 14193 | share_wstr = 1; |
| 14194 | } |
| 14195 | |
| 14196 | /* Ensure we won't overflow the length. */ |
| 14197 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14198 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14199 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14200 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14201 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14202 | if (data == NULL) { |
| 14203 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14204 | goto onError; |
| 14205 | } |
| 14206 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14207 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14208 | if (share_utf8) { |
| 14209 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14210 | _PyUnicode_UTF8(self) = data; |
| 14211 | } |
| 14212 | if (share_wstr) { |
| 14213 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14214 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14215 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14216 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14217 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14218 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14219 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14220 | #ifdef Py_DEBUG |
| 14221 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14222 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14223 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14224 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14225 | |
| 14226 | onError: |
| 14227 | Py_DECREF(unicode); |
| 14228 | Py_DECREF(self); |
| 14229 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14230 | } |
| 14231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14232 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14233 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14234 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14235 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14236 | encoding defaults to the current default string encoding.\n\ |
| 14237 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14238 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14239 | static PyObject *unicode_iter(PyObject *seq); |
| 14240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14241 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14242 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14243 | "str", /* tp_name */ |
| 14244 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14245 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14246 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14247 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14248 | 0, /* tp_print */ |
| 14249 | 0, /* tp_getattr */ |
| 14250 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14251 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14252 | unicode_repr, /* tp_repr */ |
| 14253 | &unicode_as_number, /* tp_as_number */ |
| 14254 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14255 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14256 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14257 | 0, /* tp_call*/ |
| 14258 | (reprfunc) unicode_str, /* tp_str */ |
| 14259 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14260 | 0, /* tp_setattro */ |
| 14261 | 0, /* tp_as_buffer */ |
| 14262 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14263 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14264 | unicode_doc, /* tp_doc */ |
| 14265 | 0, /* tp_traverse */ |
| 14266 | 0, /* tp_clear */ |
| 14267 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14268 | 0, /* tp_weaklistoffset */ |
| 14269 | unicode_iter, /* tp_iter */ |
| 14270 | 0, /* tp_iternext */ |
| 14271 | unicode_methods, /* tp_methods */ |
| 14272 | 0, /* tp_members */ |
| 14273 | 0, /* tp_getset */ |
| 14274 | &PyBaseObject_Type, /* tp_base */ |
| 14275 | 0, /* tp_dict */ |
| 14276 | 0, /* tp_descr_get */ |
| 14277 | 0, /* tp_descr_set */ |
| 14278 | 0, /* tp_dictoffset */ |
| 14279 | 0, /* tp_init */ |
| 14280 | 0, /* tp_alloc */ |
| 14281 | unicode_new, /* tp_new */ |
| 14282 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14283 | }; |
| 14284 | |
| 14285 | /* Initialize the Unicode implementation */ |
| 14286 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14287 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14288 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14289 | int i; |
| 14290 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14291 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14292 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14293 | 0x000A, /* LINE FEED */ |
| 14294 | 0x000D, /* CARRIAGE RETURN */ |
| 14295 | 0x001C, /* FILE SEPARATOR */ |
| 14296 | 0x001D, /* GROUP SEPARATOR */ |
| 14297 | 0x001E, /* RECORD SEPARATOR */ |
| 14298 | 0x0085, /* NEXT LINE */ |
| 14299 | 0x2028, /* LINE SEPARATOR */ |
| 14300 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14301 | }; |
| 14302 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14303 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14304 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14305 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14306 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14307 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14308 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14309 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14310 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14311 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14312 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14313 | |
| 14314 | /* initialize the linebreak bloom filter */ |
| 14315 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14316 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14317 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14318 | |
| 14319 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14320 | |
| 14321 | #ifdef HAVE_MBCS |
| 14322 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14323 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14324 | PyErr_SetFromWindowsErr(0); |
| 14325 | return -1; |
| 14326 | } |
| 14327 | #endif |
| 14328 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14329 | } |
| 14330 | |
| 14331 | /* Finalize the Unicode implementation */ |
| 14332 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14333 | int |
| 14334 | PyUnicode_ClearFreeList(void) |
| 14335 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14336 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14337 | } |
| 14338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14339 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14340 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14341 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14342 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14343 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14344 | Py_XDECREF(unicode_empty); |
| 14345 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14346 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14347 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14348 | if (unicode_latin1[i]) { |
| 14349 | Py_DECREF(unicode_latin1[i]); |
| 14350 | unicode_latin1[i] = NULL; |
| 14351 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14352 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14353 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14354 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14355 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14356 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14357 | void |
| 14358 | PyUnicode_InternInPlace(PyObject **p) |
| 14359 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14360 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14361 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14362 | #ifdef Py_DEBUG |
| 14363 | assert(s != NULL); |
| 14364 | assert(_PyUnicode_CHECK(s)); |
| 14365 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14366 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14367 | return; |
| 14368 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14369 | /* If it's a subclass, we don't really know what putting |
| 14370 | it in the interned dict might do. */ |
| 14371 | if (!PyUnicode_CheckExact(s)) |
| 14372 | return; |
| 14373 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14374 | return; |
| 14375 | if (interned == NULL) { |
| 14376 | interned = PyDict_New(); |
| 14377 | if (interned == NULL) { |
| 14378 | PyErr_Clear(); /* Don't leave an exception */ |
| 14379 | return; |
| 14380 | } |
| 14381 | } |
| 14382 | /* It might be that the GetItem call fails even |
| 14383 | though the key is present in the dictionary, |
| 14384 | namely when this happens during a stack overflow. */ |
| 14385 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14386 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14387 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14388 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14389 | if (t) { |
| 14390 | Py_INCREF(t); |
| 14391 | Py_DECREF(*p); |
| 14392 | *p = t; |
| 14393 | return; |
| 14394 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14395 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14396 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14397 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14398 | PyErr_Clear(); |
| 14399 | PyThreadState_GET()->recursion_critical = 0; |
| 14400 | return; |
| 14401 | } |
| 14402 | PyThreadState_GET()->recursion_critical = 0; |
| 14403 | /* The two references in interned are not counted by refcnt. |
| 14404 | The deallocator will take care of this */ |
| 14405 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14406 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14407 | } |
| 14408 | |
| 14409 | void |
| 14410 | PyUnicode_InternImmortal(PyObject **p) |
| 14411 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14412 | PyUnicode_InternInPlace(p); |
| 14413 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14414 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14415 | Py_INCREF(*p); |
| 14416 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14417 | } |
| 14418 | |
| 14419 | PyObject * |
| 14420 | PyUnicode_InternFromString(const char *cp) |
| 14421 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14422 | PyObject *s = PyUnicode_FromString(cp); |
| 14423 | if (s == NULL) |
| 14424 | return NULL; |
| 14425 | PyUnicode_InternInPlace(&s); |
| 14426 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14427 | } |
| 14428 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14429 | void |
| 14430 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14431 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14432 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14433 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14434 | Py_ssize_t i, n; |
| 14435 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14436 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14437 | if (interned == NULL || !PyDict_Check(interned)) |
| 14438 | return; |
| 14439 | keys = PyDict_Keys(interned); |
| 14440 | if (keys == NULL || !PyList_Check(keys)) { |
| 14441 | PyErr_Clear(); |
| 14442 | return; |
| 14443 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14444 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14445 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14446 | detector, interned unicode strings are not forcibly deallocated; |
| 14447 | rather, we give them their stolen references back, and then clear |
| 14448 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14449 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14450 | n = PyList_GET_SIZE(keys); |
| 14451 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14452 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14453 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14454 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14455 | if (PyUnicode_READY(s) == -1) { |
| 14456 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14457 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14458 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14459 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14460 | case SSTATE_NOT_INTERNED: |
| 14461 | /* XXX Shouldn't happen */ |
| 14462 | break; |
| 14463 | case SSTATE_INTERNED_IMMORTAL: |
| 14464 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14465 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14466 | break; |
| 14467 | case SSTATE_INTERNED_MORTAL: |
| 14468 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14469 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14470 | break; |
| 14471 | default: |
| 14472 | Py_FatalError("Inconsistent interned string state."); |
| 14473 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14474 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14475 | } |
| 14476 | fprintf(stderr, "total size of all interned strings: " |
| 14477 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14478 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14479 | Py_DECREF(keys); |
| 14480 | PyDict_Clear(interned); |
| 14481 | Py_DECREF(interned); |
| 14482 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14483 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14484 | |
| 14485 | |
| 14486 | /********************* Unicode Iterator **************************/ |
| 14487 | |
| 14488 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14489 | PyObject_HEAD |
| 14490 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14491 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14492 | } unicodeiterobject; |
| 14493 | |
| 14494 | static void |
| 14495 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14496 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14497 | _PyObject_GC_UNTRACK(it); |
| 14498 | Py_XDECREF(it->it_seq); |
| 14499 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14500 | } |
| 14501 | |
| 14502 | static int |
| 14503 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14504 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14505 | Py_VISIT(it->it_seq); |
| 14506 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14507 | } |
| 14508 | |
| 14509 | static PyObject * |
| 14510 | unicodeiter_next(unicodeiterobject *it) |
| 14511 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14512 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14513 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14514 | assert(it != NULL); |
| 14515 | seq = it->it_seq; |
| 14516 | if (seq == NULL) |
| 14517 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14518 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14519 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14520 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14521 | int kind = PyUnicode_KIND(seq); |
| 14522 | void *data = PyUnicode_DATA(seq); |
| 14523 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14524 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14525 | if (item != NULL) |
| 14526 | ++it->it_index; |
| 14527 | return item; |
| 14528 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14529 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14530 | Py_DECREF(seq); |
| 14531 | it->it_seq = NULL; |
| 14532 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14533 | } |
| 14534 | |
| 14535 | static PyObject * |
| 14536 | unicodeiter_len(unicodeiterobject *it) |
| 14537 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14538 | Py_ssize_t len = 0; |
| 14539 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14540 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14541 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14542 | } |
| 14543 | |
| 14544 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14545 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14546 | static PyObject * |
| 14547 | unicodeiter_reduce(unicodeiterobject *it) |
| 14548 | { |
| 14549 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14550 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14551 | it->it_seq, it->it_index); |
| 14552 | } else { |
| 14553 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14554 | if (u == NULL) |
| 14555 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14556 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14557 | } |
| 14558 | } |
| 14559 | |
| 14560 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14561 | |
| 14562 | static PyObject * |
| 14563 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14564 | { |
| 14565 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14566 | if (index == -1 && PyErr_Occurred()) |
| 14567 | return NULL; |
| 14568 | if (index < 0) |
| 14569 | index = 0; |
| 14570 | it->it_index = index; |
| 14571 | Py_RETURN_NONE; |
| 14572 | } |
| 14573 | |
| 14574 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14575 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14576 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14577 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14578 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14579 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14580 | reduce_doc}, |
| 14581 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14582 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14583 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14584 | }; |
| 14585 | |
| 14586 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14587 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14588 | "str_iterator", /* tp_name */ |
| 14589 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14590 | 0, /* tp_itemsize */ |
| 14591 | /* methods */ |
| 14592 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14593 | 0, /* tp_print */ |
| 14594 | 0, /* tp_getattr */ |
| 14595 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14596 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14597 | 0, /* tp_repr */ |
| 14598 | 0, /* tp_as_number */ |
| 14599 | 0, /* tp_as_sequence */ |
| 14600 | 0, /* tp_as_mapping */ |
| 14601 | 0, /* tp_hash */ |
| 14602 | 0, /* tp_call */ |
| 14603 | 0, /* tp_str */ |
| 14604 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14605 | 0, /* tp_setattro */ |
| 14606 | 0, /* tp_as_buffer */ |
| 14607 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14608 | 0, /* tp_doc */ |
| 14609 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14610 | 0, /* tp_clear */ |
| 14611 | 0, /* tp_richcompare */ |
| 14612 | 0, /* tp_weaklistoffset */ |
| 14613 | PyObject_SelfIter, /* tp_iter */ |
| 14614 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14615 | unicodeiter_methods, /* tp_methods */ |
| 14616 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14617 | }; |
| 14618 | |
| 14619 | static PyObject * |
| 14620 | unicode_iter(PyObject *seq) |
| 14621 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14622 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14623 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14624 | if (!PyUnicode_Check(seq)) { |
| 14625 | PyErr_BadInternalCall(); |
| 14626 | return NULL; |
| 14627 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14628 | if (PyUnicode_READY(seq) == -1) |
| 14629 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14630 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14631 | if (it == NULL) |
| 14632 | return NULL; |
| 14633 | it->it_index = 0; |
| 14634 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14635 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14636 | _PyObject_GC_TRACK(it); |
| 14637 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14638 | } |
| 14639 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14640 | |
| 14641 | size_t |
| 14642 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14643 | { |
| 14644 | int res = 0; |
| 14645 | while(*u++) |
| 14646 | res++; |
| 14647 | return res; |
| 14648 | } |
| 14649 | |
| 14650 | Py_UNICODE* |
| 14651 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14652 | { |
| 14653 | Py_UNICODE *u = s1; |
| 14654 | while ((*u++ = *s2++)); |
| 14655 | return s1; |
| 14656 | } |
| 14657 | |
| 14658 | Py_UNICODE* |
| 14659 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14660 | { |
| 14661 | Py_UNICODE *u = s1; |
| 14662 | while ((*u++ = *s2++)) |
| 14663 | if (n-- == 0) |
| 14664 | break; |
| 14665 | return s1; |
| 14666 | } |
| 14667 | |
| 14668 | Py_UNICODE* |
| 14669 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14670 | { |
| 14671 | Py_UNICODE *u1 = s1; |
| 14672 | u1 += Py_UNICODE_strlen(u1); |
| 14673 | Py_UNICODE_strcpy(u1, s2); |
| 14674 | return s1; |
| 14675 | } |
| 14676 | |
| 14677 | int |
| 14678 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14679 | { |
| 14680 | while (*s1 && *s2 && *s1 == *s2) |
| 14681 | s1++, s2++; |
| 14682 | if (*s1 && *s2) |
| 14683 | return (*s1 < *s2) ? -1 : +1; |
| 14684 | if (*s1) |
| 14685 | return 1; |
| 14686 | if (*s2) |
| 14687 | return -1; |
| 14688 | return 0; |
| 14689 | } |
| 14690 | |
| 14691 | int |
| 14692 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14693 | { |
| 14694 | register Py_UNICODE u1, u2; |
| 14695 | for (; n != 0; n--) { |
| 14696 | u1 = *s1; |
| 14697 | u2 = *s2; |
| 14698 | if (u1 != u2) |
| 14699 | return (u1 < u2) ? -1 : +1; |
| 14700 | if (u1 == '\0') |
| 14701 | return 0; |
| 14702 | s1++; |
| 14703 | s2++; |
| 14704 | } |
| 14705 | return 0; |
| 14706 | } |
| 14707 | |
| 14708 | Py_UNICODE* |
| 14709 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14710 | { |
| 14711 | const Py_UNICODE *p; |
| 14712 | for (p = s; *p; p++) |
| 14713 | if (*p == c) |
| 14714 | return (Py_UNICODE*)p; |
| 14715 | return NULL; |
| 14716 | } |
| 14717 | |
| 14718 | Py_UNICODE* |
| 14719 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14720 | { |
| 14721 | const Py_UNICODE *p; |
| 14722 | p = s + Py_UNICODE_strlen(s); |
| 14723 | while (p != s) { |
| 14724 | p--; |
| 14725 | if (*p == c) |
| 14726 | return (Py_UNICODE*)p; |
| 14727 | } |
| 14728 | return NULL; |
| 14729 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14730 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14731 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14732 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14733 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14734 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14735 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14737 | if (!PyUnicode_Check(unicode)) { |
| 14738 | PyErr_BadArgument(); |
| 14739 | return NULL; |
| 14740 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14741 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14742 | if (u == NULL) |
| 14743 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14744 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14745 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14746 | PyErr_NoMemory(); |
| 14747 | return NULL; |
| 14748 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14749 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14750 | size *= sizeof(Py_UNICODE); |
| 14751 | copy = PyMem_Malloc(size); |
| 14752 | if (copy == NULL) { |
| 14753 | PyErr_NoMemory(); |
| 14754 | return NULL; |
| 14755 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14756 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14757 | return copy; |
| 14758 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14759 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14760 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14761 | to the string.Formatter class implemented in Python. */ |
| 14762 | |
| 14763 | static PyMethodDef _string_methods[] = { |
| 14764 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14765 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14766 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14767 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14768 | {NULL, NULL} |
| 14769 | }; |
| 14770 | |
| 14771 | static struct PyModuleDef _string_module = { |
| 14772 | PyModuleDef_HEAD_INIT, |
| 14773 | "_string", |
| 14774 | PyDoc_STR("string helper module"), |
| 14775 | 0, |
| 14776 | _string_methods, |
| 14777 | NULL, |
| 14778 | NULL, |
| 14779 | NULL, |
| 14780 | NULL |
| 14781 | }; |
| 14782 | |
| 14783 | PyMODINIT_FUNC |
| 14784 | PyInit__string(void) |
| 14785 | { |
| 14786 | return PyModule_Create(&_string_module); |
| 14787 | } |
| 14788 | |
| 14789 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14790 | #ifdef __cplusplus |
| 14791 | } |
| 14792 | #endif |