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; |
| 378 | void *data = PyUnicode_DATA(ascii); |
| 379 | for (i=0; i < ascii->length; i++) |
| 380 | { |
| 381 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 382 | if (ch > maxchar) |
| 383 | maxchar = ch; |
| 384 | } |
| 385 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 386 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 387 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 388 | assert(maxchar <= 255); |
| 389 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 390 | else |
| 391 | assert(maxchar < 128); |
| 392 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 393 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 394 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 395 | assert(maxchar <= 0xFFFF); |
| 396 | } |
| 397 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 399 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 400 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 402 | return 1; |
| 403 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 404 | #endif |
| 405 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 406 | static PyObject* |
| 407 | unicode_result_wchar(PyObject *unicode) |
| 408 | { |
| 409 | #ifndef Py_DEBUG |
| 410 | Py_ssize_t len; |
| 411 | |
| 412 | assert(Py_REFCNT(unicode) == 1); |
| 413 | |
| 414 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 415 | if (len == 0) { |
| 416 | Py_INCREF(unicode_empty); |
| 417 | Py_DECREF(unicode); |
| 418 | return unicode_empty; |
| 419 | } |
| 420 | |
| 421 | if (len == 1) { |
| 422 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 423 | if (ch < 256) { |
| 424 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 425 | Py_DECREF(unicode); |
| 426 | return latin1_char; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if (_PyUnicode_Ready(unicode) < 0) { |
| 431 | Py_XDECREF(unicode); |
| 432 | return NULL; |
| 433 | } |
| 434 | #else |
| 435 | /* don't make the result ready in debug mode to ensure that the caller |
| 436 | makes the string ready before using it */ |
| 437 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 438 | #endif |
| 439 | return unicode; |
| 440 | } |
| 441 | |
| 442 | static PyObject* |
| 443 | unicode_result_ready(PyObject *unicode) |
| 444 | { |
| 445 | Py_ssize_t length; |
| 446 | |
| 447 | length = PyUnicode_GET_LENGTH(unicode); |
| 448 | if (length == 0) { |
| 449 | if (unicode != unicode_empty) { |
| 450 | Py_INCREF(unicode_empty); |
| 451 | Py_DECREF(unicode); |
| 452 | } |
| 453 | return unicode_empty; |
| 454 | } |
| 455 | |
| 456 | if (length == 1) { |
| 457 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 458 | if (ch < 256) { |
| 459 | PyObject *latin1_char = unicode_latin1[ch]; |
| 460 | if (latin1_char != NULL) { |
| 461 | if (unicode != latin1_char) { |
| 462 | Py_INCREF(latin1_char); |
| 463 | Py_DECREF(unicode); |
| 464 | } |
| 465 | return latin1_char; |
| 466 | } |
| 467 | else { |
| 468 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 469 | Py_INCREF(unicode); |
| 470 | unicode_latin1[ch] = unicode; |
| 471 | return unicode; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 477 | return unicode; |
| 478 | } |
| 479 | |
| 480 | static PyObject* |
| 481 | unicode_result(PyObject *unicode) |
| 482 | { |
| 483 | assert(_PyUnicode_CHECK(unicode)); |
| 484 | if (PyUnicode_IS_READY(unicode)) |
| 485 | return unicode_result_ready(unicode); |
| 486 | else |
| 487 | return unicode_result_wchar(unicode); |
| 488 | } |
| 489 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 490 | static PyObject* |
| 491 | unicode_result_unchanged(PyObject *unicode) |
| 492 | { |
| 493 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 494 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 495 | return NULL; |
| 496 | Py_INCREF(unicode); |
| 497 | return unicode; |
| 498 | } |
| 499 | else |
| 500 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 501 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 504 | #ifdef HAVE_MBCS |
| 505 | static OSVERSIONINFOEX winver; |
| 506 | #endif |
| 507 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 508 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 509 | |
| 510 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 511 | to keep things simple, we use a single bitmask, using the least 5 |
| 512 | bits from each unicode characters as the bit index. */ |
| 513 | |
| 514 | /* the linebreak mask is set up by Unicode_Init below */ |
| 515 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 516 | #if LONG_BIT >= 128 |
| 517 | #define BLOOM_WIDTH 128 |
| 518 | #elif LONG_BIT >= 64 |
| 519 | #define BLOOM_WIDTH 64 |
| 520 | #elif LONG_BIT >= 32 |
| 521 | #define BLOOM_WIDTH 32 |
| 522 | #else |
| 523 | #error "LONG_BIT is smaller than 32" |
| 524 | #endif |
| 525 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 526 | #define BLOOM_MASK unsigned long |
| 527 | |
| 528 | static BLOOM_MASK bloom_linebreak; |
| 529 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 530 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 531 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 533 | #define BLOOM_LINEBREAK(ch) \ |
| 534 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 535 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 536 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 537 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 538 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | { |
| 540 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 541 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 542 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | Py_ssize_t i; |
| 544 | |
| 545 | mask = 0; |
| 546 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 547 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 548 | |
| 549 | return mask; |
| 550 | } |
| 551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 552 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 553 | (BLOOM(mask, chr) \ |
| 554 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 556 | /* Compilation of templated routines */ |
| 557 | |
| 558 | #include "stringlib/asciilib.h" |
| 559 | #include "stringlib/fastsearch.h" |
| 560 | #include "stringlib/partition.h" |
| 561 | #include "stringlib/split.h" |
| 562 | #include "stringlib/count.h" |
| 563 | #include "stringlib/find.h" |
| 564 | #include "stringlib/find_max_char.h" |
| 565 | #include "stringlib/localeutil.h" |
| 566 | #include "stringlib/undef.h" |
| 567 | |
| 568 | #include "stringlib/ucs1lib.h" |
| 569 | #include "stringlib/fastsearch.h" |
| 570 | #include "stringlib/partition.h" |
| 571 | #include "stringlib/split.h" |
| 572 | #include "stringlib/count.h" |
| 573 | #include "stringlib/find.h" |
| 574 | #include "stringlib/find_max_char.h" |
| 575 | #include "stringlib/localeutil.h" |
| 576 | #include "stringlib/undef.h" |
| 577 | |
| 578 | #include "stringlib/ucs2lib.h" |
| 579 | #include "stringlib/fastsearch.h" |
| 580 | #include "stringlib/partition.h" |
| 581 | #include "stringlib/split.h" |
| 582 | #include "stringlib/count.h" |
| 583 | #include "stringlib/find.h" |
| 584 | #include "stringlib/find_max_char.h" |
| 585 | #include "stringlib/localeutil.h" |
| 586 | #include "stringlib/undef.h" |
| 587 | |
| 588 | #include "stringlib/ucs4lib.h" |
| 589 | #include "stringlib/fastsearch.h" |
| 590 | #include "stringlib/partition.h" |
| 591 | #include "stringlib/split.h" |
| 592 | #include "stringlib/count.h" |
| 593 | #include "stringlib/find.h" |
| 594 | #include "stringlib/find_max_char.h" |
| 595 | #include "stringlib/localeutil.h" |
| 596 | #include "stringlib/undef.h" |
| 597 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 598 | #include "stringlib/unicodedefs.h" |
| 599 | #include "stringlib/fastsearch.h" |
| 600 | #include "stringlib/count.h" |
| 601 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 602 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 604 | /* --- Unicode Object ----------------------------------------------------- */ |
| 605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 606 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 607 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 608 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 609 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 610 | Py_ssize_t size, Py_UCS4 ch, |
| 611 | int direction) |
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 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 614 | |
| 615 | switch (kind) { |
| 616 | case PyUnicode_1BYTE_KIND: |
| 617 | { |
| 618 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 619 | if (ch1 == ch) |
| 620 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 621 | else |
| 622 | return -1; |
| 623 | } |
| 624 | case PyUnicode_2BYTE_KIND: |
| 625 | { |
| 626 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 627 | if (ch2 == ch) |
| 628 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 629 | else |
| 630 | return -1; |
| 631 | } |
| 632 | case PyUnicode_4BYTE_KIND: |
| 633 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 634 | default: |
| 635 | assert(0); |
| 636 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 638 | } |
| 639 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 640 | static PyObject* |
| 641 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 642 | { |
| 643 | Py_ssize_t char_size; |
| 644 | Py_ssize_t struct_size; |
| 645 | Py_ssize_t new_size; |
| 646 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 647 | PyObject *new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 648 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 649 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 650 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 651 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 652 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 653 | struct_size = sizeof(PyASCIIObject); |
| 654 | else |
| 655 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 656 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 658 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 659 | PyErr_NoMemory(); |
| 660 | return NULL; |
| 661 | } |
| 662 | new_size = (struct_size + (length + 1) * char_size); |
| 663 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 664 | _Py_DEC_REFTOTAL; |
| 665 | _Py_ForgetReference(unicode); |
| 666 | |
| 667 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 668 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 669 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 670 | PyErr_NoMemory(); |
| 671 | return NULL; |
| 672 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 673 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 674 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 675 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 676 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 677 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 678 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 679 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 680 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 681 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 682 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 683 | length, 0); |
| 684 | return unicode; |
| 685 | } |
| 686 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 687 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 688 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 689 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 690 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 691 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 692 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 693 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 694 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 695 | if (PyUnicode_IS_READY(unicode)) { |
| 696 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 697 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 698 | void *data; |
| 699 | |
| 700 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 701 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 702 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 703 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 704 | |
| 705 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 706 | PyErr_NoMemory(); |
| 707 | return -1; |
| 708 | } |
| 709 | new_size = (length + 1) * char_size; |
| 710 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 711 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 712 | { |
| 713 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 714 | _PyUnicode_UTF8(unicode) = NULL; |
| 715 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 716 | } |
| 717 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 718 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 719 | if (data == NULL) { |
| 720 | PyErr_NoMemory(); |
| 721 | return -1; |
| 722 | } |
| 723 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 724 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 725 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 726 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 727 | } |
| 728 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 729 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 730 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 731 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 | _PyUnicode_LENGTH(unicode) = length; |
| 733 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 734 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 735 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 737 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 738 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 739 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 740 | |
| 741 | /* check for integer overflow */ |
| 742 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 743 | PyErr_NoMemory(); |
| 744 | return -1; |
| 745 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 746 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 747 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 748 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 749 | if (!wstr) { |
| 750 | PyErr_NoMemory(); |
| 751 | return -1; |
| 752 | } |
| 753 | _PyUnicode_WSTR(unicode) = wstr; |
| 754 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 755 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 756 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 757 | return 0; |
| 758 | } |
| 759 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 760 | static PyObject* |
| 761 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 762 | { |
| 763 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 764 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 765 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 766 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 767 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 768 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 769 | |
| 770 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 771 | if (copy == NULL) |
| 772 | return NULL; |
| 773 | |
| 774 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 775 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 776 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 777 | } |
| 778 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 779 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 780 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 781 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 782 | if (w == NULL) |
| 783 | return NULL; |
| 784 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 785 | copy_length = Py_MIN(copy_length, length); |
| 786 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 787 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 788 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 792 | /* 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] | 793 | Ux0000 terminated; some code (e.g. new_identifier) |
| 794 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 795 | |
| 796 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 797 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 798 | |
| 799 | */ |
| 800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 801 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 802 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 803 | #endif |
| 804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 805 | static PyUnicodeObject * |
| 806 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 807 | { |
| 808 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 809 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 810 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 811 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 812 | if (length == 0 && unicode_empty != NULL) { |
| 813 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 814 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 817 | /* Ensure we won't overflow the size. */ |
| 818 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 819 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 820 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 821 | if (length < 0) { |
| 822 | PyErr_SetString(PyExc_SystemError, |
| 823 | "Negative size passed to _PyUnicode_New"); |
| 824 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 827 | #ifdef Py_DEBUG |
| 828 | ++unicode_old_new_calls; |
| 829 | #endif |
| 830 | |
| 831 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 832 | if (unicode == NULL) |
| 833 | return NULL; |
| 834 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 835 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 836 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 837 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 838 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 839 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 840 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 841 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 842 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 843 | * the caller fails before initializing str -- unicode_resize() |
| 844 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 845 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 846 | * We don't want unicode_resize to read uninitialized memory in |
| 847 | * that case. |
| 848 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 849 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 850 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 851 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 852 | _PyUnicode_HASH(unicode) = -1; |
| 853 | _PyUnicode_STATE(unicode).interned = 0; |
| 854 | _PyUnicode_STATE(unicode).kind = 0; |
| 855 | _PyUnicode_STATE(unicode).compact = 0; |
| 856 | _PyUnicode_STATE(unicode).ready = 0; |
| 857 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 858 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 859 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 860 | _PyUnicode_UTF8(unicode) = NULL; |
| 861 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 862 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 863 | return unicode; |
| 864 | } |
| 865 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 866 | static const char* |
| 867 | unicode_kind_name(PyObject *unicode) |
| 868 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 869 | /* don't check consistency: unicode_kind_name() is called from |
| 870 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 871 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 872 | { |
| 873 | if (!PyUnicode_IS_READY(unicode)) |
| 874 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 875 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 876 | { |
| 877 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 878 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 879 | return "legacy ascii"; |
| 880 | else |
| 881 | return "legacy latin1"; |
| 882 | case PyUnicode_2BYTE_KIND: |
| 883 | return "legacy UCS2"; |
| 884 | case PyUnicode_4BYTE_KIND: |
| 885 | return "legacy UCS4"; |
| 886 | default: |
| 887 | return "<legacy invalid kind>"; |
| 888 | } |
| 889 | } |
| 890 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 891 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 892 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 893 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 894 | return "ascii"; |
| 895 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 896 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 897 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 898 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 899 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 900 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 901 | default: |
| 902 | return "<invalid compact kind>"; |
| 903 | } |
| 904 | } |
| 905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 906 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 907 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 908 | |
| 909 | /* Functions wrapping macros for use in debugger */ |
| 910 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 911 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | void *_PyUnicode_compact_data(void *unicode) { |
| 915 | return _PyUnicode_COMPACT_DATA(unicode); |
| 916 | } |
| 917 | void *_PyUnicode_data(void *unicode){ |
| 918 | printf("obj %p\n", unicode); |
| 919 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 920 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 921 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 922 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 923 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 924 | return PyUnicode_DATA(unicode); |
| 925 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 926 | |
| 927 | void |
| 928 | _PyUnicode_Dump(PyObject *op) |
| 929 | { |
| 930 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 931 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 932 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 933 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 934 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 935 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 936 | { |
| 937 | if (ascii->state.ascii) |
| 938 | data = (ascii + 1); |
| 939 | else |
| 940 | data = (compact + 1); |
| 941 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 942 | else |
| 943 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 944 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 945 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 946 | if (ascii->wstr == data) |
| 947 | printf("shared "); |
| 948 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 949 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 950 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 951 | printf(" (%zu), ", compact->wstr_length); |
| 952 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 953 | printf("shared "); |
| 954 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 955 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 956 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 957 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 958 | #endif |
| 959 | |
| 960 | PyObject * |
| 961 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 962 | { |
| 963 | PyObject *obj; |
| 964 | PyCompactUnicodeObject *unicode; |
| 965 | void *data; |
| 966 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 967 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 968 | Py_ssize_t char_size; |
| 969 | Py_ssize_t struct_size; |
| 970 | |
| 971 | /* Optimization for empty strings */ |
| 972 | if (size == 0 && unicode_empty != NULL) { |
| 973 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 974 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | #ifdef Py_DEBUG |
| 978 | ++unicode_new_new_calls; |
| 979 | #endif |
| 980 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 981 | is_ascii = 0; |
| 982 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 983 | struct_size = sizeof(PyCompactUnicodeObject); |
| 984 | if (maxchar < 128) { |
| 985 | kind_state = PyUnicode_1BYTE_KIND; |
| 986 | char_size = 1; |
| 987 | is_ascii = 1; |
| 988 | struct_size = sizeof(PyASCIIObject); |
| 989 | } |
| 990 | else if (maxchar < 256) { |
| 991 | kind_state = PyUnicode_1BYTE_KIND; |
| 992 | char_size = 1; |
| 993 | } |
| 994 | else if (maxchar < 65536) { |
| 995 | kind_state = PyUnicode_2BYTE_KIND; |
| 996 | char_size = 2; |
| 997 | if (sizeof(wchar_t) == 2) |
| 998 | is_sharing = 1; |
| 999 | } |
| 1000 | else { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1001 | assert(maxchar <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1002 | kind_state = PyUnicode_4BYTE_KIND; |
| 1003 | char_size = 4; |
| 1004 | if (sizeof(wchar_t) == 4) |
| 1005 | is_sharing = 1; |
| 1006 | } |
| 1007 | |
| 1008 | /* Ensure we won't overflow the size. */ |
| 1009 | if (size < 0) { |
| 1010 | PyErr_SetString(PyExc_SystemError, |
| 1011 | "Negative size passed to PyUnicode_New"); |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1015 | return PyErr_NoMemory(); |
| 1016 | |
| 1017 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1018 | * PyObject_New() so we are able to allocate space for the object and |
| 1019 | * it's data buffer. |
| 1020 | */ |
| 1021 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1022 | if (obj == NULL) |
| 1023 | return PyErr_NoMemory(); |
| 1024 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1025 | if (obj == NULL) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | unicode = (PyCompactUnicodeObject *)obj; |
| 1029 | if (is_ascii) |
| 1030 | data = ((PyASCIIObject*)obj) + 1; |
| 1031 | else |
| 1032 | data = unicode + 1; |
| 1033 | _PyUnicode_LENGTH(unicode) = size; |
| 1034 | _PyUnicode_HASH(unicode) = -1; |
| 1035 | _PyUnicode_STATE(unicode).interned = 0; |
| 1036 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 1037 | _PyUnicode_STATE(unicode).compact = 1; |
| 1038 | _PyUnicode_STATE(unicode).ready = 1; |
| 1039 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1040 | if (is_ascii) { |
| 1041 | ((char*)data)[size] = 0; |
| 1042 | _PyUnicode_WSTR(unicode) = NULL; |
| 1043 | } |
| 1044 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 1045 | ((char*)data)[size] = 0; |
| 1046 | _PyUnicode_WSTR(unicode) = NULL; |
| 1047 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1049 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | } |
| 1051 | else { |
| 1052 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1053 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 1055 | ((Py_UCS2*)data)[size] = 0; |
| 1056 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 1057 | ((Py_UCS4*)data)[size] = 0; |
| 1058 | if (is_sharing) { |
| 1059 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1060 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1061 | } |
| 1062 | else { |
| 1063 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1064 | _PyUnicode_WSTR(unicode) = NULL; |
| 1065 | } |
| 1066 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1067 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | return obj; |
| 1069 | } |
| 1070 | |
| 1071 | #if SIZEOF_WCHAR_T == 2 |
| 1072 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1073 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1074 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1075 | |
| 1076 | This function assumes that unicode can hold one more code point than wstr |
| 1077 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1078 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1079 | 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] | 1080 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1081 | { |
| 1082 | const wchar_t *iter; |
| 1083 | Py_UCS4 *ucs4_out; |
| 1084 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1085 | assert(unicode != NULL); |
| 1086 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1087 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1088 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1089 | |
| 1090 | for (iter = begin; iter < end; ) { |
| 1091 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1092 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1093 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1094 | && (iter+1) < end |
| 1095 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1096 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1097 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1098 | iter += 2; |
| 1099 | } |
| 1100 | else { |
| 1101 | *ucs4_out++ = *iter; |
| 1102 | iter++; |
| 1103 | } |
| 1104 | } |
| 1105 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1106 | _PyUnicode_GET_LENGTH(unicode))); |
| 1107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1108 | } |
| 1109 | #endif |
| 1110 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1111 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1112 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1113 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1114 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1115 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1116 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1117 | return -1; |
| 1118 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1119 | return 0; |
| 1120 | } |
| 1121 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1122 | static int |
| 1123 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1124 | PyObject *from, Py_ssize_t from_start, |
| 1125 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1127 | unsigned int from_kind, to_kind; |
| 1128 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1129 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1130 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1131 | assert(PyUnicode_Check(from)); |
| 1132 | assert(PyUnicode_Check(to)); |
| 1133 | assert(PyUnicode_IS_READY(from)); |
| 1134 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1135 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1136 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1137 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1138 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1139 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1140 | if (how_many == 0) |
| 1141 | return 0; |
| 1142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1144 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1145 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1146 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1148 | #ifdef Py_DEBUG |
| 1149 | if (!check_maxchar |
| 1150 | && (from_kind > to_kind |
| 1151 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1152 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1153 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1154 | Py_UCS4 ch; |
| 1155 | Py_ssize_t i; |
| 1156 | for (i=0; i < how_many; i++) { |
| 1157 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1158 | assert(ch <= to_maxchar); |
| 1159 | } |
| 1160 | } |
| 1161 | #endif |
| 1162 | fast = (from_kind == to_kind); |
| 1163 | if (check_maxchar |
| 1164 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1165 | { |
| 1166 | /* deny latin1 => ascii */ |
| 1167 | fast = 0; |
| 1168 | } |
| 1169 | |
| 1170 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1171 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1172 | (char*)from_data + from_kind * from_start, |
| 1173 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1174 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1175 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1176 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1177 | { |
| 1178 | _PyUnicode_CONVERT_BYTES( |
| 1179 | Py_UCS1, Py_UCS2, |
| 1180 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1181 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1182 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1183 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1184 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1185 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1186 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1187 | { |
| 1188 | _PyUnicode_CONVERT_BYTES( |
| 1189 | Py_UCS1, Py_UCS4, |
| 1190 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1191 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1192 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1193 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1194 | } |
| 1195 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1196 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1197 | { |
| 1198 | _PyUnicode_CONVERT_BYTES( |
| 1199 | Py_UCS2, Py_UCS4, |
| 1200 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1201 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1202 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1203 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1204 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1205 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1206 | /* check if max_char(from substring) <= max_char(to) */ |
| 1207 | if (from_kind > to_kind |
| 1208 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1209 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1210 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1211 | /* slow path to check for character overflow */ |
| 1212 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1213 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1214 | Py_ssize_t i; |
| 1215 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1216 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1217 | for (i=0; i < how_many; i++) { |
| 1218 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1219 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1220 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1221 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1222 | #else |
| 1223 | if (!check_maxchar) { |
| 1224 | for (i=0; i < how_many; i++) { |
| 1225 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1226 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1227 | } |
| 1228 | } |
| 1229 | else { |
| 1230 | for (i=0; i < how_many; i++) { |
| 1231 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1232 | if (ch > to_maxchar) |
| 1233 | return 1; |
| 1234 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1235 | } |
| 1236 | } |
| 1237 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1238 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1239 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1240 | assert(0 && "inconsistent state"); |
| 1241 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1242 | } |
| 1243 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static void |
| 1248 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1249 | PyObject *from, Py_ssize_t from_start, |
| 1250 | Py_ssize_t how_many) |
| 1251 | { |
| 1252 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1253 | } |
| 1254 | |
| 1255 | Py_ssize_t |
| 1256 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1257 | PyObject *from, Py_ssize_t from_start, |
| 1258 | Py_ssize_t how_many) |
| 1259 | { |
| 1260 | int err; |
| 1261 | |
| 1262 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1263 | PyErr_BadInternalCall(); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1267 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1268 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1269 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1270 | return -1; |
| 1271 | |
| 1272 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1273 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1274 | PyErr_Format(PyExc_SystemError, |
| 1275 | "Cannot write %zi characters at %zi " |
| 1276 | "in a string of %zi characters", |
| 1277 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1278 | return -1; |
| 1279 | } |
| 1280 | |
| 1281 | if (how_many == 0) |
| 1282 | return 0; |
| 1283 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1284 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1285 | return -1; |
| 1286 | |
| 1287 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1288 | if (err) { |
| 1289 | PyErr_Format(PyExc_SystemError, |
| 1290 | "Cannot copy %s characters " |
| 1291 | "into a string of %s characters", |
| 1292 | unicode_kind_name(from), |
| 1293 | unicode_kind_name(to)); |
| 1294 | return -1; |
| 1295 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1296 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1299 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1300 | correct string length can be computed before converting a string to UCS4. |
| 1301 | This function counts single surrogates as a character and not as a pair. |
| 1302 | |
| 1303 | Return 0 on success, or -1 on error. */ |
| 1304 | static int |
| 1305 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1306 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 | { |
| 1308 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1309 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1310 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1311 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1312 | *num_surrogates = 0; |
| 1313 | *maxchar = 0; |
| 1314 | |
| 1315 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1317 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1318 | && (iter+1) < end |
| 1319 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1321 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1323 | iter += 2; |
| 1324 | } |
| 1325 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1326 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1327 | { |
| 1328 | ch = *iter; |
| 1329 | iter++; |
| 1330 | } |
| 1331 | if (ch > *maxchar) { |
| 1332 | *maxchar = ch; |
| 1333 | if (*maxchar > MAX_UNICODE) { |
| 1334 | PyErr_Format(PyExc_ValueError, |
| 1335 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1336 | ch); |
| 1337 | return -1; |
| 1338 | } |
| 1339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | } |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1345 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | #endif |
| 1347 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1348 | int |
| 1349 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | { |
| 1351 | wchar_t *end; |
| 1352 | Py_UCS4 maxchar = 0; |
| 1353 | Py_ssize_t num_surrogates; |
| 1354 | #if SIZEOF_WCHAR_T == 2 |
| 1355 | Py_ssize_t length_wo_surrogates; |
| 1356 | #endif |
| 1357 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1358 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1359 | strings were created using _PyObject_New() and where no canonical |
| 1360 | representation (the str field) has been set yet aka strings |
| 1361 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1362 | assert(_PyUnicode_CHECK(unicode)); |
| 1363 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1364 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1365 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1366 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1367 | /* Actually, it should neither be interned nor be anything else: */ |
| 1368 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1369 | |
| 1370 | #ifdef Py_DEBUG |
| 1371 | ++unicode_ready_calls; |
| 1372 | #endif |
| 1373 | |
| 1374 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1375 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1376 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1378 | |
| 1379 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1380 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1381 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1382 | PyErr_NoMemory(); |
| 1383 | return -1; |
| 1384 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1385 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1386 | _PyUnicode_WSTR(unicode), end, |
| 1387 | PyUnicode_1BYTE_DATA(unicode)); |
| 1388 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1389 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1390 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1391 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1392 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1393 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1394 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | } |
| 1396 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1397 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1398 | _PyUnicode_UTF8(unicode) = NULL; |
| 1399 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | } |
| 1401 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1402 | _PyUnicode_WSTR(unicode) = NULL; |
| 1403 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1404 | } |
| 1405 | /* In this case we might have to convert down from 4-byte native |
| 1406 | wchar_t to 2-byte unicode. */ |
| 1407 | else if (maxchar < 65536) { |
| 1408 | assert(num_surrogates == 0 && |
| 1409 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1410 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1411 | #if SIZEOF_WCHAR_T == 2 |
| 1412 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1413 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1414 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1415 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1416 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1417 | _PyUnicode_UTF8(unicode) = NULL; |
| 1418 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1419 | #else |
| 1420 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1421 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1422 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1423 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1424 | PyErr_NoMemory(); |
| 1425 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1426 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1427 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1428 | _PyUnicode_WSTR(unicode), end, |
| 1429 | PyUnicode_2BYTE_DATA(unicode)); |
| 1430 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1431 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1432 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1433 | _PyUnicode_UTF8(unicode) = NULL; |
| 1434 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1435 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1436 | _PyUnicode_WSTR(unicode) = NULL; |
| 1437 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1438 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | } |
| 1440 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1441 | else { |
| 1442 | #if SIZEOF_WCHAR_T == 2 |
| 1443 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1444 | new normalized 4-byte version. */ |
| 1445 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1446 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1447 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1448 | PyErr_NoMemory(); |
| 1449 | return -1; |
| 1450 | } |
| 1451 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1452 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1453 | _PyUnicode_UTF8(unicode) = NULL; |
| 1454 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1455 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1456 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1457 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1459 | _PyUnicode_WSTR(unicode) = NULL; |
| 1460 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1461 | #else |
| 1462 | assert(num_surrogates == 0); |
| 1463 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1464 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1466 | _PyUnicode_UTF8(unicode) = NULL; |
| 1467 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1469 | #endif |
| 1470 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1471 | } |
| 1472 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1473 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1474 | return 0; |
| 1475 | } |
| 1476 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1477 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1478 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1479 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1480 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1481 | case SSTATE_NOT_INTERNED: |
| 1482 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1484 | case SSTATE_INTERNED_MORTAL: |
| 1485 | /* revive dead object temporarily for DelItem */ |
| 1486 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1487 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1488 | Py_FatalError( |
| 1489 | "deletion of interned string failed"); |
| 1490 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1491 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1492 | case SSTATE_INTERNED_IMMORTAL: |
| 1493 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1495 | default: |
| 1496 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1499 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1500 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1501 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1502 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1503 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1504 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1505 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1506 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1509 | #ifdef Py_DEBUG |
| 1510 | static int |
| 1511 | unicode_is_singleton(PyObject *unicode) |
| 1512 | { |
| 1513 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1514 | if (unicode == unicode_empty) |
| 1515 | return 1; |
| 1516 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1517 | { |
| 1518 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1519 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1520 | return 1; |
| 1521 | } |
| 1522 | return 0; |
| 1523 | } |
| 1524 | #endif |
| 1525 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1526 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1527 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1528 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1529 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1530 | if (Py_REFCNT(unicode) != 1) |
| 1531 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1532 | if (_PyUnicode_HASH(unicode) != -1) |
| 1533 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1534 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1535 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1536 | if (!PyUnicode_CheckExact(unicode)) |
| 1537 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1538 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1539 | /* singleton refcount is greater than 1 */ |
| 1540 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1541 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1542 | return 1; |
| 1543 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1544 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1545 | static int |
| 1546 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1547 | { |
| 1548 | PyObject *unicode; |
| 1549 | Py_ssize_t old_length; |
| 1550 | |
| 1551 | assert(p_unicode != NULL); |
| 1552 | unicode = *p_unicode; |
| 1553 | |
| 1554 | assert(unicode != NULL); |
| 1555 | assert(PyUnicode_Check(unicode)); |
| 1556 | assert(0 <= length); |
| 1557 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1558 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1559 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1560 | else |
| 1561 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1562 | if (old_length == length) |
| 1563 | return 0; |
| 1564 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1565 | if (length == 0) { |
| 1566 | Py_DECREF(*p_unicode); |
| 1567 | *p_unicode = unicode_empty; |
| 1568 | Py_INCREF(*p_unicode); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1572 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1573 | PyObject *copy = resize_copy(unicode, length); |
| 1574 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1575 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1576 | Py_DECREF(*p_unicode); |
| 1577 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1578 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1581 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1582 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1583 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1584 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1585 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1586 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1587 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1588 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1589 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1592 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1593 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1594 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1595 | PyObject *unicode; |
| 1596 | if (p_unicode == NULL) { |
| 1597 | PyErr_BadInternalCall(); |
| 1598 | return -1; |
| 1599 | } |
| 1600 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1601 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1602 | { |
| 1603 | PyErr_BadInternalCall(); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1607 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1608 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1609 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1610 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1611 | { |
| 1612 | PyObject *result; |
| 1613 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1614 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1615 | return 0; |
| 1616 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1617 | maxchar); |
| 1618 | if (result == NULL) |
| 1619 | return -1; |
| 1620 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1621 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1622 | Py_DECREF(*p_unicode); |
| 1623 | *p_unicode = result; |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | static int |
| 1628 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1629 | Py_UCS4 ch) |
| 1630 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1631 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1632 | if (unicode_widen(p_unicode, ch) < 0) |
| 1633 | return -1; |
| 1634 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1635 | PyUnicode_DATA(*p_unicode), |
| 1636 | (*pos)++, ch); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame^] | 1640 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
| 1641 | Return the length of the input string. |
| 1642 | |
| 1643 | WARNING: Don't copy the terminating null character and don't check the |
| 1644 | maximum character (may write a latin1 character in an ASCII string). */ |
| 1645 | static Py_ssize_t |
| 1646 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str) |
| 1647 | { |
| 1648 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1649 | void *data = PyUnicode_DATA(unicode); |
| 1650 | |
| 1651 | switch (kind) { |
| 1652 | case PyUnicode_1BYTE_KIND: { |
| 1653 | Py_ssize_t len = strlen(str); |
| 1654 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
| 1655 | memcpy(data + index, str, len); |
| 1656 | return len; |
| 1657 | } |
| 1658 | case PyUnicode_2BYTE_KIND: { |
| 1659 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1660 | Py_UCS2 *ucs2 = start; |
| 1661 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1662 | |
| 1663 | for (; *str; ++ucs2, ++str) |
| 1664 | *ucs2 = (Py_UCS2)*str; |
| 1665 | |
| 1666 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1667 | return ucs2 - start; |
| 1668 | } |
| 1669 | default: { |
| 1670 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1671 | Py_UCS4 *ucs4 = start; |
| 1672 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1673 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1674 | |
| 1675 | for (; *str; ++ucs4, ++str) |
| 1676 | *ucs4 = (Py_UCS4)*str; |
| 1677 | |
| 1678 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1679 | return ucs4 - start; |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1685 | static PyObject* |
| 1686 | get_latin1_char(unsigned char ch) |
| 1687 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1688 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1689 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1690 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1691 | if (!unicode) |
| 1692 | return NULL; |
| 1693 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1694 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1695 | unicode_latin1[ch] = unicode; |
| 1696 | } |
| 1697 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1698 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1699 | } |
| 1700 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1701 | PyObject * |
| 1702 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1703 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1704 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1705 | Py_UCS4 maxchar = 0; |
| 1706 | Py_ssize_t num_surrogates; |
| 1707 | |
| 1708 | if (u == NULL) |
| 1709 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1710 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1711 | /* If the Unicode data is known at construction time, we can apply |
| 1712 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1714 | /* Optimization for empty strings */ |
| 1715 | if (size == 0 && unicode_empty != NULL) { |
| 1716 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1717 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1718 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1720 | /* Single character Unicode objects in the Latin-1 range are |
| 1721 | shared when using this constructor */ |
| 1722 | if (size == 1 && *u < 256) |
| 1723 | return get_latin1_char((unsigned char)*u); |
| 1724 | |
| 1725 | /* If not empty and not single character, copy the Unicode data |
| 1726 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1727 | if (find_maxchar_surrogates(u, u + size, |
| 1728 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | return NULL; |
| 1730 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1731 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | if (!unicode) |
| 1733 | return NULL; |
| 1734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1735 | switch (PyUnicode_KIND(unicode)) { |
| 1736 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1737 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1738 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1739 | break; |
| 1740 | case PyUnicode_2BYTE_KIND: |
| 1741 | #if Py_UNICODE_SIZE == 2 |
| 1742 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1743 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1744 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1746 | #endif |
| 1747 | break; |
| 1748 | case PyUnicode_4BYTE_KIND: |
| 1749 | #if SIZEOF_WCHAR_T == 2 |
| 1750 | /* This is the only case which has to process surrogates, thus |
| 1751 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1752 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1753 | #else |
| 1754 | assert(num_surrogates == 0); |
| 1755 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1756 | #endif |
| 1757 | break; |
| 1758 | default: |
| 1759 | assert(0 && "Impossible state"); |
| 1760 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1761 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1762 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1765 | PyObject * |
| 1766 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1767 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1768 | if (size < 0) { |
| 1769 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1770 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1771 | return NULL; |
| 1772 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1773 | if (u != NULL) |
| 1774 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1775 | else |
| 1776 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1779 | PyObject * |
| 1780 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1781 | { |
| 1782 | size_t size = strlen(u); |
| 1783 | if (size > PY_SSIZE_T_MAX) { |
| 1784 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1785 | return NULL; |
| 1786 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1787 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1790 | PyObject * |
| 1791 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1792 | { |
| 1793 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1794 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1795 | strlen(id->string), |
| 1796 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1797 | if (!id->object) |
| 1798 | return NULL; |
| 1799 | PyUnicode_InternInPlace(&id->object); |
| 1800 | assert(!id->next); |
| 1801 | id->next = static_strings; |
| 1802 | static_strings = id; |
| 1803 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1804 | return id->object; |
| 1805 | } |
| 1806 | |
| 1807 | void |
| 1808 | _PyUnicode_ClearStaticStrings() |
| 1809 | { |
| 1810 | _Py_Identifier *i; |
| 1811 | for (i = static_strings; i; i = i->next) { |
| 1812 | Py_DECREF(i->object); |
| 1813 | i->object = NULL; |
| 1814 | i->next = NULL; |
| 1815 | } |
| 1816 | } |
| 1817 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1818 | /* Internal function, don't check maximum character */ |
| 1819 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1820 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1821 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1822 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1823 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1824 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1825 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1826 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1827 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1828 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1829 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1830 | unicode = PyUnicode_New(size, 127); |
| 1831 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1832 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1833 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1834 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1835 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1836 | } |
| 1837 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1838 | static Py_UCS4 |
| 1839 | kind_maxchar_limit(unsigned int kind) |
| 1840 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1841 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1842 | case PyUnicode_1BYTE_KIND: |
| 1843 | return 0x80; |
| 1844 | case PyUnicode_2BYTE_KIND: |
| 1845 | return 0x100; |
| 1846 | case PyUnicode_4BYTE_KIND: |
| 1847 | return 0x10000; |
| 1848 | default: |
| 1849 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1850 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1851 | } |
| 1852 | } |
| 1853 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1854 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1855 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1856 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1857 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1858 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1859 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1860 | if (size == 0) { |
| 1861 | Py_INCREF(unicode_empty); |
| 1862 | return unicode_empty; |
| 1863 | } |
| 1864 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1865 | if (size == 1) |
| 1866 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1867 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1868 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1869 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1870 | if (!res) |
| 1871 | return NULL; |
| 1872 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1873 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1874 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1877 | static PyObject* |
| 1878 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1879 | { |
| 1880 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1881 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1882 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1883 | if (size == 0) { |
| 1884 | Py_INCREF(unicode_empty); |
| 1885 | return unicode_empty; |
| 1886 | } |
| 1887 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1888 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1889 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1890 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1891 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1892 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1893 | if (!res) |
| 1894 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1895 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1896 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1897 | else { |
| 1898 | _PyUnicode_CONVERT_BYTES( |
| 1899 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1900 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1901 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1902 | return res; |
| 1903 | } |
| 1904 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1905 | static PyObject* |
| 1906 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1907 | { |
| 1908 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1909 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1910 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1911 | if (size == 0) { |
| 1912 | Py_INCREF(unicode_empty); |
| 1913 | return unicode_empty; |
| 1914 | } |
| 1915 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1916 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1917 | return get_latin1_char((unsigned char)u[0]); |
| 1918 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1919 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1920 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1921 | if (!res) |
| 1922 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1923 | if (max_char < 256) |
| 1924 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1925 | PyUnicode_1BYTE_DATA(res)); |
| 1926 | else if (max_char < 0x10000) |
| 1927 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1928 | PyUnicode_2BYTE_DATA(res)); |
| 1929 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1930 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1931 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1932 | return res; |
| 1933 | } |
| 1934 | |
| 1935 | PyObject* |
| 1936 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1937 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1938 | if (size < 0) { |
| 1939 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1940 | return NULL; |
| 1941 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1942 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1943 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1944 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1945 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1946 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1947 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1948 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1949 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1950 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1951 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1952 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1953 | } |
| 1954 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1955 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1956 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1957 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1958 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1959 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1960 | { |
| 1961 | PyObject *unicode, *copy; |
| 1962 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1963 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1964 | unsigned int kind; |
| 1965 | |
| 1966 | assert(p_unicode != NULL); |
| 1967 | unicode = *p_unicode; |
| 1968 | assert(PyUnicode_IS_READY(unicode)); |
| 1969 | if (PyUnicode_IS_ASCII(unicode)) |
| 1970 | return; |
| 1971 | |
| 1972 | len = PyUnicode_GET_LENGTH(unicode); |
| 1973 | kind = PyUnicode_KIND(unicode); |
| 1974 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1975 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1976 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1977 | if (max_char >= 128) |
| 1978 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1979 | } |
| 1980 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1981 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1982 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1983 | if (max_char >= 256) |
| 1984 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1985 | } |
| 1986 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1987 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1988 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1989 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1990 | if (max_char >= 0x10000) |
| 1991 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1992 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1993 | copy = PyUnicode_New(len, max_char); |
| 1994 | copy_characters(copy, 0, unicode, 0, len); |
| 1995 | Py_DECREF(unicode); |
| 1996 | *p_unicode = copy; |
| 1997 | } |
| 1998 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1999 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2000 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2001 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2002 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2003 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2004 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2005 | if (!PyUnicode_Check(unicode)) { |
| 2006 | PyErr_BadInternalCall(); |
| 2007 | return NULL; |
| 2008 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2009 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2010 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2011 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2012 | length = PyUnicode_GET_LENGTH(unicode); |
| 2013 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2014 | if (!copy) |
| 2015 | return NULL; |
| 2016 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2017 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2018 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2019 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2020 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2021 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2024 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2025 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2026 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2027 | |
| 2028 | void* |
| 2029 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2030 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2031 | Py_ssize_t len; |
| 2032 | void *result; |
| 2033 | unsigned int skind; |
| 2034 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2035 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2036 | return NULL; |
| 2037 | |
| 2038 | len = PyUnicode_GET_LENGTH(s); |
| 2039 | skind = PyUnicode_KIND(s); |
| 2040 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2041 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2042 | return NULL; |
| 2043 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2044 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2045 | case PyUnicode_2BYTE_KIND: |
| 2046 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2047 | if (!result) |
| 2048 | return PyErr_NoMemory(); |
| 2049 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2050 | _PyUnicode_CONVERT_BYTES( |
| 2051 | Py_UCS1, Py_UCS2, |
| 2052 | PyUnicode_1BYTE_DATA(s), |
| 2053 | PyUnicode_1BYTE_DATA(s) + len, |
| 2054 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2055 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2056 | case PyUnicode_4BYTE_KIND: |
| 2057 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2058 | if (!result) |
| 2059 | return PyErr_NoMemory(); |
| 2060 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2061 | _PyUnicode_CONVERT_BYTES( |
| 2062 | Py_UCS2, Py_UCS4, |
| 2063 | PyUnicode_2BYTE_DATA(s), |
| 2064 | PyUnicode_2BYTE_DATA(s) + len, |
| 2065 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2066 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2067 | else { |
| 2068 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2069 | _PyUnicode_CONVERT_BYTES( |
| 2070 | Py_UCS1, Py_UCS4, |
| 2071 | PyUnicode_1BYTE_DATA(s), |
| 2072 | PyUnicode_1BYTE_DATA(s) + len, |
| 2073 | result); |
| 2074 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2075 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2076 | default: |
| 2077 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2078 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2079 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2080 | return NULL; |
| 2081 | } |
| 2082 | |
| 2083 | static Py_UCS4* |
| 2084 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2085 | int copy_null) |
| 2086 | { |
| 2087 | int kind; |
| 2088 | void *data; |
| 2089 | Py_ssize_t len, targetlen; |
| 2090 | if (PyUnicode_READY(string) == -1) |
| 2091 | return NULL; |
| 2092 | kind = PyUnicode_KIND(string); |
| 2093 | data = PyUnicode_DATA(string); |
| 2094 | len = PyUnicode_GET_LENGTH(string); |
| 2095 | targetlen = len; |
| 2096 | if (copy_null) |
| 2097 | targetlen++; |
| 2098 | if (!target) { |
| 2099 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2100 | PyErr_NoMemory(); |
| 2101 | return NULL; |
| 2102 | } |
| 2103 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2104 | if (!target) { |
| 2105 | PyErr_NoMemory(); |
| 2106 | return NULL; |
| 2107 | } |
| 2108 | } |
| 2109 | else { |
| 2110 | if (targetsize < targetlen) { |
| 2111 | PyErr_Format(PyExc_SystemError, |
| 2112 | "string is longer than the buffer"); |
| 2113 | if (copy_null && 0 < targetsize) |
| 2114 | target[0] = 0; |
| 2115 | return NULL; |
| 2116 | } |
| 2117 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2118 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2119 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2120 | _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] | 2121 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2122 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2123 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2124 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2125 | } |
| 2126 | else { |
| 2127 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2128 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2129 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2130 | if (copy_null) |
| 2131 | target[len] = 0; |
| 2132 | return target; |
| 2133 | } |
| 2134 | |
| 2135 | Py_UCS4* |
| 2136 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2137 | int copy_null) |
| 2138 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2139 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2140 | PyErr_BadInternalCall(); |
| 2141 | return NULL; |
| 2142 | } |
| 2143 | return as_ucs4(string, target, targetsize, copy_null); |
| 2144 | } |
| 2145 | |
| 2146 | Py_UCS4* |
| 2147 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2148 | { |
| 2149 | return as_ucs4(string, NULL, 0, 1); |
| 2150 | } |
| 2151 | |
| 2152 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2153 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2154 | PyObject * |
| 2155 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2157 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2158 | if (size == 0) { |
| 2159 | Py_INCREF(unicode_empty); |
| 2160 | return unicode_empty; |
| 2161 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2162 | PyErr_BadInternalCall(); |
| 2163 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2166 | if (size == -1) { |
| 2167 | size = wcslen(w); |
| 2168 | } |
| 2169 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2170 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2174 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2175 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2176 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2177 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2178 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2179 | *fmt++ = '%'; |
| 2180 | if (width) { |
| 2181 | if (zeropad) |
| 2182 | *fmt++ = '0'; |
| 2183 | fmt += sprintf(fmt, "%d", width); |
| 2184 | } |
| 2185 | if (precision) |
| 2186 | fmt += sprintf(fmt, ".%d", precision); |
| 2187 | if (longflag) |
| 2188 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2189 | else if (longlongflag) { |
| 2190 | /* longlongflag should only ever be nonzero on machines with |
| 2191 | HAVE_LONG_LONG defined */ |
| 2192 | #ifdef HAVE_LONG_LONG |
| 2193 | char *f = PY_FORMAT_LONG_LONG; |
| 2194 | while (*f) |
| 2195 | *fmt++ = *f++; |
| 2196 | #else |
| 2197 | /* we shouldn't ever get here */ |
| 2198 | assert(0); |
| 2199 | *fmt++ = 'l'; |
| 2200 | #endif |
| 2201 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2202 | else if (size_tflag) { |
| 2203 | char *f = PY_FORMAT_SIZE_T; |
| 2204 | while (*f) |
| 2205 | *fmt++ = *f++; |
| 2206 | } |
| 2207 | *fmt++ = c; |
| 2208 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2211 | /* helper for PyUnicode_FromFormatV() */ |
| 2212 | |
| 2213 | static const char* |
| 2214 | parse_format_flags(const char *f, |
| 2215 | int *p_width, int *p_precision, |
| 2216 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2217 | { |
| 2218 | int width, precision, longflag, longlongflag, size_tflag; |
| 2219 | |
| 2220 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2221 | f++; |
| 2222 | width = 0; |
| 2223 | while (Py_ISDIGIT((unsigned)*f)) |
| 2224 | width = (width*10) + *f++ - '0'; |
| 2225 | precision = 0; |
| 2226 | if (*f == '.') { |
| 2227 | f++; |
| 2228 | while (Py_ISDIGIT((unsigned)*f)) |
| 2229 | precision = (precision*10) + *f++ - '0'; |
| 2230 | if (*f == '%') { |
| 2231 | /* "%.3%s" => f points to "3" */ |
| 2232 | f--; |
| 2233 | } |
| 2234 | } |
| 2235 | if (*f == '\0') { |
| 2236 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2237 | f--; |
| 2238 | } |
| 2239 | if (p_width != NULL) |
| 2240 | *p_width = width; |
| 2241 | if (p_precision != NULL) |
| 2242 | *p_precision = precision; |
| 2243 | |
| 2244 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2245 | longflag = 0; |
| 2246 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2247 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2248 | |
| 2249 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2250 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2251 | longflag = 1; |
| 2252 | ++f; |
| 2253 | } |
| 2254 | #ifdef HAVE_LONG_LONG |
| 2255 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2256 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2257 | longlongflag = 1; |
| 2258 | f += 2; |
| 2259 | } |
| 2260 | #endif |
| 2261 | } |
| 2262 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2263 | 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] | 2264 | size_tflag = 1; |
| 2265 | ++f; |
| 2266 | } |
| 2267 | if (p_longflag != NULL) |
| 2268 | *p_longflag = longflag; |
| 2269 | if (p_longlongflag != NULL) |
| 2270 | *p_longlongflag = longlongflag; |
| 2271 | if (p_size_tflag != NULL) |
| 2272 | *p_size_tflag = size_tflag; |
| 2273 | return f; |
| 2274 | } |
| 2275 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2276 | /* maximum number of characters required for output of %ld. 21 characters |
| 2277 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2278 | #define MAX_LONG_CHARS 21 |
| 2279 | /* maximum number of characters required for output of %lld. |
| 2280 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2281 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2282 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2283 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2284 | PyObject * |
| 2285 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2286 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2287 | va_list count; |
| 2288 | Py_ssize_t callcount = 0; |
| 2289 | PyObject **callresults = NULL; |
| 2290 | PyObject **callresult = NULL; |
| 2291 | Py_ssize_t n = 0; |
| 2292 | int width = 0; |
| 2293 | int precision = 0; |
| 2294 | int zeropad; |
| 2295 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2296 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2297 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2298 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2299 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2300 | Py_UCS4 argmaxchar; |
| 2301 | Py_ssize_t numbersize = 0; |
| 2302 | char *numberresults = NULL; |
| 2303 | char *numberresult = NULL; |
| 2304 | Py_ssize_t i; |
| 2305 | int kind; |
| 2306 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2307 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2308 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2309 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2310 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2311 | * 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] | 2312 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2313 | * also estimate a upper bound for all the number formats in the string, |
| 2314 | * 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] | 2315 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2316 | for (f = format; *f; f++) { |
| 2317 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2318 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2319 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2320 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2321 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2322 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2324 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2325 | #ifdef HAVE_LONG_LONG |
| 2326 | if (longlongflag) { |
| 2327 | if (width < MAX_LONG_LONG_CHARS) |
| 2328 | width = MAX_LONG_LONG_CHARS; |
| 2329 | } |
| 2330 | else |
| 2331 | #endif |
| 2332 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2333 | including sign. Decimal takes the most space. This |
| 2334 | isn't enough for octal. If a width is specified we |
| 2335 | need more (which we allocate later). */ |
| 2336 | if (width < MAX_LONG_CHARS) |
| 2337 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2338 | |
| 2339 | /* account for the size + '\0' to separate numbers |
| 2340 | inside of the numberresults buffer */ |
| 2341 | numbersize += (width + 1); |
| 2342 | } |
| 2343 | } |
| 2344 | else if ((unsigned char)*f > 127) { |
| 2345 | PyErr_Format(PyExc_ValueError, |
| 2346 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2347 | "string, got a non-ASCII byte: 0x%02x", |
| 2348 | (unsigned char)*f); |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | } |
| 2352 | /* step 2: allocate memory for the results of |
| 2353 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2354 | if (callcount) { |
| 2355 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2356 | if (!callresults) { |
| 2357 | PyErr_NoMemory(); |
| 2358 | return NULL; |
| 2359 | } |
| 2360 | callresult = callresults; |
| 2361 | } |
| 2362 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2363 | if (numbersize) { |
| 2364 | numberresults = PyObject_Malloc(numbersize); |
| 2365 | if (!numberresults) { |
| 2366 | PyErr_NoMemory(); |
| 2367 | goto fail; |
| 2368 | } |
| 2369 | numberresult = numberresults; |
| 2370 | } |
| 2371 | |
| 2372 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2373 | for (f = format; *f; f++) { |
| 2374 | if (*f == '%') { |
| 2375 | const char* p; |
| 2376 | int longflag; |
| 2377 | int longlongflag; |
| 2378 | int size_tflag; |
| 2379 | int numprinted; |
| 2380 | |
| 2381 | p = f; |
| 2382 | zeropad = (f[1] == '0'); |
| 2383 | f = parse_format_flags(f, &width, &precision, |
| 2384 | &longflag, &longlongflag, &size_tflag); |
| 2385 | switch (*f) { |
| 2386 | case 'c': |
| 2387 | { |
| 2388 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2389 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2390 | n++; |
| 2391 | break; |
| 2392 | } |
| 2393 | case '%': |
| 2394 | n++; |
| 2395 | break; |
| 2396 | case 'i': |
| 2397 | case 'd': |
| 2398 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2399 | width, precision, *f); |
| 2400 | if (longflag) |
| 2401 | numprinted = sprintf(numberresult, fmt, |
| 2402 | va_arg(count, long)); |
| 2403 | #ifdef HAVE_LONG_LONG |
| 2404 | else if (longlongflag) |
| 2405 | numprinted = sprintf(numberresult, fmt, |
| 2406 | va_arg(count, PY_LONG_LONG)); |
| 2407 | #endif |
| 2408 | else if (size_tflag) |
| 2409 | numprinted = sprintf(numberresult, fmt, |
| 2410 | va_arg(count, Py_ssize_t)); |
| 2411 | else |
| 2412 | numprinted = sprintf(numberresult, fmt, |
| 2413 | va_arg(count, int)); |
| 2414 | n += numprinted; |
| 2415 | /* advance by +1 to skip over the '\0' */ |
| 2416 | numberresult += (numprinted + 1); |
| 2417 | assert(*(numberresult - 1) == '\0'); |
| 2418 | assert(*(numberresult - 2) != '\0'); |
| 2419 | assert(numprinted >= 0); |
| 2420 | assert(numberresult <= numberresults + numbersize); |
| 2421 | break; |
| 2422 | case 'u': |
| 2423 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2424 | width, precision, 'u'); |
| 2425 | if (longflag) |
| 2426 | numprinted = sprintf(numberresult, fmt, |
| 2427 | va_arg(count, unsigned long)); |
| 2428 | #ifdef HAVE_LONG_LONG |
| 2429 | else if (longlongflag) |
| 2430 | numprinted = sprintf(numberresult, fmt, |
| 2431 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2432 | #endif |
| 2433 | else if (size_tflag) |
| 2434 | numprinted = sprintf(numberresult, fmt, |
| 2435 | va_arg(count, size_t)); |
| 2436 | else |
| 2437 | numprinted = sprintf(numberresult, fmt, |
| 2438 | va_arg(count, unsigned int)); |
| 2439 | n += numprinted; |
| 2440 | numberresult += (numprinted + 1); |
| 2441 | assert(*(numberresult - 1) == '\0'); |
| 2442 | assert(*(numberresult - 2) != '\0'); |
| 2443 | assert(numprinted >= 0); |
| 2444 | assert(numberresult <= numberresults + numbersize); |
| 2445 | break; |
| 2446 | case 'x': |
| 2447 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2448 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2449 | n += numprinted; |
| 2450 | numberresult += (numprinted + 1); |
| 2451 | assert(*(numberresult - 1) == '\0'); |
| 2452 | assert(*(numberresult - 2) != '\0'); |
| 2453 | assert(numprinted >= 0); |
| 2454 | assert(numberresult <= numberresults + numbersize); |
| 2455 | break; |
| 2456 | case 'p': |
| 2457 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2458 | /* %p is ill-defined: ensure leading 0x. */ |
| 2459 | if (numberresult[1] == 'X') |
| 2460 | numberresult[1] = 'x'; |
| 2461 | else if (numberresult[1] != 'x') { |
| 2462 | memmove(numberresult + 2, numberresult, |
| 2463 | strlen(numberresult) + 1); |
| 2464 | numberresult[0] = '0'; |
| 2465 | numberresult[1] = 'x'; |
| 2466 | numprinted += 2; |
| 2467 | } |
| 2468 | n += numprinted; |
| 2469 | numberresult += (numprinted + 1); |
| 2470 | assert(*(numberresult - 1) == '\0'); |
| 2471 | assert(*(numberresult - 2) != '\0'); |
| 2472 | assert(numprinted >= 0); |
| 2473 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2474 | break; |
| 2475 | case 's': |
| 2476 | { |
| 2477 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2478 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2479 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2480 | if (!str) |
| 2481 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2482 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2483 | unicode objects, there is no need to call ready on them */ |
| 2484 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2485 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2486 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2487 | /* Remember the str and switch to the next slot */ |
| 2488 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2489 | break; |
| 2490 | } |
| 2491 | case 'U': |
| 2492 | { |
| 2493 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2494 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2495 | if (PyUnicode_READY(obj) == -1) |
| 2496 | goto fail; |
| 2497 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2498 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2499 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | } |
| 2502 | case 'V': |
| 2503 | { |
| 2504 | PyObject *obj = va_arg(count, PyObject *); |
| 2505 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2506 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2507 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2508 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2509 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2510 | if (PyUnicode_READY(obj) == -1) |
| 2511 | goto fail; |
| 2512 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2513 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2514 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2515 | *callresult++ = NULL; |
| 2516 | } |
| 2517 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2518 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2519 | if (!str_obj) |
| 2520 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2521 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2522 | Py_DECREF(str_obj); |
| 2523 | goto fail; |
| 2524 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2525 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2526 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2528 | *callresult++ = str_obj; |
| 2529 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2530 | break; |
| 2531 | } |
| 2532 | case 'S': |
| 2533 | { |
| 2534 | PyObject *obj = va_arg(count, PyObject *); |
| 2535 | PyObject *str; |
| 2536 | assert(obj); |
| 2537 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2538 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2540 | if (PyUnicode_READY(str) == -1) { |
| 2541 | Py_DECREF(str); |
| 2542 | goto fail; |
| 2543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2544 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2545 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2546 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2547 | /* Remember the str and switch to the next slot */ |
| 2548 | *callresult++ = str; |
| 2549 | break; |
| 2550 | } |
| 2551 | case 'R': |
| 2552 | { |
| 2553 | PyObject *obj = va_arg(count, PyObject *); |
| 2554 | PyObject *repr; |
| 2555 | assert(obj); |
| 2556 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2557 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2558 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2559 | if (PyUnicode_READY(repr) == -1) { |
| 2560 | Py_DECREF(repr); |
| 2561 | goto fail; |
| 2562 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2563 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2564 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2565 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2566 | /* Remember the repr and switch to the next slot */ |
| 2567 | *callresult++ = repr; |
| 2568 | break; |
| 2569 | } |
| 2570 | case 'A': |
| 2571 | { |
| 2572 | PyObject *obj = va_arg(count, PyObject *); |
| 2573 | PyObject *ascii; |
| 2574 | assert(obj); |
| 2575 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2576 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2577 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2578 | if (PyUnicode_READY(ascii) == -1) { |
| 2579 | Py_DECREF(ascii); |
| 2580 | goto fail; |
| 2581 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2582 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2583 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2584 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2585 | /* Remember the repr and switch to the next slot */ |
| 2586 | *callresult++ = ascii; |
| 2587 | break; |
| 2588 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2589 | default: |
| 2590 | /* if we stumble upon an unknown |
| 2591 | formatting code, copy the rest of |
| 2592 | the format string to the output |
| 2593 | string. (we cannot just skip the |
| 2594 | code, since there's no way to know |
| 2595 | what's in the argument list) */ |
| 2596 | n += strlen(p); |
| 2597 | goto expand; |
| 2598 | } |
| 2599 | } else |
| 2600 | n++; |
| 2601 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2602 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2603 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2604 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2605 | we don't have to resize the string. |
| 2606 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2607 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2608 | if (!string) |
| 2609 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2610 | kind = PyUnicode_KIND(string); |
| 2611 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2612 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2613 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2614 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2615 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2616 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2617 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2618 | |
| 2619 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2620 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2621 | /* checking for == because the last argument could be a empty |
| 2622 | string, which causes i to point to end, the assert at the end of |
| 2623 | the loop */ |
| 2624 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2625 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2626 | switch (*f) { |
| 2627 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2628 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2629 | const int ordinal = va_arg(vargs, int); |
| 2630 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2631 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2632 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2633 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2634 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2636 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame^] | 2638 | { |
| 2639 | Py_ssize_t written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2640 | /* unused, since we already have the result */ |
| 2641 | if (*f == 'p') |
| 2642 | (void) va_arg(vargs, void *); |
| 2643 | else |
| 2644 | (void) va_arg(vargs, int); |
| 2645 | /* extract the result from numberresults and append. */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame^] | 2646 | written = unicode_write_cstr(string, i, numberresult); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2647 | /* skip over the separating '\0' */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame^] | 2648 | i += written; |
| 2649 | numberresult += written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2650 | assert(*numberresult == '\0'); |
| 2651 | numberresult++; |
| 2652 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2653 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame^] | 2654 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2655 | case 's': |
| 2656 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2657 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2658 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2659 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2660 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2661 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2662 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2663 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2664 | /* We're done with the unicode()/repr() => forget it */ |
| 2665 | Py_DECREF(*callresult); |
| 2666 | /* switch to next unicode()/repr() result */ |
| 2667 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | } |
| 2670 | case 'U': |
| 2671 | { |
| 2672 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | Py_ssize_t size; |
| 2674 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2675 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2676 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2677 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | } |
| 2680 | case 'V': |
| 2681 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2682 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2683 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2684 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2685 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2686 | size = PyUnicode_GET_LENGTH(obj); |
| 2687 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2688 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2689 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2690 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2691 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2692 | assert(PyUnicode_KIND(*callresult) <= |
| 2693 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2694 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2695 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2696 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2697 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2698 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | break; |
| 2700 | } |
| 2701 | case 'S': |
| 2702 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2703 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2704 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2705 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2706 | /* unused, since we already have the result */ |
| 2707 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2708 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2709 | copy_characters(string, i, *callresult, 0, size); |
| 2710 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2711 | /* We're done with the unicode()/repr() => forget it */ |
| 2712 | Py_DECREF(*callresult); |
| 2713 | /* switch to next unicode()/repr() result */ |
| 2714 | ++callresult; |
| 2715 | break; |
| 2716 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2717 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2718 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2719 | break; |
| 2720 | default: |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame^] | 2721 | i += unicode_write_cstr(string, i, p); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2722 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2723 | goto end; |
| 2724 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2725 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2726 | else { |
| 2727 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2728 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2729 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2730 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2731 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2732 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2733 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2734 | if (callresults) |
| 2735 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2736 | if (numberresults) |
| 2737 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2738 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2739 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2740 | if (callresults) { |
| 2741 | PyObject **callresult2 = callresults; |
| 2742 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2743 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2744 | ++callresult2; |
| 2745 | } |
| 2746 | PyObject_Free(callresults); |
| 2747 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2748 | if (numberresults) |
| 2749 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2750 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2753 | PyObject * |
| 2754 | PyUnicode_FromFormat(const char *format, ...) |
| 2755 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2756 | PyObject* ret; |
| 2757 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2758 | |
| 2759 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2760 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2761 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2762 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2763 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2764 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2765 | va_end(vargs); |
| 2766 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2769 | #ifdef HAVE_WCHAR_H |
| 2770 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2771 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2772 | convert a Unicode object to a wide character string. |
| 2773 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2774 | - 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] | 2775 | character) required to convert the unicode object. Ignore size argument. |
| 2776 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2777 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2778 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2779 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2780 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2781 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2782 | wchar_t *w, |
| 2783 | Py_ssize_t size) |
| 2784 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2785 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2786 | const wchar_t *wstr; |
| 2787 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2788 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2789 | if (wstr == NULL) |
| 2790 | return -1; |
| 2791 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2792 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2793 | if (size > res) |
| 2794 | size = res + 1; |
| 2795 | else |
| 2796 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2797 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2798 | return res; |
| 2799 | } |
| 2800 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2801 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2805 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2806 | wchar_t *w, |
| 2807 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | { |
| 2809 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2810 | PyErr_BadInternalCall(); |
| 2811 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2812 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2813 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2816 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2817 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2818 | Py_ssize_t *size) |
| 2819 | { |
| 2820 | wchar_t* buffer; |
| 2821 | Py_ssize_t buflen; |
| 2822 | |
| 2823 | if (unicode == NULL) { |
| 2824 | PyErr_BadInternalCall(); |
| 2825 | return NULL; |
| 2826 | } |
| 2827 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2828 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2829 | if (buflen == -1) |
| 2830 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2831 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2832 | PyErr_NoMemory(); |
| 2833 | return NULL; |
| 2834 | } |
| 2835 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2836 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2837 | if (buffer == NULL) { |
| 2838 | PyErr_NoMemory(); |
| 2839 | return NULL; |
| 2840 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2841 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2842 | if (buflen == -1) |
| 2843 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2844 | if (size != NULL) |
| 2845 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2846 | return buffer; |
| 2847 | } |
| 2848 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2849 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2850 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2851 | PyObject * |
| 2852 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2853 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2854 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2855 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2856 | PyErr_SetString(PyExc_ValueError, |
| 2857 | "chr() arg not in range(0x110000)"); |
| 2858 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2859 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2861 | if (ordinal < 256) |
| 2862 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2864 | v = PyUnicode_New(1, ordinal); |
| 2865 | if (v == NULL) |
| 2866 | return NULL; |
| 2867 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2868 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2869 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2872 | PyObject * |
| 2873 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2874 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2875 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2876 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2877 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2878 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2879 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2880 | Py_INCREF(obj); |
| 2881 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2882 | } |
| 2883 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2884 | /* For a Unicode subtype that's not a Unicode object, |
| 2885 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2886 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2887 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2888 | PyErr_Format(PyExc_TypeError, |
| 2889 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2890 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2891 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2894 | PyObject * |
| 2895 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2896 | const char *encoding, |
| 2897 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2898 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2899 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2900 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2901 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2902 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2903 | PyErr_BadInternalCall(); |
| 2904 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2907 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2908 | if (PyBytes_Check(obj)) { |
| 2909 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2910 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2911 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2912 | } |
| 2913 | else { |
| 2914 | v = PyUnicode_Decode( |
| 2915 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2916 | encoding, errors); |
| 2917 | } |
| 2918 | return v; |
| 2919 | } |
| 2920 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2921 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2922 | PyErr_SetString(PyExc_TypeError, |
| 2923 | "decoding str is not supported"); |
| 2924 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2925 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2926 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2927 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2928 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2929 | PyErr_Format(PyExc_TypeError, |
| 2930 | "coercing to str: need bytes, bytearray " |
| 2931 | "or buffer-like object, %.80s found", |
| 2932 | Py_TYPE(obj)->tp_name); |
| 2933 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2934 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2935 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2936 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2937 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2938 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2939 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2940 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2941 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2943 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2944 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2947 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2948 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2949 | 1 on success. */ |
| 2950 | static int |
| 2951 | normalize_encoding(const char *encoding, |
| 2952 | char *lower, |
| 2953 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2954 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2955 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2956 | char *l; |
| 2957 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2958 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2959 | if (encoding == NULL) { |
| 2960 | strcpy(lower, "utf-8"); |
| 2961 | return 1; |
| 2962 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2963 | e = encoding; |
| 2964 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2965 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2966 | while (*e) { |
| 2967 | if (l == l_end) |
| 2968 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2969 | if (Py_ISUPPER(*e)) { |
| 2970 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2971 | } |
| 2972 | else if (*e == '_') { |
| 2973 | *l++ = '-'; |
| 2974 | e++; |
| 2975 | } |
| 2976 | else { |
| 2977 | *l++ = *e++; |
| 2978 | } |
| 2979 | } |
| 2980 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2981 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2984 | PyObject * |
| 2985 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2986 | Py_ssize_t size, |
| 2987 | const char *encoding, |
| 2988 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2989 | { |
| 2990 | PyObject *buffer = NULL, *unicode; |
| 2991 | Py_buffer info; |
| 2992 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2993 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2994 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2995 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2996 | if ((strcmp(lower, "utf-8") == 0) || |
| 2997 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2998 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2999 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3000 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3001 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3002 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3003 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3004 | else if (strcmp(lower, "mbcs") == 0) |
| 3005 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3006 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3007 | else if (strcmp(lower, "ascii") == 0) |
| 3008 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3009 | else if (strcmp(lower, "utf-16") == 0) |
| 3010 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3011 | else if (strcmp(lower, "utf-32") == 0) |
| 3012 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3013 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3016 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3017 | 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] | 3018 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3019 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | if (buffer == NULL) |
| 3021 | goto onError; |
| 3022 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3023 | if (unicode == NULL) |
| 3024 | goto onError; |
| 3025 | if (!PyUnicode_Check(unicode)) { |
| 3026 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3027 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3028 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | Py_DECREF(unicode); |
| 3030 | goto onError; |
| 3031 | } |
| 3032 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3033 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3034 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3035 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | Py_XDECREF(buffer); |
| 3037 | return NULL; |
| 3038 | } |
| 3039 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3040 | PyObject * |
| 3041 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3042 | const char *encoding, |
| 3043 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3044 | { |
| 3045 | PyObject *v; |
| 3046 | |
| 3047 | if (!PyUnicode_Check(unicode)) { |
| 3048 | PyErr_BadArgument(); |
| 3049 | goto onError; |
| 3050 | } |
| 3051 | |
| 3052 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3053 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3054 | |
| 3055 | /* Decode via the codec registry */ |
| 3056 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3057 | if (v == NULL) |
| 3058 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3059 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3060 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3061 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3062 | return NULL; |
| 3063 | } |
| 3064 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3065 | PyObject * |
| 3066 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3067 | const char *encoding, |
| 3068 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3069 | { |
| 3070 | PyObject *v; |
| 3071 | |
| 3072 | if (!PyUnicode_Check(unicode)) { |
| 3073 | PyErr_BadArgument(); |
| 3074 | goto onError; |
| 3075 | } |
| 3076 | |
| 3077 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3078 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3079 | |
| 3080 | /* Decode via the codec registry */ |
| 3081 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3082 | if (v == NULL) |
| 3083 | goto onError; |
| 3084 | if (!PyUnicode_Check(v)) { |
| 3085 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3086 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3087 | Py_TYPE(v)->tp_name); |
| 3088 | Py_DECREF(v); |
| 3089 | goto onError; |
| 3090 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3091 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3092 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3093 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3094 | return NULL; |
| 3095 | } |
| 3096 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3097 | PyObject * |
| 3098 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3099 | Py_ssize_t size, |
| 3100 | const char *encoding, |
| 3101 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3102 | { |
| 3103 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 | unicode = PyUnicode_FromUnicode(s, size); |
| 3106 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3107 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3108 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3109 | Py_DECREF(unicode); |
| 3110 | return v; |
| 3111 | } |
| 3112 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3113 | PyObject * |
| 3114 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3115 | const char *encoding, |
| 3116 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3117 | { |
| 3118 | PyObject *v; |
| 3119 | |
| 3120 | if (!PyUnicode_Check(unicode)) { |
| 3121 | PyErr_BadArgument(); |
| 3122 | goto onError; |
| 3123 | } |
| 3124 | |
| 3125 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3126 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3127 | |
| 3128 | /* Encode via the codec registry */ |
| 3129 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3130 | if (v == NULL) |
| 3131 | goto onError; |
| 3132 | return v; |
| 3133 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3134 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3135 | return NULL; |
| 3136 | } |
| 3137 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3138 | static size_t |
| 3139 | wcstombs_errorpos(const wchar_t *wstr) |
| 3140 | { |
| 3141 | size_t len; |
| 3142 | #if SIZEOF_WCHAR_T == 2 |
| 3143 | wchar_t buf[3]; |
| 3144 | #else |
| 3145 | wchar_t buf[2]; |
| 3146 | #endif |
| 3147 | char outbuf[MB_LEN_MAX]; |
| 3148 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3149 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3150 | #if SIZEOF_WCHAR_T == 2 |
| 3151 | buf[2] = 0; |
| 3152 | #else |
| 3153 | buf[1] = 0; |
| 3154 | #endif |
| 3155 | start = wstr; |
| 3156 | while (*wstr != L'\0') |
| 3157 | { |
| 3158 | previous = wstr; |
| 3159 | #if SIZEOF_WCHAR_T == 2 |
| 3160 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3161 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3162 | { |
| 3163 | buf[0] = wstr[0]; |
| 3164 | buf[1] = wstr[1]; |
| 3165 | wstr += 2; |
| 3166 | } |
| 3167 | else { |
| 3168 | buf[0] = *wstr; |
| 3169 | buf[1] = 0; |
| 3170 | wstr++; |
| 3171 | } |
| 3172 | #else |
| 3173 | buf[0] = *wstr; |
| 3174 | wstr++; |
| 3175 | #endif |
| 3176 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3177 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3178 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3182 | return 0; |
| 3183 | } |
| 3184 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3185 | static int |
| 3186 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3187 | { |
| 3188 | if (errors == NULL) { |
| 3189 | *surrogateescape = 0; |
| 3190 | return 0; |
| 3191 | } |
| 3192 | |
| 3193 | if (strcmp(errors, "strict") == 0) { |
| 3194 | *surrogateescape = 0; |
| 3195 | return 0; |
| 3196 | } |
| 3197 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3198 | *surrogateescape = 1; |
| 3199 | return 0; |
| 3200 | } |
| 3201 | PyErr_Format(PyExc_ValueError, |
| 3202 | "only 'strict' and 'surrogateescape' error handlers " |
| 3203 | "are supported, not '%s'", |
| 3204 | errors); |
| 3205 | return -1; |
| 3206 | } |
| 3207 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3208 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3209 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3210 | { |
| 3211 | Py_ssize_t wlen, wlen2; |
| 3212 | wchar_t *wstr; |
| 3213 | PyObject *bytes = NULL; |
| 3214 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3215 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3216 | PyObject *exc; |
| 3217 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3218 | int surrogateescape; |
| 3219 | |
| 3220 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3221 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3222 | |
| 3223 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3224 | if (wstr == NULL) |
| 3225 | return NULL; |
| 3226 | |
| 3227 | wlen2 = wcslen(wstr); |
| 3228 | if (wlen2 != wlen) { |
| 3229 | PyMem_Free(wstr); |
| 3230 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3231 | return NULL; |
| 3232 | } |
| 3233 | |
| 3234 | if (surrogateescape) { |
| 3235 | /* locale encoding with surrogateescape */ |
| 3236 | char *str; |
| 3237 | |
| 3238 | str = _Py_wchar2char(wstr, &error_pos); |
| 3239 | if (str == NULL) { |
| 3240 | if (error_pos == (size_t)-1) { |
| 3241 | PyErr_NoMemory(); |
| 3242 | PyMem_Free(wstr); |
| 3243 | return NULL; |
| 3244 | } |
| 3245 | else { |
| 3246 | goto encode_error; |
| 3247 | } |
| 3248 | } |
| 3249 | PyMem_Free(wstr); |
| 3250 | |
| 3251 | bytes = PyBytes_FromString(str); |
| 3252 | PyMem_Free(str); |
| 3253 | } |
| 3254 | else { |
| 3255 | size_t len, len2; |
| 3256 | |
| 3257 | len = wcstombs(NULL, wstr, 0); |
| 3258 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3259 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3260 | goto encode_error; |
| 3261 | } |
| 3262 | |
| 3263 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3264 | if (bytes == NULL) { |
| 3265 | PyMem_Free(wstr); |
| 3266 | return NULL; |
| 3267 | } |
| 3268 | |
| 3269 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3270 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3271 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3272 | goto encode_error; |
| 3273 | } |
| 3274 | PyMem_Free(wstr); |
| 3275 | } |
| 3276 | return bytes; |
| 3277 | |
| 3278 | encode_error: |
| 3279 | errmsg = strerror(errno); |
| 3280 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3281 | |
| 3282 | if (error_pos == (size_t)-1) |
| 3283 | error_pos = wcstombs_errorpos(wstr); |
| 3284 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3285 | PyMem_Free(wstr); |
| 3286 | Py_XDECREF(bytes); |
| 3287 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3288 | if (errmsg != NULL) { |
| 3289 | size_t errlen; |
| 3290 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3291 | if (wstr != NULL) { |
| 3292 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3293 | PyMem_Free(wstr); |
| 3294 | } else |
| 3295 | errmsg = NULL; |
| 3296 | } |
| 3297 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3298 | reason = PyUnicode_FromString( |
| 3299 | "wcstombs() encountered an unencodable " |
| 3300 | "wide character"); |
| 3301 | if (reason == NULL) |
| 3302 | return NULL; |
| 3303 | |
| 3304 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3305 | "locale", unicode, |
| 3306 | (Py_ssize_t)error_pos, |
| 3307 | (Py_ssize_t)(error_pos+1), |
| 3308 | reason); |
| 3309 | Py_DECREF(reason); |
| 3310 | if (exc != NULL) { |
| 3311 | PyCodec_StrictErrors(exc); |
| 3312 | Py_XDECREF(exc); |
| 3313 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3314 | return NULL; |
| 3315 | } |
| 3316 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3317 | PyObject * |
| 3318 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3319 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3320 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3321 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3322 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3323 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3324 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3325 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3326 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3327 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3328 | the Python codec requires to encode at least its own filename. Use the C |
| 3329 | version of the locale codec until the codec registry is initialized and |
| 3330 | the Python codec is loaded. |
| 3331 | |
| 3332 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3333 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3334 | subinterpreters. */ |
| 3335 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3336 | return PyUnicode_AsEncodedString(unicode, |
| 3337 | Py_FileSystemDefaultEncoding, |
| 3338 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3339 | } |
| 3340 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3341 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3342 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3343 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3346 | PyObject * |
| 3347 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3348 | const char *encoding, |
| 3349 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | { |
| 3351 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3352 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3354 | if (!PyUnicode_Check(unicode)) { |
| 3355 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3356 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3357 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3358 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3359 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3360 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3361 | if ((strcmp(lower, "utf-8") == 0) || |
| 3362 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3363 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3364 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3365 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3366 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3367 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3368 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3369 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3370 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3371 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3372 | return _PyUnicode_AsLatin1String(unicode, errors); |
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 | else if (strcmp(lower, "mbcs") == 0) |
| 3375 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3376 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3377 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3378 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3379 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | |
| 3381 | /* Encode via the codec registry */ |
| 3382 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3383 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3384 | return NULL; |
| 3385 | |
| 3386 | /* The normal path */ |
| 3387 | if (PyBytes_Check(v)) |
| 3388 | return v; |
| 3389 | |
| 3390 | /* 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] | 3391 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3392 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3393 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3394 | |
| 3395 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3396 | "encoder %s returned bytearray instead of bytes", |
| 3397 | encoding); |
| 3398 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3399 | Py_DECREF(v); |
| 3400 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3401 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3402 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3403 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3404 | Py_DECREF(v); |
| 3405 | return b; |
| 3406 | } |
| 3407 | |
| 3408 | PyErr_Format(PyExc_TypeError, |
| 3409 | "encoder did not return a bytes object (type=%.400s)", |
| 3410 | Py_TYPE(v)->tp_name); |
| 3411 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3412 | return NULL; |
| 3413 | } |
| 3414 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3415 | PyObject * |
| 3416 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3417 | const char *encoding, |
| 3418 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3419 | { |
| 3420 | PyObject *v; |
| 3421 | |
| 3422 | if (!PyUnicode_Check(unicode)) { |
| 3423 | PyErr_BadArgument(); |
| 3424 | goto onError; |
| 3425 | } |
| 3426 | |
| 3427 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3428 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3429 | |
| 3430 | /* Encode via the codec registry */ |
| 3431 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3432 | if (v == NULL) |
| 3433 | goto onError; |
| 3434 | if (!PyUnicode_Check(v)) { |
| 3435 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3436 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3437 | Py_TYPE(v)->tp_name); |
| 3438 | Py_DECREF(v); |
| 3439 | goto onError; |
| 3440 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3443 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3444 | return NULL; |
| 3445 | } |
| 3446 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3447 | static size_t |
| 3448 | mbstowcs_errorpos(const char *str, size_t len) |
| 3449 | { |
| 3450 | #ifdef HAVE_MBRTOWC |
| 3451 | const char *start = str; |
| 3452 | mbstate_t mbs; |
| 3453 | size_t converted; |
| 3454 | wchar_t ch; |
| 3455 | |
| 3456 | memset(&mbs, 0, sizeof mbs); |
| 3457 | while (len) |
| 3458 | { |
| 3459 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3460 | if (converted == 0) |
| 3461 | /* Reached end of string */ |
| 3462 | break; |
| 3463 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3464 | /* Conversion error or incomplete character */ |
| 3465 | return str - start; |
| 3466 | } |
| 3467 | else { |
| 3468 | str += converted; |
| 3469 | len -= converted; |
| 3470 | } |
| 3471 | } |
| 3472 | /* failed to find the undecodable byte sequence */ |
| 3473 | return 0; |
| 3474 | #endif |
| 3475 | return 0; |
| 3476 | } |
| 3477 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3478 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3479 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3480 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3481 | { |
| 3482 | wchar_t smallbuf[256]; |
| 3483 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3484 | wchar_t *wstr; |
| 3485 | size_t wlen, wlen2; |
| 3486 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3487 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3488 | size_t error_pos; |
| 3489 | char *errmsg; |
| 3490 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3491 | |
| 3492 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3493 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3494 | |
| 3495 | if (str[len] != '\0' || len != strlen(str)) { |
| 3496 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3497 | return NULL; |
| 3498 | } |
| 3499 | |
| 3500 | if (surrogateescape) |
| 3501 | { |
| 3502 | wstr = _Py_char2wchar(str, &wlen); |
| 3503 | if (wstr == NULL) { |
| 3504 | if (wlen == (size_t)-1) |
| 3505 | PyErr_NoMemory(); |
| 3506 | else |
| 3507 | PyErr_SetFromErrno(PyExc_OSError); |
| 3508 | return NULL; |
| 3509 | } |
| 3510 | |
| 3511 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3512 | PyMem_Free(wstr); |
| 3513 | } |
| 3514 | else { |
| 3515 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3516 | wlen = mbstowcs(NULL, str, 0); |
| 3517 | #else |
| 3518 | wlen = len; |
| 3519 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3520 | if (wlen == (size_t)-1) |
| 3521 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3522 | if (wlen+1 <= smallbuf_len) { |
| 3523 | wstr = smallbuf; |
| 3524 | } |
| 3525 | else { |
| 3526 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3527 | return PyErr_NoMemory(); |
| 3528 | |
| 3529 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3530 | if (!wstr) |
| 3531 | return PyErr_NoMemory(); |
| 3532 | } |
| 3533 | |
| 3534 | /* This shouldn't fail now */ |
| 3535 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3536 | if (wlen2 == (size_t)-1) { |
| 3537 | if (wstr != smallbuf) |
| 3538 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3539 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3540 | } |
| 3541 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3542 | assert(wlen2 == wlen); |
| 3543 | #endif |
| 3544 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3545 | if (wstr != smallbuf) |
| 3546 | PyMem_Free(wstr); |
| 3547 | } |
| 3548 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3549 | |
| 3550 | decode_error: |
| 3551 | errmsg = strerror(errno); |
| 3552 | assert(errmsg != NULL); |
| 3553 | |
| 3554 | error_pos = mbstowcs_errorpos(str, len); |
| 3555 | if (errmsg != NULL) { |
| 3556 | size_t errlen; |
| 3557 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3558 | if (wstr != NULL) { |
| 3559 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3560 | PyMem_Free(wstr); |
| 3561 | } else |
| 3562 | errmsg = NULL; |
| 3563 | } |
| 3564 | if (errmsg == NULL) |
| 3565 | reason = PyUnicode_FromString( |
| 3566 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3567 | if (reason == NULL) |
| 3568 | return NULL; |
| 3569 | |
| 3570 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3571 | "locale", str, len, |
| 3572 | (Py_ssize_t)error_pos, |
| 3573 | (Py_ssize_t)(error_pos+1), |
| 3574 | reason); |
| 3575 | Py_DECREF(reason); |
| 3576 | if (exc != NULL) { |
| 3577 | PyCodec_StrictErrors(exc); |
| 3578 | Py_XDECREF(exc); |
| 3579 | } |
| 3580 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3584 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3585 | { |
| 3586 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3587 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | |
| 3591 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3592 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3593 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3594 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3595 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3596 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3597 | PyObject* |
| 3598 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3599 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3600 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3601 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3602 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3603 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3604 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3605 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3606 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3607 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3608 | the Python codec requires to encode at least its own filename. Use the C |
| 3609 | version of the locale codec until the codec registry is initialized and |
| 3610 | the Python codec is loaded. |
| 3611 | |
| 3612 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3613 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3614 | subinterpreters. */ |
| 3615 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3616 | return PyUnicode_Decode(s, size, |
| 3617 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3618 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3619 | } |
| 3620 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3621 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3622 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3623 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3626 | |
| 3627 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3628 | _PyUnicode_HasNULChars(PyObject* s) |
| 3629 | { |
| 3630 | static PyObject *nul = NULL; |
| 3631 | |
| 3632 | if (nul == NULL) |
| 3633 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3634 | if (nul == NULL) |
| 3635 | return -1; |
| 3636 | return PyUnicode_Contains(s, nul); |
| 3637 | } |
| 3638 | |
| 3639 | |
| 3640 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3641 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3642 | { |
| 3643 | PyObject *output = NULL; |
| 3644 | Py_ssize_t size; |
| 3645 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3646 | if (arg == NULL) { |
| 3647 | Py_DECREF(*(PyObject**)addr); |
| 3648 | return 1; |
| 3649 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3650 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3651 | output = arg; |
| 3652 | Py_INCREF(output); |
| 3653 | } |
| 3654 | else { |
| 3655 | arg = PyUnicode_FromObject(arg); |
| 3656 | if (!arg) |
| 3657 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3658 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3659 | Py_DECREF(arg); |
| 3660 | if (!output) |
| 3661 | return 0; |
| 3662 | if (!PyBytes_Check(output)) { |
| 3663 | Py_DECREF(output); |
| 3664 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3665 | return 0; |
| 3666 | } |
| 3667 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3668 | size = PyBytes_GET_SIZE(output); |
| 3669 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3670 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3671 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3672 | Py_DECREF(output); |
| 3673 | return 0; |
| 3674 | } |
| 3675 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3676 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3680 | int |
| 3681 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3682 | { |
| 3683 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3684 | if (arg == NULL) { |
| 3685 | Py_DECREF(*(PyObject**)addr); |
| 3686 | return 1; |
| 3687 | } |
| 3688 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3689 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3690 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3691 | output = arg; |
| 3692 | Py_INCREF(output); |
| 3693 | } |
| 3694 | else { |
| 3695 | arg = PyBytes_FromObject(arg); |
| 3696 | if (!arg) |
| 3697 | return 0; |
| 3698 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3699 | PyBytes_GET_SIZE(arg)); |
| 3700 | Py_DECREF(arg); |
| 3701 | if (!output) |
| 3702 | return 0; |
| 3703 | if (!PyUnicode_Check(output)) { |
| 3704 | Py_DECREF(output); |
| 3705 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3706 | return 0; |
| 3707 | } |
| 3708 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3709 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3710 | Py_DECREF(output); |
| 3711 | return 0; |
| 3712 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3713 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3714 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3715 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3716 | Py_DECREF(output); |
| 3717 | return 0; |
| 3718 | } |
| 3719 | *(PyObject**)addr = output; |
| 3720 | return Py_CLEANUP_SUPPORTED; |
| 3721 | } |
| 3722 | |
| 3723 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3724 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3725 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3726 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3727 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3728 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3729 | if (!PyUnicode_Check(unicode)) { |
| 3730 | PyErr_BadArgument(); |
| 3731 | return NULL; |
| 3732 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3733 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3734 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3735 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3736 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3737 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3738 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3739 | if (bytes == NULL) |
| 3740 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3741 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3742 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3743 | Py_DECREF(bytes); |
| 3744 | return NULL; |
| 3745 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3746 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3747 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3748 | PyBytes_AS_STRING(bytes), |
| 3749 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3750 | Py_DECREF(bytes); |
| 3751 | } |
| 3752 | |
| 3753 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3754 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3755 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3759 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3760 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3761 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3762 | } |
| 3763 | |
| 3764 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3765 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | #endif |
| 3767 | |
| 3768 | |
| 3769 | Py_UNICODE * |
| 3770 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3771 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3772 | const unsigned char *one_byte; |
| 3773 | #if SIZEOF_WCHAR_T == 4 |
| 3774 | const Py_UCS2 *two_bytes; |
| 3775 | #else |
| 3776 | const Py_UCS4 *four_bytes; |
| 3777 | const Py_UCS4 *ucs4_end; |
| 3778 | Py_ssize_t num_surrogates; |
| 3779 | #endif |
| 3780 | wchar_t *w; |
| 3781 | wchar_t *wchar_end; |
| 3782 | |
| 3783 | if (!PyUnicode_Check(unicode)) { |
| 3784 | PyErr_BadArgument(); |
| 3785 | return NULL; |
| 3786 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3787 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3789 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3790 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3791 | |
| 3792 | #ifdef Py_DEBUG |
| 3793 | ++unicode_as_unicode_calls; |
| 3794 | #endif |
| 3795 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3796 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3797 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3798 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3799 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3800 | num_surrogates = 0; |
| 3801 | |
| 3802 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3803 | if (*four_bytes > 0xFFFF) |
| 3804 | ++num_surrogates; |
| 3805 | } |
| 3806 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3807 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3808 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3809 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3810 | PyErr_NoMemory(); |
| 3811 | return NULL; |
| 3812 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3813 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3814 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3815 | w = _PyUnicode_WSTR(unicode); |
| 3816 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3817 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3819 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3820 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3822 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3823 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3824 | } |
| 3825 | else |
| 3826 | *w = *four_bytes; |
| 3827 | |
| 3828 | if (w > wchar_end) { |
| 3829 | assert(0 && "Miscalculated string end"); |
| 3830 | } |
| 3831 | } |
| 3832 | *w = 0; |
| 3833 | #else |
| 3834 | /* sizeof(wchar_t) == 4 */ |
| 3835 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3836 | "should share memory already."); |
| 3837 | return NULL; |
| 3838 | #endif |
| 3839 | } |
| 3840 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3841 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3842 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3843 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | PyErr_NoMemory(); |
| 3845 | return NULL; |
| 3846 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3847 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3848 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3849 | w = _PyUnicode_WSTR(unicode); |
| 3850 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3851 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3852 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3853 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3854 | for (; w < wchar_end; ++one_byte, ++w) |
| 3855 | *w = *one_byte; |
| 3856 | /* null-terminate the wstr */ |
| 3857 | *w = 0; |
| 3858 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3859 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3860 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3861 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3862 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3863 | *w = *two_bytes; |
| 3864 | /* null-terminate the wstr */ |
| 3865 | *w = 0; |
| 3866 | #else |
| 3867 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3868 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3869 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3870 | Py_FatalError("Impossible unicode object state, wstr " |
| 3871 | "and str should share memory already."); |
| 3872 | return NULL; |
| 3873 | #endif |
| 3874 | } |
| 3875 | else { |
| 3876 | assert(0 && "This should never happen."); |
| 3877 | } |
| 3878 | } |
| 3879 | } |
| 3880 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3881 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3882 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3885 | Py_UNICODE * |
| 3886 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3887 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3888 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3891 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3892 | Py_ssize_t |
| 3893 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3894 | { |
| 3895 | if (!PyUnicode_Check(unicode)) { |
| 3896 | PyErr_BadArgument(); |
| 3897 | goto onError; |
| 3898 | } |
| 3899 | return PyUnicode_GET_SIZE(unicode); |
| 3900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3901 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3902 | return -1; |
| 3903 | } |
| 3904 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3905 | Py_ssize_t |
| 3906 | PyUnicode_GetLength(PyObject *unicode) |
| 3907 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3908 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3909 | PyErr_BadArgument(); |
| 3910 | return -1; |
| 3911 | } |
| 3912 | |
| 3913 | return PyUnicode_GET_LENGTH(unicode); |
| 3914 | } |
| 3915 | |
| 3916 | Py_UCS4 |
| 3917 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3918 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3919 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3920 | PyErr_BadArgument(); |
| 3921 | return (Py_UCS4)-1; |
| 3922 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3923 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3924 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3925 | return (Py_UCS4)-1; |
| 3926 | } |
| 3927 | return PyUnicode_READ_CHAR(unicode, index); |
| 3928 | } |
| 3929 | |
| 3930 | int |
| 3931 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3932 | { |
| 3933 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3934 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3935 | return -1; |
| 3936 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3937 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3938 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3939 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3940 | return -1; |
| 3941 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3942 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3943 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3944 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3945 | index, ch); |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3949 | const char * |
| 3950 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3951 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3952 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3955 | /* create or adjust a UnicodeDecodeError */ |
| 3956 | static void |
| 3957 | make_decode_exception(PyObject **exceptionObject, |
| 3958 | const char *encoding, |
| 3959 | const char *input, Py_ssize_t length, |
| 3960 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3961 | const char *reason) |
| 3962 | { |
| 3963 | if (*exceptionObject == NULL) { |
| 3964 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3965 | encoding, input, length, startpos, endpos, reason); |
| 3966 | } |
| 3967 | else { |
| 3968 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3969 | goto onError; |
| 3970 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3971 | goto onError; |
| 3972 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3973 | goto onError; |
| 3974 | } |
| 3975 | return; |
| 3976 | |
| 3977 | onError: |
| 3978 | Py_DECREF(*exceptionObject); |
| 3979 | *exceptionObject = NULL; |
| 3980 | } |
| 3981 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3982 | /* error handling callback helper: |
| 3983 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3984 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3985 | and adjust various state variables. |
| 3986 | return 0 on success, -1 on error |
| 3987 | */ |
| 3988 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3989 | static int |
| 3990 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3991 | const char *encoding, const char *reason, |
| 3992 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3993 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3994 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3995 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3996 | 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] | 3997 | |
| 3998 | PyObject *restuple = NULL; |
| 3999 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4000 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4001 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4002 | Py_ssize_t requiredsize; |
| 4003 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4004 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4005 | int res = -1; |
| 4006 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4007 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4008 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4009 | else |
| 4010 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4011 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4012 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4013 | *errorHandler = PyCodec_LookupError(errors); |
| 4014 | if (*errorHandler == NULL) |
| 4015 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4018 | make_decode_exception(exceptionObject, |
| 4019 | encoding, |
| 4020 | *input, *inend - *input, |
| 4021 | *startinpos, *endinpos, |
| 4022 | reason); |
| 4023 | if (*exceptionObject == NULL) |
| 4024 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4025 | |
| 4026 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4027 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4028 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4029 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4030 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4031 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4032 | } |
| 4033 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4034 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4035 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4036 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4037 | |
| 4038 | /* Copy back the bytes variables, which might have been modified by the |
| 4039 | callback */ |
| 4040 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4041 | if (!inputobj) |
| 4042 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4043 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4045 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4046 | *input = PyBytes_AS_STRING(inputobj); |
| 4047 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4048 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4049 | /* we can DECREF safely, as the exception has another reference, |
| 4050 | so the object won't go away. */ |
| 4051 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4052 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4053 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4054 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4055 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4056 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4057 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4058 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4059 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4060 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4061 | /* need more space? (at least enough for what we |
| 4062 | have+the replacement+the rest of the string (starting |
| 4063 | at the new input position), so we won't have to check space |
| 4064 | when there are no errors in the rest of the string) */ |
| 4065 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4066 | requiredsize = *outpos + replen + insize-newpos; |
| 4067 | if (requiredsize > outsize) { |
| 4068 | if (requiredsize<2*outsize) |
| 4069 | requiredsize = 2*outsize; |
| 4070 | if (unicode_resize(output, requiredsize) < 0) |
| 4071 | goto onError; |
| 4072 | } |
| 4073 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4074 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4075 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4076 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4077 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4078 | else { |
| 4079 | wchar_t *repwstr; |
| 4080 | Py_ssize_t repwlen; |
| 4081 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4082 | if (repwstr == NULL) |
| 4083 | goto onError; |
| 4084 | /* need more space? (at least enough for what we |
| 4085 | have+the replacement+the rest of the string (starting |
| 4086 | at the new input position), so we won't have to check space |
| 4087 | when there are no errors in the rest of the string) */ |
| 4088 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4089 | if (requiredsize > outsize) { |
| 4090 | if (requiredsize < 2*outsize) |
| 4091 | requiredsize = 2*outsize; |
| 4092 | if (unicode_resize(output, requiredsize) < 0) |
| 4093 | goto onError; |
| 4094 | } |
| 4095 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4096 | *outpos += repwlen; |
| 4097 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4098 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4099 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4100 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4101 | /* we made it! */ |
| 4102 | res = 0; |
| 4103 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4104 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4105 | Py_XDECREF(restuple); |
| 4106 | return res; |
| 4107 | } |
| 4108 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4109 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4110 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4111 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4112 | |
| 4113 | /* Three simple macros defining base-64. */ |
| 4114 | |
| 4115 | /* Is c a base-64 character? */ |
| 4116 | |
| 4117 | #define IS_BASE64(c) \ |
| 4118 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4119 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4120 | ((c) >= '0' && (c) <= '9') || \ |
| 4121 | (c) == '+' || (c) == '/') |
| 4122 | |
| 4123 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4124 | |
| 4125 | #define FROM_BASE64(c) \ |
| 4126 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4127 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4128 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4129 | (c) == '+' ? 62 : 63) |
| 4130 | |
| 4131 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4132 | |
| 4133 | #define TO_BASE64(n) \ |
| 4134 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4135 | |
| 4136 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4137 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4138 | * byte not decoding to itself is the + which begins a base64 |
| 4139 | * string. */ |
| 4140 | |
| 4141 | #define DECODE_DIRECT(c) \ |
| 4142 | ((c) <= 127 && (c) != '+') |
| 4143 | |
| 4144 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4145 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4146 | * the above). See RFC2152. This array identifies these different |
| 4147 | * sets: |
| 4148 | * 0 : "Set D" |
| 4149 | * alphanumeric and '(),-./:? |
| 4150 | * 1 : "Set O" |
| 4151 | * !"#$%&*;<=>@[]^_`{|} |
| 4152 | * 2 : "whitespace" |
| 4153 | * ht nl cr sp |
| 4154 | * 3 : special (must be base64 encoded) |
| 4155 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4156 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4157 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4158 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4159 | char utf7_category[128] = { |
| 4160 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4161 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4162 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4163 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4164 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4165 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4166 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4167 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4168 | /* @ A B C D E F G H I J K L M N O */ |
| 4169 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4170 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4171 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4172 | /* ` a b c d e f g h i j k l m n o */ |
| 4173 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4174 | /* p q r s t u v w x y z { | } ~ del */ |
| 4175 | 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] | 4176 | }; |
| 4177 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4178 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4179 | * answer depends on whether we are encoding set O as itself, and also |
| 4180 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4181 | * clear that the answers to these questions vary between |
| 4182 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4183 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4184 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4185 | ((c) < 128 && (c) > 0 && \ |
| 4186 | ((utf7_category[(c)] == 0) || \ |
| 4187 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4188 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4189 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4190 | PyObject * |
| 4191 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4192 | Py_ssize_t size, |
| 4193 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4194 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4195 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4196 | } |
| 4197 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4198 | /* The decoder. The only state we preserve is our read position, |
| 4199 | * i.e. how many characters we have consumed. So if we end in the |
| 4200 | * middle of a shift sequence we have to back off the read position |
| 4201 | * and the output to the beginning of the sequence, otherwise we lose |
| 4202 | * all the shift state (seen bits, number of bits seen, high |
| 4203 | * surrogate). */ |
| 4204 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4205 | PyObject * |
| 4206 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4207 | Py_ssize_t size, |
| 4208 | const char *errors, |
| 4209 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4210 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4211 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4212 | Py_ssize_t startinpos; |
| 4213 | Py_ssize_t endinpos; |
| 4214 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4215 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4216 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4217 | const char *errmsg = ""; |
| 4218 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4219 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4220 | unsigned int base64bits = 0; |
| 4221 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4222 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4223 | PyObject *errorHandler = NULL; |
| 4224 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4225 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4226 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4227 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4228 | if (!unicode) |
| 4229 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4230 | if (size == 0) { |
| 4231 | if (consumed) |
| 4232 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4233 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4234 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4235 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4236 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4237 | e = s + size; |
| 4238 | |
| 4239 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4240 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4241 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4242 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4243 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4244 | if (inShift) { /* in a base-64 section */ |
| 4245 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4246 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4247 | base64bits += 6; |
| 4248 | s++; |
| 4249 | if (base64bits >= 16) { |
| 4250 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4251 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4252 | base64bits -= 16; |
| 4253 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4254 | if (surrogate) { |
| 4255 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4256 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4257 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4258 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4259 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4260 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4261 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4262 | } |
| 4263 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4264 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4265 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4266 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4267 | } |
| 4268 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4269 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4270 | /* first surrogate */ |
| 4271 | surrogate = outCh; |
| 4272 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4273 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4274 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4275 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | } |
| 4279 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4280 | inShift = 0; |
| 4281 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4283 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4284 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4285 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4286 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4287 | if (base64bits > 0) { /* left-over bits */ |
| 4288 | if (base64bits >= 6) { |
| 4289 | /* We've seen at least one base-64 character */ |
| 4290 | errmsg = "partial character in shift sequence"; |
| 4291 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4292 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4293 | else { |
| 4294 | /* Some bits remain; they should be zero */ |
| 4295 | if (base64buffer != 0) { |
| 4296 | errmsg = "non-zero padding bits in shift sequence"; |
| 4297 | goto utf7Error; |
| 4298 | } |
| 4299 | } |
| 4300 | } |
| 4301 | if (ch != '-') { |
| 4302 | /* '-' is absorbed; other terminating |
| 4303 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4304 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4305 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4306 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4310 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4311 | s++; /* consume '+' */ |
| 4312 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4313 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4314 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4315 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4316 | } |
| 4317 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4318 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4319 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4320 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4321 | } |
| 4322 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4323 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4324 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4325 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4326 | s++; |
| 4327 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4328 | else { |
| 4329 | startinpos = s-starts; |
| 4330 | s++; |
| 4331 | errmsg = "unexpected special character"; |
| 4332 | goto utf7Error; |
| 4333 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4335 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4336 | endinpos = s-starts; |
| 4337 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4338 | errors, &errorHandler, |
| 4339 | "utf7", errmsg, |
| 4340 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4341 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4342 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | /* end of string */ |
| 4346 | |
| 4347 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4348 | /* if we're in an inconsistent state, that's an error */ |
| 4349 | if (surrogate || |
| 4350 | (base64bits >= 6) || |
| 4351 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4352 | endinpos = size; |
| 4353 | if (unicode_decode_call_errorhandler( |
| 4354 | errors, &errorHandler, |
| 4355 | "utf7", "unterminated shift sequence", |
| 4356 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4357 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4358 | goto onError; |
| 4359 | if (s < e) |
| 4360 | goto restart; |
| 4361 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4362 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | |
| 4364 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4365 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4366 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4367 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4368 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | } |
| 4370 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4371 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4372 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4373 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4374 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4375 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4376 | goto onError; |
| 4377 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4378 | Py_XDECREF(errorHandler); |
| 4379 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4380 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4382 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4383 | Py_XDECREF(errorHandler); |
| 4384 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4385 | Py_DECREF(unicode); |
| 4386 | return NULL; |
| 4387 | } |
| 4388 | |
| 4389 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4390 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4391 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4392 | int base64SetO, |
| 4393 | int base64WhiteSpace, |
| 4394 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4395 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4396 | int kind; |
| 4397 | void *data; |
| 4398 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4399 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4400 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4402 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4403 | unsigned int base64bits = 0; |
| 4404 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4405 | char * out; |
| 4406 | char * start; |
| 4407 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4408 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4409 | return NULL; |
| 4410 | kind = PyUnicode_KIND(str); |
| 4411 | data = PyUnicode_DATA(str); |
| 4412 | len = PyUnicode_GET_LENGTH(str); |
| 4413 | |
| 4414 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4415 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4417 | /* It might be possible to tighten this worst case */ |
| 4418 | allocated = 8 * len; |
| 4419 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4420 | return PyErr_NoMemory(); |
| 4421 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4422 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4423 | if (v == NULL) |
| 4424 | return NULL; |
| 4425 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4426 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4427 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4428 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4430 | if (inShift) { |
| 4431 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4432 | /* shifting out */ |
| 4433 | if (base64bits) { /* output remaining bits */ |
| 4434 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4435 | base64buffer = 0; |
| 4436 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4437 | } |
| 4438 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4439 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4440 | so no '-' is required, except if the character is itself a '-' */ |
| 4441 | if (IS_BASE64(ch) || ch == '-') { |
| 4442 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4443 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4444 | *out++ = (char) ch; |
| 4445 | } |
| 4446 | else { |
| 4447 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4448 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4449 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4450 | else { /* not in a shift sequence */ |
| 4451 | if (ch == '+') { |
| 4452 | *out++ = '+'; |
| 4453 | *out++ = '-'; |
| 4454 | } |
| 4455 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4456 | *out++ = (char) ch; |
| 4457 | } |
| 4458 | else { |
| 4459 | *out++ = '+'; |
| 4460 | inShift = 1; |
| 4461 | goto encode_char; |
| 4462 | } |
| 4463 | } |
| 4464 | continue; |
| 4465 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4466 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4467 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4468 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4469 | /* code first surrogate */ |
| 4470 | base64bits += 16; |
| 4471 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4472 | while (base64bits >= 6) { |
| 4473 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4474 | base64bits -= 6; |
| 4475 | } |
| 4476 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4477 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4478 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4479 | base64bits += 16; |
| 4480 | base64buffer = (base64buffer << 16) | ch; |
| 4481 | while (base64bits >= 6) { |
| 4482 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4483 | base64bits -= 6; |
| 4484 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4485 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4486 | if (base64bits) |
| 4487 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4488 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4489 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4490 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4491 | return NULL; |
| 4492 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4493 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4494 | PyObject * |
| 4495 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4496 | Py_ssize_t size, |
| 4497 | int base64SetO, |
| 4498 | int base64WhiteSpace, |
| 4499 | const char *errors) |
| 4500 | { |
| 4501 | PyObject *result; |
| 4502 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4503 | if (tmp == NULL) |
| 4504 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4505 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4506 | base64WhiteSpace, errors); |
| 4507 | Py_DECREF(tmp); |
| 4508 | return result; |
| 4509 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4510 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4511 | #undef IS_BASE64 |
| 4512 | #undef FROM_BASE64 |
| 4513 | #undef TO_BASE64 |
| 4514 | #undef DECODE_DIRECT |
| 4515 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4517 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4518 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4519 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4521 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4522 | illegal prefix. See RFC 3629 for details */ |
| 4523 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4524 | 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] | 4525 | 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] | 4526 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4527 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4529 | 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] | 4530 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4531 | 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] | 4532 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4533 | 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] | 4534 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4535 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4536 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4537 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4538 | 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] | 4539 | }; |
| 4540 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4541 | PyObject * |
| 4542 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4543 | Py_ssize_t size, |
| 4544 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4545 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4546 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4547 | } |
| 4548 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4549 | #include "stringlib/ucs1lib.h" |
| 4550 | #include "stringlib/codecs.h" |
| 4551 | #include "stringlib/undef.h" |
| 4552 | |
| 4553 | #include "stringlib/ucs2lib.h" |
| 4554 | #include "stringlib/codecs.h" |
| 4555 | #include "stringlib/undef.h" |
| 4556 | |
| 4557 | #include "stringlib/ucs4lib.h" |
| 4558 | #include "stringlib/codecs.h" |
| 4559 | #include "stringlib/undef.h" |
| 4560 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4561 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4562 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4563 | |
| 4564 | /* Mask to quickly check whether a C 'long' contains a |
| 4565 | non-ASCII, UTF8-encoded char. */ |
| 4566 | #if (SIZEOF_LONG == 8) |
| 4567 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4568 | #elif (SIZEOF_LONG == 4) |
| 4569 | # define ASCII_CHAR_MASK 0x80808080L |
| 4570 | #else |
| 4571 | # error C 'long' size should be either 4 or 8! |
| 4572 | #endif |
| 4573 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4574 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4575 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4576 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4577 | 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] | 4578 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4579 | */ |
| 4580 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4581 | 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] | 4582 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4583 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4584 | const unsigned char *end = p + string_size; |
| 4585 | 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] | 4586 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4587 | assert(unicode_size != NULL); |
| 4588 | |
| 4589 | /* By having a cascade of independent loops which fallback onto each |
| 4590 | other, we minimize the amount of work done in the average loop |
| 4591 | iteration, and we also maximize the CPU's ability to predict |
| 4592 | branches correctly (because a given condition will have always the |
| 4593 | same boolean outcome except perhaps in the last iteration of the |
| 4594 | corresponding loop). |
| 4595 | In the general case this brings us rather close to decoding |
| 4596 | performance pre-PEP 393, despite the two-pass decoding. |
| 4597 | |
| 4598 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4599 | character has been encountered. It is actually a pessimization (by |
| 4600 | a significant factor) to use this loop on text with many non-ASCII |
| 4601 | characters, and it is important to avoid bad performance on valid |
| 4602 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4603 | */ |
| 4604 | |
| 4605 | /* ASCII */ |
| 4606 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4607 | /* Only check value if it's not a ASCII char... */ |
| 4608 | if (*p < 0x80) { |
| 4609 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4610 | an explanation. */ |
| 4611 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4612 | /* Help register allocation */ |
| 4613 | register const unsigned char *_p = p; |
| 4614 | while (_p < aligned_end) { |
| 4615 | unsigned long value = *(unsigned long *) _p; |
| 4616 | if (value & ASCII_CHAR_MASK) |
| 4617 | break; |
| 4618 | _p += SIZEOF_LONG; |
| 4619 | char_count += SIZEOF_LONG; |
| 4620 | } |
| 4621 | p = _p; |
| 4622 | if (p == end) |
| 4623 | break; |
| 4624 | } |
| 4625 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4626 | if (*p < 0x80) |
| 4627 | ++char_count; |
| 4628 | else |
| 4629 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4630 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4631 | *unicode_size = char_count; |
| 4632 | return 127; |
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 | _ucs1loop: |
| 4635 | for (; p < end; ++p) { |
| 4636 | if (*p < 0xc4) |
| 4637 | char_count += ((*p & 0xc0) != 0x80); |
| 4638 | else |
| 4639 | goto _ucs2loop; |
| 4640 | } |
| 4641 | *unicode_size = char_count; |
| 4642 | return 255; |
| 4643 | |
| 4644 | _ucs2loop: |
| 4645 | for (; p < end; ++p) { |
| 4646 | if (*p < 0xf0) |
| 4647 | char_count += ((*p & 0xc0) != 0x80); |
| 4648 | else |
| 4649 | goto _ucs4loop; |
| 4650 | } |
| 4651 | *unicode_size = char_count; |
| 4652 | return 65535; |
| 4653 | |
| 4654 | _ucs4loop: |
| 4655 | for (; p < end; ++p) { |
| 4656 | char_count += ((*p & 0xc0) != 0x80); |
| 4657 | } |
| 4658 | *unicode_size = char_count; |
| 4659 | return 65537; |
| 4660 | } |
| 4661 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4662 | /* 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] | 4663 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4664 | 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] | 4665 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4666 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4667 | do { \ |
| 4668 | Py_ssize_t pos = index; \ |
| 4669 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4670 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4671 | goto onError; \ |
| 4672 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4673 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4674 | } while (0) |
| 4675 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4676 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4677 | decode_utf8_errors(const char *starts, |
| 4678 | Py_ssize_t size, |
| 4679 | const char *errors, |
| 4680 | Py_ssize_t *consumed, |
| 4681 | const char *s, |
| 4682 | PyObject *unicode, |
| 4683 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4684 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4685 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4686 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4687 | Py_ssize_t startinpos; |
| 4688 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4689 | const char *e = starts + size; |
| 4690 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4691 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4692 | PyObject *errorHandler = NULL; |
| 4693 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4694 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4695 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | |
| 4697 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4698 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | |
| 4700 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4701 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4702 | input will consist of an overwhelming majority of ASCII |
| 4703 | characters, we try to optimize for this case by checking |
| 4704 | as many characters as a C 'long' can contain. |
| 4705 | First, check if we can do an aligned read, as most CPUs have |
| 4706 | a penalty for unaligned reads. |
| 4707 | */ |
| 4708 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4709 | /* Help register allocation */ |
| 4710 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4711 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4712 | while (_s < aligned_end) { |
| 4713 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4714 | and do a fast unrolled copy if it only contains ASCII |
| 4715 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4716 | unsigned long value = *(unsigned long *) _s; |
| 4717 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4718 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4719 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4720 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4721 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4722 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4723 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4724 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4725 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4726 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4727 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4728 | #endif |
| 4729 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4730 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4731 | } |
| 4732 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4733 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4734 | if (s == e) |
| 4735 | break; |
| 4736 | ch = (unsigned char)*s; |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4741 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | s++; |
| 4743 | continue; |
| 4744 | } |
| 4745 | |
| 4746 | n = utf8_code_length[ch]; |
| 4747 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4748 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4749 | if (consumed) |
| 4750 | break; |
| 4751 | else { |
| 4752 | errmsg = "unexpected end of data"; |
| 4753 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4754 | endinpos = startinpos+1; |
| 4755 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4756 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4757 | goto utf8Error; |
| 4758 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4759 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4760 | |
| 4761 | switch (n) { |
| 4762 | |
| 4763 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4764 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4765 | startinpos = s-starts; |
| 4766 | endinpos = startinpos+1; |
| 4767 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4768 | |
| 4769 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4770 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4771 | startinpos = s-starts; |
| 4772 | endinpos = startinpos+1; |
| 4773 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4774 | |
| 4775 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4776 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4777 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4778 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4779 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4780 | goto utf8Error; |
| 4781 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4782 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4783 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4784 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4785 | break; |
| 4786 | |
| 4787 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4788 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4789 | will result in surrogates in range d800-dfff. Surrogates are |
| 4790 | not valid UTF-8 so they are rejected. |
| 4791 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4792 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4793 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4794 | (s[2] & 0xc0) != 0x80 || |
| 4795 | ((unsigned char)s[0] == 0xE0 && |
| 4796 | (unsigned char)s[1] < 0xA0) || |
| 4797 | ((unsigned char)s[0] == 0xED && |
| 4798 | (unsigned char)s[1] > 0x9F)) { |
| 4799 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4800 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4801 | endinpos = startinpos + 1; |
| 4802 | |
| 4803 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4804 | continuation byte is s[2], so increment endinpos by 1, |
| 4805 | if not, s[1] is invalid and endinpos doesn't need to |
| 4806 | be incremented. */ |
| 4807 | if ((s[1] & 0xC0) == 0x80) |
| 4808 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4809 | goto utf8Error; |
| 4810 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4812 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4813 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4814 | break; |
| 4815 | |
| 4816 | case 4: |
| 4817 | if ((s[1] & 0xc0) != 0x80 || |
| 4818 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4819 | (s[3] & 0xc0) != 0x80 || |
| 4820 | ((unsigned char)s[0] == 0xF0 && |
| 4821 | (unsigned char)s[1] < 0x90) || |
| 4822 | ((unsigned char)s[0] == 0xF4 && |
| 4823 | (unsigned char)s[1] > 0x8F)) { |
| 4824 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4825 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4826 | endinpos = startinpos + 1; |
| 4827 | if ((s[1] & 0xC0) == 0x80) { |
| 4828 | endinpos++; |
| 4829 | if ((s[2] & 0xC0) == 0x80) |
| 4830 | endinpos++; |
| 4831 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4832 | goto utf8Error; |
| 4833 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4834 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4835 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4836 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4837 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4838 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4839 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 | } |
| 4841 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4842 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4843 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4844 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4845 | if (unicode_decode_call_errorhandler( |
| 4846 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4847 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4848 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4849 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4850 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4851 | /* Update data because unicode_decode_call_errorhandler might have |
| 4852 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4853 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4854 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4855 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4856 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4857 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4858 | /* Adjust length and ready string when it contained errors and |
| 4859 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4860 | if (unicode_resize(&unicode, i) < 0) |
| 4861 | goto onError; |
| 4862 | unicode_adjust_maxchar(&unicode); |
| 4863 | if (unicode == NULL) |
| 4864 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4865 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4866 | Py_XDECREF(errorHandler); |
| 4867 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4868 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4869 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4870 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4871 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4872 | Py_XDECREF(errorHandler); |
| 4873 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4874 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | return NULL; |
| 4876 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4877 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4878 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4879 | PyObject * |
| 4880 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4881 | Py_ssize_t size, |
| 4882 | const char *errors, |
| 4883 | Py_ssize_t *consumed) |
| 4884 | { |
| 4885 | Py_UCS4 maxchar = 0; |
| 4886 | Py_ssize_t unicode_size; |
| 4887 | int has_errors = 0; |
| 4888 | PyObject *unicode; |
| 4889 | int kind; |
| 4890 | void *data; |
| 4891 | const char *starts = s; |
| 4892 | const char *e; |
| 4893 | Py_ssize_t i; |
| 4894 | |
| 4895 | if (size == 0) { |
| 4896 | if (consumed) |
| 4897 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4898 | Py_INCREF(unicode_empty); |
| 4899 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4902 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4903 | |
| 4904 | /* When the string is ASCII only, just use memcpy and return. |
| 4905 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4906 | sequence at the end of the ASCII block. */ |
| 4907 | if (maxchar < 128 && size == unicode_size) { |
| 4908 | if (consumed) |
| 4909 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 4910 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4914 | if (!unicode) |
| 4915 | return NULL; |
| 4916 | kind = PyUnicode_KIND(unicode); |
| 4917 | data = PyUnicode_DATA(unicode); |
| 4918 | |
| 4919 | /* Unpack UTF-8 encoded data */ |
| 4920 | i = 0; |
| 4921 | e = starts + size; |
| 4922 | switch (kind) { |
| 4923 | case PyUnicode_1BYTE_KIND: |
| 4924 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4925 | break; |
| 4926 | case PyUnicode_2BYTE_KIND: |
| 4927 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4928 | break; |
| 4929 | case PyUnicode_4BYTE_KIND: |
| 4930 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4931 | break; |
| 4932 | } |
| 4933 | if (!has_errors) { |
| 4934 | /* Ensure the unicode size calculation was correct */ |
| 4935 | assert(i == unicode_size); |
| 4936 | assert(s == e); |
| 4937 | if (consumed) |
| 4938 | *consumed = size; |
| 4939 | return unicode; |
| 4940 | } |
| 4941 | |
| 4942 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4943 | code below refits and resizes as necessary. */ |
| 4944 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 4945 | } |
| 4946 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4947 | #ifdef __APPLE__ |
| 4948 | |
| 4949 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4950 | used to decode the command line arguments on Mac OS X. */ |
| 4951 | |
| 4952 | wchar_t* |
| 4953 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4954 | { |
| 4955 | int n; |
| 4956 | const char *e; |
| 4957 | wchar_t *unicode, *p; |
| 4958 | |
| 4959 | /* Note: size will always be longer than the resulting Unicode |
| 4960 | character count */ |
| 4961 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4962 | PyErr_NoMemory(); |
| 4963 | return NULL; |
| 4964 | } |
| 4965 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4966 | if (!unicode) |
| 4967 | return NULL; |
| 4968 | |
| 4969 | /* Unpack UTF-8 encoded data */ |
| 4970 | p = unicode; |
| 4971 | e = s + size; |
| 4972 | while (s < e) { |
| 4973 | Py_UCS4 ch = (unsigned char)*s; |
| 4974 | |
| 4975 | if (ch < 0x80) { |
| 4976 | *p++ = (wchar_t)ch; |
| 4977 | s++; |
| 4978 | continue; |
| 4979 | } |
| 4980 | |
| 4981 | n = utf8_code_length[ch]; |
| 4982 | if (s + n > e) { |
| 4983 | goto surrogateescape; |
| 4984 | } |
| 4985 | |
| 4986 | switch (n) { |
| 4987 | case 0: |
| 4988 | case 1: |
| 4989 | goto surrogateescape; |
| 4990 | |
| 4991 | case 2: |
| 4992 | if ((s[1] & 0xc0) != 0x80) |
| 4993 | goto surrogateescape; |
| 4994 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4995 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4996 | *p++ = (wchar_t)ch; |
| 4997 | break; |
| 4998 | |
| 4999 | case 3: |
| 5000 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 5001 | will result in surrogates in range d800-dfff. Surrogates are |
| 5002 | not valid UTF-8 so they are rejected. |
| 5003 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 5004 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 5005 | if ((s[1] & 0xc0) != 0x80 || |
| 5006 | (s[2] & 0xc0) != 0x80 || |
| 5007 | ((unsigned char)s[0] == 0xE0 && |
| 5008 | (unsigned char)s[1] < 0xA0) || |
| 5009 | ((unsigned char)s[0] == 0xED && |
| 5010 | (unsigned char)s[1] > 0x9F)) { |
| 5011 | |
| 5012 | goto surrogateescape; |
| 5013 | } |
| 5014 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 5015 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5016 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5017 | break; |
| 5018 | |
| 5019 | case 4: |
| 5020 | if ((s[1] & 0xc0) != 0x80 || |
| 5021 | (s[2] & 0xc0) != 0x80 || |
| 5022 | (s[3] & 0xc0) != 0x80 || |
| 5023 | ((unsigned char)s[0] == 0xF0 && |
| 5024 | (unsigned char)s[1] < 0x90) || |
| 5025 | ((unsigned char)s[0] == 0xF4 && |
| 5026 | (unsigned char)s[1] > 0x8F)) { |
| 5027 | goto surrogateescape; |
| 5028 | } |
| 5029 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 5030 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5031 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5032 | |
| 5033 | #if SIZEOF_WCHAR_T == 4 |
| 5034 | *p++ = (wchar_t)ch; |
| 5035 | #else |
| 5036 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5037 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5038 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5039 | #endif |
| 5040 | break; |
| 5041 | } |
| 5042 | s += n; |
| 5043 | continue; |
| 5044 | |
| 5045 | surrogateescape: |
| 5046 | *p++ = 0xDC00 + ch; |
| 5047 | s++; |
| 5048 | } |
| 5049 | *p = L'\0'; |
| 5050 | return unicode; |
| 5051 | } |
| 5052 | |
| 5053 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5055 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5056 | |
| 5057 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5058 | and allocate exactly as much space needed at the end. Else allocate the |
| 5059 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5060 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5061 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5062 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5063 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5064 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5065 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5066 | void *data; |
| 5067 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5069 | if (!PyUnicode_Check(unicode)) { |
| 5070 | PyErr_BadArgument(); |
| 5071 | return NULL; |
| 5072 | } |
| 5073 | |
| 5074 | if (PyUnicode_READY(unicode) == -1) |
| 5075 | return NULL; |
| 5076 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5077 | if (PyUnicode_UTF8(unicode)) |
| 5078 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5079 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5080 | |
| 5081 | kind = PyUnicode_KIND(unicode); |
| 5082 | data = PyUnicode_DATA(unicode); |
| 5083 | size = PyUnicode_GET_LENGTH(unicode); |
| 5084 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5085 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5086 | default: |
| 5087 | assert(0); |
| 5088 | case PyUnicode_1BYTE_KIND: |
| 5089 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5090 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5091 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5092 | case PyUnicode_2BYTE_KIND: |
| 5093 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5094 | case PyUnicode_4BYTE_KIND: |
| 5095 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5099 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5100 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5101 | Py_ssize_t size, |
| 5102 | const char *errors) |
| 5103 | { |
| 5104 | PyObject *v, *unicode; |
| 5105 | |
| 5106 | unicode = PyUnicode_FromUnicode(s, size); |
| 5107 | if (unicode == NULL) |
| 5108 | return NULL; |
| 5109 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5110 | Py_DECREF(unicode); |
| 5111 | return v; |
| 5112 | } |
| 5113 | |
| 5114 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5115 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5116 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5117 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5120 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5121 | |
| 5122 | PyObject * |
| 5123 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5124 | Py_ssize_t size, |
| 5125 | const char *errors, |
| 5126 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5127 | { |
| 5128 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5129 | } |
| 5130 | |
| 5131 | PyObject * |
| 5132 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5133 | Py_ssize_t size, |
| 5134 | const char *errors, |
| 5135 | int *byteorder, |
| 5136 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5137 | { |
| 5138 | const char *starts = s; |
| 5139 | Py_ssize_t startinpos; |
| 5140 | Py_ssize_t endinpos; |
| 5141 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5142 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5143 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5144 | int bo = 0; /* assume native ordering by default */ |
| 5145 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5146 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5147 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5148 | int iorder[] = {0, 1, 2, 3}; |
| 5149 | #else |
| 5150 | int iorder[] = {3, 2, 1, 0}; |
| 5151 | #endif |
| 5152 | PyObject *errorHandler = NULL; |
| 5153 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5154 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5155 | q = (unsigned char *)s; |
| 5156 | e = q + size; |
| 5157 | |
| 5158 | if (byteorder) |
| 5159 | bo = *byteorder; |
| 5160 | |
| 5161 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5162 | byte order setting accordingly. In native mode, the leading BOM |
| 5163 | mark is skipped, in all other modes, it is copied to the output |
| 5164 | stream as-is (giving a ZWNBSP character). */ |
| 5165 | if (bo == 0) { |
| 5166 | if (size >= 4) { |
| 5167 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5168 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5169 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5170 | if (bom == 0x0000FEFF) { |
| 5171 | q += 4; |
| 5172 | bo = -1; |
| 5173 | } |
| 5174 | else if (bom == 0xFFFE0000) { |
| 5175 | q += 4; |
| 5176 | bo = 1; |
| 5177 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5178 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5179 | if (bom == 0x0000FEFF) { |
| 5180 | q += 4; |
| 5181 | bo = 1; |
| 5182 | } |
| 5183 | else if (bom == 0xFFFE0000) { |
| 5184 | q += 4; |
| 5185 | bo = -1; |
| 5186 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5187 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5188 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | if (bo == -1) { |
| 5192 | /* force LE */ |
| 5193 | iorder[0] = 0; |
| 5194 | iorder[1] = 1; |
| 5195 | iorder[2] = 2; |
| 5196 | iorder[3] = 3; |
| 5197 | } |
| 5198 | else if (bo == 1) { |
| 5199 | /* force BE */ |
| 5200 | iorder[0] = 3; |
| 5201 | iorder[1] = 2; |
| 5202 | iorder[2] = 1; |
| 5203 | iorder[3] = 0; |
| 5204 | } |
| 5205 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5206 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5207 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5208 | if (!unicode) |
| 5209 | return NULL; |
| 5210 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5211 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5212 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5213 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5214 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5215 | Py_UCS4 ch; |
| 5216 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5217 | if (e-q<4) { |
| 5218 | if (consumed) |
| 5219 | break; |
| 5220 | errmsg = "truncated data"; |
| 5221 | startinpos = ((const char *)q)-starts; |
| 5222 | endinpos = ((const char *)e)-starts; |
| 5223 | goto utf32Error; |
| 5224 | /* The remaining input chars are ignored if the callback |
| 5225 | chooses to skip the input */ |
| 5226 | } |
| 5227 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5228 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5230 | if (ch >= 0x110000) |
| 5231 | { |
| 5232 | errmsg = "codepoint not in range(0x110000)"; |
| 5233 | startinpos = ((const char *)q)-starts; |
| 5234 | endinpos = startinpos+4; |
| 5235 | goto utf32Error; |
| 5236 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5237 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5238 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5239 | q += 4; |
| 5240 | continue; |
| 5241 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5242 | if (unicode_decode_call_errorhandler( |
| 5243 | errors, &errorHandler, |
| 5244 | "utf32", errmsg, |
| 5245 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5246 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5247 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | if (byteorder) |
| 5251 | *byteorder = bo; |
| 5252 | |
| 5253 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5254 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5255 | |
| 5256 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5257 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5258 | goto onError; |
| 5259 | |
| 5260 | Py_XDECREF(errorHandler); |
| 5261 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5262 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5263 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5264 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5265 | Py_DECREF(unicode); |
| 5266 | Py_XDECREF(errorHandler); |
| 5267 | Py_XDECREF(exc); |
| 5268 | return NULL; |
| 5269 | } |
| 5270 | |
| 5271 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5272 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5273 | const char *errors, |
| 5274 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5275 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5276 | int kind; |
| 5277 | void *data; |
| 5278 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5279 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5280 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5281 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5282 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5283 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5284 | int iorder[] = {0, 1, 2, 3}; |
| 5285 | #else |
| 5286 | int iorder[] = {3, 2, 1, 0}; |
| 5287 | #endif |
| 5288 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5289 | #define STORECHAR(CH) \ |
| 5290 | do { \ |
| 5291 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5292 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5293 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5294 | p[iorder[0]] = (CH) & 0xff; \ |
| 5295 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5296 | } while(0) |
| 5297 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5298 | if (!PyUnicode_Check(str)) { |
| 5299 | PyErr_BadArgument(); |
| 5300 | return NULL; |
| 5301 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5302 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5303 | return NULL; |
| 5304 | kind = PyUnicode_KIND(str); |
| 5305 | data = PyUnicode_DATA(str); |
| 5306 | len = PyUnicode_GET_LENGTH(str); |
| 5307 | |
| 5308 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5309 | bytesize = nsize * 4; |
| 5310 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5312 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5313 | if (v == NULL) |
| 5314 | return NULL; |
| 5315 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5316 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5317 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5318 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5319 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5320 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5321 | |
| 5322 | if (byteorder == -1) { |
| 5323 | /* force LE */ |
| 5324 | iorder[0] = 0; |
| 5325 | iorder[1] = 1; |
| 5326 | iorder[2] = 2; |
| 5327 | iorder[3] = 3; |
| 5328 | } |
| 5329 | else if (byteorder == 1) { |
| 5330 | /* force BE */ |
| 5331 | iorder[0] = 3; |
| 5332 | iorder[1] = 2; |
| 5333 | iorder[2] = 1; |
| 5334 | iorder[3] = 0; |
| 5335 | } |
| 5336 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5337 | for (i = 0; i < len; i++) |
| 5338 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5339 | |
| 5340 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5341 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5342 | #undef STORECHAR |
| 5343 | } |
| 5344 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5345 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5346 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5347 | Py_ssize_t size, |
| 5348 | const char *errors, |
| 5349 | int byteorder) |
| 5350 | { |
| 5351 | PyObject *result; |
| 5352 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5353 | if (tmp == NULL) |
| 5354 | return NULL; |
| 5355 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5356 | Py_DECREF(tmp); |
| 5357 | return result; |
| 5358 | } |
| 5359 | |
| 5360 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5361 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5362 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5363 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5367 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5368 | PyObject * |
| 5369 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5370 | Py_ssize_t size, |
| 5371 | const char *errors, |
| 5372 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5374 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5375 | } |
| 5376 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5377 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5378 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5379 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5380 | rare in most input. |
| 5381 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5382 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5383 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5384 | #if (SIZEOF_LONG == 8) |
| 5385 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5386 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5387 | #elif (SIZEOF_LONG == 4) |
| 5388 | # define FAST_CHAR_MASK 0x80008000L |
| 5389 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5390 | #else |
| 5391 | # error C 'long' size should be either 4 or 8! |
| 5392 | #endif |
| 5393 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5394 | PyObject * |
| 5395 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5396 | Py_ssize_t size, |
| 5397 | const char *errors, |
| 5398 | int *byteorder, |
| 5399 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5400 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5401 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5402 | Py_ssize_t startinpos; |
| 5403 | Py_ssize_t endinpos; |
| 5404 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5405 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5406 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5407 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5408 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5409 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5410 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5411 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5412 | int ihi = 1, ilo = 0; |
| 5413 | #else |
| 5414 | int ihi = 0, ilo = 1; |
| 5415 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5416 | PyObject *errorHandler = NULL; |
| 5417 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | |
| 5419 | /* Note: size will always be longer than the resulting Unicode |
| 5420 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5421 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | if (!unicode) |
| 5423 | return NULL; |
| 5424 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5425 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5426 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5427 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5428 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5429 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | |
| 5431 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5432 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5434 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5435 | byte order setting accordingly. In native mode, the leading BOM |
| 5436 | mark is skipped, in all other modes, it is copied to the output |
| 5437 | stream as-is (giving a ZWNBSP character). */ |
| 5438 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5439 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5440 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5441 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5442 | if (bom == 0xFEFF) { |
| 5443 | q += 2; |
| 5444 | bo = -1; |
| 5445 | } |
| 5446 | else if (bom == 0xFFFE) { |
| 5447 | q += 2; |
| 5448 | bo = 1; |
| 5449 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5450 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5451 | if (bom == 0xFEFF) { |
| 5452 | q += 2; |
| 5453 | bo = 1; |
| 5454 | } |
| 5455 | else if (bom == 0xFFFE) { |
| 5456 | q += 2; |
| 5457 | bo = -1; |
| 5458 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5459 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5460 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5461 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5462 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5463 | if (bo == -1) { |
| 5464 | /* force LE */ |
| 5465 | ihi = 1; |
| 5466 | ilo = 0; |
| 5467 | } |
| 5468 | else if (bo == 1) { |
| 5469 | /* force BE */ |
| 5470 | ihi = 0; |
| 5471 | ilo = 1; |
| 5472 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5473 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5474 | native_ordering = ilo < ihi; |
| 5475 | #else |
| 5476 | native_ordering = ilo > ihi; |
| 5477 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5478 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5479 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5480 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5481 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5482 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5483 | reads are more expensive, better to defer to another iteration. */ |
| 5484 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5485 | /* Fast path for runs of non-surrogate chars. */ |
| 5486 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5487 | int kind = PyUnicode_KIND(unicode); |
| 5488 | void *data = PyUnicode_DATA(unicode); |
| 5489 | while (_q < aligned_end) { |
| 5490 | unsigned long block = * (unsigned long *) _q; |
| 5491 | unsigned short *pblock = (unsigned short*)█ |
| 5492 | Py_UCS4 maxch; |
| 5493 | if (native_ordering) { |
| 5494 | /* Can use buffer directly */ |
| 5495 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5496 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5497 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5498 | else { |
| 5499 | /* Need to byte-swap */ |
| 5500 | unsigned char *_p = (unsigned char*)pblock; |
| 5501 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5502 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5503 | _p[0] = _q[1]; |
| 5504 | _p[1] = _q[0]; |
| 5505 | _p[2] = _q[3]; |
| 5506 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5507 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5508 | _p[4] = _q[5]; |
| 5509 | _p[5] = _q[4]; |
| 5510 | _p[6] = _q[7]; |
| 5511 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5512 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5513 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5514 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5515 | #if SIZEOF_LONG == 8 |
| 5516 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5517 | #endif |
| 5518 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5519 | if (unicode_widen(&unicode, maxch) < 0) |
| 5520 | goto onError; |
| 5521 | kind = PyUnicode_KIND(unicode); |
| 5522 | data = PyUnicode_DATA(unicode); |
| 5523 | } |
| 5524 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5525 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5526 | #if SIZEOF_LONG == 8 |
| 5527 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5528 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5529 | #endif |
| 5530 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5531 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5532 | q = _q; |
| 5533 | if (q >= e) |
| 5534 | break; |
| 5535 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5536 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5537 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5538 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5540 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5541 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5542 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5543 | continue; |
| 5544 | } |
| 5545 | |
| 5546 | /* UTF-16 code pair: */ |
| 5547 | if (q > e) { |
| 5548 | errmsg = "unexpected end of data"; |
| 5549 | startinpos = (((const char *)q) - 2) - starts; |
| 5550 | endinpos = ((const char *)e) + 1 - starts; |
| 5551 | goto utf16Error; |
| 5552 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5553 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5554 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5555 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5556 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5557 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5558 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5559 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5560 | continue; |
| 5561 | } |
| 5562 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5563 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5564 | startinpos = (((const char *)q)-4)-starts; |
| 5565 | endinpos = startinpos+2; |
| 5566 | goto utf16Error; |
| 5567 | } |
| 5568 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5569 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5570 | errmsg = "illegal encoding"; |
| 5571 | startinpos = (((const char *)q)-2)-starts; |
| 5572 | endinpos = startinpos+2; |
| 5573 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5574 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5575 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5577 | errors, |
| 5578 | &errorHandler, |
| 5579 | "utf16", errmsg, |
| 5580 | &starts, |
| 5581 | (const char **)&e, |
| 5582 | &startinpos, |
| 5583 | &endinpos, |
| 5584 | &exc, |
| 5585 | (const char **)&q, |
| 5586 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5587 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5588 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5590 | /* remaining byte at the end? (size should be even) */ |
| 5591 | if (e == q) { |
| 5592 | if (!consumed) { |
| 5593 | errmsg = "truncated data"; |
| 5594 | startinpos = ((const char *)q) - starts; |
| 5595 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5596 | if (unicode_decode_call_errorhandler( |
| 5597 | errors, |
| 5598 | &errorHandler, |
| 5599 | "utf16", errmsg, |
| 5600 | &starts, |
| 5601 | (const char **)&e, |
| 5602 | &startinpos, |
| 5603 | &endinpos, |
| 5604 | &exc, |
| 5605 | (const char **)&q, |
| 5606 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5607 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5608 | goto onError; |
| 5609 | /* The remaining input chars are ignored if the callback |
| 5610 | chooses to skip the input */ |
| 5611 | } |
| 5612 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | |
| 5614 | if (byteorder) |
| 5615 | *byteorder = bo; |
| 5616 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5617 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5618 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5621 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | goto onError; |
| 5623 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5624 | Py_XDECREF(errorHandler); |
| 5625 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5626 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5630 | Py_XDECREF(errorHandler); |
| 5631 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | return NULL; |
| 5633 | } |
| 5634 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5635 | #undef FAST_CHAR_MASK |
| 5636 | #undef SWAPPED_FAST_CHAR_MASK |
| 5637 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5638 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5639 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5640 | const char *errors, |
| 5641 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5643 | int kind; |
| 5644 | void *data; |
| 5645 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5646 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5647 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5648 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5649 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5650 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5651 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5652 | int ihi = 1, ilo = 0; |
| 5653 | #else |
| 5654 | int ihi = 0, ilo = 1; |
| 5655 | #endif |
| 5656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | #define STORECHAR(CH) \ |
| 5658 | do { \ |
| 5659 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5660 | p[ilo] = (CH) & 0xff; \ |
| 5661 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5662 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5664 | if (!PyUnicode_Check(str)) { |
| 5665 | PyErr_BadArgument(); |
| 5666 | return NULL; |
| 5667 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5668 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5669 | return NULL; |
| 5670 | kind = PyUnicode_KIND(str); |
| 5671 | data = PyUnicode_DATA(str); |
| 5672 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5673 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5674 | pairs = 0; |
| 5675 | if (kind == PyUnicode_4BYTE_KIND) |
| 5676 | for (i = 0; i < len; i++) |
| 5677 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5678 | pairs++; |
| 5679 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5680 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5681 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5682 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5683 | bytesize = nsize * 2; |
| 5684 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5685 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5686 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | if (v == NULL) |
| 5688 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5690 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5693 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5694 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5695 | |
| 5696 | if (byteorder == -1) { |
| 5697 | /* force LE */ |
| 5698 | ihi = 1; |
| 5699 | ilo = 0; |
| 5700 | } |
| 5701 | else if (byteorder == 1) { |
| 5702 | /* force BE */ |
| 5703 | ihi = 0; |
| 5704 | ilo = 1; |
| 5705 | } |
| 5706 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5707 | for (i = 0; i < len; i++) { |
| 5708 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5709 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5710 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5711 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5712 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5713 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5714 | STORECHAR(ch); |
| 5715 | if (ch2) |
| 5716 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5717 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5718 | |
| 5719 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5720 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5721 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5722 | } |
| 5723 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5724 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5725 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5726 | Py_ssize_t size, |
| 5727 | const char *errors, |
| 5728 | int byteorder) |
| 5729 | { |
| 5730 | PyObject *result; |
| 5731 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5732 | if (tmp == NULL) |
| 5733 | return NULL; |
| 5734 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5735 | Py_DECREF(tmp); |
| 5736 | return result; |
| 5737 | } |
| 5738 | |
| 5739 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5740 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5742 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
| 5745 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5747 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5748 | if all the escapes in the string make it still a valid ASCII string. |
| 5749 | Returns -1 if any escapes were found which cause the string to |
| 5750 | pop out of ASCII range. Otherwise returns the length of the |
| 5751 | required buffer to hold the string. |
| 5752 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5753 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5754 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5755 | { |
| 5756 | const unsigned char *p = (const unsigned char *)s; |
| 5757 | const unsigned char *end = p + size; |
| 5758 | Py_ssize_t length = 0; |
| 5759 | |
| 5760 | if (size < 0) |
| 5761 | return -1; |
| 5762 | |
| 5763 | for (; p < end; ++p) { |
| 5764 | if (*p > 127) { |
| 5765 | /* Non-ASCII */ |
| 5766 | return -1; |
| 5767 | } |
| 5768 | else if (*p != '\\') { |
| 5769 | /* Normal character */ |
| 5770 | ++length; |
| 5771 | } |
| 5772 | else { |
| 5773 | /* Backslash-escape, check next char */ |
| 5774 | ++p; |
| 5775 | /* Escape sequence reaches till end of string or |
| 5776 | non-ASCII follow-up. */ |
| 5777 | if (p >= end || *p > 127) |
| 5778 | return -1; |
| 5779 | switch (*p) { |
| 5780 | case '\n': |
| 5781 | /* backslash + \n result in zero characters */ |
| 5782 | break; |
| 5783 | case '\\': case '\'': case '\"': |
| 5784 | case 'b': case 'f': case 't': |
| 5785 | case 'n': case 'r': case 'v': case 'a': |
| 5786 | ++length; |
| 5787 | break; |
| 5788 | case '0': case '1': case '2': case '3': |
| 5789 | case '4': case '5': case '6': case '7': |
| 5790 | case 'x': case 'u': case 'U': case 'N': |
| 5791 | /* these do not guarantee ASCII characters */ |
| 5792 | return -1; |
| 5793 | default: |
| 5794 | /* count the backslash + the other character */ |
| 5795 | length += 2; |
| 5796 | } |
| 5797 | } |
| 5798 | } |
| 5799 | return length; |
| 5800 | } |
| 5801 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5802 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5803 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5804 | PyObject * |
| 5805 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5806 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5807 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5809 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5810 | Py_ssize_t startinpos; |
| 5811 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5812 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5813 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5815 | char* message; |
| 5816 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5817 | PyObject *errorHandler = NULL; |
| 5818 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5819 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5820 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5821 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5822 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5823 | |
| 5824 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5825 | either the string is pure ASCII with named escapes like \n, etc. |
| 5826 | and we determined it's exact size (common case) |
| 5827 | or it contains \x, \u, ... escape sequences. then we create a |
| 5828 | 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] | 5829 | if (len >= 0) { |
| 5830 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5831 | if (!v) |
| 5832 | goto onError; |
| 5833 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5834 | } |
| 5835 | else { |
| 5836 | /* Escaped strings will always be longer than the resulting |
| 5837 | Unicode string, so we start with size here and then reduce the |
| 5838 | length after conversion to the true value. |
| 5839 | (but if the error callback returns a long replacement string |
| 5840 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5841 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5842 | if (!v) |
| 5843 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5844 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5845 | } |
| 5846 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5847 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5848 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5849 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5852 | while (s < end) { |
| 5853 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5854 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5855 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5857 | /* The only case in which i == ascii_length is a backslash |
| 5858 | followed by a newline. */ |
| 5859 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5860 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5862 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5863 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5864 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | continue; |
| 5866 | } |
| 5867 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5868 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | /* \ - Escapes */ |
| 5870 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5871 | c = *s++; |
| 5872 | if (s > end) |
| 5873 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5874 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5875 | /* The only case in which i == ascii_length is a backslash |
| 5876 | followed by a newline. */ |
| 5877 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5878 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5879 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5881 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5882 | #define WRITECHAR(ch) \ |
| 5883 | do { \ |
| 5884 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5885 | goto onError; \ |
| 5886 | }while(0) |
| 5887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5889 | case '\\': WRITECHAR('\\'); break; |
| 5890 | case '\'': WRITECHAR('\''); break; |
| 5891 | case '\"': WRITECHAR('\"'); break; |
| 5892 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5893 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5894 | case 'f': WRITECHAR('\014'); break; |
| 5895 | case 't': WRITECHAR('\t'); break; |
| 5896 | case 'n': WRITECHAR('\n'); break; |
| 5897 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5898 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5899 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5900 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5901 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5904 | case '0': case '1': case '2': case '3': |
| 5905 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5906 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5907 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5908 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5909 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5910 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5912 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | break; |
| 5914 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5915 | /* hex escapes */ |
| 5916 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5918 | digits = 2; |
| 5919 | message = "truncated \\xXX escape"; |
| 5920 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5922 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5924 | digits = 4; |
| 5925 | message = "truncated \\uXXXX escape"; |
| 5926 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5928 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5929 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5930 | digits = 8; |
| 5931 | message = "truncated \\UXXXXXXXX escape"; |
| 5932 | hexescape: |
| 5933 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5934 | if (s+digits>end) { |
| 5935 | endinpos = size; |
| 5936 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5937 | errors, &errorHandler, |
| 5938 | "unicodeescape", "end of string in escape sequence", |
| 5939 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5940 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5941 | goto onError; |
| 5942 | goto nextByte; |
| 5943 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5944 | for (j = 0; j < digits; ++j) { |
| 5945 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5946 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5947 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5948 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5949 | errors, &errorHandler, |
| 5950 | "unicodeescape", message, |
| 5951 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5952 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5953 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5954 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5955 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5956 | } |
| 5957 | chr = (chr<<4) & ~0xF; |
| 5958 | if (c >= '0' && c <= '9') |
| 5959 | chr += c - '0'; |
| 5960 | else if (c >= 'a' && c <= 'f') |
| 5961 | chr += 10 + c - 'a'; |
| 5962 | else |
| 5963 | chr += 10 + c - 'A'; |
| 5964 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5965 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5966 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5967 | /* _decoding_error will have already written into the |
| 5968 | target buffer. */ |
| 5969 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5970 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5971 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5972 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5973 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5974 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5975 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5976 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5977 | errors, &errorHandler, |
| 5978 | "unicodeescape", "illegal Unicode character", |
| 5979 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5980 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5981 | goto onError; |
| 5982 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5983 | break; |
| 5984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5986 | case 'N': |
| 5987 | message = "malformed \\N character escape"; |
| 5988 | if (ucnhash_CAPI == NULL) { |
| 5989 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5990 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5991 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5992 | if (ucnhash_CAPI == NULL) |
| 5993 | goto ucnhashError; |
| 5994 | } |
| 5995 | if (*s == '{') { |
| 5996 | const char *start = s+1; |
| 5997 | /* look for the closing brace */ |
| 5998 | while (*s != '}' && s < end) |
| 5999 | s++; |
| 6000 | if (s > start && s < end && *s == '}') { |
| 6001 | /* found a name. look it up in the unicode database */ |
| 6002 | message = "unknown Unicode character name"; |
| 6003 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6004 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6005 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6006 | goto store; |
| 6007 | } |
| 6008 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6009 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6010 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6011 | errors, &errorHandler, |
| 6012 | "unicodeescape", message, |
| 6013 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6014 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6015 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6016 | break; |
| 6017 | |
| 6018 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6019 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6020 | message = "\\ at end of string"; |
| 6021 | s--; |
| 6022 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6023 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6024 | errors, &errorHandler, |
| 6025 | "unicodeescape", message, |
| 6026 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6027 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6028 | goto onError; |
| 6029 | } |
| 6030 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6031 | WRITECHAR('\\'); |
| 6032 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6033 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6034 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6036 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6037 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6039 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6040 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6041 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6042 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6043 | Py_XDECREF(errorHandler); |
| 6044 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6045 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6048 | PyErr_SetString( |
| 6049 | PyExc_UnicodeError, |
| 6050 | "\\N escapes not supported (can't load unicodedata module)" |
| 6051 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6052 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6053 | Py_XDECREF(errorHandler); |
| 6054 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6055 | return NULL; |
| 6056 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6057 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6059 | Py_XDECREF(errorHandler); |
| 6060 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | return NULL; |
| 6062 | } |
| 6063 | |
| 6064 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6065 | |
| 6066 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6067 | appropriate. |
| 6068 | |
| 6069 | */ |
| 6070 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6071 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6072 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6074 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6075 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6077 | int kind; |
| 6078 | void *data; |
| 6079 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6081 | /* Initial allocation is based on the longest-possible unichr |
| 6082 | escape. |
| 6083 | |
| 6084 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6085 | unichr, so in this case it's the longest unichr escape. In |
| 6086 | narrow (UTF-16) builds this is five chars per source unichr |
| 6087 | since there are two unichrs in the surrogate pair, so in narrow |
| 6088 | (UTF-16) builds it's not the longest unichr escape. |
| 6089 | |
| 6090 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6091 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6092 | escape. |
| 6093 | */ |
| 6094 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6095 | if (!PyUnicode_Check(unicode)) { |
| 6096 | PyErr_BadArgument(); |
| 6097 | return NULL; |
| 6098 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6099 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6100 | return NULL; |
| 6101 | len = PyUnicode_GET_LENGTH(unicode); |
| 6102 | kind = PyUnicode_KIND(unicode); |
| 6103 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6104 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6105 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6106 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6107 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6108 | } |
| 6109 | |
| 6110 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6111 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6112 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6113 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6114 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6115 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6116 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6117 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6118 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | if (repr == NULL) |
| 6121 | return NULL; |
| 6122 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6123 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6125 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6126 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6127 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6128 | /* Escape backslashes */ |
| 6129 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | *p++ = '\\'; |
| 6131 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6132 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6133 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6134 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6135 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6136 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6137 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6138 | *p++ = '\\'; |
| 6139 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6140 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6141 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6142 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6143 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6144 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6145 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6146 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6147 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6148 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6149 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6152 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | *p++ = '\\'; |
| 6154 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6155 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6156 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6157 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6158 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6160 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6161 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6162 | else if (ch == '\t') { |
| 6163 | *p++ = '\\'; |
| 6164 | *p++ = 't'; |
| 6165 | } |
| 6166 | else if (ch == '\n') { |
| 6167 | *p++ = '\\'; |
| 6168 | *p++ = 'n'; |
| 6169 | } |
| 6170 | else if (ch == '\r') { |
| 6171 | *p++ = '\\'; |
| 6172 | *p++ = 'r'; |
| 6173 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6174 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6175 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6176 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6177 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6178 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6179 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6180 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6181 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6182 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | /* Copy everything else as-is */ |
| 6184 | else |
| 6185 | *p++ = (char) ch; |
| 6186 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6188 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6189 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6190 | return NULL; |
| 6191 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | } |
| 6193 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6194 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6195 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6196 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6198 | PyObject *result; |
| 6199 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6200 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6202 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6203 | Py_DECREF(tmp); |
| 6204 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6208 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6209 | PyObject * |
| 6210 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6211 | Py_ssize_t size, |
| 6212 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6214 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6215 | Py_ssize_t startinpos; |
| 6216 | Py_ssize_t endinpos; |
| 6217 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6218 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6219 | const char *end; |
| 6220 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6221 | PyObject *errorHandler = NULL; |
| 6222 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | /* Escaped strings will always be longer than the resulting |
| 6225 | 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] | 6226 | length after conversion to the true value. (But decoding error |
| 6227 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6228 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6232 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6233 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | end = s + size; |
| 6235 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | unsigned char c; |
| 6237 | Py_UCS4 x; |
| 6238 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6239 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6241 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6242 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6243 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6244 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6245 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6246 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6247 | startinpos = s-starts; |
| 6248 | |
| 6249 | /* \u-escapes are only interpreted iff the number of leading |
| 6250 | backslashes if odd */ |
| 6251 | bs = s; |
| 6252 | for (;s < end;) { |
| 6253 | if (*s != '\\') |
| 6254 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6255 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6256 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | } |
| 6258 | if (((s - bs) & 1) == 0 || |
| 6259 | s >= end || |
| 6260 | (*s != 'u' && *s != 'U')) { |
| 6261 | continue; |
| 6262 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6263 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6264 | count = *s=='u' ? 4 : 8; |
| 6265 | s++; |
| 6266 | |
| 6267 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6269 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6270 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6271 | endinpos = s-starts; |
| 6272 | if (unicode_decode_call_errorhandler( |
| 6273 | errors, &errorHandler, |
| 6274 | "rawunicodeescape", "truncated \\uXXXX", |
| 6275 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6276 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | goto onError; |
| 6278 | goto nextByte; |
| 6279 | } |
| 6280 | x = (x<<4) & ~0xF; |
| 6281 | if (c >= '0' && c <= '9') |
| 6282 | x += c - '0'; |
| 6283 | else if (c >= 'a' && c <= 'f') |
| 6284 | x += 10 + c - 'a'; |
| 6285 | else |
| 6286 | x += 10 + c - 'A'; |
| 6287 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6288 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6289 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6290 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6291 | } else { |
| 6292 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6293 | if (unicode_decode_call_errorhandler( |
| 6294 | errors, &errorHandler, |
| 6295 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6297 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6299 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | nextByte: |
| 6301 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6303 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6304 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6305 | Py_XDECREF(errorHandler); |
| 6306 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6307 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6308 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6309 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6311 | Py_XDECREF(errorHandler); |
| 6312 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | return NULL; |
| 6314 | } |
| 6315 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6316 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6317 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6318 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6319 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6320 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | char *p; |
| 6322 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6323 | Py_ssize_t expandsize, pos; |
| 6324 | int kind; |
| 6325 | void *data; |
| 6326 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6328 | if (!PyUnicode_Check(unicode)) { |
| 6329 | PyErr_BadArgument(); |
| 6330 | return NULL; |
| 6331 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6332 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6333 | return NULL; |
| 6334 | kind = PyUnicode_KIND(unicode); |
| 6335 | data = PyUnicode_DATA(unicode); |
| 6336 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6337 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6338 | bytes, and 1 byte characters 4. */ |
| 6339 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6340 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6341 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6342 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6343 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6344 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | if (repr == NULL) |
| 6346 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6347 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6348 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6349 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6350 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6351 | for (pos = 0; pos < len; pos++) { |
| 6352 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6354 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6355 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6356 | *p++ = '\\'; |
| 6357 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6358 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6359 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6360 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6361 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6362 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6363 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6364 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6365 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6366 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6367 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6368 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | *p++ = '\\'; |
| 6370 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6371 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6372 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6373 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6374 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6376 | /* Copy everything else as-is */ |
| 6377 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | *p++ = (char) ch; |
| 6379 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6380 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6381 | assert(p > q); |
| 6382 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6383 | return NULL; |
| 6384 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6385 | } |
| 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_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6389 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6391 | PyObject *result; |
| 6392 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6393 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6394 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6395 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6396 | Py_DECREF(tmp); |
| 6397 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6400 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6402 | PyObject * |
| 6403 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6404 | Py_ssize_t size, |
| 6405 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6406 | { |
| 6407 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6408 | Py_ssize_t startinpos; |
| 6409 | Py_ssize_t endinpos; |
| 6410 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6411 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6412 | const char *end; |
| 6413 | const char *reason; |
| 6414 | PyObject *errorHandler = NULL; |
| 6415 | PyObject *exc = NULL; |
| 6416 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6417 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6418 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6419 | 1)) |
| 6420 | return NULL; |
| 6421 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6422 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6423 | 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] | 6424 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6425 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6426 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6427 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6428 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6429 | end = s + size; |
| 6430 | |
| 6431 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6432 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6433 | Py_UCS4 ch; |
| 6434 | /* We copy the raw representation one byte at a time because the |
| 6435 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6436 | ((char *) &uch)[0] = s[0]; |
| 6437 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6438 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6439 | ((char *) &uch)[2] = s[2]; |
| 6440 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6441 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6442 | ch = uch; |
| 6443 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6444 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6445 | some malformed UCS-4 data. */ |
| 6446 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6447 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6448 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6449 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6450 | end-s < Py_UNICODE_SIZE |
| 6451 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6452 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6453 | startinpos = s - starts; |
| 6454 | if (end-s < Py_UNICODE_SIZE) { |
| 6455 | endinpos = end-starts; |
| 6456 | reason = "truncated input"; |
| 6457 | } |
| 6458 | else { |
| 6459 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6460 | reason = "illegal code point (> 0x10FFFF)"; |
| 6461 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6462 | if (unicode_decode_call_errorhandler( |
| 6463 | errors, &errorHandler, |
| 6464 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6465 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6466 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6467 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6468 | continue; |
| 6469 | } |
| 6470 | |
| 6471 | s += Py_UNICODE_SIZE; |
| 6472 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6473 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6474 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6475 | Py_UNICODE uch2; |
| 6476 | ((char *) &uch2)[0] = s[0]; |
| 6477 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6478 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6479 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6480 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6481 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6482 | } |
| 6483 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6484 | #endif |
| 6485 | |
| 6486 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6487 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6490 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6491 | goto onError; |
| 6492 | Py_XDECREF(errorHandler); |
| 6493 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6494 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6495 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6496 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6497 | Py_XDECREF(v); |
| 6498 | Py_XDECREF(errorHandler); |
| 6499 | Py_XDECREF(exc); |
| 6500 | return NULL; |
| 6501 | } |
| 6502 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6504 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6505 | PyObject * |
| 6506 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6507 | Py_ssize_t size, |
| 6508 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6511 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6514 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6515 | static void |
| 6516 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6517 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6518 | PyObject *unicode, |
| 6519 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6520 | const char *reason) |
| 6521 | { |
| 6522 | if (*exceptionObject == NULL) { |
| 6523 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6524 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6525 | encoding, unicode, startpos, endpos, reason); |
| 6526 | } |
| 6527 | else { |
| 6528 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6529 | goto onError; |
| 6530 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6531 | goto onError; |
| 6532 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6533 | goto onError; |
| 6534 | return; |
| 6535 | onError: |
| 6536 | Py_DECREF(*exceptionObject); |
| 6537 | *exceptionObject = NULL; |
| 6538 | } |
| 6539 | } |
| 6540 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6541 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6542 | static void |
| 6543 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6544 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6545 | PyObject *unicode, |
| 6546 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6547 | const char *reason) |
| 6548 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6549 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6550 | encoding, unicode, startpos, endpos, reason); |
| 6551 | if (*exceptionObject != NULL) |
| 6552 | PyCodec_StrictErrors(*exceptionObject); |
| 6553 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6554 | |
| 6555 | /* error handling callback helper: |
| 6556 | build arguments, call the callback and check the arguments, |
| 6557 | put the result into newpos and return the replacement string, which |
| 6558 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6559 | static PyObject * |
| 6560 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6561 | PyObject **errorHandler, |
| 6562 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6563 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6564 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6565 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6566 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6567 | 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] | 6568 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6569 | PyObject *restuple; |
| 6570 | PyObject *resunicode; |
| 6571 | |
| 6572 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6574 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6575 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6576 | } |
| 6577 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6578 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6579 | return NULL; |
| 6580 | len = PyUnicode_GET_LENGTH(unicode); |
| 6581 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6582 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6583 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6584 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6585 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6586 | |
| 6587 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6588 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6589 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6591 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6592 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | Py_DECREF(restuple); |
| 6594 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6595 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6596 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6597 | &resunicode, newpos)) { |
| 6598 | Py_DECREF(restuple); |
| 6599 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6600 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6601 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6602 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6603 | Py_DECREF(restuple); |
| 6604 | return NULL; |
| 6605 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6606 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6607 | *newpos = len + *newpos; |
| 6608 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6609 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6610 | Py_DECREF(restuple); |
| 6611 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6612 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6613 | Py_INCREF(resunicode); |
| 6614 | Py_DECREF(restuple); |
| 6615 | return resunicode; |
| 6616 | } |
| 6617 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6618 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6619 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6620 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6621 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6622 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6623 | /* input state */ |
| 6624 | Py_ssize_t pos=0, size; |
| 6625 | int kind; |
| 6626 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6627 | /* output object */ |
| 6628 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6629 | /* pointer into the output */ |
| 6630 | char *str; |
| 6631 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6632 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6633 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6634 | 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] | 6635 | PyObject *errorHandler = NULL; |
| 6636 | PyObject *exc = NULL; |
| 6637 | /* the following variable is used for caching string comparisons |
| 6638 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6639 | int known_errorHandler = -1; |
| 6640 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6641 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6642 | return NULL; |
| 6643 | size = PyUnicode_GET_LENGTH(unicode); |
| 6644 | kind = PyUnicode_KIND(unicode); |
| 6645 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6646 | /* allocate enough for a simple encoding without |
| 6647 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6648 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6649 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6650 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6651 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6652 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6653 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6654 | ressize = size; |
| 6655 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6656 | while (pos < size) { |
| 6657 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6658 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6659 | /* can we encode this? */ |
| 6660 | if (c<limit) { |
| 6661 | /* no overflow check, because we know that the space is enough */ |
| 6662 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6663 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6664 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6665 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6666 | Py_ssize_t requiredsize; |
| 6667 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6668 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6669 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6670 | Py_ssize_t collstart = pos; |
| 6671 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6672 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6673 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6674 | ++collend; |
| 6675 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6676 | if (known_errorHandler==-1) { |
| 6677 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6678 | known_errorHandler = 1; |
| 6679 | else if (!strcmp(errors, "replace")) |
| 6680 | known_errorHandler = 2; |
| 6681 | else if (!strcmp(errors, "ignore")) |
| 6682 | known_errorHandler = 3; |
| 6683 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6684 | known_errorHandler = 4; |
| 6685 | else |
| 6686 | known_errorHandler = 0; |
| 6687 | } |
| 6688 | switch (known_errorHandler) { |
| 6689 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6690 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6691 | goto onError; |
| 6692 | case 2: /* replace */ |
| 6693 | while (collstart++<collend) |
| 6694 | *str++ = '?'; /* fall through */ |
| 6695 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6696 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6697 | break; |
| 6698 | case 4: /* xmlcharrefreplace */ |
| 6699 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6700 | /* determine replacement size */ |
| 6701 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6702 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6703 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6704 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6705 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6706 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6707 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6708 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6709 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6710 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6711 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6712 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6713 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6714 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6715 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6716 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6717 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6718 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6719 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6720 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6721 | if (requiredsize > ressize) { |
| 6722 | if (requiredsize<2*ressize) |
| 6723 | requiredsize = 2*ressize; |
| 6724 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6725 | goto onError; |
| 6726 | str = PyBytes_AS_STRING(res) + respos; |
| 6727 | ressize = requiredsize; |
| 6728 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6729 | /* generate replacement */ |
| 6730 | for (i = collstart; i < collend; ++i) { |
| 6731 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6732 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6733 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6734 | break; |
| 6735 | default: |
| 6736 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6737 | encoding, reason, unicode, &exc, |
| 6738 | collstart, collend, &newpos); |
| 6739 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6740 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6742 | if (PyBytes_Check(repunicode)) { |
| 6743 | /* Directly copy bytes result to output. */ |
| 6744 | repsize = PyBytes_Size(repunicode); |
| 6745 | if (repsize > 1) { |
| 6746 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6747 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6748 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6749 | Py_DECREF(repunicode); |
| 6750 | goto onError; |
| 6751 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6752 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6753 | ressize += repsize-1; |
| 6754 | } |
| 6755 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6756 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6757 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6758 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6759 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6760 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6761 | /* need more space? (at least enough for what we |
| 6762 | have+the replacement+the rest of the string, so |
| 6763 | we won't have to check space for encodable characters) */ |
| 6764 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6765 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6766 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | if (requiredsize > ressize) { |
| 6768 | if (requiredsize<2*ressize) |
| 6769 | requiredsize = 2*ressize; |
| 6770 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6771 | Py_DECREF(repunicode); |
| 6772 | goto onError; |
| 6773 | } |
| 6774 | str = PyBytes_AS_STRING(res) + respos; |
| 6775 | ressize = requiredsize; |
| 6776 | } |
| 6777 | /* check if there is anything unencodable in the replacement |
| 6778 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6779 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6780 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6781 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6782 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6783 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6784 | Py_DECREF(repunicode); |
| 6785 | goto onError; |
| 6786 | } |
| 6787 | *str = (char)c; |
| 6788 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6789 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6790 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6791 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6792 | } |
| 6793 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6794 | /* Resize if we allocated to much */ |
| 6795 | size = str - PyBytes_AS_STRING(res); |
| 6796 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6797 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6798 | if (_PyBytes_Resize(&res, size) < 0) |
| 6799 | goto onError; |
| 6800 | } |
| 6801 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6802 | Py_XDECREF(errorHandler); |
| 6803 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6804 | return res; |
| 6805 | |
| 6806 | onError: |
| 6807 | Py_XDECREF(res); |
| 6808 | Py_XDECREF(errorHandler); |
| 6809 | Py_XDECREF(exc); |
| 6810 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6813 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6814 | PyObject * |
| 6815 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6816 | Py_ssize_t size, |
| 6817 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6819 | PyObject *result; |
| 6820 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6821 | if (unicode == NULL) |
| 6822 | return NULL; |
| 6823 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6824 | Py_DECREF(unicode); |
| 6825 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6828 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6829 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | { |
| 6831 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6832 | PyErr_BadArgument(); |
| 6833 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6835 | if (PyUnicode_READY(unicode) == -1) |
| 6836 | return NULL; |
| 6837 | /* Fast path: if it is a one-byte string, construct |
| 6838 | bytes object directly. */ |
| 6839 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6840 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6841 | PyUnicode_GET_LENGTH(unicode)); |
| 6842 | /* Non-Latin-1 characters present. Defer to above function to |
| 6843 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6844 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6845 | } |
| 6846 | |
| 6847 | PyObject* |
| 6848 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6849 | { |
| 6850 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | } |
| 6852 | |
| 6853 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6854 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6855 | PyObject * |
| 6856 | PyUnicode_DecodeASCII(const char *s, |
| 6857 | Py_ssize_t size, |
| 6858 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6860 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6861 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6862 | int kind; |
| 6863 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6864 | Py_ssize_t startinpos; |
| 6865 | Py_ssize_t endinpos; |
| 6866 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6867 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6868 | int has_error; |
| 6869 | const unsigned char *p = (const unsigned char *)s; |
| 6870 | const unsigned char *end = p + size; |
| 6871 | 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] | 6872 | PyObject *errorHandler = NULL; |
| 6873 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6874 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6875 | if (size == 0) { |
| 6876 | Py_INCREF(unicode_empty); |
| 6877 | return unicode_empty; |
| 6878 | } |
| 6879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6880 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6881 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6882 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6883 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6884 | has_error = 0; |
| 6885 | while (p < end && !has_error) { |
| 6886 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6887 | an explanation. */ |
| 6888 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6889 | /* Help register allocation */ |
| 6890 | register const unsigned char *_p = p; |
| 6891 | while (_p < aligned_end) { |
| 6892 | unsigned long value = *(unsigned long *) _p; |
| 6893 | if (value & ASCII_CHAR_MASK) { |
| 6894 | has_error = 1; |
| 6895 | break; |
| 6896 | } |
| 6897 | _p += SIZEOF_LONG; |
| 6898 | } |
| 6899 | if (_p == end) |
| 6900 | break; |
| 6901 | if (has_error) |
| 6902 | break; |
| 6903 | p = _p; |
| 6904 | } |
| 6905 | if (*p & 0x80) { |
| 6906 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6907 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6908 | } |
| 6909 | else { |
| 6910 | ++p; |
| 6911 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6912 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6913 | if (!has_error) |
| 6914 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6915 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6916 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6918 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6920 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6921 | kind = PyUnicode_KIND(v); |
| 6922 | data = PyUnicode_DATA(v); |
| 6923 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6924 | e = s + size; |
| 6925 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6926 | register unsigned char c = (unsigned char)*s; |
| 6927 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6928 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6929 | ++s; |
| 6930 | } |
| 6931 | else { |
| 6932 | startinpos = s-starts; |
| 6933 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6934 | if (unicode_decode_call_errorhandler( |
| 6935 | errors, &errorHandler, |
| 6936 | "ascii", "ordinal not in range(128)", |
| 6937 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6938 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6939 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6940 | kind = PyUnicode_KIND(v); |
| 6941 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6942 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6944 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6945 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6946 | Py_XDECREF(errorHandler); |
| 6947 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6948 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6949 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6951 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6952 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6953 | Py_XDECREF(errorHandler); |
| 6954 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6955 | return NULL; |
| 6956 | } |
| 6957 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6958 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6959 | PyObject * |
| 6960 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6961 | Py_ssize_t size, |
| 6962 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6964 | PyObject *result; |
| 6965 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6966 | if (unicode == NULL) |
| 6967 | return NULL; |
| 6968 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6969 | Py_DECREF(unicode); |
| 6970 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6973 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6974 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | { |
| 6976 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6977 | PyErr_BadArgument(); |
| 6978 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6980 | if (PyUnicode_READY(unicode) == -1) |
| 6981 | return NULL; |
| 6982 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6983 | directly. Else defer to above function to raise the exception. */ |
| 6984 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6985 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6986 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6987 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6988 | } |
| 6989 | |
| 6990 | PyObject * |
| 6991 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6992 | { |
| 6993 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6996 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6997 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6998 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6999 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 7000 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7001 | #define NEED_RETRY |
| 7002 | #endif |
| 7003 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7004 | #ifndef WC_ERR_INVALID_CHARS |
| 7005 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 7006 | #endif |
| 7007 | |
| 7008 | static char* |
| 7009 | code_page_name(UINT code_page, PyObject **obj) |
| 7010 | { |
| 7011 | *obj = NULL; |
| 7012 | if (code_page == CP_ACP) |
| 7013 | return "mbcs"; |
| 7014 | if (code_page == CP_UTF7) |
| 7015 | return "CP_UTF7"; |
| 7016 | if (code_page == CP_UTF8) |
| 7017 | return "CP_UTF8"; |
| 7018 | |
| 7019 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7020 | if (*obj == NULL) |
| 7021 | return NULL; |
| 7022 | return PyBytes_AS_STRING(*obj); |
| 7023 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7024 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7025 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7027 | { |
| 7028 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7029 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7030 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7031 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 7032 | return 0; |
| 7033 | |
| 7034 | prev = CharPrevExA(code_page, s, curr, 0); |
| 7035 | if (prev == curr) |
| 7036 | return 1; |
| 7037 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 7038 | as it assumes an incomplete character consists of a single |
| 7039 | byte. */ |
| 7040 | if (curr - prev == 2) |
| 7041 | return 1; |
| 7042 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 7043 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7044 | return 0; |
| 7045 | } |
| 7046 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7047 | static DWORD |
| 7048 | decode_code_page_flags(UINT code_page) |
| 7049 | { |
| 7050 | if (code_page == CP_UTF7) { |
| 7051 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7052 | return 0; |
| 7053 | } |
| 7054 | else |
| 7055 | return MB_ERR_INVALID_CHARS; |
| 7056 | } |
| 7057 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7058 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7059 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7060 | * mode. |
| 7061 | * |
| 7062 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7063 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7064 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7065 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7066 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7067 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7068 | const char *in, |
| 7069 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7070 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7072 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7073 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7074 | |
| 7075 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7076 | assert(insize > 0); |
| 7077 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7078 | if (outsize <= 0) |
| 7079 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7080 | |
| 7081 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7082 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7083 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7084 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7085 | if (*v == NULL) |
| 7086 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7087 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7088 | } |
| 7089 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7090 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7091 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7092 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7093 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7094 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7098 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7099 | if (outsize <= 0) |
| 7100 | goto error; |
| 7101 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7102 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7103 | error: |
| 7104 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7105 | return -2; |
| 7106 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7107 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7108 | } |
| 7109 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | /* |
| 7111 | * Decode a byte string from a code page into unicode object with an error |
| 7112 | * handler. |
| 7113 | * |
| 7114 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7115 | * UnicodeDecodeError exception and returns -1 on error. |
| 7116 | */ |
| 7117 | static int |
| 7118 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7119 | PyObject **v, |
| 7120 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7121 | const char *errors) |
| 7122 | { |
| 7123 | const char *startin = in; |
| 7124 | const char *endin = in + size; |
| 7125 | const DWORD flags = decode_code_page_flags(code_page); |
| 7126 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7127 | 2000 English version of the message. */ |
| 7128 | const char *reason = "No mapping for the Unicode character exists " |
| 7129 | "in the target code page."; |
| 7130 | /* each step cannot decode more than 1 character, but a character can be |
| 7131 | represented as a surrogate pair */ |
| 7132 | wchar_t buffer[2], *startout, *out; |
| 7133 | int insize, outsize; |
| 7134 | PyObject *errorHandler = NULL; |
| 7135 | PyObject *exc = NULL; |
| 7136 | PyObject *encoding_obj = NULL; |
| 7137 | char *encoding; |
| 7138 | DWORD err; |
| 7139 | int ret = -1; |
| 7140 | |
| 7141 | assert(size > 0); |
| 7142 | |
| 7143 | encoding = code_page_name(code_page, &encoding_obj); |
| 7144 | if (encoding == NULL) |
| 7145 | return -1; |
| 7146 | |
| 7147 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7148 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7149 | UnicodeDecodeError. */ |
| 7150 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7151 | if (exc != NULL) { |
| 7152 | PyCodec_StrictErrors(exc); |
| 7153 | Py_CLEAR(exc); |
| 7154 | } |
| 7155 | goto error; |
| 7156 | } |
| 7157 | |
| 7158 | if (*v == NULL) { |
| 7159 | /* Create unicode object */ |
| 7160 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7161 | PyErr_NoMemory(); |
| 7162 | goto error; |
| 7163 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7164 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7165 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7166 | if (*v == NULL) |
| 7167 | goto error; |
| 7168 | startout = PyUnicode_AS_UNICODE(*v); |
| 7169 | } |
| 7170 | else { |
| 7171 | /* Extend unicode object */ |
| 7172 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7173 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7174 | PyErr_NoMemory(); |
| 7175 | goto error; |
| 7176 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7177 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7178 | goto error; |
| 7179 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7180 | } |
| 7181 | |
| 7182 | /* Decode the byte string character per character */ |
| 7183 | out = startout; |
| 7184 | while (in < endin) |
| 7185 | { |
| 7186 | /* Decode a character */ |
| 7187 | insize = 1; |
| 7188 | do |
| 7189 | { |
| 7190 | outsize = MultiByteToWideChar(code_page, flags, |
| 7191 | in, insize, |
| 7192 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7193 | if (outsize > 0) |
| 7194 | break; |
| 7195 | err = GetLastError(); |
| 7196 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7197 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7198 | { |
| 7199 | PyErr_SetFromWindowsErr(0); |
| 7200 | goto error; |
| 7201 | } |
| 7202 | insize++; |
| 7203 | } |
| 7204 | /* 4=maximum length of a UTF-8 sequence */ |
| 7205 | while (insize <= 4 && (in + insize) <= endin); |
| 7206 | |
| 7207 | if (outsize <= 0) { |
| 7208 | Py_ssize_t startinpos, endinpos, outpos; |
| 7209 | |
| 7210 | startinpos = in - startin; |
| 7211 | endinpos = startinpos + 1; |
| 7212 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7213 | if (unicode_decode_call_errorhandler( |
| 7214 | errors, &errorHandler, |
| 7215 | encoding, reason, |
| 7216 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7217 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7218 | { |
| 7219 | goto error; |
| 7220 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7221 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | } |
| 7223 | else { |
| 7224 | in += insize; |
| 7225 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7226 | out += outsize; |
| 7227 | } |
| 7228 | } |
| 7229 | |
| 7230 | /* write a NUL character at the end */ |
| 7231 | *out = 0; |
| 7232 | |
| 7233 | /* Extend unicode object */ |
| 7234 | outsize = out - startout; |
| 7235 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7236 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7237 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7238 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7239 | |
| 7240 | error: |
| 7241 | Py_XDECREF(encoding_obj); |
| 7242 | Py_XDECREF(errorHandler); |
| 7243 | Py_XDECREF(exc); |
| 7244 | return ret; |
| 7245 | } |
| 7246 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | static PyObject * |
| 7248 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7249 | const char *s, Py_ssize_t size, |
| 7250 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7251 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7252 | PyObject *v = NULL; |
| 7253 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7254 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7255 | if (code_page < 0) { |
| 7256 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7257 | return NULL; |
| 7258 | } |
| 7259 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7260 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7261 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7262 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7263 | do |
| 7264 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7265 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7266 | if (size > INT_MAX) { |
| 7267 | chunk_size = INT_MAX; |
| 7268 | final = 0; |
| 7269 | done = 0; |
| 7270 | } |
| 7271 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7272 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7273 | { |
| 7274 | chunk_size = (int)size; |
| 7275 | final = (consumed == NULL); |
| 7276 | done = 1; |
| 7277 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7278 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7279 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7280 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7281 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7282 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7283 | if (chunk_size == 0 && done) { |
| 7284 | if (v != NULL) |
| 7285 | break; |
| 7286 | Py_INCREF(unicode_empty); |
| 7287 | return unicode_empty; |
| 7288 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7289 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7290 | |
| 7291 | converted = decode_code_page_strict(code_page, &v, |
| 7292 | s, chunk_size); |
| 7293 | if (converted == -2) |
| 7294 | converted = decode_code_page_errors(code_page, &v, |
| 7295 | s, chunk_size, |
| 7296 | errors); |
| 7297 | assert(converted != 0); |
| 7298 | |
| 7299 | if (converted < 0) { |
| 7300 | Py_XDECREF(v); |
| 7301 | return NULL; |
| 7302 | } |
| 7303 | |
| 7304 | if (consumed) |
| 7305 | *consumed += converted; |
| 7306 | |
| 7307 | s += converted; |
| 7308 | size -= converted; |
| 7309 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7310 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7311 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7312 | } |
| 7313 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7314 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7315 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7316 | const char *s, |
| 7317 | Py_ssize_t size, |
| 7318 | const char *errors, |
| 7319 | Py_ssize_t *consumed) |
| 7320 | { |
| 7321 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7322 | } |
| 7323 | |
| 7324 | PyObject * |
| 7325 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7326 | Py_ssize_t size, |
| 7327 | const char *errors, |
| 7328 | Py_ssize_t *consumed) |
| 7329 | { |
| 7330 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7331 | } |
| 7332 | |
| 7333 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7334 | PyUnicode_DecodeMBCS(const char *s, |
| 7335 | Py_ssize_t size, |
| 7336 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7337 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7338 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7339 | } |
| 7340 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7341 | static DWORD |
| 7342 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7343 | { |
| 7344 | if (code_page == CP_UTF8) { |
| 7345 | if (winver.dwMajorVersion >= 6) |
| 7346 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7347 | and later */ |
| 7348 | return WC_ERR_INVALID_CHARS; |
| 7349 | else |
| 7350 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7351 | return 0; |
| 7352 | } |
| 7353 | else if (code_page == CP_UTF7) { |
| 7354 | /* CP_UTF7 only supports flags=0 */ |
| 7355 | return 0; |
| 7356 | } |
| 7357 | else { |
| 7358 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7359 | return 0; |
| 7360 | else |
| 7361 | return WC_NO_BEST_FIT_CHARS; |
| 7362 | } |
| 7363 | } |
| 7364 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7365 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7366 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7367 | * mode. |
| 7368 | * |
| 7369 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7370 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7371 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7372 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7373 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7374 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7375 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7376 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7377 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7378 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7379 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7380 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7381 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7382 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7383 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7384 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7385 | /* Create a substring so that we can get the UTF-16 representation |
| 7386 | of just the slice under consideration. */ |
| 7387 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7388 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7389 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7390 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7391 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7392 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7393 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7394 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7395 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7396 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7397 | if (substring == NULL) |
| 7398 | return -1; |
| 7399 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7400 | if (p == NULL) { |
| 7401 | Py_DECREF(substring); |
| 7402 | return -1; |
| 7403 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7404 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7405 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7406 | outsize = WideCharToMultiByte(code_page, flags, |
| 7407 | p, size, |
| 7408 | NULL, 0, |
| 7409 | NULL, pusedDefaultChar); |
| 7410 | if (outsize <= 0) |
| 7411 | goto error; |
| 7412 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7413 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7414 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7415 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7416 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7417 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7418 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7419 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7420 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7421 | if (*outbytes == NULL) { |
| 7422 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7423 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7424 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7425 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7426 | } |
| 7427 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7428 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7429 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7430 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7431 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7432 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7433 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7434 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7435 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7436 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7437 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7438 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7439 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7440 | } |
| 7441 | |
| 7442 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7443 | outsize = WideCharToMultiByte(code_page, flags, |
| 7444 | p, size, |
| 7445 | out, outsize, |
| 7446 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7447 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7448 | if (outsize <= 0) |
| 7449 | goto error; |
| 7450 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7451 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7452 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7453 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7454 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7455 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7456 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7457 | return -2; |
| 7458 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7459 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7462 | /* |
| 7463 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7464 | * error handler. |
| 7465 | * |
| 7466 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7467 | * -1 on other error. |
| 7468 | */ |
| 7469 | static int |
| 7470 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7471 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7472 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7473 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7474 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7475 | Py_ssize_t pos = unicode_offset; |
| 7476 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7477 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7478 | 2000 English version of the message. */ |
| 7479 | const char *reason = "invalid character"; |
| 7480 | /* 4=maximum length of a UTF-8 sequence */ |
| 7481 | char buffer[4]; |
| 7482 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7483 | Py_ssize_t outsize; |
| 7484 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7485 | PyObject *errorHandler = NULL; |
| 7486 | PyObject *exc = NULL; |
| 7487 | PyObject *encoding_obj = NULL; |
| 7488 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7489 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7490 | PyObject *rep; |
| 7491 | int ret = -1; |
| 7492 | |
| 7493 | assert(insize > 0); |
| 7494 | |
| 7495 | encoding = code_page_name(code_page, &encoding_obj); |
| 7496 | if (encoding == NULL) |
| 7497 | return -1; |
| 7498 | |
| 7499 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7500 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7501 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7502 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7503 | if (exc != NULL) { |
| 7504 | PyCodec_StrictErrors(exc); |
| 7505 | Py_DECREF(exc); |
| 7506 | } |
| 7507 | Py_XDECREF(encoding_obj); |
| 7508 | return -1; |
| 7509 | } |
| 7510 | |
| 7511 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7512 | pusedDefaultChar = &usedDefaultChar; |
| 7513 | else |
| 7514 | pusedDefaultChar = NULL; |
| 7515 | |
| 7516 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7517 | PyErr_NoMemory(); |
| 7518 | goto error; |
| 7519 | } |
| 7520 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7521 | |
| 7522 | if (*outbytes == NULL) { |
| 7523 | /* Create string object */ |
| 7524 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7525 | if (*outbytes == NULL) |
| 7526 | goto error; |
| 7527 | out = PyBytes_AS_STRING(*outbytes); |
| 7528 | } |
| 7529 | else { |
| 7530 | /* Extend string object */ |
| 7531 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7532 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7533 | PyErr_NoMemory(); |
| 7534 | goto error; |
| 7535 | } |
| 7536 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7537 | goto error; |
| 7538 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7539 | } |
| 7540 | |
| 7541 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7542 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7543 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7544 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7545 | wchar_t chars[2]; |
| 7546 | int charsize; |
| 7547 | if (ch < 0x10000) { |
| 7548 | chars[0] = (wchar_t)ch; |
| 7549 | charsize = 1; |
| 7550 | } |
| 7551 | else { |
| 7552 | ch -= 0x10000; |
| 7553 | chars[0] = 0xd800 + (ch >> 10); |
| 7554 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7555 | charsize = 2; |
| 7556 | } |
| 7557 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7558 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7559 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7560 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7561 | NULL, pusedDefaultChar); |
| 7562 | if (outsize > 0) { |
| 7563 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7564 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7565 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7566 | memcpy(out, buffer, outsize); |
| 7567 | out += outsize; |
| 7568 | continue; |
| 7569 | } |
| 7570 | } |
| 7571 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7572 | PyErr_SetFromWindowsErr(0); |
| 7573 | goto error; |
| 7574 | } |
| 7575 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7576 | rep = unicode_encode_call_errorhandler( |
| 7577 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7578 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7579 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7580 | if (rep == NULL) |
| 7581 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7582 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7583 | |
| 7584 | if (PyBytes_Check(rep)) { |
| 7585 | outsize = PyBytes_GET_SIZE(rep); |
| 7586 | if (outsize != 1) { |
| 7587 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7588 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7589 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7590 | Py_DECREF(rep); |
| 7591 | goto error; |
| 7592 | } |
| 7593 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7594 | } |
| 7595 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7596 | out += outsize; |
| 7597 | } |
| 7598 | else { |
| 7599 | Py_ssize_t i; |
| 7600 | enum PyUnicode_Kind kind; |
| 7601 | void *data; |
| 7602 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7603 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7604 | Py_DECREF(rep); |
| 7605 | goto error; |
| 7606 | } |
| 7607 | |
| 7608 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7609 | if (outsize != 1) { |
| 7610 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7611 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7612 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7613 | Py_DECREF(rep); |
| 7614 | goto error; |
| 7615 | } |
| 7616 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7617 | } |
| 7618 | kind = PyUnicode_KIND(rep); |
| 7619 | data = PyUnicode_DATA(rep); |
| 7620 | for (i=0; i < outsize; i++) { |
| 7621 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7622 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7623 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7624 | encoding, unicode, |
| 7625 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7626 | "unable to encode error handler result to ASCII"); |
| 7627 | Py_DECREF(rep); |
| 7628 | goto error; |
| 7629 | } |
| 7630 | *out = (unsigned char)ch; |
| 7631 | out++; |
| 7632 | } |
| 7633 | } |
| 7634 | Py_DECREF(rep); |
| 7635 | } |
| 7636 | /* write a NUL byte */ |
| 7637 | *out = 0; |
| 7638 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7639 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7640 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7641 | goto error; |
| 7642 | ret = 0; |
| 7643 | |
| 7644 | error: |
| 7645 | Py_XDECREF(encoding_obj); |
| 7646 | Py_XDECREF(errorHandler); |
| 7647 | Py_XDECREF(exc); |
| 7648 | return ret; |
| 7649 | } |
| 7650 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7651 | static PyObject * |
| 7652 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7653 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7654 | const char *errors) |
| 7655 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7656 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7657 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7658 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7659 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7660 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7661 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7662 | return NULL; |
| 7663 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7664 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7665 | if (code_page < 0) { |
| 7666 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7667 | return NULL; |
| 7668 | } |
| 7669 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7670 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7671 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7672 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7673 | offset = 0; |
| 7674 | do |
| 7675 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7676 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7677 | /* 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] | 7678 | chunks. */ |
| 7679 | if (len > INT_MAX/2) { |
| 7680 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7681 | done = 0; |
| 7682 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7683 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7684 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7685 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7686 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7687 | done = 1; |
| 7688 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7689 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7690 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7691 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7692 | errors); |
| 7693 | if (ret == -2) |
| 7694 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7695 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7696 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7697 | if (ret < 0) { |
| 7698 | Py_XDECREF(outbytes); |
| 7699 | return NULL; |
| 7700 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7701 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7702 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7703 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7704 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7705 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7706 | return outbytes; |
| 7707 | } |
| 7708 | |
| 7709 | PyObject * |
| 7710 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7711 | Py_ssize_t size, |
| 7712 | const char *errors) |
| 7713 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7714 | PyObject *unicode, *res; |
| 7715 | unicode = PyUnicode_FromUnicode(p, size); |
| 7716 | if (unicode == NULL) |
| 7717 | return NULL; |
| 7718 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7719 | Py_DECREF(unicode); |
| 7720 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7721 | } |
| 7722 | |
| 7723 | PyObject * |
| 7724 | PyUnicode_EncodeCodePage(int code_page, |
| 7725 | PyObject *unicode, |
| 7726 | const char *errors) |
| 7727 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7728 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7729 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7730 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7731 | PyObject * |
| 7732 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7733 | { |
| 7734 | if (!PyUnicode_Check(unicode)) { |
| 7735 | PyErr_BadArgument(); |
| 7736 | return NULL; |
| 7737 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7738 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7739 | } |
| 7740 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7741 | #undef NEED_RETRY |
| 7742 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7743 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7747 | PyObject * |
| 7748 | PyUnicode_DecodeCharmap(const char *s, |
| 7749 | Py_ssize_t size, |
| 7750 | PyObject *mapping, |
| 7751 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7753 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7754 | Py_ssize_t startinpos; |
| 7755 | Py_ssize_t endinpos; |
| 7756 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7757 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7758 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7759 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7760 | PyObject *errorHandler = NULL; |
| 7761 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | /* Default to Latin-1 */ |
| 7764 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7765 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7767 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7771 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7772 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7773 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7774 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7775 | Py_ssize_t maplen; |
| 7776 | enum PyUnicode_Kind kind; |
| 7777 | void *data; |
| 7778 | Py_UCS4 x; |
| 7779 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7780 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7781 | return NULL; |
| 7782 | |
| 7783 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7784 | data = PyUnicode_DATA(mapping); |
| 7785 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7786 | while (s < e) { |
| 7787 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7788 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7789 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7790 | x = PyUnicode_READ(kind, data, ch); |
| 7791 | else |
| 7792 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7794 | if (x == 0xfffe) |
| 7795 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7796 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | startinpos = s-starts; |
| 7798 | endinpos = startinpos+1; |
| 7799 | if (unicode_decode_call_errorhandler( |
| 7800 | errors, &errorHandler, |
| 7801 | "charmap", "character maps to <undefined>", |
| 7802 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7803 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7804 | goto onError; |
| 7805 | } |
| 7806 | continue; |
| 7807 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7808 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7809 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7810 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7811 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7812 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7813 | } |
| 7814 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7815 | while (s < e) { |
| 7816 | unsigned char ch = *s; |
| 7817 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7820 | w = PyLong_FromLong((long)ch); |
| 7821 | if (w == NULL) |
| 7822 | goto onError; |
| 7823 | x = PyObject_GetItem(mapping, w); |
| 7824 | Py_DECREF(w); |
| 7825 | if (x == NULL) { |
| 7826 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7827 | /* No mapping found means: mapping is undefined. */ |
| 7828 | PyErr_Clear(); |
| 7829 | x = Py_None; |
| 7830 | Py_INCREF(x); |
| 7831 | } else |
| 7832 | goto onError; |
| 7833 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7834 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7835 | /* Apply mapping */ |
| 7836 | if (PyLong_Check(x)) { |
| 7837 | long value = PyLong_AS_LONG(x); |
| 7838 | if (value < 0 || value > 65535) { |
| 7839 | PyErr_SetString(PyExc_TypeError, |
| 7840 | "character mapping must be in range(65536)"); |
| 7841 | Py_DECREF(x); |
| 7842 | goto onError; |
| 7843 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7844 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7845 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7846 | } |
| 7847 | else if (x == Py_None) { |
| 7848 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7849 | startinpos = s-starts; |
| 7850 | endinpos = startinpos+1; |
| 7851 | if (unicode_decode_call_errorhandler( |
| 7852 | errors, &errorHandler, |
| 7853 | "charmap", "character maps to <undefined>", |
| 7854 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7855 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7856 | Py_DECREF(x); |
| 7857 | goto onError; |
| 7858 | } |
| 7859 | Py_DECREF(x); |
| 7860 | continue; |
| 7861 | } |
| 7862 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7863 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7864 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7865 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7866 | goto onError; |
| 7867 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7868 | |
| 7869 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7870 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7871 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7872 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7873 | goto onError; |
| 7874 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7875 | else if (targetsize > 1) { |
| 7876 | /* 1-n mapping */ |
| 7877 | if (targetsize > extrachars) { |
| 7878 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7880 | (targetsize << 2); |
| 7881 | extrachars += needed; |
| 7882 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7883 | if (unicode_resize(&v, |
| 7884 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7885 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | Py_DECREF(x); |
| 7887 | goto onError; |
| 7888 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7889 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7890 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7891 | goto onError; |
| 7892 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7893 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7894 | extrachars -= targetsize; |
| 7895 | } |
| 7896 | /* 1-0 mapping: skip the character */ |
| 7897 | } |
| 7898 | else { |
| 7899 | /* wrong return value */ |
| 7900 | PyErr_SetString(PyExc_TypeError, |
| 7901 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7902 | Py_DECREF(x); |
| 7903 | goto onError; |
| 7904 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | Py_DECREF(x); |
| 7906 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7907 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7908 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7909 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7910 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7911 | Py_XDECREF(errorHandler); |
| 7912 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7913 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7914 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7915 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7916 | Py_XDECREF(errorHandler); |
| 7917 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | Py_XDECREF(v); |
| 7919 | return NULL; |
| 7920 | } |
| 7921 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7922 | /* Charmap encoding: the lookup table */ |
| 7923 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7924 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | PyObject_HEAD |
| 7926 | unsigned char level1[32]; |
| 7927 | int count2, count3; |
| 7928 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7929 | }; |
| 7930 | |
| 7931 | static PyObject* |
| 7932 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7933 | { |
| 7934 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7935 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7936 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7940 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7941 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7942 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7943 | }; |
| 7944 | |
| 7945 | static void |
| 7946 | encoding_map_dealloc(PyObject* o) |
| 7947 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7948 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7949 | } |
| 7950 | |
| 7951 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7952 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7953 | "EncodingMap", /*tp_name*/ |
| 7954 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7955 | 0, /*tp_itemsize*/ |
| 7956 | /* methods */ |
| 7957 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7958 | 0, /*tp_print*/ |
| 7959 | 0, /*tp_getattr*/ |
| 7960 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7961 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7962 | 0, /*tp_repr*/ |
| 7963 | 0, /*tp_as_number*/ |
| 7964 | 0, /*tp_as_sequence*/ |
| 7965 | 0, /*tp_as_mapping*/ |
| 7966 | 0, /*tp_hash*/ |
| 7967 | 0, /*tp_call*/ |
| 7968 | 0, /*tp_str*/ |
| 7969 | 0, /*tp_getattro*/ |
| 7970 | 0, /*tp_setattro*/ |
| 7971 | 0, /*tp_as_buffer*/ |
| 7972 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7973 | 0, /*tp_doc*/ |
| 7974 | 0, /*tp_traverse*/ |
| 7975 | 0, /*tp_clear*/ |
| 7976 | 0, /*tp_richcompare*/ |
| 7977 | 0, /*tp_weaklistoffset*/ |
| 7978 | 0, /*tp_iter*/ |
| 7979 | 0, /*tp_iternext*/ |
| 7980 | encoding_map_methods, /*tp_methods*/ |
| 7981 | 0, /*tp_members*/ |
| 7982 | 0, /*tp_getset*/ |
| 7983 | 0, /*tp_base*/ |
| 7984 | 0, /*tp_dict*/ |
| 7985 | 0, /*tp_descr_get*/ |
| 7986 | 0, /*tp_descr_set*/ |
| 7987 | 0, /*tp_dictoffset*/ |
| 7988 | 0, /*tp_init*/ |
| 7989 | 0, /*tp_alloc*/ |
| 7990 | 0, /*tp_new*/ |
| 7991 | 0, /*tp_free*/ |
| 7992 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7993 | }; |
| 7994 | |
| 7995 | PyObject* |
| 7996 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7997 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7998 | PyObject *result; |
| 7999 | struct encoding_map *mresult; |
| 8000 | int i; |
| 8001 | int need_dict = 0; |
| 8002 | unsigned char level1[32]; |
| 8003 | unsigned char level2[512]; |
| 8004 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8005 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8006 | int kind; |
| 8007 | void *data; |
| 8008 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8009 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8010 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8011 | PyErr_BadArgument(); |
| 8012 | return NULL; |
| 8013 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8014 | kind = PyUnicode_KIND(string); |
| 8015 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8016 | memset(level1, 0xFF, sizeof level1); |
| 8017 | memset(level2, 0xFF, sizeof level2); |
| 8018 | |
| 8019 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8020 | or if there are non-BMP characters, we need to use |
| 8021 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8022 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8023 | need_dict = 1; |
| 8024 | for (i = 1; i < 256; i++) { |
| 8025 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8026 | ch = PyUnicode_READ(kind, data, i); |
| 8027 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8028 | need_dict = 1; |
| 8029 | break; |
| 8030 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8031 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8032 | /* unmapped character */ |
| 8033 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8034 | l1 = ch >> 11; |
| 8035 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8036 | if (level1[l1] == 0xFF) |
| 8037 | level1[l1] = count2++; |
| 8038 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8039 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8040 | } |
| 8041 | |
| 8042 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8043 | need_dict = 1; |
| 8044 | |
| 8045 | if (need_dict) { |
| 8046 | PyObject *result = PyDict_New(); |
| 8047 | PyObject *key, *value; |
| 8048 | if (!result) |
| 8049 | return NULL; |
| 8050 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8051 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8052 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8053 | if (!key || !value) |
| 8054 | goto failed1; |
| 8055 | if (PyDict_SetItem(result, key, value) == -1) |
| 8056 | goto failed1; |
| 8057 | Py_DECREF(key); |
| 8058 | Py_DECREF(value); |
| 8059 | } |
| 8060 | return result; |
| 8061 | failed1: |
| 8062 | Py_XDECREF(key); |
| 8063 | Py_XDECREF(value); |
| 8064 | Py_DECREF(result); |
| 8065 | return NULL; |
| 8066 | } |
| 8067 | |
| 8068 | /* Create a three-level trie */ |
| 8069 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8070 | 16*count2 + 128*count3 - 1); |
| 8071 | if (!result) |
| 8072 | return PyErr_NoMemory(); |
| 8073 | PyObject_Init(result, &EncodingMapType); |
| 8074 | mresult = (struct encoding_map*)result; |
| 8075 | mresult->count2 = count2; |
| 8076 | mresult->count3 = count3; |
| 8077 | mlevel1 = mresult->level1; |
| 8078 | mlevel2 = mresult->level23; |
| 8079 | mlevel3 = mresult->level23 + 16*count2; |
| 8080 | memcpy(mlevel1, level1, 32); |
| 8081 | memset(mlevel2, 0xFF, 16*count2); |
| 8082 | memset(mlevel3, 0, 128*count3); |
| 8083 | count3 = 0; |
| 8084 | for (i = 1; i < 256; i++) { |
| 8085 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8086 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8087 | /* unmapped character */ |
| 8088 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8089 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8090 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8091 | i2 = 16*mlevel1[o1] + o2; |
| 8092 | if (mlevel2[i2] == 0xFF) |
| 8093 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8094 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8095 | i3 = 128*mlevel2[i2] + o3; |
| 8096 | mlevel3[i3] = i; |
| 8097 | } |
| 8098 | return result; |
| 8099 | } |
| 8100 | |
| 8101 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8102 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8103 | { |
| 8104 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8105 | int l1 = c>>11; |
| 8106 | int l2 = (c>>7) & 0xF; |
| 8107 | int l3 = c & 0x7F; |
| 8108 | int i; |
| 8109 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8110 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8111 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8112 | if (c == 0) |
| 8113 | return 0; |
| 8114 | /* level 1*/ |
| 8115 | i = map->level1[l1]; |
| 8116 | if (i == 0xFF) { |
| 8117 | return -1; |
| 8118 | } |
| 8119 | /* level 2*/ |
| 8120 | i = map->level23[16*i+l2]; |
| 8121 | if (i == 0xFF) { |
| 8122 | return -1; |
| 8123 | } |
| 8124 | /* level 3 */ |
| 8125 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8126 | if (i == 0) { |
| 8127 | return -1; |
| 8128 | } |
| 8129 | return i; |
| 8130 | } |
| 8131 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8132 | /* Lookup the character ch in the mapping. If the character |
| 8133 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8134 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8135 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8136 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8138 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8139 | PyObject *x; |
| 8140 | |
| 8141 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8142 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8143 | x = PyObject_GetItem(mapping, w); |
| 8144 | Py_DECREF(w); |
| 8145 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8146 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8147 | /* No mapping found means: mapping is undefined. */ |
| 8148 | PyErr_Clear(); |
| 8149 | x = Py_None; |
| 8150 | Py_INCREF(x); |
| 8151 | return x; |
| 8152 | } else |
| 8153 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8154 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8155 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8156 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8157 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8158 | long value = PyLong_AS_LONG(x); |
| 8159 | if (value < 0 || value > 255) { |
| 8160 | PyErr_SetString(PyExc_TypeError, |
| 8161 | "character mapping must be in range(256)"); |
| 8162 | Py_DECREF(x); |
| 8163 | return NULL; |
| 8164 | } |
| 8165 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8167 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8169 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8170 | /* wrong return value */ |
| 8171 | PyErr_Format(PyExc_TypeError, |
| 8172 | "character mapping must return integer, bytes or None, not %.400s", |
| 8173 | x->ob_type->tp_name); |
| 8174 | Py_DECREF(x); |
| 8175 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | } |
| 8177 | } |
| 8178 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8179 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8180 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8181 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8182 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8183 | /* exponentially overallocate to minimize reallocations */ |
| 8184 | if (requiredsize < 2*outsize) |
| 8185 | requiredsize = 2*outsize; |
| 8186 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8187 | return -1; |
| 8188 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8189 | } |
| 8190 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8191 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8193 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8194 | /* 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] | 8195 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8196 | space is available. Return a new reference to the object that |
| 8197 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8198 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8199 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8200 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8201 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8202 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8203 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8204 | PyObject *rep; |
| 8205 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8206 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8207 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8208 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8209 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8211 | if (res == -1) |
| 8212 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8213 | if (outsize<requiredsize) |
| 8214 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8215 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8216 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8217 | outstart[(*outpos)++] = (char)res; |
| 8218 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8219 | } |
| 8220 | |
| 8221 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8222 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8224 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8225 | Py_DECREF(rep); |
| 8226 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8227 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8228 | if (PyLong_Check(rep)) { |
| 8229 | Py_ssize_t requiredsize = *outpos+1; |
| 8230 | if (outsize<requiredsize) |
| 8231 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8232 | Py_DECREF(rep); |
| 8233 | return enc_EXCEPTION; |
| 8234 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8235 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8236 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8237 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 | else { |
| 8239 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8240 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8241 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8242 | if (outsize<requiredsize) |
| 8243 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8244 | Py_DECREF(rep); |
| 8245 | return enc_EXCEPTION; |
| 8246 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8247 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | memcpy(outstart + *outpos, repchars, repsize); |
| 8249 | *outpos += repsize; |
| 8250 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8251 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8252 | Py_DECREF(rep); |
| 8253 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8254 | } |
| 8255 | |
| 8256 | /* handle an error in PyUnicode_EncodeCharmap |
| 8257 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8258 | static int |
| 8259 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8260 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8261 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8262 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8263 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8264 | { |
| 8265 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8266 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8267 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8268 | enum PyUnicode_Kind kind; |
| 8269 | void *data; |
| 8270 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8271 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8272 | Py_ssize_t collstartpos = *inpos; |
| 8273 | Py_ssize_t collendpos = *inpos+1; |
| 8274 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 | char *encoding = "charmap"; |
| 8276 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8277 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8278 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8279 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8280 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8281 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8282 | return -1; |
| 8283 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8284 | /* find all unencodable characters */ |
| 8285 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8286 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8287 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8288 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8289 | val = encoding_map_lookup(ch, mapping); |
| 8290 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8291 | break; |
| 8292 | ++collendpos; |
| 8293 | continue; |
| 8294 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8295 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8296 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8297 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | if (rep==NULL) |
| 8299 | return -1; |
| 8300 | else if (rep!=Py_None) { |
| 8301 | Py_DECREF(rep); |
| 8302 | break; |
| 8303 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8304 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8305 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8306 | } |
| 8307 | /* cache callback name lookup |
| 8308 | * (if not done yet, i.e. it's the first error) */ |
| 8309 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8310 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8311 | *known_errorHandler = 1; |
| 8312 | else if (!strcmp(errors, "replace")) |
| 8313 | *known_errorHandler = 2; |
| 8314 | else if (!strcmp(errors, "ignore")) |
| 8315 | *known_errorHandler = 3; |
| 8316 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8317 | *known_errorHandler = 4; |
| 8318 | else |
| 8319 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8320 | } |
| 8321 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8322 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8323 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8324 | return -1; |
| 8325 | case 2: /* replace */ |
| 8326 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8327 | x = charmapencode_output('?', mapping, res, respos); |
| 8328 | if (x==enc_EXCEPTION) { |
| 8329 | return -1; |
| 8330 | } |
| 8331 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8332 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | return -1; |
| 8334 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8335 | } |
| 8336 | /* fall through */ |
| 8337 | case 3: /* ignore */ |
| 8338 | *inpos = collendpos; |
| 8339 | break; |
| 8340 | case 4: /* xmlcharrefreplace */ |
| 8341 | /* generate replacement (temporarily (mis)uses p) */ |
| 8342 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8343 | char buffer[2+29+1+1]; |
| 8344 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8345 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | for (cp = buffer; *cp; ++cp) { |
| 8347 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8348 | if (x==enc_EXCEPTION) |
| 8349 | return -1; |
| 8350 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8351 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8352 | return -1; |
| 8353 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8354 | } |
| 8355 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8356 | *inpos = collendpos; |
| 8357 | break; |
| 8358 | default: |
| 8359 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8360 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8362 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8364 | if (PyBytes_Check(repunicode)) { |
| 8365 | /* Directly copy bytes result to output. */ |
| 8366 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8367 | Py_ssize_t requiredsize; |
| 8368 | repsize = PyBytes_Size(repunicode); |
| 8369 | requiredsize = *respos + repsize; |
| 8370 | if (requiredsize > outsize) |
| 8371 | /* Make room for all additional bytes. */ |
| 8372 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8373 | Py_DECREF(repunicode); |
| 8374 | return -1; |
| 8375 | } |
| 8376 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8377 | PyBytes_AsString(repunicode), repsize); |
| 8378 | *respos += repsize; |
| 8379 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8380 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8381 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8382 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8383 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8384 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8385 | Py_DECREF(repunicode); |
| 8386 | return -1; |
| 8387 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8388 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8389 | data = PyUnicode_DATA(repunicode); |
| 8390 | kind = PyUnicode_KIND(repunicode); |
| 8391 | for (index = 0; index < repsize; index++) { |
| 8392 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8393 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8395 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8396 | return -1; |
| 8397 | } |
| 8398 | else if (x==enc_FAILED) { |
| 8399 | Py_DECREF(repunicode); |
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 | *inpos = newpos; |
| 8405 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8406 | } |
| 8407 | return 0; |
| 8408 | } |
| 8409 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8410 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8411 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8412 | PyObject *mapping, |
| 8413 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8414 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8415 | /* output object */ |
| 8416 | PyObject *res = NULL; |
| 8417 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8418 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8419 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8420 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8421 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8422 | PyObject *errorHandler = NULL; |
| 8423 | PyObject *exc = NULL; |
| 8424 | /* the following variable is used for caching string comparisons |
| 8425 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8426 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8427 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8429 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8430 | return NULL; |
| 8431 | size = PyUnicode_GET_LENGTH(unicode); |
| 8432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | /* Default to Latin-1 */ |
| 8434 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8435 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8437 | /* allocate enough for a simple encoding without |
| 8438 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8439 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8440 | if (res == NULL) |
| 8441 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8442 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8444 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8445 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8446 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8447 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8448 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | if (x==enc_EXCEPTION) /* error */ |
| 8450 | goto onError; |
| 8451 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8452 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | &exc, |
| 8454 | &known_errorHandler, &errorHandler, errors, |
| 8455 | &res, &respos)) { |
| 8456 | goto onError; |
| 8457 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8458 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8459 | else |
| 8460 | /* done with this character => adjust input position */ |
| 8461 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8464 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8465 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8466 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8467 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8468 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8469 | Py_XDECREF(exc); |
| 8470 | Py_XDECREF(errorHandler); |
| 8471 | return res; |
| 8472 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8474 | Py_XDECREF(res); |
| 8475 | Py_XDECREF(exc); |
| 8476 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8477 | return NULL; |
| 8478 | } |
| 8479 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8480 | /* Deprecated */ |
| 8481 | PyObject * |
| 8482 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8483 | Py_ssize_t size, |
| 8484 | PyObject *mapping, |
| 8485 | const char *errors) |
| 8486 | { |
| 8487 | PyObject *result; |
| 8488 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8489 | if (unicode == NULL) |
| 8490 | return NULL; |
| 8491 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8492 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8493 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8494 | } |
| 8495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8496 | PyObject * |
| 8497 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8498 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8499 | { |
| 8500 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8501 | PyErr_BadArgument(); |
| 8502 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8503 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8504 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8505 | } |
| 8506 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8507 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8508 | static void |
| 8509 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8510 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8511 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8512 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8513 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8514 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8516 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8517 | } |
| 8518 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8520 | goto onError; |
| 8521 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8522 | goto onError; |
| 8523 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8524 | goto onError; |
| 8525 | return; |
| 8526 | onError: |
| 8527 | Py_DECREF(*exceptionObject); |
| 8528 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8529 | } |
| 8530 | } |
| 8531 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8532 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8533 | static void |
| 8534 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8535 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8536 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8537 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8538 | { |
| 8539 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8540 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8541 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8542 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | /* error handling callback helper: |
| 8546 | build arguments, call the callback and check the arguments, |
| 8547 | put the result into newpos and return the replacement string, which |
| 8548 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8549 | static PyObject * |
| 8550 | unicode_translate_call_errorhandler(const char *errors, |
| 8551 | PyObject **errorHandler, |
| 8552 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8553 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8554 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8555 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8556 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8557 | 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] | 8558 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8559 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8560 | PyObject *restuple; |
| 8561 | PyObject *resunicode; |
| 8562 | |
| 8563 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8564 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8565 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8566 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8567 | } |
| 8568 | |
| 8569 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8570 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8571 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8572 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8573 | |
| 8574 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8575 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8576 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8577 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8578 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8579 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8580 | Py_DECREF(restuple); |
| 8581 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8582 | } |
| 8583 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | &resunicode, &i_newpos)) { |
| 8585 | Py_DECREF(restuple); |
| 8586 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8587 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8588 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8589 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8590 | else |
| 8591 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8593 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8594 | Py_DECREF(restuple); |
| 8595 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8596 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8597 | Py_INCREF(resunicode); |
| 8598 | Py_DECREF(restuple); |
| 8599 | return resunicode; |
| 8600 | } |
| 8601 | |
| 8602 | /* Lookup the character ch in the mapping and put the result in result, |
| 8603 | which must be decrefed by the caller. |
| 8604 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8605 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8606 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8607 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8608 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8609 | PyObject *x; |
| 8610 | |
| 8611 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8612 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8613 | x = PyObject_GetItem(mapping, w); |
| 8614 | Py_DECREF(w); |
| 8615 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8616 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8617 | /* No mapping found means: use 1:1 mapping. */ |
| 8618 | PyErr_Clear(); |
| 8619 | *result = NULL; |
| 8620 | return 0; |
| 8621 | } else |
| 8622 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8623 | } |
| 8624 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | *result = x; |
| 8626 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8627 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8628 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8629 | long value = PyLong_AS_LONG(x); |
| 8630 | long max = PyUnicode_GetMax(); |
| 8631 | if (value < 0 || value > max) { |
| 8632 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8633 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8634 | Py_DECREF(x); |
| 8635 | return -1; |
| 8636 | } |
| 8637 | *result = x; |
| 8638 | return 0; |
| 8639 | } |
| 8640 | else if (PyUnicode_Check(x)) { |
| 8641 | *result = x; |
| 8642 | return 0; |
| 8643 | } |
| 8644 | else { |
| 8645 | /* wrong return value */ |
| 8646 | PyErr_SetString(PyExc_TypeError, |
| 8647 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8648 | Py_DECREF(x); |
| 8649 | return -1; |
| 8650 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8651 | } |
| 8652 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8653 | if not reallocate and adjust various state variables. |
| 8654 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8655 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8656 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8657 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8658 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8660 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8661 | /* exponentially overallocate to minimize reallocations */ |
| 8662 | if (requiredsize < 2 * oldsize) |
| 8663 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8665 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8666 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8667 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8668 | } |
| 8669 | return 0; |
| 8670 | } |
| 8671 | /* lookup the character, put the result in the output string and adjust |
| 8672 | various state variables. Return a new reference to the object that |
| 8673 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8674 | undefined (in which case no character was written). |
| 8675 | The called must decref result. |
| 8676 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8677 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8679 | PyObject *mapping, Py_UCS4 **output, |
| 8680 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8681 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8682 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8683 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8684 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8685 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8686 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8687 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8688 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8689 | } |
| 8690 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8691 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8692 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | /* 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] | 8694 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8695 | } |
| 8696 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | Py_ssize_t repsize; |
| 8698 | if (PyUnicode_READY(*res) == -1) |
| 8699 | return -1; |
| 8700 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | if (repsize==1) { |
| 8702 | /* 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] | 8703 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8704 | } |
| 8705 | else if (repsize!=0) { |
| 8706 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8707 | Py_ssize_t requiredsize = *opos + |
| 8708 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8709 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8710 | Py_ssize_t i; |
| 8711 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8712 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | for(i = 0; i < repsize; i++) |
| 8714 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8715 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8716 | } |
| 8717 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8719 | return 0; |
| 8720 | } |
| 8721 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8722 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8723 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8724 | PyObject *mapping, |
| 8725 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8726 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8727 | /* input object */ |
| 8728 | char *idata; |
| 8729 | Py_ssize_t size, i; |
| 8730 | int kind; |
| 8731 | /* output buffer */ |
| 8732 | Py_UCS4 *output = NULL; |
| 8733 | Py_ssize_t osize; |
| 8734 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8735 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8736 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8737 | char *reason = "character maps to <undefined>"; |
| 8738 | PyObject *errorHandler = NULL; |
| 8739 | PyObject *exc = NULL; |
| 8740 | /* the following variable is used for caching string comparisons |
| 8741 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8742 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8743 | int known_errorHandler = -1; |
| 8744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8745 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8746 | PyErr_BadArgument(); |
| 8747 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8748 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8750 | if (PyUnicode_READY(input) == -1) |
| 8751 | return NULL; |
| 8752 | idata = (char*)PyUnicode_DATA(input); |
| 8753 | kind = PyUnicode_KIND(input); |
| 8754 | size = PyUnicode_GET_LENGTH(input); |
| 8755 | i = 0; |
| 8756 | |
| 8757 | if (size == 0) { |
| 8758 | Py_INCREF(input); |
| 8759 | return input; |
| 8760 | } |
| 8761 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8762 | /* allocate enough for a simple 1:1 translation without |
| 8763 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | osize = size; |
| 8765 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8766 | opos = 0; |
| 8767 | if (output == NULL) { |
| 8768 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8769 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8770 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8771 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8772 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8773 | /* try to encode it */ |
| 8774 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8775 | if (charmaptranslate_output(input, i, mapping, |
| 8776 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8777 | Py_XDECREF(x); |
| 8778 | goto onError; |
| 8779 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8780 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8781 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8782 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8783 | else { /* untranslatable character */ |
| 8784 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8785 | Py_ssize_t repsize; |
| 8786 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8787 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8788 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8789 | Py_ssize_t collstart = i; |
| 8790 | Py_ssize_t collend = i+1; |
| 8791 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8792 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8793 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8794 | while (collend < size) { |
| 8795 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8796 | goto onError; |
| 8797 | Py_XDECREF(x); |
| 8798 | if (x!=Py_None) |
| 8799 | break; |
| 8800 | ++collend; |
| 8801 | } |
| 8802 | /* cache callback name lookup |
| 8803 | * (if not done yet, i.e. it's the first error) */ |
| 8804 | if (known_errorHandler==-1) { |
| 8805 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8806 | known_errorHandler = 1; |
| 8807 | else if (!strcmp(errors, "replace")) |
| 8808 | known_errorHandler = 2; |
| 8809 | else if (!strcmp(errors, "ignore")) |
| 8810 | known_errorHandler = 3; |
| 8811 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8812 | known_errorHandler = 4; |
| 8813 | else |
| 8814 | known_errorHandler = 0; |
| 8815 | } |
| 8816 | switch (known_errorHandler) { |
| 8817 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8818 | raise_translate_exception(&exc, input, collstart, |
| 8819 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8820 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8821 | case 2: /* replace */ |
| 8822 | /* 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] | 8823 | for (coll = collstart; coll<collend; coll++) |
| 8824 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8825 | /* fall through */ |
| 8826 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8827 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8828 | break; |
| 8829 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8830 | /* generate replacement (temporarily (mis)uses i) */ |
| 8831 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8832 | char buffer[2+29+1+1]; |
| 8833 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8834 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8835 | if (charmaptranslate_makespace(&output, &osize, |
| 8836 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8837 | goto onError; |
| 8838 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8839 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8840 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8841 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8842 | break; |
| 8843 | default: |
| 8844 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8845 | reason, input, &exc, |
| 8846 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8847 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8848 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8849 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8850 | Py_DECREF(repunicode); |
| 8851 | goto onError; |
| 8852 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8853 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8854 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8855 | if (charmaptranslate_makespace(&output, &osize, |
| 8856 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8857 | Py_DECREF(repunicode); |
| 8858 | goto onError; |
| 8859 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8860 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8861 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8862 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8863 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8864 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8865 | } |
| 8866 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8867 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8868 | if (!res) |
| 8869 | goto onError; |
| 8870 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8871 | Py_XDECREF(exc); |
| 8872 | Py_XDECREF(errorHandler); |
| 8873 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8874 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8875 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8876 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8877 | Py_XDECREF(exc); |
| 8878 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | return NULL; |
| 8880 | } |
| 8881 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8882 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8883 | PyObject * |
| 8884 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8885 | Py_ssize_t size, |
| 8886 | PyObject *mapping, |
| 8887 | const char *errors) |
| 8888 | { |
| 8889 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8890 | if (!unicode) |
| 8891 | return NULL; |
| 8892 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8893 | } |
| 8894 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8895 | PyObject * |
| 8896 | PyUnicode_Translate(PyObject *str, |
| 8897 | PyObject *mapping, |
| 8898 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8899 | { |
| 8900 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8901 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | str = PyUnicode_FromObject(str); |
| 8903 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8904 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8905 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8906 | Py_DECREF(str); |
| 8907 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8908 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8909 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8910 | Py_XDECREF(str); |
| 8911 | return NULL; |
| 8912 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8913 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8914 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8915 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8916 | { |
| 8917 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8918 | called as a callback from fixup() which does it already. */ |
| 8919 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8920 | const int kind = PyUnicode_KIND(self); |
| 8921 | void *data = PyUnicode_DATA(self); |
| 8922 | Py_UCS4 maxchar = 0, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8923 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8924 | Py_ssize_t i; |
| 8925 | |
| 8926 | for (i = 0; i < len; ++i) { |
| 8927 | ch = PyUnicode_READ(kind, data, i); |
| 8928 | fixed = 0; |
| 8929 | if (ch > 127) { |
| 8930 | if (Py_UNICODE_ISSPACE(ch)) |
| 8931 | fixed = ' '; |
| 8932 | else { |
| 8933 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8934 | if (decimal >= 0) |
| 8935 | fixed = '0' + decimal; |
| 8936 | } |
| 8937 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8938 | modified = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8939 | if (fixed > maxchar) |
| 8940 | maxchar = fixed; |
| 8941 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8942 | } |
| 8943 | else if (ch > maxchar) |
| 8944 | maxchar = ch; |
| 8945 | } |
| 8946 | else if (ch > maxchar) |
| 8947 | maxchar = ch; |
| 8948 | } |
| 8949 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8950 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | PyObject * |
| 8954 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8955 | { |
| 8956 | if (!PyUnicode_Check(unicode)) { |
| 8957 | PyErr_BadInternalCall(); |
| 8958 | return NULL; |
| 8959 | } |
| 8960 | if (PyUnicode_READY(unicode) == -1) |
| 8961 | return NULL; |
| 8962 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8963 | /* If the string is already ASCII, just return the same string */ |
| 8964 | Py_INCREF(unicode); |
| 8965 | return unicode; |
| 8966 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8967 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8968 | } |
| 8969 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8970 | PyObject * |
| 8971 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8972 | Py_ssize_t length) |
| 8973 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8974 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8975 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8976 | Py_UCS4 maxchar; |
| 8977 | enum PyUnicode_Kind kind; |
| 8978 | void *data; |
| 8979 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8980 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8981 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8982 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8983 | if (ch > 127) { |
| 8984 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8985 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8986 | ch = '0' + decimal; |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8987 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8988 | } |
| 8989 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8990 | |
| 8991 | /* Copy to a new string */ |
| 8992 | decimal = PyUnicode_New(length, maxchar); |
| 8993 | if (decimal == NULL) |
| 8994 | return decimal; |
| 8995 | kind = PyUnicode_KIND(decimal); |
| 8996 | data = PyUnicode_DATA(decimal); |
| 8997 | /* Iterate over code points */ |
| 8998 | for (i = 0; i < length; i++) { |
| 8999 | Py_UNICODE ch = s[i]; |
| 9000 | if (ch > 127) { |
| 9001 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9002 | if (decimal >= 0) |
| 9003 | ch = '0' + decimal; |
| 9004 | } |
| 9005 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9006 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9007 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9008 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9009 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9011 | int |
| 9012 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9013 | Py_ssize_t length, |
| 9014 | char *output, |
| 9015 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9016 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9017 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9018 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9019 | enum PyUnicode_Kind kind; |
| 9020 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9021 | |
| 9022 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9023 | PyErr_BadArgument(); |
| 9024 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9025 | } |
| 9026 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9027 | unicode = PyUnicode_FromUnicode(s, length); |
| 9028 | if (unicode == NULL) |
| 9029 | return -1; |
| 9030 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9031 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9032 | Py_DECREF(unicode); |
| 9033 | return -1; |
| 9034 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9035 | kind = PyUnicode_KIND(unicode); |
| 9036 | data = PyUnicode_DATA(unicode); |
| 9037 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9038 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9039 | PyObject *exc; |
| 9040 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9041 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9042 | Py_ssize_t startpos; |
| 9043 | |
| 9044 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9046 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9047 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9048 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9049 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9050 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9051 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9052 | if (decimal >= 0) { |
| 9053 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9054 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9055 | continue; |
| 9056 | } |
| 9057 | if (0 < ch && ch < 256) { |
| 9058 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9059 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9060 | continue; |
| 9061 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9062 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9063 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9064 | exc = NULL; |
| 9065 | raise_encode_exception(&exc, "decimal", unicode, |
| 9066 | startpos, startpos+1, |
| 9067 | "invalid decimal Unicode string"); |
| 9068 | Py_XDECREF(exc); |
| 9069 | Py_DECREF(unicode); |
| 9070 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9071 | } |
| 9072 | /* 0-terminate the output string */ |
| 9073 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9074 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9075 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9076 | } |
| 9077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9078 | /* --- Helpers ------------------------------------------------------------ */ |
| 9079 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9080 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9081 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9082 | Py_ssize_t start, |
| 9083 | Py_ssize_t end) |
| 9084 | { |
| 9085 | int kind1, kind2, kind; |
| 9086 | void *buf1, *buf2; |
| 9087 | Py_ssize_t len1, len2, result; |
| 9088 | |
| 9089 | kind1 = PyUnicode_KIND(s1); |
| 9090 | kind2 = PyUnicode_KIND(s2); |
| 9091 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9092 | buf1 = PyUnicode_DATA(s1); |
| 9093 | buf2 = PyUnicode_DATA(s2); |
| 9094 | if (kind1 != kind) |
| 9095 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9096 | if (!buf1) |
| 9097 | return -2; |
| 9098 | if (kind2 != kind) |
| 9099 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9100 | if (!buf2) { |
| 9101 | if (kind1 != kind) PyMem_Free(buf1); |
| 9102 | return -2; |
| 9103 | } |
| 9104 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9105 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9106 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9107 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9108 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9109 | case PyUnicode_1BYTE_KIND: |
| 9110 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9111 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9112 | else |
| 9113 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9114 | break; |
| 9115 | case PyUnicode_2BYTE_KIND: |
| 9116 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9117 | break; |
| 9118 | case PyUnicode_4BYTE_KIND: |
| 9119 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9120 | break; |
| 9121 | default: |
| 9122 | assert(0); result = -2; |
| 9123 | } |
| 9124 | } |
| 9125 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9126 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9127 | case PyUnicode_1BYTE_KIND: |
| 9128 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9129 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9130 | else |
| 9131 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9132 | break; |
| 9133 | case PyUnicode_2BYTE_KIND: |
| 9134 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9135 | break; |
| 9136 | case PyUnicode_4BYTE_KIND: |
| 9137 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9138 | break; |
| 9139 | default: |
| 9140 | assert(0); result = -2; |
| 9141 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9142 | } |
| 9143 | |
| 9144 | if (kind1 != kind) |
| 9145 | PyMem_Free(buf1); |
| 9146 | if (kind2 != kind) |
| 9147 | PyMem_Free(buf2); |
| 9148 | |
| 9149 | return result; |
| 9150 | } |
| 9151 | |
| 9152 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9153 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9154 | Py_ssize_t n_buffer, |
| 9155 | void *digits, Py_ssize_t n_digits, |
| 9156 | Py_ssize_t min_width, |
| 9157 | const char *grouping, |
| 9158 | const char *thousands_sep) |
| 9159 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9160 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9161 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9162 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9163 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9164 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9165 | min_width, grouping, thousands_sep); |
| 9166 | else |
| 9167 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9168 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9169 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9170 | case PyUnicode_2BYTE_KIND: |
| 9171 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9172 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9173 | min_width, grouping, thousands_sep); |
| 9174 | case PyUnicode_4BYTE_KIND: |
| 9175 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9176 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9177 | min_width, grouping, thousands_sep); |
| 9178 | } |
| 9179 | assert(0); |
| 9180 | return -1; |
| 9181 | } |
| 9182 | |
| 9183 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9184 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9185 | #define ADJUST_INDICES(start, end, len) \ |
| 9186 | if (end > len) \ |
| 9187 | end = len; \ |
| 9188 | else if (end < 0) { \ |
| 9189 | end += len; \ |
| 9190 | if (end < 0) \ |
| 9191 | end = 0; \ |
| 9192 | } \ |
| 9193 | if (start < 0) { \ |
| 9194 | start += len; \ |
| 9195 | if (start < 0) \ |
| 9196 | start = 0; \ |
| 9197 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9198 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9199 | Py_ssize_t |
| 9200 | PyUnicode_Count(PyObject *str, |
| 9201 | PyObject *substr, |
| 9202 | Py_ssize_t start, |
| 9203 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9204 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9205 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9206 | PyObject* str_obj; |
| 9207 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9208 | int kind1, kind2, kind; |
| 9209 | void *buf1 = NULL, *buf2 = NULL; |
| 9210 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9211 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9212 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9213 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9214 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9215 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9216 | if (!sub_obj) { |
| 9217 | Py_DECREF(str_obj); |
| 9218 | return -1; |
| 9219 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9220 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9221 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9222 | Py_DECREF(str_obj); |
| 9223 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9224 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9225 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9226 | kind1 = PyUnicode_KIND(str_obj); |
| 9227 | kind2 = PyUnicode_KIND(sub_obj); |
| 9228 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9229 | buf1 = PyUnicode_DATA(str_obj); |
| 9230 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9231 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9232 | if (!buf1) |
| 9233 | goto onError; |
| 9234 | buf2 = PyUnicode_DATA(sub_obj); |
| 9235 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9236 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9237 | if (!buf2) |
| 9238 | goto onError; |
| 9239 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9240 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9241 | |
| 9242 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9243 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9244 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9245 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9246 | result = asciilib_count( |
| 9247 | ((Py_UCS1*)buf1) + start, end - start, |
| 9248 | buf2, len2, PY_SSIZE_T_MAX |
| 9249 | ); |
| 9250 | else |
| 9251 | result = ucs1lib_count( |
| 9252 | ((Py_UCS1*)buf1) + start, end - start, |
| 9253 | buf2, len2, PY_SSIZE_T_MAX |
| 9254 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9255 | break; |
| 9256 | case PyUnicode_2BYTE_KIND: |
| 9257 | result = ucs2lib_count( |
| 9258 | ((Py_UCS2*)buf1) + start, end - start, |
| 9259 | buf2, len2, PY_SSIZE_T_MAX |
| 9260 | ); |
| 9261 | break; |
| 9262 | case PyUnicode_4BYTE_KIND: |
| 9263 | result = ucs4lib_count( |
| 9264 | ((Py_UCS4*)buf1) + start, end - start, |
| 9265 | buf2, len2, PY_SSIZE_T_MAX |
| 9266 | ); |
| 9267 | break; |
| 9268 | default: |
| 9269 | assert(0); result = 0; |
| 9270 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9271 | |
| 9272 | Py_DECREF(sub_obj); |
| 9273 | Py_DECREF(str_obj); |
| 9274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9275 | if (kind1 != kind) |
| 9276 | PyMem_Free(buf1); |
| 9277 | if (kind2 != kind) |
| 9278 | PyMem_Free(buf2); |
| 9279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9280 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | onError: |
| 9282 | Py_DECREF(sub_obj); |
| 9283 | Py_DECREF(str_obj); |
| 9284 | if (kind1 != kind && buf1) |
| 9285 | PyMem_Free(buf1); |
| 9286 | if (kind2 != kind && buf2) |
| 9287 | PyMem_Free(buf2); |
| 9288 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | } |
| 9290 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9291 | Py_ssize_t |
| 9292 | PyUnicode_Find(PyObject *str, |
| 9293 | PyObject *sub, |
| 9294 | Py_ssize_t start, |
| 9295 | Py_ssize_t end, |
| 9296 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9298 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9300 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9301 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9302 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9303 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9304 | if (!sub) { |
| 9305 | Py_DECREF(str); |
| 9306 | return -2; |
| 9307 | } |
| 9308 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9309 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9310 | Py_DECREF(str); |
| 9311 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9312 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9313 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9314 | result = any_find_slice(direction, |
| 9315 | str, sub, start, end |
| 9316 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9317 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9318 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9319 | Py_DECREF(sub); |
| 9320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | return result; |
| 9322 | } |
| 9323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9324 | Py_ssize_t |
| 9325 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9326 | Py_ssize_t start, Py_ssize_t end, |
| 9327 | int direction) |
| 9328 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9329 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9330 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | if (PyUnicode_READY(str) == -1) |
| 9332 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9333 | if (start < 0 || end < 0) { |
| 9334 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9335 | return -2; |
| 9336 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9338 | end = PyUnicode_GET_LENGTH(str); |
| 9339 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9340 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9341 | kind, end-start, ch, direction); |
| 9342 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9343 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9344 | else |
| 9345 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9346 | } |
| 9347 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9348 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9349 | tailmatch(PyObject *self, |
| 9350 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9351 | Py_ssize_t start, |
| 9352 | Py_ssize_t end, |
| 9353 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9355 | int kind_self; |
| 9356 | int kind_sub; |
| 9357 | void *data_self; |
| 9358 | void *data_sub; |
| 9359 | Py_ssize_t offset; |
| 9360 | Py_ssize_t i; |
| 9361 | Py_ssize_t end_sub; |
| 9362 | |
| 9363 | if (PyUnicode_READY(self) == -1 || |
| 9364 | PyUnicode_READY(substring) == -1) |
| 9365 | return 0; |
| 9366 | |
| 9367 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | return 1; |
| 9369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9370 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9371 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9373 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9375 | kind_self = PyUnicode_KIND(self); |
| 9376 | data_self = PyUnicode_DATA(self); |
| 9377 | kind_sub = PyUnicode_KIND(substring); |
| 9378 | data_sub = PyUnicode_DATA(substring); |
| 9379 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9380 | |
| 9381 | if (direction > 0) |
| 9382 | offset = end; |
| 9383 | else |
| 9384 | offset = start; |
| 9385 | |
| 9386 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9387 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9388 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9389 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9390 | /* If both are of the same kind, memcmp is sufficient */ |
| 9391 | if (kind_self == kind_sub) { |
| 9392 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9393 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9394 | data_sub, |
| 9395 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9396 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9397 | } |
| 9398 | /* otherwise we have to compare each character by first accesing it */ |
| 9399 | else { |
| 9400 | /* We do not need to compare 0 and len(substring)-1 because |
| 9401 | the if statement above ensured already that they are equal |
| 9402 | when we end up here. */ |
| 9403 | // TODO: honor direction and do a forward or backwards search |
| 9404 | for (i = 1; i < end_sub; ++i) { |
| 9405 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9406 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9407 | return 0; |
| 9408 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9409 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9411 | } |
| 9412 | |
| 9413 | return 0; |
| 9414 | } |
| 9415 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9416 | Py_ssize_t |
| 9417 | PyUnicode_Tailmatch(PyObject *str, |
| 9418 | PyObject *substr, |
| 9419 | Py_ssize_t start, |
| 9420 | Py_ssize_t end, |
| 9421 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9422 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9423 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9425 | str = PyUnicode_FromObject(str); |
| 9426 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9427 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9428 | substr = PyUnicode_FromObject(substr); |
| 9429 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9430 | Py_DECREF(str); |
| 9431 | return -1; |
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 | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9434 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9435 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9436 | Py_DECREF(str); |
| 9437 | Py_DECREF(substr); |
| 9438 | return result; |
| 9439 | } |
| 9440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9441 | /* Apply fixfct filter to the Unicode object self and return a |
| 9442 | reference to the modified object */ |
| 9443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9444 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9445 | fixup(PyObject *self, |
| 9446 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9447 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9448 | PyObject *u; |
| 9449 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9450 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9451 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9452 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9453 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9454 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9455 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9456 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9457 | /* fix functions return the new maximum character in a string, |
| 9458 | if the kind of the resulting unicode object does not change, |
| 9459 | everything is fine. Otherwise we need to change the string kind |
| 9460 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9461 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9462 | |
| 9463 | if (maxchar_new == 0) { |
| 9464 | /* no changes */; |
| 9465 | if (PyUnicode_CheckExact(self)) { |
| 9466 | Py_DECREF(u); |
| 9467 | Py_INCREF(self); |
| 9468 | return self; |
| 9469 | } |
| 9470 | else |
| 9471 | return u; |
| 9472 | } |
| 9473 | |
| 9474 | if (maxchar_new <= 127) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | maxchar_new = 127; |
| 9476 | else if (maxchar_new <= 255) |
| 9477 | maxchar_new = 255; |
| 9478 | else if (maxchar_new <= 65535) |
| 9479 | maxchar_new = 65535; |
| 9480 | else |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 9481 | maxchar_new = MAX_UNICODE; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9483 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9484 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9485 | |
| 9486 | /* In case the maximum character changed, we need to |
| 9487 | convert the string to the new category. */ |
| 9488 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9489 | if (v == NULL) { |
| 9490 | Py_DECREF(u); |
| 9491 | return NULL; |
| 9492 | } |
| 9493 | if (maxchar_new > maxchar_old) { |
| 9494 | /* If the maxchar increased so that the kind changed, not all |
| 9495 | characters are representable anymore and we need to fix the |
| 9496 | string again. This only happens in very few cases. */ |
| 9497 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9498 | maxchar_old = fixfct(v); |
| 9499 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9500 | } |
| 9501 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9502 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9504 | Py_DECREF(u); |
| 9505 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9506 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9507 | } |
| 9508 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9509 | static PyObject * |
| 9510 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9512 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9513 | char *resdata, *data = PyUnicode_DATA(self); |
| 9514 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9515 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9516 | res = PyUnicode_New(len, 127); |
| 9517 | if (res == NULL) |
| 9518 | return NULL; |
| 9519 | resdata = PyUnicode_DATA(res); |
| 9520 | if (lower) |
| 9521 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9522 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9523 | _Py_bytes_upper(resdata, data, len); |
| 9524 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9525 | } |
| 9526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9527 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9528 | 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] | 9529 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9530 | Py_ssize_t j; |
| 9531 | int final_sigma; |
| 9532 | Py_UCS4 c; |
| 9533 | /* 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] | 9534 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9535 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9536 | |
| 9537 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9538 | */ |
| 9539 | for (j = i - 1; j >= 0; j--) { |
| 9540 | c = PyUnicode_READ(kind, data, j); |
| 9541 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9542 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9543 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9544 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9545 | if (final_sigma) { |
| 9546 | for (j = i + 1; j < length; j++) { |
| 9547 | c = PyUnicode_READ(kind, data, j); |
| 9548 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9549 | break; |
| 9550 | } |
| 9551 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9552 | } |
| 9553 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9554 | } |
| 9555 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9556 | static int |
| 9557 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9558 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9559 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9560 | /* Obscure special case. */ |
| 9561 | if (c == 0x3A3) { |
| 9562 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9563 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9565 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9568 | static Py_ssize_t |
| 9569 | 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] | 9570 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9571 | Py_ssize_t i, k = 0; |
| 9572 | int n_res, j; |
| 9573 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9574 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9575 | c = PyUnicode_READ(kind, data, 0); |
| 9576 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9577 | for (j = 0; j < n_res; j++) { |
| 9578 | if (mapped[j] > *maxchar) |
| 9579 | *maxchar = mapped[j]; |
| 9580 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9582 | for (i = 1; i < length; i++) { |
| 9583 | c = PyUnicode_READ(kind, data, i); |
| 9584 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9585 | for (j = 0; j < n_res; j++) { |
| 9586 | if (mapped[j] > *maxchar) |
| 9587 | *maxchar = mapped[j]; |
| 9588 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9589 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9590 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9591 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9592 | } |
| 9593 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9594 | static Py_ssize_t |
| 9595 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9596 | Py_ssize_t i, k = 0; |
| 9597 | |
| 9598 | for (i = 0; i < length; i++) { |
| 9599 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9600 | int n_res, j; |
| 9601 | if (Py_UNICODE_ISUPPER(c)) { |
| 9602 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9603 | } |
| 9604 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9605 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9606 | } |
| 9607 | else { |
| 9608 | n_res = 1; |
| 9609 | mapped[0] = c; |
| 9610 | } |
| 9611 | for (j = 0; j < n_res; j++) { |
| 9612 | if (mapped[j] > *maxchar) |
| 9613 | *maxchar = mapped[j]; |
| 9614 | res[k++] = mapped[j]; |
| 9615 | } |
| 9616 | } |
| 9617 | return k; |
| 9618 | } |
| 9619 | |
| 9620 | static Py_ssize_t |
| 9621 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9622 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9623 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9624 | Py_ssize_t i, k = 0; |
| 9625 | |
| 9626 | for (i = 0; i < length; i++) { |
| 9627 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9628 | int n_res, j; |
| 9629 | if (lower) |
| 9630 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9631 | else |
| 9632 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9633 | for (j = 0; j < n_res; j++) { |
| 9634 | if (mapped[j] > *maxchar) |
| 9635 | *maxchar = mapped[j]; |
| 9636 | res[k++] = mapped[j]; |
| 9637 | } |
| 9638 | } |
| 9639 | return k; |
| 9640 | } |
| 9641 | |
| 9642 | static Py_ssize_t |
| 9643 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9644 | { |
| 9645 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9646 | } |
| 9647 | |
| 9648 | static Py_ssize_t |
| 9649 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9650 | { |
| 9651 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9652 | } |
| 9653 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9654 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9655 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9656 | { |
| 9657 | Py_ssize_t i, k = 0; |
| 9658 | |
| 9659 | for (i = 0; i < length; i++) { |
| 9660 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9661 | Py_UCS4 mapped[3]; |
| 9662 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9663 | for (j = 0; j < n_res; j++) { |
| 9664 | if (mapped[j] > *maxchar) |
| 9665 | *maxchar = mapped[j]; |
| 9666 | res[k++] = mapped[j]; |
| 9667 | } |
| 9668 | } |
| 9669 | return k; |
| 9670 | } |
| 9671 | |
| 9672 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9673 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9674 | { |
| 9675 | Py_ssize_t i, k = 0; |
| 9676 | int previous_is_cased; |
| 9677 | |
| 9678 | previous_is_cased = 0; |
| 9679 | for (i = 0; i < length; i++) { |
| 9680 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9681 | Py_UCS4 mapped[3]; |
| 9682 | int n_res, j; |
| 9683 | |
| 9684 | if (previous_is_cased) |
| 9685 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9686 | else |
| 9687 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9688 | |
| 9689 | for (j = 0; j < n_res; j++) { |
| 9690 | if (mapped[j] > *maxchar) |
| 9691 | *maxchar = mapped[j]; |
| 9692 | res[k++] = mapped[j]; |
| 9693 | } |
| 9694 | |
| 9695 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9696 | } |
| 9697 | return k; |
| 9698 | } |
| 9699 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9700 | static PyObject * |
| 9701 | case_operation(PyObject *self, |
| 9702 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9703 | { |
| 9704 | PyObject *res = NULL; |
| 9705 | Py_ssize_t length, newlength = 0; |
| 9706 | int kind, outkind; |
| 9707 | void *data, *outdata; |
| 9708 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9709 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9710 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9711 | |
| 9712 | kind = PyUnicode_KIND(self); |
| 9713 | data = PyUnicode_DATA(self); |
| 9714 | length = PyUnicode_GET_LENGTH(self); |
| 9715 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9716 | if (tmp == NULL) |
| 9717 | return PyErr_NoMemory(); |
| 9718 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9719 | res = PyUnicode_New(newlength, maxchar); |
| 9720 | if (res == NULL) |
| 9721 | goto leave; |
| 9722 | tmpend = tmp + newlength; |
| 9723 | outdata = PyUnicode_DATA(res); |
| 9724 | outkind = PyUnicode_KIND(res); |
| 9725 | switch (outkind) { |
| 9726 | case PyUnicode_1BYTE_KIND: |
| 9727 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9728 | break; |
| 9729 | case PyUnicode_2BYTE_KIND: |
| 9730 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9731 | break; |
| 9732 | case PyUnicode_4BYTE_KIND: |
| 9733 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9734 | break; |
| 9735 | default: |
| 9736 | assert(0); |
| 9737 | break; |
| 9738 | } |
| 9739 | leave: |
| 9740 | PyMem_FREE(tmp); |
| 9741 | return res; |
| 9742 | } |
| 9743 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9744 | PyObject * |
| 9745 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9746 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9747 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9748 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9749 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9750 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9751 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9752 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9753 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9754 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9755 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9757 | int use_memcpy; |
| 9758 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9759 | PyObject *last_obj; |
| 9760 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9761 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9762 | fseq = PySequence_Fast(seq, ""); |
| 9763 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9764 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9765 | } |
| 9766 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9767 | /* NOTE: the following code can't call back into Python code, |
| 9768 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9769 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9770 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9771 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9772 | /* If empty sequence, return u"". */ |
| 9773 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9774 | Py_DECREF(fseq); |
| 9775 | Py_INCREF(unicode_empty); |
| 9776 | res = unicode_empty; |
| 9777 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9778 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9779 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9780 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9781 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9782 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9783 | if (seqlen == 1) { |
| 9784 | if (PyUnicode_CheckExact(items[0])) { |
| 9785 | res = items[0]; |
| 9786 | Py_INCREF(res); |
| 9787 | Py_DECREF(fseq); |
| 9788 | return res; |
| 9789 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9790 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9791 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9792 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9793 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9794 | /* Set up sep and seplen */ |
| 9795 | if (separator == NULL) { |
| 9796 | /* fall back to a blank space separator */ |
| 9797 | sep = PyUnicode_FromOrdinal(' '); |
| 9798 | if (!sep) |
| 9799 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9800 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9801 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9802 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9803 | else { |
| 9804 | if (!PyUnicode_Check(separator)) { |
| 9805 | PyErr_Format(PyExc_TypeError, |
| 9806 | "separator: expected str instance," |
| 9807 | " %.80s found", |
| 9808 | Py_TYPE(separator)->tp_name); |
| 9809 | goto onError; |
| 9810 | } |
| 9811 | if (PyUnicode_READY(separator)) |
| 9812 | goto onError; |
| 9813 | sep = separator; |
| 9814 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9815 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9816 | /* inc refcount to keep this code path symmetric with the |
| 9817 | above case of a blank separator */ |
| 9818 | Py_INCREF(sep); |
| 9819 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9820 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9821 | } |
| 9822 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9823 | /* There are at least two things to join, or else we have a subclass |
| 9824 | * of str in the sequence. |
| 9825 | * Do a pre-pass to figure out the total amount of space we'll |
| 9826 | * need (sz), and see whether all argument are strings. |
| 9827 | */ |
| 9828 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9829 | #ifdef Py_DEBUG |
| 9830 | use_memcpy = 0; |
| 9831 | #else |
| 9832 | use_memcpy = 1; |
| 9833 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9834 | for (i = 0; i < seqlen; i++) { |
| 9835 | const Py_ssize_t old_sz = sz; |
| 9836 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9837 | if (!PyUnicode_Check(item)) { |
| 9838 | PyErr_Format(PyExc_TypeError, |
| 9839 | "sequence item %zd: expected str instance," |
| 9840 | " %.80s found", |
| 9841 | i, Py_TYPE(item)->tp_name); |
| 9842 | goto onError; |
| 9843 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9844 | if (PyUnicode_READY(item) == -1) |
| 9845 | goto onError; |
| 9846 | sz += PyUnicode_GET_LENGTH(item); |
| 9847 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9848 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9849 | if (i != 0) |
| 9850 | sz += seplen; |
| 9851 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9852 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9853 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9854 | goto onError; |
| 9855 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9856 | if (use_memcpy && last_obj != NULL) { |
| 9857 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9858 | use_memcpy = 0; |
| 9859 | } |
| 9860 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9861 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9862 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9863 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9864 | if (res == NULL) |
| 9865 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9866 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9867 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9868 | #ifdef Py_DEBUG |
| 9869 | use_memcpy = 0; |
| 9870 | #else |
| 9871 | if (use_memcpy) { |
| 9872 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9873 | kind = PyUnicode_KIND(res); |
| 9874 | if (seplen != 0) |
| 9875 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9876 | } |
| 9877 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9878 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9879 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9880 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9881 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9882 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9883 | if (use_memcpy) { |
| 9884 | Py_MEMCPY(res_data, |
| 9885 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9886 | kind * seplen); |
| 9887 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9888 | } |
| 9889 | else { |
| 9890 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9891 | res_offset += seplen; |
| 9892 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9893 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9894 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9895 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9896 | if (use_memcpy) { |
| 9897 | Py_MEMCPY(res_data, |
| 9898 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9899 | kind * itemlen); |
| 9900 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9901 | } |
| 9902 | else { |
| 9903 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9904 | res_offset += itemlen; |
| 9905 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9906 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9907 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9908 | if (use_memcpy) |
| 9909 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9910 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9911 | else |
| 9912 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9913 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9914 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9916 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9919 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9920 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9922 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | return NULL; |
| 9924 | } |
| 9925 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9926 | #define FILL(kind, data, value, start, length) \ |
| 9927 | do { \ |
| 9928 | Py_ssize_t i_ = 0; \ |
| 9929 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9930 | switch ((kind)) { \ |
| 9931 | case PyUnicode_1BYTE_KIND: { \ |
| 9932 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9933 | memset(to_, (unsigned char)value, length); \ |
| 9934 | break; \ |
| 9935 | } \ |
| 9936 | case PyUnicode_2BYTE_KIND: { \ |
| 9937 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9938 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9939 | break; \ |
| 9940 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9941 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9943 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9944 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9945 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9946 | } \ |
| 9947 | } \ |
| 9948 | } while (0) |
| 9949 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9950 | Py_ssize_t |
| 9951 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9952 | Py_UCS4 fill_char) |
| 9953 | { |
| 9954 | Py_ssize_t maxlen; |
| 9955 | enum PyUnicode_Kind kind; |
| 9956 | void *data; |
| 9957 | |
| 9958 | if (!PyUnicode_Check(unicode)) { |
| 9959 | PyErr_BadInternalCall(); |
| 9960 | return -1; |
| 9961 | } |
| 9962 | if (PyUnicode_READY(unicode) == -1) |
| 9963 | return -1; |
| 9964 | if (unicode_check_modifiable(unicode)) |
| 9965 | return -1; |
| 9966 | |
| 9967 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9968 | PyErr_SetString(PyExc_ValueError, |
| 9969 | "fill character is bigger than " |
| 9970 | "the string maximum character"); |
| 9971 | return -1; |
| 9972 | } |
| 9973 | |
| 9974 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9975 | length = Py_MIN(maxlen, length); |
| 9976 | if (length <= 0) |
| 9977 | return 0; |
| 9978 | |
| 9979 | kind = PyUnicode_KIND(unicode); |
| 9980 | data = PyUnicode_DATA(unicode); |
| 9981 | FILL(kind, data, fill_char, start, length); |
| 9982 | return length; |
| 9983 | } |
| 9984 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9985 | static PyObject * |
| 9986 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9987 | Py_ssize_t left, |
| 9988 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9989 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | PyObject *u; |
| 9992 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9993 | int kind; |
| 9994 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | |
| 9996 | if (left < 0) |
| 9997 | left = 0; |
| 9998 | if (right < 0) |
| 9999 | right = 0; |
| 10000 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10001 | if (left == 0 && right == 0) |
| 10002 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10003 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10005 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10006 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10007 | return NULL; |
| 10008 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10010 | if (fill > maxchar) |
| 10011 | maxchar = fill; |
| 10012 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10013 | if (!u) |
| 10014 | return NULL; |
| 10015 | |
| 10016 | kind = PyUnicode_KIND(u); |
| 10017 | data = PyUnicode_DATA(u); |
| 10018 | if (left) |
| 10019 | FILL(kind, data, fill, 0, left); |
| 10020 | if (right) |
| 10021 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10022 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10023 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10024 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10025 | } |
| 10026 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10027 | PyObject * |
| 10028 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10029 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10030 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10031 | |
| 10032 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10033 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10034 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10035 | if (PyUnicode_READY(string) == -1) { |
| 10036 | Py_DECREF(string); |
| 10037 | return NULL; |
| 10038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10039 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10040 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10042 | if (PyUnicode_IS_ASCII(string)) |
| 10043 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10044 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10045 | PyUnicode_GET_LENGTH(string), keepends); |
| 10046 | else |
| 10047 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10048 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10049 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10050 | break; |
| 10051 | case PyUnicode_2BYTE_KIND: |
| 10052 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10053 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10054 | PyUnicode_GET_LENGTH(string), keepends); |
| 10055 | break; |
| 10056 | case PyUnicode_4BYTE_KIND: |
| 10057 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10058 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | PyUnicode_GET_LENGTH(string), keepends); |
| 10060 | break; |
| 10061 | default: |
| 10062 | assert(0); |
| 10063 | list = 0; |
| 10064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10065 | Py_DECREF(string); |
| 10066 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10067 | } |
| 10068 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10069 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10070 | split(PyObject *self, |
| 10071 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10072 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10073 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10074 | int kind1, kind2, kind; |
| 10075 | void *buf1, *buf2; |
| 10076 | Py_ssize_t len1, len2; |
| 10077 | PyObject* out; |
| 10078 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10079 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10080 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10081 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | if (PyUnicode_READY(self) == -1) |
| 10083 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10086 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10088 | if (PyUnicode_IS_ASCII(self)) |
| 10089 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10090 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10091 | PyUnicode_GET_LENGTH(self), maxcount |
| 10092 | ); |
| 10093 | else |
| 10094 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10095 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10096 | PyUnicode_GET_LENGTH(self), maxcount |
| 10097 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10098 | case PyUnicode_2BYTE_KIND: |
| 10099 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10100 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10101 | PyUnicode_GET_LENGTH(self), maxcount |
| 10102 | ); |
| 10103 | case PyUnicode_4BYTE_KIND: |
| 10104 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10105 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10106 | PyUnicode_GET_LENGTH(self), maxcount |
| 10107 | ); |
| 10108 | default: |
| 10109 | assert(0); |
| 10110 | return NULL; |
| 10111 | } |
| 10112 | |
| 10113 | if (PyUnicode_READY(substring) == -1) |
| 10114 | return NULL; |
| 10115 | |
| 10116 | kind1 = PyUnicode_KIND(self); |
| 10117 | kind2 = PyUnicode_KIND(substring); |
| 10118 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10119 | buf1 = PyUnicode_DATA(self); |
| 10120 | buf2 = PyUnicode_DATA(substring); |
| 10121 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10122 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | if (!buf1) |
| 10124 | return NULL; |
| 10125 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10126 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10127 | if (!buf2) { |
| 10128 | if (kind1 != kind) PyMem_Free(buf1); |
| 10129 | return NULL; |
| 10130 | } |
| 10131 | len1 = PyUnicode_GET_LENGTH(self); |
| 10132 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10133 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10134 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10136 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10137 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10138 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10139 | else |
| 10140 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10141 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10142 | break; |
| 10143 | case PyUnicode_2BYTE_KIND: |
| 10144 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10145 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10146 | break; |
| 10147 | case PyUnicode_4BYTE_KIND: |
| 10148 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10149 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | break; |
| 10151 | default: |
| 10152 | out = NULL; |
| 10153 | } |
| 10154 | if (kind1 != kind) |
| 10155 | PyMem_Free(buf1); |
| 10156 | if (kind2 != kind) |
| 10157 | PyMem_Free(buf2); |
| 10158 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10159 | } |
| 10160 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10161 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10162 | rsplit(PyObject *self, |
| 10163 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10164 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10165 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | int kind1, kind2, kind; |
| 10167 | void *buf1, *buf2; |
| 10168 | Py_ssize_t len1, len2; |
| 10169 | PyObject* out; |
| 10170 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10171 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10172 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | if (PyUnicode_READY(self) == -1) |
| 10175 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10178 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10180 | if (PyUnicode_IS_ASCII(self)) |
| 10181 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10182 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10183 | PyUnicode_GET_LENGTH(self), maxcount |
| 10184 | ); |
| 10185 | else |
| 10186 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10187 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10188 | PyUnicode_GET_LENGTH(self), maxcount |
| 10189 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | case PyUnicode_2BYTE_KIND: |
| 10191 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10192 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10193 | PyUnicode_GET_LENGTH(self), maxcount |
| 10194 | ); |
| 10195 | case PyUnicode_4BYTE_KIND: |
| 10196 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10197 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | PyUnicode_GET_LENGTH(self), maxcount |
| 10199 | ); |
| 10200 | default: |
| 10201 | assert(0); |
| 10202 | return NULL; |
| 10203 | } |
| 10204 | |
| 10205 | if (PyUnicode_READY(substring) == -1) |
| 10206 | return NULL; |
| 10207 | |
| 10208 | kind1 = PyUnicode_KIND(self); |
| 10209 | kind2 = PyUnicode_KIND(substring); |
| 10210 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10211 | buf1 = PyUnicode_DATA(self); |
| 10212 | buf2 = PyUnicode_DATA(substring); |
| 10213 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10214 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | if (!buf1) |
| 10216 | return NULL; |
| 10217 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10218 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10219 | if (!buf2) { |
| 10220 | if (kind1 != kind) PyMem_Free(buf1); |
| 10221 | return NULL; |
| 10222 | } |
| 10223 | len1 = PyUnicode_GET_LENGTH(self); |
| 10224 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10225 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10226 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10228 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10229 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10230 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10231 | else |
| 10232 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10233 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 | break; |
| 10235 | case PyUnicode_2BYTE_KIND: |
| 10236 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10237 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | break; |
| 10239 | case PyUnicode_4BYTE_KIND: |
| 10240 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10241 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | break; |
| 10243 | default: |
| 10244 | out = NULL; |
| 10245 | } |
| 10246 | if (kind1 != kind) |
| 10247 | PyMem_Free(buf1); |
| 10248 | if (kind2 != kind) |
| 10249 | PyMem_Free(buf2); |
| 10250 | return out; |
| 10251 | } |
| 10252 | |
| 10253 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10254 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10255 | 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] | 10256 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10257 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10259 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10260 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10261 | else |
| 10262 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | case PyUnicode_2BYTE_KIND: |
| 10264 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10265 | case PyUnicode_4BYTE_KIND: |
| 10266 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10267 | } |
| 10268 | assert(0); |
| 10269 | return -1; |
| 10270 | } |
| 10271 | |
| 10272 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10273 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10274 | 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] | 10275 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10276 | switch (kind) { |
| 10277 | case PyUnicode_1BYTE_KIND: |
| 10278 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10279 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10280 | else |
| 10281 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10282 | case PyUnicode_2BYTE_KIND: |
| 10283 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10284 | case PyUnicode_4BYTE_KIND: |
| 10285 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10286 | } |
| 10287 | assert(0); |
| 10288 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10289 | } |
| 10290 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10291 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10292 | replace(PyObject *self, PyObject *str1, |
| 10293 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10294 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | PyObject *u; |
| 10296 | char *sbuf = PyUnicode_DATA(self); |
| 10297 | char *buf1 = PyUnicode_DATA(str1); |
| 10298 | char *buf2 = PyUnicode_DATA(str2); |
| 10299 | int srelease = 0, release1 = 0, release2 = 0; |
| 10300 | int skind = PyUnicode_KIND(self); |
| 10301 | int kind1 = PyUnicode_KIND(str1); |
| 10302 | int kind2 = PyUnicode_KIND(str2); |
| 10303 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10304 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10305 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10306 | int mayshrink; |
| 10307 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10308 | |
| 10309 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10310 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10312 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10313 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10314 | if (str1 == str2) |
| 10315 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10316 | if (skind < kind1) |
| 10317 | /* substring too wide to be present */ |
| 10318 | goto nothing; |
| 10319 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10320 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10321 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10322 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10323 | result string. */ |
| 10324 | mayshrink = (maxchar_str2 < maxchar); |
| 10325 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10326 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10327 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10328 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10329 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10330 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10332 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10333 | Py_UCS4 u1, u2; |
| 10334 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10335 | Py_ssize_t index, pos; |
| 10336 | char *src; |
| 10337 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10339 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10340 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10341 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10342 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10343 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10344 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10345 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10346 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10347 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10348 | |
| 10349 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10350 | index = 0; |
| 10351 | src = sbuf; |
| 10352 | while (--maxcount) |
| 10353 | { |
| 10354 | pos++; |
| 10355 | src += pos * PyUnicode_KIND(self); |
| 10356 | slen -= pos; |
| 10357 | index += pos; |
| 10358 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10359 | if (pos < 0) |
| 10360 | break; |
| 10361 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10362 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10363 | } |
| 10364 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10365 | int rkind = skind; |
| 10366 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10367 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10368 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10369 | if (kind1 < rkind) { |
| 10370 | /* widen substring */ |
| 10371 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10372 | if (!buf1) goto error; |
| 10373 | release1 = 1; |
| 10374 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10375 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10376 | if (i < 0) |
| 10377 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | if (rkind > kind2) { |
| 10379 | /* widen replacement */ |
| 10380 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10381 | if (!buf2) goto error; |
| 10382 | release2 = 1; |
| 10383 | } |
| 10384 | else if (rkind < kind2) { |
| 10385 | /* widen self and buf1 */ |
| 10386 | rkind = kind2; |
| 10387 | if (release1) PyMem_Free(buf1); |
| 10388 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10389 | if (!sbuf) goto error; |
| 10390 | srelease = 1; |
| 10391 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10392 | if (!buf1) goto error; |
| 10393 | release1 = 1; |
| 10394 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10395 | u = PyUnicode_New(slen, maxchar); |
| 10396 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10397 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10398 | assert(PyUnicode_KIND(u) == rkind); |
| 10399 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10400 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10401 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10402 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10403 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10404 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10405 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10406 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10407 | |
| 10408 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10409 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10410 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10411 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10412 | if (i == -1) |
| 10413 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10414 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10415 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10416 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10417 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10418 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10419 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10420 | } |
| 10421 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10422 | Py_ssize_t n, i, j, ires; |
| 10423 | Py_ssize_t product, new_size; |
| 10424 | int rkind = skind; |
| 10425 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10427 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10428 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10429 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10430 | if (!buf1) goto error; |
| 10431 | release1 = 1; |
| 10432 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10433 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10434 | if (n == 0) |
| 10435 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10437 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10438 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10439 | if (!buf2) goto error; |
| 10440 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10441 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10442 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10443 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10444 | rkind = kind2; |
| 10445 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10446 | if (!sbuf) goto error; |
| 10447 | srelease = 1; |
| 10448 | if (release1) PyMem_Free(buf1); |
| 10449 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10450 | if (!buf1) goto error; |
| 10451 | release1 = 1; |
| 10452 | } |
| 10453 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10454 | PyUnicode_GET_LENGTH(str1))); */ |
| 10455 | product = n * (len2-len1); |
| 10456 | if ((product / (len2-len1)) != n) { |
| 10457 | PyErr_SetString(PyExc_OverflowError, |
| 10458 | "replace string is too long"); |
| 10459 | goto error; |
| 10460 | } |
| 10461 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10462 | if (new_size == 0) { |
| 10463 | Py_INCREF(unicode_empty); |
| 10464 | u = unicode_empty; |
| 10465 | goto done; |
| 10466 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10467 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10468 | PyErr_SetString(PyExc_OverflowError, |
| 10469 | "replace string is too long"); |
| 10470 | goto error; |
| 10471 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10472 | u = PyUnicode_New(new_size, maxchar); |
| 10473 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10474 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10475 | assert(PyUnicode_KIND(u) == rkind); |
| 10476 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10477 | ires = i = 0; |
| 10478 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10479 | while (n-- > 0) { |
| 10480 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10481 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10482 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10483 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10484 | if (j == -1) |
| 10485 | break; |
| 10486 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10487 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10488 | memcpy(res + rkind * ires, |
| 10489 | sbuf + rkind * i, |
| 10490 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10492 | } |
| 10493 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10495 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10496 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10497 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10499 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10500 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10501 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10502 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10503 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10504 | memcpy(res + rkind * ires, |
| 10505 | sbuf + rkind * i, |
| 10506 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10507 | } |
| 10508 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10509 | /* interleave */ |
| 10510 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10511 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10512 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10513 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10514 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10515 | if (--n <= 0) |
| 10516 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10517 | memcpy(res + rkind * ires, |
| 10518 | sbuf + rkind * i, |
| 10519 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | ires++; |
| 10521 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10522 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10523 | memcpy(res + rkind * ires, |
| 10524 | sbuf + rkind * i, |
| 10525 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10526 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10527 | } |
| 10528 | |
| 10529 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10530 | unicode_adjust_maxchar(&u); |
| 10531 | if (u == NULL) |
| 10532 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10533 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10534 | |
| 10535 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10536 | if (srelease) |
| 10537 | PyMem_FREE(sbuf); |
| 10538 | if (release1) |
| 10539 | PyMem_FREE(buf1); |
| 10540 | if (release2) |
| 10541 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10542 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10543 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10544 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10545 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10546 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10547 | if (srelease) |
| 10548 | PyMem_FREE(sbuf); |
| 10549 | if (release1) |
| 10550 | PyMem_FREE(buf1); |
| 10551 | if (release2) |
| 10552 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10553 | return unicode_result_unchanged(self); |
| 10554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | error: |
| 10556 | if (srelease && sbuf) |
| 10557 | PyMem_FREE(sbuf); |
| 10558 | if (release1 && buf1) |
| 10559 | PyMem_FREE(buf1); |
| 10560 | if (release2 && buf2) |
| 10561 | PyMem_FREE(buf2); |
| 10562 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10563 | } |
| 10564 | |
| 10565 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10567 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10568 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10569 | \n\ |
| 10570 | 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] | 10571 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10572 | |
| 10573 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10574 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10575 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10576 | if (PyUnicode_READY(self) == -1) |
| 10577 | return NULL; |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10578 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10579 | } |
| 10580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10581 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10582 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 | \n\ |
| 10584 | 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] | 10585 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | |
| 10587 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10588 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10589 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10590 | if (PyUnicode_READY(self) == -1) |
| 10591 | return NULL; |
| 10592 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10593 | return unicode_result_unchanged(self); |
| 10594 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10595 | } |
| 10596 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10597 | PyDoc_STRVAR(casefold__doc__, |
| 10598 | "S.casefold() -> str\n\ |
| 10599 | \n\ |
| 10600 | Return a version of S suitable for caseless comparisons."); |
| 10601 | |
| 10602 | static PyObject * |
| 10603 | unicode_casefold(PyObject *self) |
| 10604 | { |
| 10605 | if (PyUnicode_READY(self) == -1) |
| 10606 | return NULL; |
| 10607 | if (PyUnicode_IS_ASCII(self)) |
| 10608 | return ascii_upper_or_lower(self, 1); |
| 10609 | return case_operation(self, do_casefold); |
| 10610 | } |
| 10611 | |
| 10612 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10613 | /* Argument converter. Coerces to a single unicode character */ |
| 10614 | |
| 10615 | static int |
| 10616 | convert_uc(PyObject *obj, void *addr) |
| 10617 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10619 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10620 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10621 | uniobj = PyUnicode_FromObject(obj); |
| 10622 | if (uniobj == NULL) { |
| 10623 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10624 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10625 | return 0; |
| 10626 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10627 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10628 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10629 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10630 | Py_DECREF(uniobj); |
| 10631 | return 0; |
| 10632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10633 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10634 | Py_DECREF(uniobj); |
| 10635 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10636 | } |
| 10637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10638 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10639 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10641 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10642 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10643 | |
| 10644 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10645 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10646 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10647 | Py_ssize_t marg, left; |
| 10648 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10649 | Py_UCS4 fillchar = ' '; |
| 10650 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10651 | 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] | 10652 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10653 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10654 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10655 | return NULL; |
| 10656 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10657 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10658 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10659 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10660 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10661 | left = marg / 2 + (marg & width & 1); |
| 10662 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10663 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | } |
| 10665 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10666 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10667 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10668 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10669 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10670 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 | int kind1, kind2; |
| 10672 | void *data1, *data2; |
| 10673 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10675 | kind1 = PyUnicode_KIND(str1); |
| 10676 | kind2 = PyUnicode_KIND(str2); |
| 10677 | data1 = PyUnicode_DATA(str1); |
| 10678 | data2 = PyUnicode_DATA(str2); |
| 10679 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10680 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10681 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10682 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10683 | Py_UCS4 c1, c2; |
| 10684 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10685 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10686 | |
| 10687 | if (c1 != c2) |
| 10688 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10689 | } |
| 10690 | |
| 10691 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10692 | } |
| 10693 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10694 | int |
| 10695 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10697 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10698 | if (PyUnicode_READY(left) == -1 || |
| 10699 | PyUnicode_READY(right) == -1) |
| 10700 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10701 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10702 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10703 | PyErr_Format(PyExc_TypeError, |
| 10704 | "Can't compare %.100s and %.100s", |
| 10705 | left->ob_type->tp_name, |
| 10706 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10707 | return -1; |
| 10708 | } |
| 10709 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10710 | int |
| 10711 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10712 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | Py_ssize_t i; |
| 10714 | int kind; |
| 10715 | void *data; |
| 10716 | Py_UCS4 chr; |
| 10717 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10718 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10719 | if (PyUnicode_READY(uni) == -1) |
| 10720 | return -1; |
| 10721 | kind = PyUnicode_KIND(uni); |
| 10722 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10723 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10724 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10725 | if (chr != str[i]) |
| 10726 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10727 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10728 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10729 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10730 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10731 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10732 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10733 | return 0; |
| 10734 | } |
| 10735 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10736 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10737 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10738 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10739 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10740 | PyObject * |
| 10741 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10742 | { |
| 10743 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10744 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10745 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10746 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | if (PyUnicode_READY(left) == -1 || |
| 10748 | PyUnicode_READY(right) == -1) |
| 10749 | return NULL; |
| 10750 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10751 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10752 | if (op == Py_EQ) { |
| 10753 | Py_INCREF(Py_False); |
| 10754 | return Py_False; |
| 10755 | } |
| 10756 | if (op == Py_NE) { |
| 10757 | Py_INCREF(Py_True); |
| 10758 | return Py_True; |
| 10759 | } |
| 10760 | } |
| 10761 | if (left == right) |
| 10762 | result = 0; |
| 10763 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10764 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10765 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10766 | /* Convert the return value to a Boolean */ |
| 10767 | switch (op) { |
| 10768 | case Py_EQ: |
| 10769 | v = TEST_COND(result == 0); |
| 10770 | break; |
| 10771 | case Py_NE: |
| 10772 | v = TEST_COND(result != 0); |
| 10773 | break; |
| 10774 | case Py_LE: |
| 10775 | v = TEST_COND(result <= 0); |
| 10776 | break; |
| 10777 | case Py_GE: |
| 10778 | v = TEST_COND(result >= 0); |
| 10779 | break; |
| 10780 | case Py_LT: |
| 10781 | v = TEST_COND(result == -1); |
| 10782 | break; |
| 10783 | case Py_GT: |
| 10784 | v = TEST_COND(result == 1); |
| 10785 | break; |
| 10786 | default: |
| 10787 | PyErr_BadArgument(); |
| 10788 | return NULL; |
| 10789 | } |
| 10790 | Py_INCREF(v); |
| 10791 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10792 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10793 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10794 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10795 | } |
| 10796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10797 | int |
| 10798 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10799 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10800 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10801 | int kind1, kind2, kind; |
| 10802 | void *buf1, *buf2; |
| 10803 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10804 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10805 | |
| 10806 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10807 | sub = PyUnicode_FromObject(element); |
| 10808 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10809 | PyErr_Format(PyExc_TypeError, |
| 10810 | "'in <string>' requires string as left operand, not %s", |
| 10811 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10812 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10813 | } |
| 10814 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10815 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10816 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10817 | Py_DECREF(sub); |
| 10818 | return -1; |
| 10819 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10820 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10821 | Py_DECREF(sub); |
| 10822 | Py_DECREF(str); |
| 10823 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10824 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10825 | kind1 = PyUnicode_KIND(str); |
| 10826 | kind2 = PyUnicode_KIND(sub); |
| 10827 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10828 | buf1 = PyUnicode_DATA(str); |
| 10829 | buf2 = PyUnicode_DATA(sub); |
| 10830 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10831 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10832 | if (!buf1) { |
| 10833 | Py_DECREF(sub); |
| 10834 | return -1; |
| 10835 | } |
| 10836 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10837 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | if (!buf2) { |
| 10839 | Py_DECREF(sub); |
| 10840 | if (kind1 != kind) PyMem_Free(buf1); |
| 10841 | return -1; |
| 10842 | } |
| 10843 | len1 = PyUnicode_GET_LENGTH(str); |
| 10844 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10845 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10846 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10847 | case PyUnicode_1BYTE_KIND: |
| 10848 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10849 | break; |
| 10850 | case PyUnicode_2BYTE_KIND: |
| 10851 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10852 | break; |
| 10853 | case PyUnicode_4BYTE_KIND: |
| 10854 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10855 | break; |
| 10856 | default: |
| 10857 | result = -1; |
| 10858 | assert(0); |
| 10859 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10860 | |
| 10861 | Py_DECREF(str); |
| 10862 | Py_DECREF(sub); |
| 10863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10864 | if (kind1 != kind) |
| 10865 | PyMem_Free(buf1); |
| 10866 | if (kind2 != kind) |
| 10867 | PyMem_Free(buf2); |
| 10868 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10869 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10870 | } |
| 10871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10872 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10874 | PyObject * |
| 10875 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10876 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10877 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10878 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10879 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | |
| 10881 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10882 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10883 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10884 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10885 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10886 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10887 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10888 | |
| 10889 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10890 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10891 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10893 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10894 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10895 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10896 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10897 | } |
| 10898 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10899 | u_len = PyUnicode_GET_LENGTH(u); |
| 10900 | v_len = PyUnicode_GET_LENGTH(v); |
| 10901 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10902 | PyErr_SetString(PyExc_OverflowError, |
| 10903 | "strings are too large to concat"); |
| 10904 | goto onError; |
| 10905 | } |
| 10906 | new_len = u_len + v_len; |
| 10907 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10908 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10909 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10910 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10912 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10913 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10914 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10915 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10916 | copy_characters(w, 0, u, 0, u_len); |
| 10917 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10918 | Py_DECREF(u); |
| 10919 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10920 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10922 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10923 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | Py_XDECREF(u); |
| 10925 | Py_XDECREF(v); |
| 10926 | return NULL; |
| 10927 | } |
| 10928 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10929 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10930 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10931 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10932 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10933 | Py_UCS4 maxchar, maxchar2; |
| 10934 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10935 | |
| 10936 | if (p_left == NULL) { |
| 10937 | if (!PyErr_Occurred()) |
| 10938 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10939 | return; |
| 10940 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10941 | left = *p_left; |
| 10942 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10943 | if (!PyErr_Occurred()) |
| 10944 | PyErr_BadInternalCall(); |
| 10945 | goto error; |
| 10946 | } |
| 10947 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10948 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10949 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10950 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10951 | goto error; |
| 10952 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10953 | /* Shortcuts */ |
| 10954 | if (left == unicode_empty) { |
| 10955 | Py_DECREF(left); |
| 10956 | Py_INCREF(right); |
| 10957 | *p_left = right; |
| 10958 | return; |
| 10959 | } |
| 10960 | if (right == unicode_empty) |
| 10961 | return; |
| 10962 | |
| 10963 | left_len = PyUnicode_GET_LENGTH(left); |
| 10964 | right_len = PyUnicode_GET_LENGTH(right); |
| 10965 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10966 | PyErr_SetString(PyExc_OverflowError, |
| 10967 | "strings are too large to concat"); |
| 10968 | goto error; |
| 10969 | } |
| 10970 | new_len = left_len + right_len; |
| 10971 | |
| 10972 | if (unicode_modifiable(left) |
| 10973 | && PyUnicode_CheckExact(right) |
| 10974 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10975 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10976 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10977 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10978 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10979 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10980 | { |
| 10981 | /* append inplace */ |
| 10982 | if (unicode_resize(p_left, new_len) != 0) { |
| 10983 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10984 | * deallocated so it cannot be put back into |
| 10985 | * 'variable'. The MemoryError is raised when there |
| 10986 | * is no value in 'variable', which might (very |
| 10987 | * remotely) be a cause of incompatibilities. |
| 10988 | */ |
| 10989 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10990 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10991 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10992 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10993 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10994 | else { |
| 10995 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10996 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
| 10997 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10998 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10999 | /* Concat the two Unicode strings */ |
| 11000 | res = PyUnicode_New(new_len, maxchar); |
| 11001 | if (res == NULL) |
| 11002 | goto error; |
| 11003 | copy_characters(res, 0, left, 0, left_len); |
| 11004 | copy_characters(res, left_len, right, 0, right_len); |
| 11005 | Py_DECREF(left); |
| 11006 | *p_left = res; |
| 11007 | } |
| 11008 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11009 | return; |
| 11010 | |
| 11011 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11012 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11013 | } |
| 11014 | |
| 11015 | void |
| 11016 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11017 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11018 | PyUnicode_Append(pleft, right); |
| 11019 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11020 | } |
| 11021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11022 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11023 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11025 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11026 | 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] | 11027 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | |
| 11029 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11030 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11032 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11033 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11034 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11035 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11036 | int kind1, kind2, kind; |
| 11037 | void *buf1, *buf2; |
| 11038 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11040 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11041 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11042 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11043 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11044 | kind1 = PyUnicode_KIND(self); |
| 11045 | kind2 = PyUnicode_KIND(substring); |
| 11046 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11047 | buf1 = PyUnicode_DATA(self); |
| 11048 | buf2 = PyUnicode_DATA(substring); |
| 11049 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11050 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | if (!buf1) { |
| 11052 | Py_DECREF(substring); |
| 11053 | return NULL; |
| 11054 | } |
| 11055 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11056 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11057 | if (!buf2) { |
| 11058 | Py_DECREF(substring); |
| 11059 | if (kind1 != kind) PyMem_Free(buf1); |
| 11060 | return NULL; |
| 11061 | } |
| 11062 | len1 = PyUnicode_GET_LENGTH(self); |
| 11063 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11064 | |
| 11065 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11066 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11067 | case PyUnicode_1BYTE_KIND: |
| 11068 | iresult = ucs1lib_count( |
| 11069 | ((Py_UCS1*)buf1) + start, end - start, |
| 11070 | buf2, len2, PY_SSIZE_T_MAX |
| 11071 | ); |
| 11072 | break; |
| 11073 | case PyUnicode_2BYTE_KIND: |
| 11074 | iresult = ucs2lib_count( |
| 11075 | ((Py_UCS2*)buf1) + start, end - start, |
| 11076 | buf2, len2, PY_SSIZE_T_MAX |
| 11077 | ); |
| 11078 | break; |
| 11079 | case PyUnicode_4BYTE_KIND: |
| 11080 | iresult = ucs4lib_count( |
| 11081 | ((Py_UCS4*)buf1) + start, end - start, |
| 11082 | buf2, len2, PY_SSIZE_T_MAX |
| 11083 | ); |
| 11084 | break; |
| 11085 | default: |
| 11086 | assert(0); iresult = 0; |
| 11087 | } |
| 11088 | |
| 11089 | result = PyLong_FromSsize_t(iresult); |
| 11090 | |
| 11091 | if (kind1 != kind) |
| 11092 | PyMem_Free(buf1); |
| 11093 | if (kind2 != kind) |
| 11094 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | |
| 11096 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11098 | return result; |
| 11099 | } |
| 11100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11101 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11102 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11104 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11105 | 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] | 11106 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11107 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11108 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11109 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11110 | |
| 11111 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11112 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11114 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | char *encoding = NULL; |
| 11116 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11117 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11118 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11119 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11121 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11122 | } |
| 11123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11124 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11125 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11126 | \n\ |
| 11127 | 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] | 11128 | 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] | 11129 | |
| 11130 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11131 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11132 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11133 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11134 | Py_UCS4 ch; |
| 11135 | PyObject *u; |
| 11136 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11137 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11138 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11139 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11140 | |
| 11141 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11142 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11143 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11144 | if (PyUnicode_READY(self) == -1) |
| 11145 | return NULL; |
| 11146 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11147 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11148 | src_len = PyUnicode_GET_LENGTH(self); |
| 11149 | i = j = line_pos = 0; |
| 11150 | kind = PyUnicode_KIND(self); |
| 11151 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11152 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11153 | for (; i < src_len; i++) { |
| 11154 | ch = PyUnicode_READ(kind, src_data, i); |
| 11155 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11156 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11157 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11158 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11159 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11160 | goto overflow; |
| 11161 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11162 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11163 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11164 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11166 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11167 | goto overflow; |
| 11168 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11170 | if (ch == '\n' || ch == '\r') |
| 11171 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11173 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11174 | if (!found) |
| 11175 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11176 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11178 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | if (!u) |
| 11180 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11181 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11182 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11183 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11185 | for (; i < src_len; i++) { |
| 11186 | ch = PyUnicode_READ(kind, src_data, i); |
| 11187 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11188 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11189 | incr = tabsize - (line_pos % tabsize); |
| 11190 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11191 | FILL(kind, dest_data, ' ', j, incr); |
| 11192 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11193 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11194 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11195 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11196 | line_pos++; |
| 11197 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11198 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11199 | if (ch == '\n' || ch == '\r') |
| 11200 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11201 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11202 | } |
| 11203 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11204 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11205 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11206 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11207 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11208 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | } |
| 11210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11211 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11212 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | \n\ |
| 11214 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11215 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | arguments start and end are interpreted as in slice notation.\n\ |
| 11217 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11218 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | |
| 11220 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11221 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11223 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11224 | Py_ssize_t start; |
| 11225 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11226 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11228 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11229 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11232 | if (PyUnicode_READY(self) == -1) |
| 11233 | return NULL; |
| 11234 | if (PyUnicode_READY(substring) == -1) |
| 11235 | return NULL; |
| 11236 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11237 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | |
| 11239 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11240 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11241 | if (result == -2) |
| 11242 | return NULL; |
| 11243 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11244 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | } |
| 11246 | |
| 11247 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11248 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11250 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11251 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11252 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11253 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | } |
| 11255 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11256 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11257 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11258 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11259 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11261 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11262 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11263 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11264 | assert(_Py_HashSecret_Initialized); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11265 | if (_PyUnicode_HASH(self) != -1) |
| 11266 | return _PyUnicode_HASH(self); |
| 11267 | if (PyUnicode_READY(self) == -1) |
| 11268 | return -1; |
| 11269 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11270 | /* |
| 11271 | We make the hash of the empty string be 0, rather than using |
| 11272 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11273 | */ |
| 11274 | if (len == 0) { |
| 11275 | _PyUnicode_HASH(self) = 0; |
| 11276 | return 0; |
| 11277 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11278 | |
| 11279 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11280 | #define HASH(P) \ |
| 11281 | x ^= (Py_uhash_t) *P << 7; \ |
| 11282 | while (--len >= 0) \ |
| 11283 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11284 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11285 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11286 | switch (PyUnicode_KIND(self)) { |
| 11287 | case PyUnicode_1BYTE_KIND: { |
| 11288 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11289 | HASH(c); |
| 11290 | break; |
| 11291 | } |
| 11292 | case PyUnicode_2BYTE_KIND: { |
| 11293 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11294 | HASH(s); |
| 11295 | break; |
| 11296 | } |
| 11297 | default: { |
| 11298 | Py_UCS4 *l; |
| 11299 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11300 | "Impossible switch case in unicode_hash"); |
| 11301 | l = PyUnicode_4BYTE_DATA(self); |
| 11302 | HASH(l); |
| 11303 | break; |
| 11304 | } |
| 11305 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11306 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11307 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11308 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11309 | if (x == -1) |
| 11310 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11311 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11312 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11316 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11317 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11318 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11319 | 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] | 11320 | |
| 11321 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11324 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11325 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11326 | Py_ssize_t start; |
| 11327 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11328 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11329 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11330 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | if (PyUnicode_READY(self) == -1) |
| 11334 | return NULL; |
| 11335 | if (PyUnicode_READY(substring) == -1) |
| 11336 | return NULL; |
| 11337 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11338 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | |
| 11340 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11342 | if (result == -2) |
| 11343 | return NULL; |
| 11344 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | if (result < 0) { |
| 11346 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11347 | return NULL; |
| 11348 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11349 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11350 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | } |
| 11352 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11353 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11354 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11355 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11356 | 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] | 11357 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | |
| 11359 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11360 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | Py_ssize_t i, length; |
| 11363 | int kind; |
| 11364 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | int cased; |
| 11366 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11367 | if (PyUnicode_READY(self) == -1) |
| 11368 | return NULL; |
| 11369 | length = PyUnicode_GET_LENGTH(self); |
| 11370 | kind = PyUnicode_KIND(self); |
| 11371 | data = PyUnicode_DATA(self); |
| 11372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | if (length == 1) |
| 11375 | return PyBool_FromLong( |
| 11376 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11377 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11378 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11379 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11380 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11383 | for (i = 0; i < length; i++) { |
| 11384 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11385 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11386 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11387 | return PyBool_FromLong(0); |
| 11388 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11389 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11391 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11392 | } |
| 11393 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11394 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11395 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11396 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11397 | 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] | 11398 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | |
| 11400 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11401 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11402 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11403 | Py_ssize_t i, length; |
| 11404 | int kind; |
| 11405 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | int cased; |
| 11407 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11408 | if (PyUnicode_READY(self) == -1) |
| 11409 | return NULL; |
| 11410 | length = PyUnicode_GET_LENGTH(self); |
| 11411 | kind = PyUnicode_KIND(self); |
| 11412 | data = PyUnicode_DATA(self); |
| 11413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | if (length == 1) |
| 11416 | return PyBool_FromLong( |
| 11417 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11419 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11420 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11421 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11424 | for (i = 0; i < length; i++) { |
| 11425 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11426 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11427 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11428 | return PyBool_FromLong(0); |
| 11429 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11430 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11432 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11433 | } |
| 11434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11435 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11436 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11438 | Return True if S is a titlecased string and there is at least one\n\ |
| 11439 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11440 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11441 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | |
| 11443 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11444 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | Py_ssize_t i, length; |
| 11447 | int kind; |
| 11448 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | int cased, previous_is_cased; |
| 11450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | if (PyUnicode_READY(self) == -1) |
| 11452 | return NULL; |
| 11453 | length = PyUnicode_GET_LENGTH(self); |
| 11454 | kind = PyUnicode_KIND(self); |
| 11455 | data = PyUnicode_DATA(self); |
| 11456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | if (length == 1) { |
| 11459 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11460 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11461 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11464 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11466 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11467 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11468 | cased = 0; |
| 11469 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 | for (i = 0; i < length; i++) { |
| 11471 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11472 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11473 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11474 | if (previous_is_cased) |
| 11475 | return PyBool_FromLong(0); |
| 11476 | previous_is_cased = 1; |
| 11477 | cased = 1; |
| 11478 | } |
| 11479 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11480 | if (!previous_is_cased) |
| 11481 | return PyBool_FromLong(0); |
| 11482 | previous_is_cased = 1; |
| 11483 | cased = 1; |
| 11484 | } |
| 11485 | else |
| 11486 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11488 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | } |
| 11490 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11491 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11492 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11494 | Return True if all characters in S are whitespace\n\ |
| 11495 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11496 | |
| 11497 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11498 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11500 | Py_ssize_t i, length; |
| 11501 | int kind; |
| 11502 | void *data; |
| 11503 | |
| 11504 | if (PyUnicode_READY(self) == -1) |
| 11505 | return NULL; |
| 11506 | length = PyUnicode_GET_LENGTH(self); |
| 11507 | kind = PyUnicode_KIND(self); |
| 11508 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11511 | if (length == 1) |
| 11512 | return PyBool_FromLong( |
| 11513 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11515 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11516 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11517 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11518 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11519 | for (i = 0; i < length; i++) { |
| 11520 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11521 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11522 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11523 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11524 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | } |
| 11526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11527 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11528 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11529 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11530 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11531 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11532 | |
| 11533 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11534 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11535 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | Py_ssize_t i, length; |
| 11537 | int kind; |
| 11538 | void *data; |
| 11539 | |
| 11540 | if (PyUnicode_READY(self) == -1) |
| 11541 | return NULL; |
| 11542 | length = PyUnicode_GET_LENGTH(self); |
| 11543 | kind = PyUnicode_KIND(self); |
| 11544 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11545 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11546 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11547 | if (length == 1) |
| 11548 | return PyBool_FromLong( |
| 11549 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11550 | |
| 11551 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11552 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11553 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11555 | for (i = 0; i < length; i++) { |
| 11556 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11557 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11558 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11559 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11560 | } |
| 11561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11562 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11563 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11564 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11565 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11566 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11567 | |
| 11568 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11569 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11570 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11571 | int kind; |
| 11572 | void *data; |
| 11573 | Py_ssize_t len, i; |
| 11574 | |
| 11575 | if (PyUnicode_READY(self) == -1) |
| 11576 | return NULL; |
| 11577 | |
| 11578 | kind = PyUnicode_KIND(self); |
| 11579 | data = PyUnicode_DATA(self); |
| 11580 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11581 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11582 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11583 | if (len == 1) { |
| 11584 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11585 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11586 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11587 | |
| 11588 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11590 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11591 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11592 | for (i = 0; i < len; i++) { |
| 11593 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11594 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11595 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11596 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11597 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11598 | } |
| 11599 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11600 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11601 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11602 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11603 | 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] | 11604 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11605 | |
| 11606 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11607 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11609 | Py_ssize_t i, length; |
| 11610 | int kind; |
| 11611 | void *data; |
| 11612 | |
| 11613 | if (PyUnicode_READY(self) == -1) |
| 11614 | return NULL; |
| 11615 | length = PyUnicode_GET_LENGTH(self); |
| 11616 | kind = PyUnicode_KIND(self); |
| 11617 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | if (length == 1) |
| 11621 | return PyBool_FromLong( |
| 11622 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11623 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11624 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11625 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11626 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11627 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11628 | for (i = 0; i < length; i++) { |
| 11629 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11630 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11632 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11633 | } |
| 11634 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11635 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11636 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11637 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11638 | Return True if all characters in S are digits\n\ |
| 11639 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11640 | |
| 11641 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11642 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11643 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11644 | Py_ssize_t i, length; |
| 11645 | int kind; |
| 11646 | void *data; |
| 11647 | |
| 11648 | if (PyUnicode_READY(self) == -1) |
| 11649 | return NULL; |
| 11650 | length = PyUnicode_GET_LENGTH(self); |
| 11651 | kind = PyUnicode_KIND(self); |
| 11652 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11654 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | if (length == 1) { |
| 11656 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11657 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11658 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11660 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11661 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11662 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11663 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11664 | for (i = 0; i < length; i++) { |
| 11665 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11666 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11667 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11668 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11669 | } |
| 11670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11671 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11672 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11673 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11674 | 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] | 11675 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | |
| 11677 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11678 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11679 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11680 | Py_ssize_t i, length; |
| 11681 | int kind; |
| 11682 | void *data; |
| 11683 | |
| 11684 | if (PyUnicode_READY(self) == -1) |
| 11685 | return NULL; |
| 11686 | length = PyUnicode_GET_LENGTH(self); |
| 11687 | kind = PyUnicode_KIND(self); |
| 11688 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11690 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11691 | if (length == 1) |
| 11692 | return PyBool_FromLong( |
| 11693 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11694 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11695 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11697 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11698 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11699 | for (i = 0; i < length; i++) { |
| 11700 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11701 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11703 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11704 | } |
| 11705 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11706 | int |
| 11707 | PyUnicode_IsIdentifier(PyObject *self) |
| 11708 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11709 | int kind; |
| 11710 | void *data; |
| 11711 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11712 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | if (PyUnicode_READY(self) == -1) { |
| 11715 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11716 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11717 | } |
| 11718 | |
| 11719 | /* Special case for empty strings */ |
| 11720 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11721 | return 0; |
| 11722 | kind = PyUnicode_KIND(self); |
| 11723 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11724 | |
| 11725 | /* PEP 3131 says that the first character must be in |
| 11726 | XID_Start and subsequent characters in XID_Continue, |
| 11727 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11728 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11729 | letters, digits, underscore). However, given the current |
| 11730 | definition of XID_Start and XID_Continue, it is sufficient |
| 11731 | to check just for these, except that _ must be allowed |
| 11732 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11733 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11734 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11735 | return 0; |
| 11736 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11737 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11738 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11739 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11740 | return 1; |
| 11741 | } |
| 11742 | |
| 11743 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11744 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11745 | \n\ |
| 11746 | Return True if S is a valid identifier according\n\ |
| 11747 | to the language definition."); |
| 11748 | |
| 11749 | static PyObject* |
| 11750 | unicode_isidentifier(PyObject *self) |
| 11751 | { |
| 11752 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11753 | } |
| 11754 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11755 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11756 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11757 | \n\ |
| 11758 | Return True if all characters in S are considered\n\ |
| 11759 | printable in repr() or S is empty, False otherwise."); |
| 11760 | |
| 11761 | static PyObject* |
| 11762 | unicode_isprintable(PyObject *self) |
| 11763 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11764 | Py_ssize_t i, length; |
| 11765 | int kind; |
| 11766 | void *data; |
| 11767 | |
| 11768 | if (PyUnicode_READY(self) == -1) |
| 11769 | return NULL; |
| 11770 | length = PyUnicode_GET_LENGTH(self); |
| 11771 | kind = PyUnicode_KIND(self); |
| 11772 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11773 | |
| 11774 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | if (length == 1) |
| 11776 | return PyBool_FromLong( |
| 11777 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11778 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | for (i = 0; i < length; i++) { |
| 11780 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11781 | Py_RETURN_FALSE; |
| 11782 | } |
| 11783 | } |
| 11784 | Py_RETURN_TRUE; |
| 11785 | } |
| 11786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11787 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11788 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11789 | \n\ |
| 11790 | 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] | 11791 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11792 | |
| 11793 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11794 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11795 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11796 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | } |
| 11798 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11799 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11800 | unicode_length(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 | if (PyUnicode_READY(self) == -1) |
| 11803 | return -1; |
| 11804 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11805 | } |
| 11806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11807 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11808 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11809 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11810 | 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] | 11811 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11812 | |
| 11813 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11814 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11815 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11816 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11817 | Py_UCS4 fillchar = ' '; |
| 11818 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11819 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | return NULL; |
| 11821 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11822 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11823 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11824 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11825 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11826 | return unicode_result_unchanged(self); |
| 11827 | |
| 11828 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | } |
| 11830 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11831 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11832 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11833 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11834 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11835 | |
| 11836 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11837 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11838 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11839 | if (PyUnicode_READY(self) == -1) |
| 11840 | return NULL; |
| 11841 | if (PyUnicode_IS_ASCII(self)) |
| 11842 | return ascii_upper_or_lower(self, 1); |
| 11843 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11844 | } |
| 11845 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11846 | #define LEFTSTRIP 0 |
| 11847 | #define RIGHTSTRIP 1 |
| 11848 | #define BOTHSTRIP 2 |
| 11849 | |
| 11850 | /* Arrays indexed by above */ |
| 11851 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11852 | |
| 11853 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11854 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11855 | /* externally visible for str.strip(unicode) */ |
| 11856 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11857 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11858 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11859 | void *data; |
| 11860 | int kind; |
| 11861 | Py_ssize_t i, j, len; |
| 11862 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11864 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11865 | return NULL; |
| 11866 | |
| 11867 | kind = PyUnicode_KIND(self); |
| 11868 | data = PyUnicode_DATA(self); |
| 11869 | len = PyUnicode_GET_LENGTH(self); |
| 11870 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11871 | PyUnicode_DATA(sepobj), |
| 11872 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11873 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11874 | i = 0; |
| 11875 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | while (i < len && |
| 11877 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11878 | i++; |
| 11879 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11880 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11881 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11882 | j = len; |
| 11883 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11884 | do { |
| 11885 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | } while (j >= i && |
| 11887 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11888 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11889 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11890 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11891 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11892 | } |
| 11893 | |
| 11894 | PyObject* |
| 11895 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11896 | { |
| 11897 | unsigned char *data; |
| 11898 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11899 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11901 | if (PyUnicode_READY(self) == -1) |
| 11902 | return NULL; |
| 11903 | |
| 11904 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11905 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11906 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11907 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11908 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11909 | length = end - start; |
| 11910 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11911 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11913 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11914 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11915 | return NULL; |
| 11916 | } |
| 11917 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11918 | if (PyUnicode_IS_ASCII(self)) { |
| 11919 | kind = PyUnicode_KIND(self); |
| 11920 | data = PyUnicode_1BYTE_DATA(self); |
| 11921 | return unicode_fromascii(data + start, length); |
| 11922 | } |
| 11923 | else { |
| 11924 | kind = PyUnicode_KIND(self); |
| 11925 | data = PyUnicode_1BYTE_DATA(self); |
| 11926 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11927 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11928 | length); |
| 11929 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11930 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11931 | |
| 11932 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11933 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11935 | int kind; |
| 11936 | void *data; |
| 11937 | Py_ssize_t len, i, j; |
| 11938 | |
| 11939 | if (PyUnicode_READY(self) == -1) |
| 11940 | return NULL; |
| 11941 | |
| 11942 | kind = PyUnicode_KIND(self); |
| 11943 | data = PyUnicode_DATA(self); |
| 11944 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11945 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11946 | i = 0; |
| 11947 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11948 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11949 | i++; |
| 11950 | } |
| 11951 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11952 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11953 | j = len; |
| 11954 | if (striptype != LEFTSTRIP) { |
| 11955 | do { |
| 11956 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11958 | j++; |
| 11959 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11960 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11961 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | } |
| 11963 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11964 | |
| 11965 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11966 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11967 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11968 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11969 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11970 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11971 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11972 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11973 | if (sep != NULL && sep != Py_None) { |
| 11974 | if (PyUnicode_Check(sep)) |
| 11975 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11976 | else { |
| 11977 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11978 | "%s arg must be None or str", |
| 11979 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11980 | return NULL; |
| 11981 | } |
| 11982 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11983 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11984 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11985 | } |
| 11986 | |
| 11987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11988 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11989 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11990 | \n\ |
| 11991 | Return a copy of the string S with leading and trailing\n\ |
| 11992 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11993 | 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] | 11994 | |
| 11995 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11996 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11997 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11998 | if (PyTuple_GET_SIZE(args) == 0) |
| 11999 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12000 | else |
| 12001 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12002 | } |
| 12003 | |
| 12004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12005 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12006 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12007 | \n\ |
| 12008 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12009 | 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] | 12010 | |
| 12011 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12012 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12013 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12014 | if (PyTuple_GET_SIZE(args) == 0) |
| 12015 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12016 | else |
| 12017 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12018 | } |
| 12019 | |
| 12020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12021 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12022 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12023 | \n\ |
| 12024 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12025 | 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] | 12026 | |
| 12027 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12028 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12029 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12030 | if (PyTuple_GET_SIZE(args) == 0) |
| 12031 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12032 | else |
| 12033 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12034 | } |
| 12035 | |
| 12036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12037 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12038 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12039 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12040 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12042 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12043 | if (len < 1) { |
| 12044 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12045 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12046 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12047 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12048 | /* no repeat, return original string */ |
| 12049 | if (len == 1) |
| 12050 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12051 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12052 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12053 | return NULL; |
| 12054 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12055 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12056 | PyErr_SetString(PyExc_OverflowError, |
| 12057 | "repeated string is too long"); |
| 12058 | return NULL; |
| 12059 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12060 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12061 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12062 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12063 | if (!u) |
| 12064 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12065 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12066 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12067 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12068 | const int kind = PyUnicode_KIND(str); |
| 12069 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12070 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12071 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12072 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12073 | } |
| 12074 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12075 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12076 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12077 | ucs2[n] = fill_char; |
| 12078 | } else { |
| 12079 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12080 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12081 | for (n = 0; n < len; ++n) |
| 12082 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12083 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12084 | } |
| 12085 | else { |
| 12086 | /* number of characters copied this far */ |
| 12087 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12088 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12089 | char *to = (char *) PyUnicode_DATA(u); |
| 12090 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12091 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12092 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12093 | n = (done <= nchars-done) ? done : nchars-done; |
| 12094 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12095 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | } |
| 12098 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12099 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12100 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | } |
| 12102 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12103 | PyObject * |
| 12104 | PyUnicode_Replace(PyObject *obj, |
| 12105 | PyObject *subobj, |
| 12106 | PyObject *replobj, |
| 12107 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12108 | { |
| 12109 | PyObject *self; |
| 12110 | PyObject *str1; |
| 12111 | PyObject *str2; |
| 12112 | PyObject *result; |
| 12113 | |
| 12114 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12115 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12116 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12117 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12118 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12119 | Py_DECREF(self); |
| 12120 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | } |
| 12122 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12123 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12124 | Py_DECREF(self); |
| 12125 | Py_DECREF(str1); |
| 12126 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12127 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12128 | if (PyUnicode_READY(self) == -1 || |
| 12129 | PyUnicode_READY(str1) == -1 || |
| 12130 | PyUnicode_READY(str2) == -1) |
| 12131 | result = NULL; |
| 12132 | else |
| 12133 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12134 | Py_DECREF(self); |
| 12135 | Py_DECREF(str1); |
| 12136 | Py_DECREF(str2); |
| 12137 | return result; |
| 12138 | } |
| 12139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12140 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12141 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | \n\ |
| 12143 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12144 | old replaced by new. If the optional argument count is\n\ |
| 12145 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12146 | |
| 12147 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12148 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12149 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12150 | PyObject *str1; |
| 12151 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12152 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12153 | PyObject *result; |
| 12154 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12155 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12157 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12158 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12159 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12160 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12161 | return NULL; |
| 12162 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12163 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12164 | Py_DECREF(str1); |
| 12165 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12166 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12167 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12168 | result = NULL; |
| 12169 | else |
| 12170 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12171 | |
| 12172 | Py_DECREF(str1); |
| 12173 | Py_DECREF(str2); |
| 12174 | return result; |
| 12175 | } |
| 12176 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12177 | static PyObject * |
| 12178 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12179 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12180 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | Py_ssize_t isize; |
| 12182 | Py_ssize_t osize, squote, dquote, i, o; |
| 12183 | Py_UCS4 max, quote; |
| 12184 | int ikind, okind; |
| 12185 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12186 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12187 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12188 | return NULL; |
| 12189 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12190 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12191 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12193 | /* Compute length of output, quote characters, and |
| 12194 | maximum character */ |
| 12195 | osize = 2; /* quotes */ |
| 12196 | max = 127; |
| 12197 | squote = dquote = 0; |
| 12198 | ikind = PyUnicode_KIND(unicode); |
| 12199 | for (i = 0; i < isize; i++) { |
| 12200 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12201 | switch (ch) { |
| 12202 | case '\'': squote++; osize++; break; |
| 12203 | case '"': dquote++; osize++; break; |
| 12204 | case '\\': case '\t': case '\r': case '\n': |
| 12205 | osize += 2; break; |
| 12206 | default: |
| 12207 | /* Fast-path ASCII */ |
| 12208 | if (ch < ' ' || ch == 0x7f) |
| 12209 | osize += 4; /* \xHH */ |
| 12210 | else if (ch < 0x7f) |
| 12211 | osize++; |
| 12212 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12213 | osize++; |
| 12214 | max = ch > max ? ch : max; |
| 12215 | } |
| 12216 | else if (ch < 0x100) |
| 12217 | osize += 4; /* \xHH */ |
| 12218 | else if (ch < 0x10000) |
| 12219 | osize += 6; /* \uHHHH */ |
| 12220 | else |
| 12221 | osize += 10; /* \uHHHHHHHH */ |
| 12222 | } |
| 12223 | } |
| 12224 | |
| 12225 | quote = '\''; |
| 12226 | if (squote) { |
| 12227 | if (dquote) |
| 12228 | /* Both squote and dquote present. Use squote, |
| 12229 | and escape them */ |
| 12230 | osize += squote; |
| 12231 | else |
| 12232 | quote = '"'; |
| 12233 | } |
| 12234 | |
| 12235 | repr = PyUnicode_New(osize, max); |
| 12236 | if (repr == NULL) |
| 12237 | return NULL; |
| 12238 | okind = PyUnicode_KIND(repr); |
| 12239 | odata = PyUnicode_DATA(repr); |
| 12240 | |
| 12241 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12242 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12243 | |
| 12244 | for (i = 0, o = 1; i < isize; i++) { |
| 12245 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12246 | |
| 12247 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12248 | if ((ch == quote) || (ch == '\\')) { |
| 12249 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12250 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12251 | continue; |
| 12252 | } |
| 12253 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12254 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12255 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12257 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12258 | } |
| 12259 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12260 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12261 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12262 | } |
| 12263 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12264 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12265 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12266 | } |
| 12267 | |
| 12268 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12269 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12270 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12271 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12272 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12273 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12274 | } |
| 12275 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12276 | /* Copy ASCII characters as-is */ |
| 12277 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12279 | } |
| 12280 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12281 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12282 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12283 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12284 | (categories Z* and C* except ASCII space) |
| 12285 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12286 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12287 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12288 | if (ch <= 0xff) { |
| 12289 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12290 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12291 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12292 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12293 | } |
| 12294 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12295 | else if (ch >= 0x10000) { |
| 12296 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12297 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12298 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12299 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12300 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12301 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12302 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12303 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12304 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12305 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12306 | } |
| 12307 | /* Map 16-bit characters to '\uxxxx' */ |
| 12308 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12309 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12310 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12311 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12312 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12313 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12314 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12315 | } |
| 12316 | } |
| 12317 | /* Copy characters as-is */ |
| 12318 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12319 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12320 | } |
| 12321 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12322 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12323 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12324 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12325 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12326 | } |
| 12327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12328 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12329 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12330 | \n\ |
| 12331 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12332 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12333 | arguments start and end are interpreted as in slice notation.\n\ |
| 12334 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12335 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | |
| 12337 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12338 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12340 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12341 | Py_ssize_t start; |
| 12342 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12343 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12344 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12345 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12346 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12347 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12349 | if (PyUnicode_READY(self) == -1) |
| 12350 | return NULL; |
| 12351 | if (PyUnicode_READY(substring) == -1) |
| 12352 | return NULL; |
| 12353 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12354 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12355 | |
| 12356 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12357 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12358 | if (result == -2) |
| 12359 | return NULL; |
| 12360 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12361 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | } |
| 12363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12364 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12365 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12366 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12367 | 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] | 12368 | |
| 12369 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12370 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12371 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12372 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12373 | Py_ssize_t start; |
| 12374 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12375 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12376 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12377 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12378 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12379 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12381 | if (PyUnicode_READY(self) == -1) |
| 12382 | return NULL; |
| 12383 | if (PyUnicode_READY(substring) == -1) |
| 12384 | return NULL; |
| 12385 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12386 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12387 | |
| 12388 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12390 | if (result == -2) |
| 12391 | return NULL; |
| 12392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12393 | if (result < 0) { |
| 12394 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12395 | return NULL; |
| 12396 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12397 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12398 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12399 | } |
| 12400 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12401 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12402 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12403 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12404 | 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] | 12405 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12406 | |
| 12407 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12408 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12409 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12410 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12411 | Py_UCS4 fillchar = ' '; |
| 12412 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12413 | 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] | 12414 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12415 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12416 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12417 | return NULL; |
| 12418 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12419 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12420 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12421 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12422 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12423 | } |
| 12424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12425 | PyObject * |
| 12426 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12427 | { |
| 12428 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12430 | s = PyUnicode_FromObject(s); |
| 12431 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12432 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12433 | if (sep != NULL) { |
| 12434 | sep = PyUnicode_FromObject(sep); |
| 12435 | if (sep == NULL) { |
| 12436 | Py_DECREF(s); |
| 12437 | return NULL; |
| 12438 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12439 | } |
| 12440 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12441 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12442 | |
| 12443 | Py_DECREF(s); |
| 12444 | Py_XDECREF(sep); |
| 12445 | return result; |
| 12446 | } |
| 12447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12448 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12449 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12450 | \n\ |
| 12451 | Return a list of the words in S, using sep as the\n\ |
| 12452 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12453 | 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] | 12454 | whitespace string is a separator and empty strings are\n\ |
| 12455 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12456 | |
| 12457 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12458 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12459 | { |
| 12460 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12461 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12463 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12464 | return NULL; |
| 12465 | |
| 12466 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12467 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12468 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12469 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12470 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12471 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12472 | } |
| 12473 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12474 | PyObject * |
| 12475 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12476 | { |
| 12477 | PyObject* str_obj; |
| 12478 | PyObject* sep_obj; |
| 12479 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12480 | int kind1, kind2, kind; |
| 12481 | void *buf1 = NULL, *buf2 = NULL; |
| 12482 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12483 | |
| 12484 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12485 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12486 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12487 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12488 | if (!sep_obj) { |
| 12489 | Py_DECREF(str_obj); |
| 12490 | return NULL; |
| 12491 | } |
| 12492 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12493 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12494 | Py_DECREF(str_obj); |
| 12495 | return NULL; |
| 12496 | } |
| 12497 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12498 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12499 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12500 | kind = Py_MAX(kind1, kind2); |
| 12501 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12502 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12503 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12504 | if (!buf1) |
| 12505 | goto onError; |
| 12506 | buf2 = PyUnicode_DATA(sep_obj); |
| 12507 | if (kind2 != kind) |
| 12508 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12509 | if (!buf2) |
| 12510 | goto onError; |
| 12511 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12512 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12513 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12514 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12516 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12517 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12518 | else |
| 12519 | 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] | 12520 | break; |
| 12521 | case PyUnicode_2BYTE_KIND: |
| 12522 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12523 | break; |
| 12524 | case PyUnicode_4BYTE_KIND: |
| 12525 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12526 | break; |
| 12527 | default: |
| 12528 | assert(0); |
| 12529 | out = 0; |
| 12530 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12531 | |
| 12532 | Py_DECREF(sep_obj); |
| 12533 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12534 | if (kind1 != kind) |
| 12535 | PyMem_Free(buf1); |
| 12536 | if (kind2 != kind) |
| 12537 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12538 | |
| 12539 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12540 | onError: |
| 12541 | Py_DECREF(sep_obj); |
| 12542 | Py_DECREF(str_obj); |
| 12543 | if (kind1 != kind && buf1) |
| 12544 | PyMem_Free(buf1); |
| 12545 | if (kind2 != kind && buf2) |
| 12546 | PyMem_Free(buf2); |
| 12547 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12548 | } |
| 12549 | |
| 12550 | |
| 12551 | PyObject * |
| 12552 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12553 | { |
| 12554 | PyObject* str_obj; |
| 12555 | PyObject* sep_obj; |
| 12556 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12557 | int kind1, kind2, kind; |
| 12558 | void *buf1 = NULL, *buf2 = NULL; |
| 12559 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12560 | |
| 12561 | str_obj = PyUnicode_FromObject(str_in); |
| 12562 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12563 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12564 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12565 | if (!sep_obj) { |
| 12566 | Py_DECREF(str_obj); |
| 12567 | return NULL; |
| 12568 | } |
| 12569 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12570 | kind1 = PyUnicode_KIND(str_in); |
| 12571 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12572 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12573 | buf1 = PyUnicode_DATA(str_in); |
| 12574 | if (kind1 != kind) |
| 12575 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12576 | if (!buf1) |
| 12577 | goto onError; |
| 12578 | buf2 = PyUnicode_DATA(sep_obj); |
| 12579 | if (kind2 != kind) |
| 12580 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12581 | if (!buf2) |
| 12582 | goto onError; |
| 12583 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12584 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12585 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12586 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12587 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12588 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12589 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12590 | else |
| 12591 | 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] | 12592 | break; |
| 12593 | case PyUnicode_2BYTE_KIND: |
| 12594 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12595 | break; |
| 12596 | case PyUnicode_4BYTE_KIND: |
| 12597 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12598 | break; |
| 12599 | default: |
| 12600 | assert(0); |
| 12601 | out = 0; |
| 12602 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12603 | |
| 12604 | Py_DECREF(sep_obj); |
| 12605 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12606 | if (kind1 != kind) |
| 12607 | PyMem_Free(buf1); |
| 12608 | if (kind2 != kind) |
| 12609 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12610 | |
| 12611 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | onError: |
| 12613 | Py_DECREF(sep_obj); |
| 12614 | Py_DECREF(str_obj); |
| 12615 | if (kind1 != kind && buf1) |
| 12616 | PyMem_Free(buf1); |
| 12617 | if (kind2 != kind && buf2) |
| 12618 | PyMem_Free(buf2); |
| 12619 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12620 | } |
| 12621 | |
| 12622 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12623 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12624 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12625 | 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] | 12626 | 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] | 12627 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12628 | |
| 12629 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12630 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12631 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12632 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12633 | } |
| 12634 | |
| 12635 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12636 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12637 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12638 | 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] | 12639 | 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] | 12640 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12641 | |
| 12642 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12643 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12644 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12645 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12646 | } |
| 12647 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12648 | PyObject * |
| 12649 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12650 | { |
| 12651 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12652 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12653 | s = PyUnicode_FromObject(s); |
| 12654 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12655 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12656 | if (sep != NULL) { |
| 12657 | sep = PyUnicode_FromObject(sep); |
| 12658 | if (sep == NULL) { |
| 12659 | Py_DECREF(s); |
| 12660 | return NULL; |
| 12661 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12662 | } |
| 12663 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12664 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12665 | |
| 12666 | Py_DECREF(s); |
| 12667 | Py_XDECREF(sep); |
| 12668 | return result; |
| 12669 | } |
| 12670 | |
| 12671 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12672 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12673 | \n\ |
| 12674 | Return a list of the words in S, using sep as the\n\ |
| 12675 | delimiter string, starting at the end of the string and\n\ |
| 12676 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12677 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12678 | is a separator."); |
| 12679 | |
| 12680 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12681 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12682 | { |
| 12683 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12684 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12685 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12686 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12687 | return NULL; |
| 12688 | |
| 12689 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12690 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12691 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12692 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12693 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12694 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12695 | } |
| 12696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12697 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12698 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | \n\ |
| 12700 | 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] | 12701 | 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] | 12702 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12703 | |
| 12704 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12705 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12706 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12707 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12708 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12709 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12710 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12711 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12712 | return NULL; |
| 12713 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12714 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12715 | } |
| 12716 | |
| 12717 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12718 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12719 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12720 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | } |
| 12722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12723 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12724 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12725 | \n\ |
| 12726 | 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] | 12727 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | |
| 12729 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12730 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12731 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12732 | if (PyUnicode_READY(self) == -1) |
| 12733 | return NULL; |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12734 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12735 | } |
| 12736 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12737 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12738 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12739 | \n\ |
| 12740 | Return a translation table usable for str.translate().\n\ |
| 12741 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12742 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12743 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12744 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12745 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12746 | character at the same position in y. If there is a third argument, it\n\ |
| 12747 | must be a string, whose characters will be mapped to None in the result."); |
| 12748 | |
| 12749 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12750 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12751 | { |
| 12752 | PyObject *x, *y = NULL, *z = NULL; |
| 12753 | PyObject *new = NULL, *key, *value; |
| 12754 | Py_ssize_t i = 0; |
| 12755 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12756 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12757 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12758 | return NULL; |
| 12759 | new = PyDict_New(); |
| 12760 | if (!new) |
| 12761 | return NULL; |
| 12762 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12763 | int x_kind, y_kind, z_kind; |
| 12764 | void *x_data, *y_data, *z_data; |
| 12765 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12766 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12767 | if (!PyUnicode_Check(x)) { |
| 12768 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12769 | "be a string if there is a second argument"); |
| 12770 | goto err; |
| 12771 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12772 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12773 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12774 | "arguments must have equal length"); |
| 12775 | goto err; |
| 12776 | } |
| 12777 | /* 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] | 12778 | x_kind = PyUnicode_KIND(x); |
| 12779 | y_kind = PyUnicode_KIND(y); |
| 12780 | x_data = PyUnicode_DATA(x); |
| 12781 | y_data = PyUnicode_DATA(y); |
| 12782 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12783 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12784 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12785 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12786 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12787 | if (!value) { |
| 12788 | Py_DECREF(key); |
| 12789 | goto err; |
| 12790 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12791 | res = PyDict_SetItem(new, key, value); |
| 12792 | Py_DECREF(key); |
| 12793 | Py_DECREF(value); |
| 12794 | if (res < 0) |
| 12795 | goto err; |
| 12796 | } |
| 12797 | /* create entries for deleting chars in z */ |
| 12798 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12799 | z_kind = PyUnicode_KIND(z); |
| 12800 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12801 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12802 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12803 | if (!key) |
| 12804 | goto err; |
| 12805 | res = PyDict_SetItem(new, key, Py_None); |
| 12806 | Py_DECREF(key); |
| 12807 | if (res < 0) |
| 12808 | goto err; |
| 12809 | } |
| 12810 | } |
| 12811 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12812 | int kind; |
| 12813 | void *data; |
| 12814 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12815 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12816 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12817 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12818 | "to maketrans it must be a dict"); |
| 12819 | goto err; |
| 12820 | } |
| 12821 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12822 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12823 | if (PyUnicode_Check(key)) { |
| 12824 | /* convert string keys to integer keys */ |
| 12825 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12826 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12827 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12828 | "table must be of length 1"); |
| 12829 | goto err; |
| 12830 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12831 | kind = PyUnicode_KIND(key); |
| 12832 | data = PyUnicode_DATA(key); |
| 12833 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12834 | if (!newkey) |
| 12835 | goto err; |
| 12836 | res = PyDict_SetItem(new, newkey, value); |
| 12837 | Py_DECREF(newkey); |
| 12838 | if (res < 0) |
| 12839 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12840 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12841 | /* just keep integer keys */ |
| 12842 | if (PyDict_SetItem(new, key, value) < 0) |
| 12843 | goto err; |
| 12844 | } else { |
| 12845 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12846 | "be strings or integers"); |
| 12847 | goto err; |
| 12848 | } |
| 12849 | } |
| 12850 | } |
| 12851 | return new; |
| 12852 | err: |
| 12853 | Py_DECREF(new); |
| 12854 | return NULL; |
| 12855 | } |
| 12856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12857 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12858 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12859 | \n\ |
| 12860 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12861 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12862 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12863 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12864 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12865 | |
| 12866 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12867 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12868 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12869 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12870 | } |
| 12871 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12872 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12873 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12874 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12875 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12876 | |
| 12877 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12878 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12879 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12880 | if (PyUnicode_READY(self) == -1) |
| 12881 | return NULL; |
| 12882 | if (PyUnicode_IS_ASCII(self)) |
| 12883 | return ascii_upper_or_lower(self, 0); |
| 12884 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12885 | } |
| 12886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12887 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12888 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12889 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12890 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12891 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12892 | |
| 12893 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12894 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12895 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12896 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12897 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12898 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12899 | int kind; |
| 12900 | void *data; |
| 12901 | Py_UCS4 chr; |
| 12902 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12903 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12904 | return NULL; |
| 12905 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12906 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12907 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12908 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12909 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12910 | return unicode_result_unchanged(self); |
| 12911 | |
| 12912 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12913 | |
| 12914 | u = pad(self, fill, 0, '0'); |
| 12915 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12916 | if (u == NULL) |
| 12917 | return NULL; |
| 12918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | kind = PyUnicode_KIND(u); |
| 12920 | data = PyUnicode_DATA(u); |
| 12921 | chr = PyUnicode_READ(kind, data, fill); |
| 12922 | |
| 12923 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12924 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12925 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12926 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12927 | } |
| 12928 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12929 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12930 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12931 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12932 | |
| 12933 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12934 | static PyObject * |
| 12935 | unicode__decimal2ascii(PyObject *self) |
| 12936 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12937 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12938 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12939 | #endif |
| 12940 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12941 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12942 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12943 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12944 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12945 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12946 | With optional end, stop comparing S at that position.\n\ |
| 12947 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12948 | |
| 12949 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12950 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12951 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12952 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12953 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12954 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12955 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12956 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12957 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12958 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12959 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12960 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12961 | if (PyTuple_Check(subobj)) { |
| 12962 | Py_ssize_t i; |
| 12963 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12964 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12965 | if (substring == NULL) |
| 12966 | return NULL; |
| 12967 | result = tailmatch(self, substring, start, end, -1); |
| 12968 | Py_DECREF(substring); |
| 12969 | if (result) { |
| 12970 | Py_RETURN_TRUE; |
| 12971 | } |
| 12972 | } |
| 12973 | /* nothing matched */ |
| 12974 | Py_RETURN_FALSE; |
| 12975 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12976 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12977 | if (substring == NULL) { |
| 12978 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12979 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12980 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12981 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12982 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12983 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12984 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12985 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12986 | } |
| 12987 | |
| 12988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12989 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12990 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12991 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12992 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12993 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12994 | With optional end, stop comparing S at that position.\n\ |
| 12995 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12996 | |
| 12997 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12998 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12999 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13000 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13001 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13002 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13003 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13004 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13005 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13006 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13007 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13008 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13009 | if (PyTuple_Check(subobj)) { |
| 13010 | Py_ssize_t i; |
| 13011 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13012 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13013 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13014 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13015 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13016 | result = tailmatch(self, substring, start, end, +1); |
| 13017 | Py_DECREF(substring); |
| 13018 | if (result) { |
| 13019 | Py_RETURN_TRUE; |
| 13020 | } |
| 13021 | } |
| 13022 | Py_RETURN_FALSE; |
| 13023 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13024 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13025 | if (substring == NULL) { |
| 13026 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13027 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13028 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13029 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13030 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13031 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13032 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13033 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13034 | } |
| 13035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13036 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13037 | |
| 13038 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13039 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13040 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13041 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13042 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13043 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13044 | PyDoc_STRVAR(format_map__doc__, |
| 13045 | "S.format_map(mapping) -> str\n\ |
| 13046 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13047 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13048 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13049 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13050 | static PyObject * |
| 13051 | unicode__format__(PyObject* self, PyObject* args) |
| 13052 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13053 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13054 | |
| 13055 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13056 | return NULL; |
| 13057 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13058 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13059 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13060 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13061 | } |
| 13062 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13063 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13064 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13065 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13066 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13067 | |
| 13068 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13069 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13070 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13071 | Py_ssize_t size; |
| 13072 | |
| 13073 | /* If it's a compact object, account for base structure + |
| 13074 | character data. */ |
| 13075 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13076 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13077 | else if (PyUnicode_IS_COMPACT(v)) |
| 13078 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13079 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13080 | else { |
| 13081 | /* If it is a two-block object, account for base object, and |
| 13082 | for character block if present. */ |
| 13083 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13084 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13085 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13086 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13087 | } |
| 13088 | /* 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] | 13089 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13090 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13091 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13092 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13093 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13094 | |
| 13095 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13096 | } |
| 13097 | |
| 13098 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13099 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13100 | |
| 13101 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13102 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13103 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13104 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13105 | if (!copy) |
| 13106 | return NULL; |
| 13107 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13108 | } |
| 13109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | static PyMethodDef unicode_methods[] = { |
| 13111 | |
| 13112 | /* Order is according to common usage: often used methods should |
| 13113 | appear first, since lookup is done sequentially. */ |
| 13114 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13115 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13116 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 13117 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13118 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13119 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13120 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13121 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13122 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13123 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13124 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13125 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13126 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13127 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13128 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13129 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13130 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13131 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13132 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13133 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13134 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13135 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13136 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13137 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13138 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13139 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13140 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13141 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13142 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13143 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13144 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13145 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13146 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13147 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13148 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13149 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13150 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13151 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13152 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13153 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13154 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13155 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13156 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13157 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13158 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13159 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13160 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13161 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13162 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13163 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13164 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13165 | #endif |
| 13166 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13167 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | {NULL, NULL} |
| 13169 | }; |
| 13170 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13171 | static PyObject * |
| 13172 | unicode_mod(PyObject *v, PyObject *w) |
| 13173 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13174 | if (!PyUnicode_Check(v)) |
| 13175 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13176 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13177 | } |
| 13178 | |
| 13179 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13180 | 0, /*nb_add*/ |
| 13181 | 0, /*nb_subtract*/ |
| 13182 | 0, /*nb_multiply*/ |
| 13183 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13184 | }; |
| 13185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13186 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13187 | (lenfunc) unicode_length, /* sq_length */ |
| 13188 | PyUnicode_Concat, /* sq_concat */ |
| 13189 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13190 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13191 | 0, /* sq_slice */ |
| 13192 | 0, /* sq_ass_item */ |
| 13193 | 0, /* sq_ass_slice */ |
| 13194 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13195 | }; |
| 13196 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13197 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13198 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13199 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13200 | if (PyUnicode_READY(self) == -1) |
| 13201 | return NULL; |
| 13202 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13203 | if (PyIndex_Check(item)) { |
| 13204 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13205 | if (i == -1 && PyErr_Occurred()) |
| 13206 | return NULL; |
| 13207 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13208 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13209 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13210 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13211 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13212 | PyObject *result; |
| 13213 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13214 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13215 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13217 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13218 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13219 | return NULL; |
| 13220 | } |
| 13221 | |
| 13222 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13223 | Py_INCREF(unicode_empty); |
| 13224 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13225 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13226 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13227 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13228 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13229 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13230 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13231 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13232 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13233 | src_kind = PyUnicode_KIND(self); |
| 13234 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13235 | if (!PyUnicode_IS_ASCII(self)) { |
| 13236 | kind_limit = kind_maxchar_limit(src_kind); |
| 13237 | max_char = 0; |
| 13238 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13239 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13240 | if (ch > max_char) { |
| 13241 | max_char = ch; |
| 13242 | if (max_char >= kind_limit) |
| 13243 | break; |
| 13244 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13245 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13246 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13247 | else |
| 13248 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13249 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13250 | if (result == NULL) |
| 13251 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13252 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13253 | dest_data = PyUnicode_DATA(result); |
| 13254 | |
| 13255 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13256 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13257 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13258 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13259 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13260 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13261 | } else { |
| 13262 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13263 | return NULL; |
| 13264 | } |
| 13265 | } |
| 13266 | |
| 13267 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13268 | (lenfunc)unicode_length, /* mp_length */ |
| 13269 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13270 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13271 | }; |
| 13272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13274 | /* Helpers for PyUnicode_Format() */ |
| 13275 | |
| 13276 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13277 | 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] | 13278 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13279 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13280 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13281 | (*p_argidx)++; |
| 13282 | if (arglen < 0) |
| 13283 | return args; |
| 13284 | else |
| 13285 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13286 | } |
| 13287 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13288 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13289 | return NULL; |
| 13290 | } |
| 13291 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13292 | /* 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] | 13293 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13294 | static PyObject * |
| 13295 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13296 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13297 | char *p; |
| 13298 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13299 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13301 | x = PyFloat_AsDouble(v); |
| 13302 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13303 | return NULL; |
| 13304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13305 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13306 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13307 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13308 | p = PyOS_double_to_string(x, type, prec, |
| 13309 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13310 | if (p == NULL) |
| 13311 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13312 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13313 | PyMem_Free(p); |
| 13314 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13315 | } |
| 13316 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13317 | static PyObject* |
| 13318 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13319 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13320 | char *buf; |
| 13321 | int len; |
| 13322 | PyObject *str; /* temporary string object. */ |
| 13323 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13324 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13325 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13326 | if (!str) |
| 13327 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13328 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13329 | Py_DECREF(str); |
| 13330 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13331 | } |
| 13332 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13333 | static Py_UCS4 |
| 13334 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13335 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13336 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13337 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13338 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13339 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13340 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13341 | goto onError; |
| 13342 | } |
| 13343 | else { |
| 13344 | /* Integer input truncated to a character */ |
| 13345 | long x; |
| 13346 | x = PyLong_AsLong(v); |
| 13347 | if (x == -1 && PyErr_Occurred()) |
| 13348 | goto onError; |
| 13349 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13350 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13351 | PyErr_SetString(PyExc_OverflowError, |
| 13352 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13353 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13354 | } |
| 13355 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13356 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13357 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13358 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13359 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13360 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13361 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13362 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13363 | } |
| 13364 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13365 | static int |
| 13366 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13367 | { |
| 13368 | int r; |
| 13369 | assert(count > 0); |
| 13370 | assert(PyUnicode_Check(obj)); |
| 13371 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13372 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13373 | if (repeated == NULL) |
| 13374 | return -1; |
| 13375 | r = _PyAccu_Accumulate(acc, repeated); |
| 13376 | Py_DECREF(repeated); |
| 13377 | return r; |
| 13378 | } |
| 13379 | else { |
| 13380 | do { |
| 13381 | if (_PyAccu_Accumulate(acc, obj)) |
| 13382 | return -1; |
| 13383 | } while (--count); |
| 13384 | return 0; |
| 13385 | } |
| 13386 | } |
| 13387 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13388 | PyObject * |
| 13389 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13390 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13391 | void *fmt; |
| 13392 | int fmtkind; |
| 13393 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13394 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13395 | int r; |
| 13396 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13397 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13398 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13399 | PyObject *temp = NULL; |
| 13400 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13401 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13402 | _PyAccu acc; |
| 13403 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13404 | |
| 13405 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13406 | return NULL; |
| 13407 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13408 | return NULL; |
| 13409 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13410 | return NULL; |
| 13411 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13412 | return NULL; |
| 13413 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13414 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13416 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13417 | PyErr_BadInternalCall(); |
| 13418 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13419 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13420 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13421 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13422 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13423 | if (PyUnicode_READY(uformat) == -1) |
| 13424 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13425 | if (_PyAccu_Init(&acc)) |
| 13426 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13427 | fmt = PyUnicode_DATA(uformat); |
| 13428 | fmtkind = PyUnicode_KIND(uformat); |
| 13429 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13430 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13432 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13433 | arglen = PyTuple_Size(args); |
| 13434 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13435 | } |
| 13436 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13437 | arglen = -1; |
| 13438 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13439 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13440 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13441 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13442 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13443 | |
| 13444 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13445 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13446 | PyObject *nonfmt; |
| 13447 | Py_ssize_t nonfmtpos; |
| 13448 | nonfmtpos = fmtpos++; |
| 13449 | while (fmtcnt >= 0 && |
| 13450 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13451 | fmtpos++; |
| 13452 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13453 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13454 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13455 | if (nonfmt == NULL) |
| 13456 | goto onError; |
| 13457 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13458 | Py_DECREF(nonfmt); |
| 13459 | if (r) |
| 13460 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13461 | } |
| 13462 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13463 | /* Got a format specifier */ |
| 13464 | int flags = 0; |
| 13465 | Py_ssize_t width = -1; |
| 13466 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13467 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13468 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13469 | int isnumok; |
| 13470 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13471 | void *pbuf = NULL; |
| 13472 | Py_ssize_t pindex, len; |
| 13473 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13474 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13475 | fmtpos++; |
| 13476 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13477 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13478 | Py_ssize_t keylen; |
| 13479 | PyObject *key; |
| 13480 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13482 | if (dict == NULL) { |
| 13483 | PyErr_SetString(PyExc_TypeError, |
| 13484 | "format requires a mapping"); |
| 13485 | goto onError; |
| 13486 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13487 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13488 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13489 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13490 | /* Skip over balanced parentheses */ |
| 13491 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13492 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13493 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13494 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13495 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13496 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13498 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13499 | if (fmtcnt < 0 || pcount > 0) { |
| 13500 | PyErr_SetString(PyExc_ValueError, |
| 13501 | "incomplete format key"); |
| 13502 | goto onError; |
| 13503 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13504 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13505 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13506 | if (key == NULL) |
| 13507 | goto onError; |
| 13508 | if (args_owned) { |
| 13509 | Py_DECREF(args); |
| 13510 | args_owned = 0; |
| 13511 | } |
| 13512 | args = PyObject_GetItem(dict, key); |
| 13513 | Py_DECREF(key); |
| 13514 | if (args == NULL) { |
| 13515 | goto onError; |
| 13516 | } |
| 13517 | args_owned = 1; |
| 13518 | arglen = -1; |
| 13519 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13520 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13521 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13522 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13523 | case '-': flags |= F_LJUST; continue; |
| 13524 | case '+': flags |= F_SIGN; continue; |
| 13525 | case ' ': flags |= F_BLANK; continue; |
| 13526 | case '#': flags |= F_ALT; continue; |
| 13527 | case '0': flags |= F_ZERO; continue; |
| 13528 | } |
| 13529 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13530 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13531 | if (c == '*') { |
| 13532 | v = getnextarg(args, arglen, &argidx); |
| 13533 | if (v == NULL) |
| 13534 | goto onError; |
| 13535 | if (!PyLong_Check(v)) { |
| 13536 | PyErr_SetString(PyExc_TypeError, |
| 13537 | "* wants int"); |
| 13538 | goto onError; |
| 13539 | } |
| 13540 | width = PyLong_AsLong(v); |
| 13541 | if (width == -1 && PyErr_Occurred()) |
| 13542 | goto onError; |
| 13543 | if (width < 0) { |
| 13544 | flags |= F_LJUST; |
| 13545 | width = -width; |
| 13546 | } |
| 13547 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13548 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13549 | } |
| 13550 | else if (c >= '0' && c <= '9') { |
| 13551 | width = c - '0'; |
| 13552 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13553 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13554 | if (c < '0' || c > '9') |
| 13555 | break; |
| 13556 | if ((width*10) / 10 != width) { |
| 13557 | PyErr_SetString(PyExc_ValueError, |
| 13558 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13559 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13560 | } |
| 13561 | width = width*10 + (c - '0'); |
| 13562 | } |
| 13563 | } |
| 13564 | if (c == '.') { |
| 13565 | prec = 0; |
| 13566 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13567 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13568 | if (c == '*') { |
| 13569 | v = getnextarg(args, arglen, &argidx); |
| 13570 | if (v == NULL) |
| 13571 | goto onError; |
| 13572 | if (!PyLong_Check(v)) { |
| 13573 | PyErr_SetString(PyExc_TypeError, |
| 13574 | "* wants int"); |
| 13575 | goto onError; |
| 13576 | } |
| 13577 | prec = PyLong_AsLong(v); |
| 13578 | if (prec == -1 && PyErr_Occurred()) |
| 13579 | goto onError; |
| 13580 | if (prec < 0) |
| 13581 | prec = 0; |
| 13582 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13583 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13584 | } |
| 13585 | else if (c >= '0' && c <= '9') { |
| 13586 | prec = c - '0'; |
| 13587 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13588 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13589 | if (c < '0' || c > '9') |
| 13590 | break; |
| 13591 | if ((prec*10) / 10 != prec) { |
| 13592 | PyErr_SetString(PyExc_ValueError, |
| 13593 | "prec too big"); |
| 13594 | goto onError; |
| 13595 | } |
| 13596 | prec = prec*10 + (c - '0'); |
| 13597 | } |
| 13598 | } |
| 13599 | } /* prec */ |
| 13600 | if (fmtcnt >= 0) { |
| 13601 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13602 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13603 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13604 | } |
| 13605 | } |
| 13606 | if (fmtcnt < 0) { |
| 13607 | PyErr_SetString(PyExc_ValueError, |
| 13608 | "incomplete format"); |
| 13609 | goto onError; |
| 13610 | } |
| 13611 | if (c != '%') { |
| 13612 | v = getnextarg(args, arglen, &argidx); |
| 13613 | if (v == NULL) |
| 13614 | goto onError; |
| 13615 | } |
| 13616 | sign = 0; |
| 13617 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13618 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13619 | switch (c) { |
| 13620 | |
| 13621 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13622 | _PyAccu_Accumulate(&acc, percent); |
| 13623 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13624 | |
| 13625 | case 's': |
| 13626 | case 'r': |
| 13627 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13628 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13629 | temp = v; |
| 13630 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13631 | } |
| 13632 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13633 | if (c == 's') |
| 13634 | temp = PyObject_Str(v); |
| 13635 | else if (c == 'r') |
| 13636 | temp = PyObject_Repr(v); |
| 13637 | else |
| 13638 | temp = PyObject_ASCII(v); |
| 13639 | if (temp == NULL) |
| 13640 | goto onError; |
| 13641 | if (PyUnicode_Check(temp)) |
| 13642 | /* nothing to do */; |
| 13643 | else { |
| 13644 | Py_DECREF(temp); |
| 13645 | PyErr_SetString(PyExc_TypeError, |
| 13646 | "%s argument has non-string str()"); |
| 13647 | goto onError; |
| 13648 | } |
| 13649 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13650 | if (PyUnicode_READY(temp) == -1) { |
| 13651 | Py_CLEAR(temp); |
| 13652 | goto onError; |
| 13653 | } |
| 13654 | pbuf = PyUnicode_DATA(temp); |
| 13655 | kind = PyUnicode_KIND(temp); |
| 13656 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13657 | if (prec >= 0 && len > prec) |
| 13658 | len = prec; |
| 13659 | break; |
| 13660 | |
| 13661 | case 'i': |
| 13662 | case 'd': |
| 13663 | case 'u': |
| 13664 | case 'o': |
| 13665 | case 'x': |
| 13666 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13667 | isnumok = 0; |
| 13668 | if (PyNumber_Check(v)) { |
| 13669 | PyObject *iobj=NULL; |
| 13670 | |
| 13671 | if (PyLong_Check(v)) { |
| 13672 | iobj = v; |
| 13673 | Py_INCREF(iobj); |
| 13674 | } |
| 13675 | else { |
| 13676 | iobj = PyNumber_Long(v); |
| 13677 | } |
| 13678 | if (iobj!=NULL) { |
| 13679 | if (PyLong_Check(iobj)) { |
| 13680 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13681 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13682 | Py_DECREF(iobj); |
| 13683 | if (!temp) |
| 13684 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13685 | if (PyUnicode_READY(temp) == -1) { |
| 13686 | Py_CLEAR(temp); |
| 13687 | goto onError; |
| 13688 | } |
| 13689 | pbuf = PyUnicode_DATA(temp); |
| 13690 | kind = PyUnicode_KIND(temp); |
| 13691 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13692 | sign = 1; |
| 13693 | } |
| 13694 | else { |
| 13695 | Py_DECREF(iobj); |
| 13696 | } |
| 13697 | } |
| 13698 | } |
| 13699 | if (!isnumok) { |
| 13700 | PyErr_Format(PyExc_TypeError, |
| 13701 | "%%%c format: a number is required, " |
| 13702 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13703 | goto onError; |
| 13704 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13705 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13706 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13707 | fillobj = zero; |
| 13708 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13709 | break; |
| 13710 | |
| 13711 | case 'e': |
| 13712 | case 'E': |
| 13713 | case 'f': |
| 13714 | case 'F': |
| 13715 | case 'g': |
| 13716 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13717 | temp = formatfloat(v, flags, prec, c); |
| 13718 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13719 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13720 | if (PyUnicode_READY(temp) == -1) { |
| 13721 | Py_CLEAR(temp); |
| 13722 | goto onError; |
| 13723 | } |
| 13724 | pbuf = PyUnicode_DATA(temp); |
| 13725 | kind = PyUnicode_KIND(temp); |
| 13726 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13727 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13728 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13729 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13730 | fillobj = zero; |
| 13731 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13732 | break; |
| 13733 | |
| 13734 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13735 | { |
| 13736 | Py_UCS4 ch = formatchar(v); |
| 13737 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13738 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13739 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13740 | if (temp == NULL) |
| 13741 | goto onError; |
| 13742 | pbuf = PyUnicode_DATA(temp); |
| 13743 | kind = PyUnicode_KIND(temp); |
| 13744 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13745 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13746 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13747 | |
| 13748 | default: |
| 13749 | PyErr_Format(PyExc_ValueError, |
| 13750 | "unsupported format character '%c' (0x%x) " |
| 13751 | "at index %zd", |
| 13752 | (31<=c && c<=126) ? (char)c : '?', |
| 13753 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13754 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13755 | goto onError; |
| 13756 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13757 | /* pbuf is initialized here. */ |
| 13758 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13759 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13760 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13761 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13762 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13763 | pindex++; |
| 13764 | } |
| 13765 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13766 | signobj = plus; |
| 13767 | len--; |
| 13768 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13769 | } |
| 13770 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13771 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13772 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13773 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13774 | else |
| 13775 | sign = 0; |
| 13776 | } |
| 13777 | if (width < len) |
| 13778 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13779 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13780 | if (fill != ' ') { |
| 13781 | assert(signobj != NULL); |
| 13782 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13783 | goto onError; |
| 13784 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13785 | if (width > len) |
| 13786 | width--; |
| 13787 | } |
| 13788 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13789 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13790 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13791 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13792 | second = get_latin1_char( |
| 13793 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13794 | pindex += 2; |
| 13795 | if (second == NULL || |
| 13796 | _PyAccu_Accumulate(&acc, zero) || |
| 13797 | _PyAccu_Accumulate(&acc, second)) |
| 13798 | goto onError; |
| 13799 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13800 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13801 | width -= 2; |
| 13802 | if (width < 0) |
| 13803 | width = 0; |
| 13804 | len -= 2; |
| 13805 | } |
| 13806 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13807 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13808 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13809 | goto onError; |
| 13810 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13811 | } |
| 13812 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13813 | if (sign) { |
| 13814 | assert(signobj != NULL); |
| 13815 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13816 | goto onError; |
| 13817 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13818 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13819 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13820 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13821 | second = get_latin1_char( |
| 13822 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13823 | pindex += 2; |
| 13824 | if (second == NULL || |
| 13825 | _PyAccu_Accumulate(&acc, zero) || |
| 13826 | _PyAccu_Accumulate(&acc, second)) |
| 13827 | goto onError; |
| 13828 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13829 | } |
| 13830 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13831 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13832 | if (temp != NULL) { |
| 13833 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13834 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13835 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13836 | else { |
| 13837 | const char *p = (const char *) pbuf; |
| 13838 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13839 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13840 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13841 | } |
| 13842 | if (v == NULL) |
| 13843 | goto onError; |
| 13844 | r = _PyAccu_Accumulate(&acc, v); |
| 13845 | Py_DECREF(v); |
| 13846 | if (r) |
| 13847 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13848 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13849 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13850 | if (dict && (argidx < arglen) && c != '%') { |
| 13851 | PyErr_SetString(PyExc_TypeError, |
| 13852 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13853 | goto onError; |
| 13854 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13855 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13856 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13857 | } /* until end */ |
| 13858 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13859 | PyErr_SetString(PyExc_TypeError, |
| 13860 | "not all arguments converted during string formatting"); |
| 13861 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13862 | } |
| 13863 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13864 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13865 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13866 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13867 | } |
| 13868 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13869 | Py_XDECREF(temp); |
| 13870 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13871 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13872 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13873 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13874 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13875 | Py_XDECREF(temp); |
| 13876 | Py_XDECREF(second); |
| 13877 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13878 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13879 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13880 | } |
| 13881 | return NULL; |
| 13882 | } |
| 13883 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13884 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13885 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13886 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13887 | static PyObject * |
| 13888 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13889 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13890 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13891 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13892 | char *encoding = NULL; |
| 13893 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13894 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13895 | if (type != &PyUnicode_Type) |
| 13896 | return unicode_subtype_new(type, args, kwds); |
| 13897 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13898 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13899 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13900 | if (x == NULL) { |
| 13901 | Py_INCREF(unicode_empty); |
| 13902 | return unicode_empty; |
| 13903 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13904 | if (encoding == NULL && errors == NULL) |
| 13905 | return PyObject_Str(x); |
| 13906 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13907 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13908 | } |
| 13909 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13910 | static PyObject * |
| 13911 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13912 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13913 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13914 | Py_ssize_t length, char_size; |
| 13915 | int share_wstr, share_utf8; |
| 13916 | unsigned int kind; |
| 13917 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13918 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13919 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13920 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13921 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13922 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13923 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13924 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13925 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13926 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13927 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13928 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13929 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13930 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13931 | if (self == NULL) { |
| 13932 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13933 | return NULL; |
| 13934 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13935 | kind = PyUnicode_KIND(unicode); |
| 13936 | length = PyUnicode_GET_LENGTH(unicode); |
| 13937 | |
| 13938 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13939 | #ifdef Py_DEBUG |
| 13940 | _PyUnicode_HASH(self) = -1; |
| 13941 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13942 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13943 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13944 | _PyUnicode_STATE(self).interned = 0; |
| 13945 | _PyUnicode_STATE(self).kind = kind; |
| 13946 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13947 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13948 | _PyUnicode_STATE(self).ready = 1; |
| 13949 | _PyUnicode_WSTR(self) = NULL; |
| 13950 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13951 | _PyUnicode_UTF8(self) = NULL; |
| 13952 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13953 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13954 | |
| 13955 | share_utf8 = 0; |
| 13956 | share_wstr = 0; |
| 13957 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13958 | char_size = 1; |
| 13959 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13960 | share_utf8 = 1; |
| 13961 | } |
| 13962 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13963 | char_size = 2; |
| 13964 | if (sizeof(wchar_t) == 2) |
| 13965 | share_wstr = 1; |
| 13966 | } |
| 13967 | else { |
| 13968 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13969 | char_size = 4; |
| 13970 | if (sizeof(wchar_t) == 4) |
| 13971 | share_wstr = 1; |
| 13972 | } |
| 13973 | |
| 13974 | /* Ensure we won't overflow the length. */ |
| 13975 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13976 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13977 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13978 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13979 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13980 | if (data == NULL) { |
| 13981 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13982 | goto onError; |
| 13983 | } |
| 13984 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13985 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13986 | if (share_utf8) { |
| 13987 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13988 | _PyUnicode_UTF8(self) = data; |
| 13989 | } |
| 13990 | if (share_wstr) { |
| 13991 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13992 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13993 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13994 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13995 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13996 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13997 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13998 | #ifdef Py_DEBUG |
| 13999 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14000 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14001 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14002 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14003 | |
| 14004 | onError: |
| 14005 | Py_DECREF(unicode); |
| 14006 | Py_DECREF(self); |
| 14007 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14008 | } |
| 14009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14010 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14011 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14012 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14013 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14014 | encoding defaults to the current default string encoding.\n\ |
| 14015 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14016 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14017 | static PyObject *unicode_iter(PyObject *seq); |
| 14018 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14019 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14020 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14021 | "str", /* tp_name */ |
| 14022 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14023 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14024 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14025 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14026 | 0, /* tp_print */ |
| 14027 | 0, /* tp_getattr */ |
| 14028 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14029 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14030 | unicode_repr, /* tp_repr */ |
| 14031 | &unicode_as_number, /* tp_as_number */ |
| 14032 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14033 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14034 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14035 | 0, /* tp_call*/ |
| 14036 | (reprfunc) unicode_str, /* tp_str */ |
| 14037 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14038 | 0, /* tp_setattro */ |
| 14039 | 0, /* tp_as_buffer */ |
| 14040 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14041 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14042 | unicode_doc, /* tp_doc */ |
| 14043 | 0, /* tp_traverse */ |
| 14044 | 0, /* tp_clear */ |
| 14045 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14046 | 0, /* tp_weaklistoffset */ |
| 14047 | unicode_iter, /* tp_iter */ |
| 14048 | 0, /* tp_iternext */ |
| 14049 | unicode_methods, /* tp_methods */ |
| 14050 | 0, /* tp_members */ |
| 14051 | 0, /* tp_getset */ |
| 14052 | &PyBaseObject_Type, /* tp_base */ |
| 14053 | 0, /* tp_dict */ |
| 14054 | 0, /* tp_descr_get */ |
| 14055 | 0, /* tp_descr_set */ |
| 14056 | 0, /* tp_dictoffset */ |
| 14057 | 0, /* tp_init */ |
| 14058 | 0, /* tp_alloc */ |
| 14059 | unicode_new, /* tp_new */ |
| 14060 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14061 | }; |
| 14062 | |
| 14063 | /* Initialize the Unicode implementation */ |
| 14064 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14065 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14066 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14067 | int i; |
| 14068 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14069 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14070 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14071 | 0x000A, /* LINE FEED */ |
| 14072 | 0x000D, /* CARRIAGE RETURN */ |
| 14073 | 0x001C, /* FILE SEPARATOR */ |
| 14074 | 0x001D, /* GROUP SEPARATOR */ |
| 14075 | 0x001E, /* RECORD SEPARATOR */ |
| 14076 | 0x0085, /* NEXT LINE */ |
| 14077 | 0x2028, /* LINE SEPARATOR */ |
| 14078 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14079 | }; |
| 14080 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14081 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14082 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14083 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14084 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14085 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14086 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14087 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14088 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14089 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14090 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14091 | |
| 14092 | /* initialize the linebreak bloom filter */ |
| 14093 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14094 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14095 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14096 | |
| 14097 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14098 | |
| 14099 | #ifdef HAVE_MBCS |
| 14100 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14101 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14102 | PyErr_SetFromWindowsErr(0); |
| 14103 | return -1; |
| 14104 | } |
| 14105 | #endif |
| 14106 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14107 | } |
| 14108 | |
| 14109 | /* Finalize the Unicode implementation */ |
| 14110 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14111 | int |
| 14112 | PyUnicode_ClearFreeList(void) |
| 14113 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14114 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14115 | } |
| 14116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14117 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14118 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14119 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14120 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14121 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14122 | Py_XDECREF(unicode_empty); |
| 14123 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14124 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14125 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14126 | if (unicode_latin1[i]) { |
| 14127 | Py_DECREF(unicode_latin1[i]); |
| 14128 | unicode_latin1[i] = NULL; |
| 14129 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14130 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14131 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14132 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14133 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14134 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14135 | void |
| 14136 | PyUnicode_InternInPlace(PyObject **p) |
| 14137 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14138 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14139 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14140 | #ifdef Py_DEBUG |
| 14141 | assert(s != NULL); |
| 14142 | assert(_PyUnicode_CHECK(s)); |
| 14143 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14144 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14145 | return; |
| 14146 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14147 | /* If it's a subclass, we don't really know what putting |
| 14148 | it in the interned dict might do. */ |
| 14149 | if (!PyUnicode_CheckExact(s)) |
| 14150 | return; |
| 14151 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14152 | return; |
| 14153 | if (interned == NULL) { |
| 14154 | interned = PyDict_New(); |
| 14155 | if (interned == NULL) { |
| 14156 | PyErr_Clear(); /* Don't leave an exception */ |
| 14157 | return; |
| 14158 | } |
| 14159 | } |
| 14160 | /* It might be that the GetItem call fails even |
| 14161 | though the key is present in the dictionary, |
| 14162 | namely when this happens during a stack overflow. */ |
| 14163 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14164 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14165 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14166 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14167 | if (t) { |
| 14168 | Py_INCREF(t); |
| 14169 | Py_DECREF(*p); |
| 14170 | *p = t; |
| 14171 | return; |
| 14172 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14173 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14174 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14175 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14176 | PyErr_Clear(); |
| 14177 | PyThreadState_GET()->recursion_critical = 0; |
| 14178 | return; |
| 14179 | } |
| 14180 | PyThreadState_GET()->recursion_critical = 0; |
| 14181 | /* The two references in interned are not counted by refcnt. |
| 14182 | The deallocator will take care of this */ |
| 14183 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14184 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14185 | } |
| 14186 | |
| 14187 | void |
| 14188 | PyUnicode_InternImmortal(PyObject **p) |
| 14189 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14190 | PyUnicode_InternInPlace(p); |
| 14191 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14192 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14193 | Py_INCREF(*p); |
| 14194 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14195 | } |
| 14196 | |
| 14197 | PyObject * |
| 14198 | PyUnicode_InternFromString(const char *cp) |
| 14199 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14200 | PyObject *s = PyUnicode_FromString(cp); |
| 14201 | if (s == NULL) |
| 14202 | return NULL; |
| 14203 | PyUnicode_InternInPlace(&s); |
| 14204 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14205 | } |
| 14206 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14207 | void |
| 14208 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14209 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14210 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14211 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14212 | Py_ssize_t i, n; |
| 14213 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14214 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14215 | if (interned == NULL || !PyDict_Check(interned)) |
| 14216 | return; |
| 14217 | keys = PyDict_Keys(interned); |
| 14218 | if (keys == NULL || !PyList_Check(keys)) { |
| 14219 | PyErr_Clear(); |
| 14220 | return; |
| 14221 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14222 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14223 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14224 | detector, interned unicode strings are not forcibly deallocated; |
| 14225 | rather, we give them their stolen references back, and then clear |
| 14226 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14227 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14228 | n = PyList_GET_SIZE(keys); |
| 14229 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14230 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14231 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14232 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14233 | if (PyUnicode_READY(s) == -1) { |
| 14234 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14235 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14236 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14237 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14238 | case SSTATE_NOT_INTERNED: |
| 14239 | /* XXX Shouldn't happen */ |
| 14240 | break; |
| 14241 | case SSTATE_INTERNED_IMMORTAL: |
| 14242 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14243 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14244 | break; |
| 14245 | case SSTATE_INTERNED_MORTAL: |
| 14246 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14247 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14248 | break; |
| 14249 | default: |
| 14250 | Py_FatalError("Inconsistent interned string state."); |
| 14251 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14252 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14253 | } |
| 14254 | fprintf(stderr, "total size of all interned strings: " |
| 14255 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14256 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14257 | Py_DECREF(keys); |
| 14258 | PyDict_Clear(interned); |
| 14259 | Py_DECREF(interned); |
| 14260 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14261 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14262 | |
| 14263 | |
| 14264 | /********************* Unicode Iterator **************************/ |
| 14265 | |
| 14266 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14267 | PyObject_HEAD |
| 14268 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14269 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14270 | } unicodeiterobject; |
| 14271 | |
| 14272 | static void |
| 14273 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14274 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14275 | _PyObject_GC_UNTRACK(it); |
| 14276 | Py_XDECREF(it->it_seq); |
| 14277 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14278 | } |
| 14279 | |
| 14280 | static int |
| 14281 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14282 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14283 | Py_VISIT(it->it_seq); |
| 14284 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14285 | } |
| 14286 | |
| 14287 | static PyObject * |
| 14288 | unicodeiter_next(unicodeiterobject *it) |
| 14289 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14290 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14291 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14292 | assert(it != NULL); |
| 14293 | seq = it->it_seq; |
| 14294 | if (seq == NULL) |
| 14295 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14296 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14298 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14299 | int kind = PyUnicode_KIND(seq); |
| 14300 | void *data = PyUnicode_DATA(seq); |
| 14301 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14302 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14303 | if (item != NULL) |
| 14304 | ++it->it_index; |
| 14305 | return item; |
| 14306 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14307 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14308 | Py_DECREF(seq); |
| 14309 | it->it_seq = NULL; |
| 14310 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14311 | } |
| 14312 | |
| 14313 | static PyObject * |
| 14314 | unicodeiter_len(unicodeiterobject *it) |
| 14315 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14316 | Py_ssize_t len = 0; |
| 14317 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14318 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14319 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14320 | } |
| 14321 | |
| 14322 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14323 | |
| 14324 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14325 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14326 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14327 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14328 | }; |
| 14329 | |
| 14330 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14331 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14332 | "str_iterator", /* tp_name */ |
| 14333 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14334 | 0, /* tp_itemsize */ |
| 14335 | /* methods */ |
| 14336 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14337 | 0, /* tp_print */ |
| 14338 | 0, /* tp_getattr */ |
| 14339 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14340 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14341 | 0, /* tp_repr */ |
| 14342 | 0, /* tp_as_number */ |
| 14343 | 0, /* tp_as_sequence */ |
| 14344 | 0, /* tp_as_mapping */ |
| 14345 | 0, /* tp_hash */ |
| 14346 | 0, /* tp_call */ |
| 14347 | 0, /* tp_str */ |
| 14348 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14349 | 0, /* tp_setattro */ |
| 14350 | 0, /* tp_as_buffer */ |
| 14351 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14352 | 0, /* tp_doc */ |
| 14353 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14354 | 0, /* tp_clear */ |
| 14355 | 0, /* tp_richcompare */ |
| 14356 | 0, /* tp_weaklistoffset */ |
| 14357 | PyObject_SelfIter, /* tp_iter */ |
| 14358 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14359 | unicodeiter_methods, /* tp_methods */ |
| 14360 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14361 | }; |
| 14362 | |
| 14363 | static PyObject * |
| 14364 | unicode_iter(PyObject *seq) |
| 14365 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14366 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14367 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14368 | if (!PyUnicode_Check(seq)) { |
| 14369 | PyErr_BadInternalCall(); |
| 14370 | return NULL; |
| 14371 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14372 | if (PyUnicode_READY(seq) == -1) |
| 14373 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14374 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14375 | if (it == NULL) |
| 14376 | return NULL; |
| 14377 | it->it_index = 0; |
| 14378 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14379 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14380 | _PyObject_GC_TRACK(it); |
| 14381 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14382 | } |
| 14383 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14384 | |
| 14385 | size_t |
| 14386 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14387 | { |
| 14388 | int res = 0; |
| 14389 | while(*u++) |
| 14390 | res++; |
| 14391 | return res; |
| 14392 | } |
| 14393 | |
| 14394 | Py_UNICODE* |
| 14395 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14396 | { |
| 14397 | Py_UNICODE *u = s1; |
| 14398 | while ((*u++ = *s2++)); |
| 14399 | return s1; |
| 14400 | } |
| 14401 | |
| 14402 | Py_UNICODE* |
| 14403 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14404 | { |
| 14405 | Py_UNICODE *u = s1; |
| 14406 | while ((*u++ = *s2++)) |
| 14407 | if (n-- == 0) |
| 14408 | break; |
| 14409 | return s1; |
| 14410 | } |
| 14411 | |
| 14412 | Py_UNICODE* |
| 14413 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14414 | { |
| 14415 | Py_UNICODE *u1 = s1; |
| 14416 | u1 += Py_UNICODE_strlen(u1); |
| 14417 | Py_UNICODE_strcpy(u1, s2); |
| 14418 | return s1; |
| 14419 | } |
| 14420 | |
| 14421 | int |
| 14422 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14423 | { |
| 14424 | while (*s1 && *s2 && *s1 == *s2) |
| 14425 | s1++, s2++; |
| 14426 | if (*s1 && *s2) |
| 14427 | return (*s1 < *s2) ? -1 : +1; |
| 14428 | if (*s1) |
| 14429 | return 1; |
| 14430 | if (*s2) |
| 14431 | return -1; |
| 14432 | return 0; |
| 14433 | } |
| 14434 | |
| 14435 | int |
| 14436 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14437 | { |
| 14438 | register Py_UNICODE u1, u2; |
| 14439 | for (; n != 0; n--) { |
| 14440 | u1 = *s1; |
| 14441 | u2 = *s2; |
| 14442 | if (u1 != u2) |
| 14443 | return (u1 < u2) ? -1 : +1; |
| 14444 | if (u1 == '\0') |
| 14445 | return 0; |
| 14446 | s1++; |
| 14447 | s2++; |
| 14448 | } |
| 14449 | return 0; |
| 14450 | } |
| 14451 | |
| 14452 | Py_UNICODE* |
| 14453 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14454 | { |
| 14455 | const Py_UNICODE *p; |
| 14456 | for (p = s; *p; p++) |
| 14457 | if (*p == c) |
| 14458 | return (Py_UNICODE*)p; |
| 14459 | return NULL; |
| 14460 | } |
| 14461 | |
| 14462 | Py_UNICODE* |
| 14463 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14464 | { |
| 14465 | const Py_UNICODE *p; |
| 14466 | p = s + Py_UNICODE_strlen(s); |
| 14467 | while (p != s) { |
| 14468 | p--; |
| 14469 | if (*p == c) |
| 14470 | return (Py_UNICODE*)p; |
| 14471 | } |
| 14472 | return NULL; |
| 14473 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14474 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14475 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14476 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14477 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14478 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14479 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14480 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14481 | if (!PyUnicode_Check(unicode)) { |
| 14482 | PyErr_BadArgument(); |
| 14483 | return NULL; |
| 14484 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14485 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14486 | if (u == NULL) |
| 14487 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14488 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14489 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14490 | PyErr_NoMemory(); |
| 14491 | return NULL; |
| 14492 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14493 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14494 | size *= sizeof(Py_UNICODE); |
| 14495 | copy = PyMem_Malloc(size); |
| 14496 | if (copy == NULL) { |
| 14497 | PyErr_NoMemory(); |
| 14498 | return NULL; |
| 14499 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14500 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14501 | return copy; |
| 14502 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14503 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14504 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14505 | to the string.Formatter class implemented in Python. */ |
| 14506 | |
| 14507 | static PyMethodDef _string_methods[] = { |
| 14508 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14509 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14510 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14511 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14512 | {NULL, NULL} |
| 14513 | }; |
| 14514 | |
| 14515 | static struct PyModuleDef _string_module = { |
| 14516 | PyModuleDef_HEAD_INIT, |
| 14517 | "_string", |
| 14518 | PyDoc_STR("string helper module"), |
| 14519 | 0, |
| 14520 | _string_methods, |
| 14521 | NULL, |
| 14522 | NULL, |
| 14523 | NULL, |
| 14524 | NULL |
| 14525 | }; |
| 14526 | |
| 14527 | PyMODINIT_FUNC |
| 14528 | PyInit__string(void) |
| 14529 | { |
| 14530 | return PyModule_Create(&_string_module); |
| 14531 | } |
| 14532 | |
| 14533 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14534 | #ifdef __cplusplus |
| 14535 | } |
| 14536 | #endif |