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 { |
| 1001 | kind_state = PyUnicode_4BYTE_KIND; |
| 1002 | char_size = 4; |
| 1003 | if (sizeof(wchar_t) == 4) |
| 1004 | is_sharing = 1; |
| 1005 | } |
| 1006 | |
| 1007 | /* Ensure we won't overflow the size. */ |
| 1008 | if (size < 0) { |
| 1009 | PyErr_SetString(PyExc_SystemError, |
| 1010 | "Negative size passed to PyUnicode_New"); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1014 | return PyErr_NoMemory(); |
| 1015 | |
| 1016 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1017 | * PyObject_New() so we are able to allocate space for the object and |
| 1018 | * it's data buffer. |
| 1019 | */ |
| 1020 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1021 | if (obj == NULL) |
| 1022 | return PyErr_NoMemory(); |
| 1023 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1024 | if (obj == NULL) |
| 1025 | return NULL; |
| 1026 | |
| 1027 | unicode = (PyCompactUnicodeObject *)obj; |
| 1028 | if (is_ascii) |
| 1029 | data = ((PyASCIIObject*)obj) + 1; |
| 1030 | else |
| 1031 | data = unicode + 1; |
| 1032 | _PyUnicode_LENGTH(unicode) = size; |
| 1033 | _PyUnicode_HASH(unicode) = -1; |
| 1034 | _PyUnicode_STATE(unicode).interned = 0; |
| 1035 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 1036 | _PyUnicode_STATE(unicode).compact = 1; |
| 1037 | _PyUnicode_STATE(unicode).ready = 1; |
| 1038 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1039 | if (is_ascii) { |
| 1040 | ((char*)data)[size] = 0; |
| 1041 | _PyUnicode_WSTR(unicode) = NULL; |
| 1042 | } |
| 1043 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 1044 | ((char*)data)[size] = 0; |
| 1045 | _PyUnicode_WSTR(unicode) = NULL; |
| 1046 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1048 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1049 | } |
| 1050 | else { |
| 1051 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1052 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 1054 | ((Py_UCS2*)data)[size] = 0; |
| 1055 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 1056 | ((Py_UCS4*)data)[size] = 0; |
| 1057 | if (is_sharing) { |
| 1058 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1059 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1060 | } |
| 1061 | else { |
| 1062 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1063 | _PyUnicode_WSTR(unicode) = NULL; |
| 1064 | } |
| 1065 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1066 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1067 | return obj; |
| 1068 | } |
| 1069 | |
| 1070 | #if SIZEOF_WCHAR_T == 2 |
| 1071 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1072 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1073 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | |
| 1075 | This function assumes that unicode can hold one more code point than wstr |
| 1076 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1077 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1078 | 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] | 1079 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1080 | { |
| 1081 | const wchar_t *iter; |
| 1082 | Py_UCS4 *ucs4_out; |
| 1083 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1084 | assert(unicode != NULL); |
| 1085 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1086 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1087 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1088 | |
| 1089 | for (iter = begin; iter < end; ) { |
| 1090 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1091 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1092 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1093 | && (iter+1) < end |
| 1094 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1095 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1096 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | iter += 2; |
| 1098 | } |
| 1099 | else { |
| 1100 | *ucs4_out++ = *iter; |
| 1101 | iter++; |
| 1102 | } |
| 1103 | } |
| 1104 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1105 | _PyUnicode_GET_LENGTH(unicode))); |
| 1106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | } |
| 1108 | #endif |
| 1109 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1110 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1111 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1112 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1113 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1114 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1115 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1116 | return -1; |
| 1117 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1121 | static int |
| 1122 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1123 | PyObject *from, Py_ssize_t from_start, |
| 1124 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1125 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1126 | unsigned int from_kind, to_kind; |
| 1127 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1128 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1129 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1130 | assert(PyUnicode_Check(from)); |
| 1131 | assert(PyUnicode_Check(to)); |
| 1132 | assert(PyUnicode_IS_READY(from)); |
| 1133 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1134 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1135 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1136 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1137 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1138 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1139 | if (how_many == 0) |
| 1140 | return 0; |
| 1141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1143 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1145 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1147 | #ifdef Py_DEBUG |
| 1148 | if (!check_maxchar |
| 1149 | && (from_kind > to_kind |
| 1150 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1151 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1152 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1153 | Py_UCS4 ch; |
| 1154 | Py_ssize_t i; |
| 1155 | for (i=0; i < how_many; i++) { |
| 1156 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1157 | assert(ch <= to_maxchar); |
| 1158 | } |
| 1159 | } |
| 1160 | #endif |
| 1161 | fast = (from_kind == to_kind); |
| 1162 | if (check_maxchar |
| 1163 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1164 | { |
| 1165 | /* deny latin1 => ascii */ |
| 1166 | fast = 0; |
| 1167 | } |
| 1168 | |
| 1169 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1170 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1171 | (char*)from_data + from_kind * from_start, |
| 1172 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1173 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1174 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1175 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1176 | { |
| 1177 | _PyUnicode_CONVERT_BYTES( |
| 1178 | Py_UCS1, Py_UCS2, |
| 1179 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1180 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1181 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1182 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1183 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1184 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1185 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1186 | { |
| 1187 | _PyUnicode_CONVERT_BYTES( |
| 1188 | Py_UCS1, Py_UCS4, |
| 1189 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1190 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1191 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1192 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1193 | } |
| 1194 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1195 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1196 | { |
| 1197 | _PyUnicode_CONVERT_BYTES( |
| 1198 | Py_UCS2, Py_UCS4, |
| 1199 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1200 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1201 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1202 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1203 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1204 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1205 | /* check if max_char(from substring) <= max_char(to) */ |
| 1206 | if (from_kind > to_kind |
| 1207 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1208 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1209 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1210 | /* slow path to check for character overflow */ |
| 1211 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1212 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1213 | Py_ssize_t i; |
| 1214 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1215 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1216 | for (i=0; i < how_many; i++) { |
| 1217 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1218 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1219 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1220 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1221 | #else |
| 1222 | if (!check_maxchar) { |
| 1223 | for (i=0; i < how_many; i++) { |
| 1224 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1225 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1226 | } |
| 1227 | } |
| 1228 | else { |
| 1229 | for (i=0; i < how_many; i++) { |
| 1230 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1231 | if (ch > to_maxchar) |
| 1232 | return 1; |
| 1233 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1237 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1238 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1239 | assert(0 && "inconsistent state"); |
| 1240 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1241 | } |
| 1242 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1248 | PyObject *from, Py_ssize_t from_start, |
| 1249 | Py_ssize_t how_many) |
| 1250 | { |
| 1251 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1252 | } |
| 1253 | |
| 1254 | Py_ssize_t |
| 1255 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1256 | PyObject *from, Py_ssize_t from_start, |
| 1257 | Py_ssize_t how_many) |
| 1258 | { |
| 1259 | int err; |
| 1260 | |
| 1261 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1262 | PyErr_BadInternalCall(); |
| 1263 | return -1; |
| 1264 | } |
| 1265 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1266 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1267 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1268 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1269 | return -1; |
| 1270 | |
| 1271 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1272 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1273 | PyErr_Format(PyExc_SystemError, |
| 1274 | "Cannot write %zi characters at %zi " |
| 1275 | "in a string of %zi characters", |
| 1276 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (how_many == 0) |
| 1281 | return 0; |
| 1282 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1283 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1284 | return -1; |
| 1285 | |
| 1286 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1287 | if (err) { |
| 1288 | PyErr_Format(PyExc_SystemError, |
| 1289 | "Cannot copy %s characters " |
| 1290 | "into a string of %s characters", |
| 1291 | unicode_kind_name(from), |
| 1292 | unicode_kind_name(to)); |
| 1293 | return -1; |
| 1294 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1295 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1298 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1299 | correct string length can be computed before converting a string to UCS4. |
| 1300 | This function counts single surrogates as a character and not as a pair. |
| 1301 | |
| 1302 | Return 0 on success, or -1 on error. */ |
| 1303 | static int |
| 1304 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1305 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1306 | { |
| 1307 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1308 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1309 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1310 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1311 | *num_surrogates = 0; |
| 1312 | *maxchar = 0; |
| 1313 | |
| 1314 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1316 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1317 | && (iter+1) < end |
| 1318 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1319 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1320 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1321 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | iter += 2; |
| 1323 | } |
| 1324 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1325 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1326 | { |
| 1327 | ch = *iter; |
| 1328 | iter++; |
| 1329 | } |
| 1330 | if (ch > *maxchar) { |
| 1331 | *maxchar = ch; |
| 1332 | if (*maxchar > MAX_UNICODE) { |
| 1333 | PyErr_Format(PyExc_ValueError, |
| 1334 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1335 | ch); |
| 1336 | return -1; |
| 1337 | } |
| 1338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1344 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1345 | #endif |
| 1346 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1347 | int |
| 1348 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1349 | { |
| 1350 | wchar_t *end; |
| 1351 | Py_UCS4 maxchar = 0; |
| 1352 | Py_ssize_t num_surrogates; |
| 1353 | #if SIZEOF_WCHAR_T == 2 |
| 1354 | Py_ssize_t length_wo_surrogates; |
| 1355 | #endif |
| 1356 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1357 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1358 | strings were created using _PyObject_New() and where no canonical |
| 1359 | representation (the str field) has been set yet aka strings |
| 1360 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1361 | assert(_PyUnicode_CHECK(unicode)); |
| 1362 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1364 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1365 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1366 | /* Actually, it should neither be interned nor be anything else: */ |
| 1367 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1368 | |
| 1369 | #ifdef Py_DEBUG |
| 1370 | ++unicode_ready_calls; |
| 1371 | #endif |
| 1372 | |
| 1373 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1374 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1375 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | |
| 1378 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1379 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1380 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | PyErr_NoMemory(); |
| 1382 | return -1; |
| 1383 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1384 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | _PyUnicode_WSTR(unicode), end, |
| 1386 | PyUnicode_1BYTE_DATA(unicode)); |
| 1387 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1388 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1389 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1390 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1391 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1392 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1393 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | } |
| 1395 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1396 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1397 | _PyUnicode_UTF8(unicode) = NULL; |
| 1398 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | } |
| 1400 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1401 | _PyUnicode_WSTR(unicode) = NULL; |
| 1402 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1403 | } |
| 1404 | /* In this case we might have to convert down from 4-byte native |
| 1405 | wchar_t to 2-byte unicode. */ |
| 1406 | else if (maxchar < 65536) { |
| 1407 | assert(num_surrogates == 0 && |
| 1408 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1409 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1410 | #if SIZEOF_WCHAR_T == 2 |
| 1411 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1412 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1413 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1414 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1415 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1416 | _PyUnicode_UTF8(unicode) = NULL; |
| 1417 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1418 | #else |
| 1419 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1420 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1421 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1422 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1423 | PyErr_NoMemory(); |
| 1424 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1425 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1426 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1427 | _PyUnicode_WSTR(unicode), end, |
| 1428 | PyUnicode_2BYTE_DATA(unicode)); |
| 1429 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1430 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1431 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1432 | _PyUnicode_UTF8(unicode) = NULL; |
| 1433 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1434 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1435 | _PyUnicode_WSTR(unicode) = NULL; |
| 1436 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1437 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1438 | } |
| 1439 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1440 | else { |
| 1441 | #if SIZEOF_WCHAR_T == 2 |
| 1442 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1443 | new normalized 4-byte version. */ |
| 1444 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1445 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1446 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1447 | PyErr_NoMemory(); |
| 1448 | return -1; |
| 1449 | } |
| 1450 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1451 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 | _PyUnicode_UTF8(unicode) = NULL; |
| 1453 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1454 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1455 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1456 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1457 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1458 | _PyUnicode_WSTR(unicode) = NULL; |
| 1459 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1460 | #else |
| 1461 | assert(num_surrogates == 0); |
| 1462 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1463 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1465 | _PyUnicode_UTF8(unicode) = NULL; |
| 1466 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1468 | #endif |
| 1469 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1470 | } |
| 1471 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1472 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1476 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1477 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1478 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1479 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1480 | case SSTATE_NOT_INTERNED: |
| 1481 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1483 | case SSTATE_INTERNED_MORTAL: |
| 1484 | /* revive dead object temporarily for DelItem */ |
| 1485 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1486 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1487 | Py_FatalError( |
| 1488 | "deletion of interned string failed"); |
| 1489 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1490 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1491 | case SSTATE_INTERNED_IMMORTAL: |
| 1492 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1494 | default: |
| 1495 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1498 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1499 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1500 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1501 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1502 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1503 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1504 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1505 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1508 | #ifdef Py_DEBUG |
| 1509 | static int |
| 1510 | unicode_is_singleton(PyObject *unicode) |
| 1511 | { |
| 1512 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1513 | if (unicode == unicode_empty) |
| 1514 | return 1; |
| 1515 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1516 | { |
| 1517 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1518 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1519 | return 1; |
| 1520 | } |
| 1521 | return 0; |
| 1522 | } |
| 1523 | #endif |
| 1524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1525 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1526 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1527 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1528 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1529 | if (Py_REFCNT(unicode) != 1) |
| 1530 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1531 | if (_PyUnicode_HASH(unicode) != -1) |
| 1532 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1533 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1534 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1535 | if (!PyUnicode_CheckExact(unicode)) |
| 1536 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1537 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1538 | /* singleton refcount is greater than 1 */ |
| 1539 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1540 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1541 | return 1; |
| 1542 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1543 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1544 | static int |
| 1545 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1546 | { |
| 1547 | PyObject *unicode; |
| 1548 | Py_ssize_t old_length; |
| 1549 | |
| 1550 | assert(p_unicode != NULL); |
| 1551 | unicode = *p_unicode; |
| 1552 | |
| 1553 | assert(unicode != NULL); |
| 1554 | assert(PyUnicode_Check(unicode)); |
| 1555 | assert(0 <= length); |
| 1556 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1557 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1558 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1559 | else |
| 1560 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1561 | if (old_length == length) |
| 1562 | return 0; |
| 1563 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1564 | if (length == 0) { |
| 1565 | Py_DECREF(*p_unicode); |
| 1566 | *p_unicode = unicode_empty; |
| 1567 | Py_INCREF(*p_unicode); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1571 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1572 | PyObject *copy = resize_copy(unicode, length); |
| 1573 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1575 | Py_DECREF(*p_unicode); |
| 1576 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1580 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1581 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1582 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1583 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1584 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1585 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1586 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1587 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1588 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1591 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1592 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1593 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1594 | PyObject *unicode; |
| 1595 | if (p_unicode == NULL) { |
| 1596 | PyErr_BadInternalCall(); |
| 1597 | return -1; |
| 1598 | } |
| 1599 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1600 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1601 | { |
| 1602 | PyErr_BadInternalCall(); |
| 1603 | return -1; |
| 1604 | } |
| 1605 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1606 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1607 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1608 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1609 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1610 | { |
| 1611 | PyObject *result; |
| 1612 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1613 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1614 | return 0; |
| 1615 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1616 | maxchar); |
| 1617 | if (result == NULL) |
| 1618 | return -1; |
| 1619 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1620 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1621 | Py_DECREF(*p_unicode); |
| 1622 | *p_unicode = result; |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | static int |
| 1627 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1628 | Py_UCS4 ch) |
| 1629 | { |
| 1630 | if (unicode_widen(p_unicode, ch) < 0) |
| 1631 | return -1; |
| 1632 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1633 | PyUnicode_DATA(*p_unicode), |
| 1634 | (*pos)++, ch); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1638 | static PyObject* |
| 1639 | get_latin1_char(unsigned char ch) |
| 1640 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1641 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1642 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1643 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1644 | if (!unicode) |
| 1645 | return NULL; |
| 1646 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1647 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1648 | unicode_latin1[ch] = unicode; |
| 1649 | } |
| 1650 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1651 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1654 | PyObject * |
| 1655 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1656 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1657 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | Py_UCS4 maxchar = 0; |
| 1659 | Py_ssize_t num_surrogates; |
| 1660 | |
| 1661 | if (u == NULL) |
| 1662 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1663 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1664 | /* If the Unicode data is known at construction time, we can apply |
| 1665 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1666 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | /* Optimization for empty strings */ |
| 1668 | if (size == 0 && unicode_empty != NULL) { |
| 1669 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1670 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1671 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1672 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1673 | /* Single character Unicode objects in the Latin-1 range are |
| 1674 | shared when using this constructor */ |
| 1675 | if (size == 1 && *u < 256) |
| 1676 | return get_latin1_char((unsigned char)*u); |
| 1677 | |
| 1678 | /* If not empty and not single character, copy the Unicode data |
| 1679 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1680 | if (find_maxchar_surrogates(u, u + size, |
| 1681 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1682 | return NULL; |
| 1683 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1684 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1685 | if (!unicode) |
| 1686 | return NULL; |
| 1687 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1688 | switch (PyUnicode_KIND(unicode)) { |
| 1689 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1690 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1691 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1692 | break; |
| 1693 | case PyUnicode_2BYTE_KIND: |
| 1694 | #if Py_UNICODE_SIZE == 2 |
| 1695 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1696 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1697 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1698 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1699 | #endif |
| 1700 | break; |
| 1701 | case PyUnicode_4BYTE_KIND: |
| 1702 | #if SIZEOF_WCHAR_T == 2 |
| 1703 | /* This is the only case which has to process surrogates, thus |
| 1704 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1705 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | #else |
| 1707 | assert(num_surrogates == 0); |
| 1708 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1709 | #endif |
| 1710 | break; |
| 1711 | default: |
| 1712 | assert(0 && "Impossible state"); |
| 1713 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1715 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1718 | PyObject * |
| 1719 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1720 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1721 | if (size < 0) { |
| 1722 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1723 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1724 | return NULL; |
| 1725 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1726 | if (u != NULL) |
| 1727 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1728 | else |
| 1729 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1732 | PyObject * |
| 1733 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1734 | { |
| 1735 | size_t size = strlen(u); |
| 1736 | if (size > PY_SSIZE_T_MAX) { |
| 1737 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1738 | return NULL; |
| 1739 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1740 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1743 | PyObject * |
| 1744 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1745 | { |
| 1746 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1747 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1748 | strlen(id->string), |
| 1749 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1750 | if (!id->object) |
| 1751 | return NULL; |
| 1752 | PyUnicode_InternInPlace(&id->object); |
| 1753 | assert(!id->next); |
| 1754 | id->next = static_strings; |
| 1755 | static_strings = id; |
| 1756 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1757 | return id->object; |
| 1758 | } |
| 1759 | |
| 1760 | void |
| 1761 | _PyUnicode_ClearStaticStrings() |
| 1762 | { |
| 1763 | _Py_Identifier *i; |
| 1764 | for (i = static_strings; i; i = i->next) { |
| 1765 | Py_DECREF(i->object); |
| 1766 | i->object = NULL; |
| 1767 | i->next = NULL; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1771 | /* Internal function, don't check maximum character */ |
| 1772 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1773 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1774 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1775 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1776 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1777 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1778 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1779 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1780 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1781 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1782 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1783 | unicode = PyUnicode_New(size, 127); |
| 1784 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1785 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1786 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1787 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1788 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1791 | static Py_UCS4 |
| 1792 | kind_maxchar_limit(unsigned int kind) |
| 1793 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1794 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1795 | case PyUnicode_1BYTE_KIND: |
| 1796 | return 0x80; |
| 1797 | case PyUnicode_2BYTE_KIND: |
| 1798 | return 0x100; |
| 1799 | case PyUnicode_4BYTE_KIND: |
| 1800 | return 0x10000; |
| 1801 | default: |
| 1802 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1803 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1807 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1808 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1809 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1810 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1811 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1812 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1813 | if (size == 0) { |
| 1814 | Py_INCREF(unicode_empty); |
| 1815 | return unicode_empty; |
| 1816 | } |
| 1817 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1818 | if (size == 1) |
| 1819 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1820 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1821 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1822 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1823 | if (!res) |
| 1824 | return NULL; |
| 1825 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1826 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1827 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1830 | static PyObject* |
| 1831 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1832 | { |
| 1833 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1834 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1835 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1836 | if (size == 0) { |
| 1837 | Py_INCREF(unicode_empty); |
| 1838 | return unicode_empty; |
| 1839 | } |
| 1840 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1841 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1842 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1843 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1844 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1845 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1846 | if (!res) |
| 1847 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1848 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1849 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1850 | else { |
| 1851 | _PyUnicode_CONVERT_BYTES( |
| 1852 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1853 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1854 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1855 | return res; |
| 1856 | } |
| 1857 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1858 | static PyObject* |
| 1859 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1860 | { |
| 1861 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1862 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1863 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1864 | if (size == 0) { |
| 1865 | Py_INCREF(unicode_empty); |
| 1866 | return unicode_empty; |
| 1867 | } |
| 1868 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1869 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1870 | return get_latin1_char((unsigned char)u[0]); |
| 1871 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1872 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1873 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1874 | if (!res) |
| 1875 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1876 | if (max_char < 256) |
| 1877 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1878 | PyUnicode_1BYTE_DATA(res)); |
| 1879 | else if (max_char < 0x10000) |
| 1880 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1881 | PyUnicode_2BYTE_DATA(res)); |
| 1882 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1883 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1884 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1885 | return res; |
| 1886 | } |
| 1887 | |
| 1888 | PyObject* |
| 1889 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1890 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1891 | if (size < 0) { |
| 1892 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1893 | return NULL; |
| 1894 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1895 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1896 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1897 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1898 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1899 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1900 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1901 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1902 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1903 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1904 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1905 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1908 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1909 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1910 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1911 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1912 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1913 | { |
| 1914 | PyObject *unicode, *copy; |
| 1915 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1916 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1917 | unsigned int kind; |
| 1918 | |
| 1919 | assert(p_unicode != NULL); |
| 1920 | unicode = *p_unicode; |
| 1921 | assert(PyUnicode_IS_READY(unicode)); |
| 1922 | if (PyUnicode_IS_ASCII(unicode)) |
| 1923 | return; |
| 1924 | |
| 1925 | len = PyUnicode_GET_LENGTH(unicode); |
| 1926 | kind = PyUnicode_KIND(unicode); |
| 1927 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1928 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1929 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1930 | if (max_char >= 128) |
| 1931 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1932 | } |
| 1933 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1934 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1935 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1936 | if (max_char >= 256) |
| 1937 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1938 | } |
| 1939 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1940 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1941 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1942 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1943 | if (max_char >= 0x10000) |
| 1944 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1945 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1946 | copy = PyUnicode_New(len, max_char); |
| 1947 | copy_characters(copy, 0, unicode, 0, len); |
| 1948 | Py_DECREF(unicode); |
| 1949 | *p_unicode = copy; |
| 1950 | } |
| 1951 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1952 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 1953 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1954 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1955 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1956 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1957 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1958 | if (!PyUnicode_Check(unicode)) { |
| 1959 | PyErr_BadInternalCall(); |
| 1960 | return NULL; |
| 1961 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1962 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1963 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1964 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1965 | length = PyUnicode_GET_LENGTH(unicode); |
| 1966 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1967 | if (!copy) |
| 1968 | return NULL; |
| 1969 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1970 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1971 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 1972 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1973 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1974 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1978 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1979 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1980 | |
| 1981 | void* |
| 1982 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1983 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1984 | Py_ssize_t len; |
| 1985 | void *result; |
| 1986 | unsigned int skind; |
| 1987 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1988 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1989 | return NULL; |
| 1990 | |
| 1991 | len = PyUnicode_GET_LENGTH(s); |
| 1992 | skind = PyUnicode_KIND(s); |
| 1993 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1994 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | return NULL; |
| 1996 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1997 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1998 | case PyUnicode_2BYTE_KIND: |
| 1999 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2000 | if (!result) |
| 2001 | return PyErr_NoMemory(); |
| 2002 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2003 | _PyUnicode_CONVERT_BYTES( |
| 2004 | Py_UCS1, Py_UCS2, |
| 2005 | PyUnicode_1BYTE_DATA(s), |
| 2006 | PyUnicode_1BYTE_DATA(s) + len, |
| 2007 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2008 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2009 | case PyUnicode_4BYTE_KIND: |
| 2010 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2011 | if (!result) |
| 2012 | return PyErr_NoMemory(); |
| 2013 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2014 | _PyUnicode_CONVERT_BYTES( |
| 2015 | Py_UCS2, Py_UCS4, |
| 2016 | PyUnicode_2BYTE_DATA(s), |
| 2017 | PyUnicode_2BYTE_DATA(s) + len, |
| 2018 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2019 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2020 | else { |
| 2021 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2022 | _PyUnicode_CONVERT_BYTES( |
| 2023 | Py_UCS1, Py_UCS4, |
| 2024 | PyUnicode_1BYTE_DATA(s), |
| 2025 | PyUnicode_1BYTE_DATA(s) + len, |
| 2026 | result); |
| 2027 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2028 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2029 | default: |
| 2030 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2031 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2032 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2033 | return NULL; |
| 2034 | } |
| 2035 | |
| 2036 | static Py_UCS4* |
| 2037 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2038 | int copy_null) |
| 2039 | { |
| 2040 | int kind; |
| 2041 | void *data; |
| 2042 | Py_ssize_t len, targetlen; |
| 2043 | if (PyUnicode_READY(string) == -1) |
| 2044 | return NULL; |
| 2045 | kind = PyUnicode_KIND(string); |
| 2046 | data = PyUnicode_DATA(string); |
| 2047 | len = PyUnicode_GET_LENGTH(string); |
| 2048 | targetlen = len; |
| 2049 | if (copy_null) |
| 2050 | targetlen++; |
| 2051 | if (!target) { |
| 2052 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2053 | PyErr_NoMemory(); |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2057 | if (!target) { |
| 2058 | PyErr_NoMemory(); |
| 2059 | return NULL; |
| 2060 | } |
| 2061 | } |
| 2062 | else { |
| 2063 | if (targetsize < targetlen) { |
| 2064 | PyErr_Format(PyExc_SystemError, |
| 2065 | "string is longer than the buffer"); |
| 2066 | if (copy_null && 0 < targetsize) |
| 2067 | target[0] = 0; |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2071 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2072 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2073 | _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] | 2074 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2075 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2076 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2077 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2078 | } |
| 2079 | else { |
| 2080 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2081 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2082 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2083 | if (copy_null) |
| 2084 | target[len] = 0; |
| 2085 | return target; |
| 2086 | } |
| 2087 | |
| 2088 | Py_UCS4* |
| 2089 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2090 | int copy_null) |
| 2091 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2092 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2093 | PyErr_BadInternalCall(); |
| 2094 | return NULL; |
| 2095 | } |
| 2096 | return as_ucs4(string, target, targetsize, copy_null); |
| 2097 | } |
| 2098 | |
| 2099 | Py_UCS4* |
| 2100 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2101 | { |
| 2102 | return as_ucs4(string, NULL, 0, 1); |
| 2103 | } |
| 2104 | |
| 2105 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2106 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2107 | PyObject * |
| 2108 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2109 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2110 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2111 | if (size == 0) { |
| 2112 | Py_INCREF(unicode_empty); |
| 2113 | return unicode_empty; |
| 2114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2115 | PyErr_BadInternalCall(); |
| 2116 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2119 | if (size == -1) { |
| 2120 | size = wcslen(w); |
| 2121 | } |
| 2122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2123 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2126 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2127 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2128 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2129 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2130 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2131 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2132 | *fmt++ = '%'; |
| 2133 | if (width) { |
| 2134 | if (zeropad) |
| 2135 | *fmt++ = '0'; |
| 2136 | fmt += sprintf(fmt, "%d", width); |
| 2137 | } |
| 2138 | if (precision) |
| 2139 | fmt += sprintf(fmt, ".%d", precision); |
| 2140 | if (longflag) |
| 2141 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2142 | else if (longlongflag) { |
| 2143 | /* longlongflag should only ever be nonzero on machines with |
| 2144 | HAVE_LONG_LONG defined */ |
| 2145 | #ifdef HAVE_LONG_LONG |
| 2146 | char *f = PY_FORMAT_LONG_LONG; |
| 2147 | while (*f) |
| 2148 | *fmt++ = *f++; |
| 2149 | #else |
| 2150 | /* we shouldn't ever get here */ |
| 2151 | assert(0); |
| 2152 | *fmt++ = 'l'; |
| 2153 | #endif |
| 2154 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2155 | else if (size_tflag) { |
| 2156 | char *f = PY_FORMAT_SIZE_T; |
| 2157 | while (*f) |
| 2158 | *fmt++ = *f++; |
| 2159 | } |
| 2160 | *fmt++ = c; |
| 2161 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2164 | /* helper for PyUnicode_FromFormatV() */ |
| 2165 | |
| 2166 | static const char* |
| 2167 | parse_format_flags(const char *f, |
| 2168 | int *p_width, int *p_precision, |
| 2169 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2170 | { |
| 2171 | int width, precision, longflag, longlongflag, size_tflag; |
| 2172 | |
| 2173 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2174 | f++; |
| 2175 | width = 0; |
| 2176 | while (Py_ISDIGIT((unsigned)*f)) |
| 2177 | width = (width*10) + *f++ - '0'; |
| 2178 | precision = 0; |
| 2179 | if (*f == '.') { |
| 2180 | f++; |
| 2181 | while (Py_ISDIGIT((unsigned)*f)) |
| 2182 | precision = (precision*10) + *f++ - '0'; |
| 2183 | if (*f == '%') { |
| 2184 | /* "%.3%s" => f points to "3" */ |
| 2185 | f--; |
| 2186 | } |
| 2187 | } |
| 2188 | if (*f == '\0') { |
| 2189 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2190 | f--; |
| 2191 | } |
| 2192 | if (p_width != NULL) |
| 2193 | *p_width = width; |
| 2194 | if (p_precision != NULL) |
| 2195 | *p_precision = precision; |
| 2196 | |
| 2197 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2198 | longflag = 0; |
| 2199 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2200 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2203 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2204 | longflag = 1; |
| 2205 | ++f; |
| 2206 | } |
| 2207 | #ifdef HAVE_LONG_LONG |
| 2208 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2209 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2210 | longlongflag = 1; |
| 2211 | f += 2; |
| 2212 | } |
| 2213 | #endif |
| 2214 | } |
| 2215 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2216 | 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] | 2217 | size_tflag = 1; |
| 2218 | ++f; |
| 2219 | } |
| 2220 | if (p_longflag != NULL) |
| 2221 | *p_longflag = longflag; |
| 2222 | if (p_longlongflag != NULL) |
| 2223 | *p_longlongflag = longlongflag; |
| 2224 | if (p_size_tflag != NULL) |
| 2225 | *p_size_tflag = size_tflag; |
| 2226 | return f; |
| 2227 | } |
| 2228 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2229 | /* maximum number of characters required for output of %ld. 21 characters |
| 2230 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2231 | #define MAX_LONG_CHARS 21 |
| 2232 | /* maximum number of characters required for output of %lld. |
| 2233 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2234 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2235 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2236 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2237 | PyObject * |
| 2238 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2239 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2240 | va_list count; |
| 2241 | Py_ssize_t callcount = 0; |
| 2242 | PyObject **callresults = NULL; |
| 2243 | PyObject **callresult = NULL; |
| 2244 | Py_ssize_t n = 0; |
| 2245 | int width = 0; |
| 2246 | int precision = 0; |
| 2247 | int zeropad; |
| 2248 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2249 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2250 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2251 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2252 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2253 | Py_UCS4 argmaxchar; |
| 2254 | Py_ssize_t numbersize = 0; |
| 2255 | char *numberresults = NULL; |
| 2256 | char *numberresult = NULL; |
| 2257 | Py_ssize_t i; |
| 2258 | int kind; |
| 2259 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2260 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2261 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2262 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2263 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2264 | * 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] | 2265 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2266 | * also estimate a upper bound for all the number formats in the string, |
| 2267 | * 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] | 2268 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2269 | for (f = format; *f; f++) { |
| 2270 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2271 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2272 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2273 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2274 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2275 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2277 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2278 | #ifdef HAVE_LONG_LONG |
| 2279 | if (longlongflag) { |
| 2280 | if (width < MAX_LONG_LONG_CHARS) |
| 2281 | width = MAX_LONG_LONG_CHARS; |
| 2282 | } |
| 2283 | else |
| 2284 | #endif |
| 2285 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2286 | including sign. Decimal takes the most space. This |
| 2287 | isn't enough for octal. If a width is specified we |
| 2288 | need more (which we allocate later). */ |
| 2289 | if (width < MAX_LONG_CHARS) |
| 2290 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2291 | |
| 2292 | /* account for the size + '\0' to separate numbers |
| 2293 | inside of the numberresults buffer */ |
| 2294 | numbersize += (width + 1); |
| 2295 | } |
| 2296 | } |
| 2297 | else if ((unsigned char)*f > 127) { |
| 2298 | PyErr_Format(PyExc_ValueError, |
| 2299 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2300 | "string, got a non-ASCII byte: 0x%02x", |
| 2301 | (unsigned char)*f); |
| 2302 | return NULL; |
| 2303 | } |
| 2304 | } |
| 2305 | /* step 2: allocate memory for the results of |
| 2306 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2307 | if (callcount) { |
| 2308 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2309 | if (!callresults) { |
| 2310 | PyErr_NoMemory(); |
| 2311 | return NULL; |
| 2312 | } |
| 2313 | callresult = callresults; |
| 2314 | } |
| 2315 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2316 | if (numbersize) { |
| 2317 | numberresults = PyObject_Malloc(numbersize); |
| 2318 | if (!numberresults) { |
| 2319 | PyErr_NoMemory(); |
| 2320 | goto fail; |
| 2321 | } |
| 2322 | numberresult = numberresults; |
| 2323 | } |
| 2324 | |
| 2325 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2326 | for (f = format; *f; f++) { |
| 2327 | if (*f == '%') { |
| 2328 | const char* p; |
| 2329 | int longflag; |
| 2330 | int longlongflag; |
| 2331 | int size_tflag; |
| 2332 | int numprinted; |
| 2333 | |
| 2334 | p = f; |
| 2335 | zeropad = (f[1] == '0'); |
| 2336 | f = parse_format_flags(f, &width, &precision, |
| 2337 | &longflag, &longlongflag, &size_tflag); |
| 2338 | switch (*f) { |
| 2339 | case 'c': |
| 2340 | { |
| 2341 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2342 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2343 | n++; |
| 2344 | break; |
| 2345 | } |
| 2346 | case '%': |
| 2347 | n++; |
| 2348 | break; |
| 2349 | case 'i': |
| 2350 | case 'd': |
| 2351 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2352 | width, precision, *f); |
| 2353 | if (longflag) |
| 2354 | numprinted = sprintf(numberresult, fmt, |
| 2355 | va_arg(count, long)); |
| 2356 | #ifdef HAVE_LONG_LONG |
| 2357 | else if (longlongflag) |
| 2358 | numprinted = sprintf(numberresult, fmt, |
| 2359 | va_arg(count, PY_LONG_LONG)); |
| 2360 | #endif |
| 2361 | else if (size_tflag) |
| 2362 | numprinted = sprintf(numberresult, fmt, |
| 2363 | va_arg(count, Py_ssize_t)); |
| 2364 | else |
| 2365 | numprinted = sprintf(numberresult, fmt, |
| 2366 | va_arg(count, int)); |
| 2367 | n += numprinted; |
| 2368 | /* advance by +1 to skip over the '\0' */ |
| 2369 | numberresult += (numprinted + 1); |
| 2370 | assert(*(numberresult - 1) == '\0'); |
| 2371 | assert(*(numberresult - 2) != '\0'); |
| 2372 | assert(numprinted >= 0); |
| 2373 | assert(numberresult <= numberresults + numbersize); |
| 2374 | break; |
| 2375 | case 'u': |
| 2376 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2377 | width, precision, 'u'); |
| 2378 | if (longflag) |
| 2379 | numprinted = sprintf(numberresult, fmt, |
| 2380 | va_arg(count, unsigned long)); |
| 2381 | #ifdef HAVE_LONG_LONG |
| 2382 | else if (longlongflag) |
| 2383 | numprinted = sprintf(numberresult, fmt, |
| 2384 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2385 | #endif |
| 2386 | else if (size_tflag) |
| 2387 | numprinted = sprintf(numberresult, fmt, |
| 2388 | va_arg(count, size_t)); |
| 2389 | else |
| 2390 | numprinted = sprintf(numberresult, fmt, |
| 2391 | va_arg(count, unsigned int)); |
| 2392 | n += numprinted; |
| 2393 | numberresult += (numprinted + 1); |
| 2394 | assert(*(numberresult - 1) == '\0'); |
| 2395 | assert(*(numberresult - 2) != '\0'); |
| 2396 | assert(numprinted >= 0); |
| 2397 | assert(numberresult <= numberresults + numbersize); |
| 2398 | break; |
| 2399 | case 'x': |
| 2400 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2401 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2402 | n += numprinted; |
| 2403 | numberresult += (numprinted + 1); |
| 2404 | assert(*(numberresult - 1) == '\0'); |
| 2405 | assert(*(numberresult - 2) != '\0'); |
| 2406 | assert(numprinted >= 0); |
| 2407 | assert(numberresult <= numberresults + numbersize); |
| 2408 | break; |
| 2409 | case 'p': |
| 2410 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2411 | /* %p is ill-defined: ensure leading 0x. */ |
| 2412 | if (numberresult[1] == 'X') |
| 2413 | numberresult[1] = 'x'; |
| 2414 | else if (numberresult[1] != 'x') { |
| 2415 | memmove(numberresult + 2, numberresult, |
| 2416 | strlen(numberresult) + 1); |
| 2417 | numberresult[0] = '0'; |
| 2418 | numberresult[1] = 'x'; |
| 2419 | numprinted += 2; |
| 2420 | } |
| 2421 | n += numprinted; |
| 2422 | numberresult += (numprinted + 1); |
| 2423 | assert(*(numberresult - 1) == '\0'); |
| 2424 | assert(*(numberresult - 2) != '\0'); |
| 2425 | assert(numprinted >= 0); |
| 2426 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2427 | break; |
| 2428 | case 's': |
| 2429 | { |
| 2430 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2431 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2432 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2433 | if (!str) |
| 2434 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2435 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2436 | unicode objects, there is no need to call ready on them */ |
| 2437 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2438 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2439 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2440 | /* Remember the str and switch to the next slot */ |
| 2441 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2442 | break; |
| 2443 | } |
| 2444 | case 'U': |
| 2445 | { |
| 2446 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2447 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2448 | if (PyUnicode_READY(obj) == -1) |
| 2449 | goto fail; |
| 2450 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2451 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2452 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2453 | break; |
| 2454 | } |
| 2455 | case 'V': |
| 2456 | { |
| 2457 | PyObject *obj = va_arg(count, PyObject *); |
| 2458 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2459 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2460 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2461 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2462 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2463 | if (PyUnicode_READY(obj) == -1) |
| 2464 | goto fail; |
| 2465 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2466 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2467 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2468 | *callresult++ = NULL; |
| 2469 | } |
| 2470 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2471 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2472 | if (!str_obj) |
| 2473 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2474 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2475 | Py_DECREF(str_obj); |
| 2476 | goto fail; |
| 2477 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2478 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2479 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2480 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2481 | *callresult++ = str_obj; |
| 2482 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2483 | break; |
| 2484 | } |
| 2485 | case 'S': |
| 2486 | { |
| 2487 | PyObject *obj = va_arg(count, PyObject *); |
| 2488 | PyObject *str; |
| 2489 | assert(obj); |
| 2490 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2491 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2492 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2493 | if (PyUnicode_READY(str) == -1) { |
| 2494 | Py_DECREF(str); |
| 2495 | goto fail; |
| 2496 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2497 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
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(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2500 | /* Remember the str and switch to the next slot */ |
| 2501 | *callresult++ = str; |
| 2502 | break; |
| 2503 | } |
| 2504 | case 'R': |
| 2505 | { |
| 2506 | PyObject *obj = va_arg(count, PyObject *); |
| 2507 | PyObject *repr; |
| 2508 | assert(obj); |
| 2509 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2510 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2511 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2512 | if (PyUnicode_READY(repr) == -1) { |
| 2513 | Py_DECREF(repr); |
| 2514 | goto fail; |
| 2515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2516 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2517 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2518 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2519 | /* Remember the repr and switch to the next slot */ |
| 2520 | *callresult++ = repr; |
| 2521 | break; |
| 2522 | } |
| 2523 | case 'A': |
| 2524 | { |
| 2525 | PyObject *obj = va_arg(count, PyObject *); |
| 2526 | PyObject *ascii; |
| 2527 | assert(obj); |
| 2528 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2529 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2530 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2531 | if (PyUnicode_READY(ascii) == -1) { |
| 2532 | Py_DECREF(ascii); |
| 2533 | goto fail; |
| 2534 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2535 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2536 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2537 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2538 | /* Remember the repr and switch to the next slot */ |
| 2539 | *callresult++ = ascii; |
| 2540 | break; |
| 2541 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2542 | default: |
| 2543 | /* if we stumble upon an unknown |
| 2544 | formatting code, copy the rest of |
| 2545 | the format string to the output |
| 2546 | string. (we cannot just skip the |
| 2547 | code, since there's no way to know |
| 2548 | what's in the argument list) */ |
| 2549 | n += strlen(p); |
| 2550 | goto expand; |
| 2551 | } |
| 2552 | } else |
| 2553 | n++; |
| 2554 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2555 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2556 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2557 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2558 | we don't have to resize the string. |
| 2559 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2560 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2561 | if (!string) |
| 2562 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2563 | kind = PyUnicode_KIND(string); |
| 2564 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2566 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2567 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2568 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2569 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2570 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2571 | |
| 2572 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2573 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2574 | /* checking for == because the last argument could be a empty |
| 2575 | string, which causes i to point to end, the assert at the end of |
| 2576 | the loop */ |
| 2577 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2578 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2579 | switch (*f) { |
| 2580 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2581 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2582 | const int ordinal = va_arg(vargs, int); |
| 2583 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2584 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2585 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2586 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2587 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2588 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2589 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | case 'p': |
| 2591 | /* unused, since we already have the result */ |
| 2592 | if (*f == 'p') |
| 2593 | (void) va_arg(vargs, void *); |
| 2594 | else |
| 2595 | (void) va_arg(vargs, int); |
| 2596 | /* extract the result from numberresults and append. */ |
| 2597 | for (; *numberresult; ++i, ++numberresult) |
| 2598 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2599 | /* skip over the separating '\0' */ |
| 2600 | assert(*numberresult == '\0'); |
| 2601 | numberresult++; |
| 2602 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2603 | break; |
| 2604 | case 's': |
| 2605 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2606 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2607 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2608 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2609 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2610 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2611 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2612 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2613 | /* We're done with the unicode()/repr() => forget it */ |
| 2614 | Py_DECREF(*callresult); |
| 2615 | /* switch to next unicode()/repr() result */ |
| 2616 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | } |
| 2619 | case 'U': |
| 2620 | { |
| 2621 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2622 | Py_ssize_t size; |
| 2623 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2624 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2625 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2626 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2627 | break; |
| 2628 | } |
| 2629 | case 'V': |
| 2630 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2631 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2632 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2633 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2634 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | size = PyUnicode_GET_LENGTH(obj); |
| 2636 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2637 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2639 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2640 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2641 | assert(PyUnicode_KIND(*callresult) <= |
| 2642 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2643 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2644 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2645 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2646 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2647 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2648 | break; |
| 2649 | } |
| 2650 | case 'S': |
| 2651 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2652 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2653 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2654 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2655 | /* unused, since we already have the result */ |
| 2656 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2658 | copy_characters(string, i, *callresult, 0, size); |
| 2659 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2660 | /* We're done with the unicode()/repr() => forget it */ |
| 2661 | Py_DECREF(*callresult); |
| 2662 | /* switch to next unicode()/repr() result */ |
| 2663 | ++callresult; |
| 2664 | break; |
| 2665 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2666 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2667 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2670 | for (; *p; ++p, ++i) |
| 2671 | PyUnicode_WRITE(kind, data, i, *p); |
| 2672 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2673 | goto end; |
| 2674 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2676 | else { |
| 2677 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2678 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2679 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2680 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2681 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2683 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2684 | if (callresults) |
| 2685 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2686 | if (numberresults) |
| 2687 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2688 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2689 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2690 | if (callresults) { |
| 2691 | PyObject **callresult2 = callresults; |
| 2692 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2693 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | ++callresult2; |
| 2695 | } |
| 2696 | PyObject_Free(callresults); |
| 2697 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2698 | if (numberresults) |
| 2699 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2700 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2703 | PyObject * |
| 2704 | PyUnicode_FromFormat(const char *format, ...) |
| 2705 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2706 | PyObject* ret; |
| 2707 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2708 | |
| 2709 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2710 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2711 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2712 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2713 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2714 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2715 | va_end(vargs); |
| 2716 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2719 | #ifdef HAVE_WCHAR_H |
| 2720 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2721 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2722 | convert a Unicode object to a wide character string. |
| 2723 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2724 | - 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] | 2725 | character) required to convert the unicode object. Ignore size argument. |
| 2726 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2727 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2728 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2729 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2730 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2731 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2732 | wchar_t *w, |
| 2733 | Py_ssize_t size) |
| 2734 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2735 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2736 | const wchar_t *wstr; |
| 2737 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2738 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2739 | if (wstr == NULL) |
| 2740 | return -1; |
| 2741 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2742 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2743 | if (size > res) |
| 2744 | size = res + 1; |
| 2745 | else |
| 2746 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2747 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2748 | return res; |
| 2749 | } |
| 2750 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2751 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2755 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2756 | wchar_t *w, |
| 2757 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2758 | { |
| 2759 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2760 | PyErr_BadInternalCall(); |
| 2761 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2763 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2766 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2767 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2768 | Py_ssize_t *size) |
| 2769 | { |
| 2770 | wchar_t* buffer; |
| 2771 | Py_ssize_t buflen; |
| 2772 | |
| 2773 | if (unicode == NULL) { |
| 2774 | PyErr_BadInternalCall(); |
| 2775 | return NULL; |
| 2776 | } |
| 2777 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2778 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2779 | if (buflen == -1) |
| 2780 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2781 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2782 | PyErr_NoMemory(); |
| 2783 | return NULL; |
| 2784 | } |
| 2785 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2786 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2787 | if (buffer == NULL) { |
| 2788 | PyErr_NoMemory(); |
| 2789 | return NULL; |
| 2790 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2791 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2792 | if (buflen == -1) |
| 2793 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2794 | if (size != NULL) |
| 2795 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2796 | return buffer; |
| 2797 | } |
| 2798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2799 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2800 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2801 | PyObject * |
| 2802 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2803 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2804 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2805 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2806 | PyErr_SetString(PyExc_ValueError, |
| 2807 | "chr() arg not in range(0x110000)"); |
| 2808 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2809 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2811 | if (ordinal < 256) |
| 2812 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2813 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2814 | v = PyUnicode_New(1, ordinal); |
| 2815 | if (v == NULL) |
| 2816 | return NULL; |
| 2817 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2818 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2819 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2822 | PyObject * |
| 2823 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2824 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2825 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2826 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2827 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2828 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2829 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2830 | Py_INCREF(obj); |
| 2831 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2832 | } |
| 2833 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2834 | /* For a Unicode subtype that's not a Unicode object, |
| 2835 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2836 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2837 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2838 | PyErr_Format(PyExc_TypeError, |
| 2839 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2840 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2841 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2844 | PyObject * |
| 2845 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2846 | const char *encoding, |
| 2847 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2848 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2849 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2850 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2852 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2853 | PyErr_BadInternalCall(); |
| 2854 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2855 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2857 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2858 | if (PyBytes_Check(obj)) { |
| 2859 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2860 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2861 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2862 | } |
| 2863 | else { |
| 2864 | v = PyUnicode_Decode( |
| 2865 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2866 | encoding, errors); |
| 2867 | } |
| 2868 | return v; |
| 2869 | } |
| 2870 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2871 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2872 | PyErr_SetString(PyExc_TypeError, |
| 2873 | "decoding str is not supported"); |
| 2874 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2875 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2876 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2877 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2878 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2879 | PyErr_Format(PyExc_TypeError, |
| 2880 | "coercing to str: need bytes, bytearray " |
| 2881 | "or buffer-like object, %.80s found", |
| 2882 | Py_TYPE(obj)->tp_name); |
| 2883 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2884 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2886 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2887 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2888 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2889 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2890 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2891 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2893 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2894 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2897 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2898 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2899 | 1 on success. */ |
| 2900 | static int |
| 2901 | normalize_encoding(const char *encoding, |
| 2902 | char *lower, |
| 2903 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2905 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2906 | char *l; |
| 2907 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2908 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2909 | if (encoding == NULL) { |
| 2910 | strcpy(lower, "utf-8"); |
| 2911 | return 1; |
| 2912 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2913 | e = encoding; |
| 2914 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2915 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2916 | while (*e) { |
| 2917 | if (l == l_end) |
| 2918 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2919 | if (Py_ISUPPER(*e)) { |
| 2920 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2921 | } |
| 2922 | else if (*e == '_') { |
| 2923 | *l++ = '-'; |
| 2924 | e++; |
| 2925 | } |
| 2926 | else { |
| 2927 | *l++ = *e++; |
| 2928 | } |
| 2929 | } |
| 2930 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2931 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2934 | PyObject * |
| 2935 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2936 | Py_ssize_t size, |
| 2937 | const char *encoding, |
| 2938 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2939 | { |
| 2940 | PyObject *buffer = NULL, *unicode; |
| 2941 | Py_buffer info; |
| 2942 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2943 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2944 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2945 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2946 | if ((strcmp(lower, "utf-8") == 0) || |
| 2947 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2948 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2949 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2950 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2951 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2952 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2953 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2954 | else if (strcmp(lower, "mbcs") == 0) |
| 2955 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2956 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2957 | else if (strcmp(lower, "ascii") == 0) |
| 2958 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2959 | else if (strcmp(lower, "utf-16") == 0) |
| 2960 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2961 | else if (strcmp(lower, "utf-32") == 0) |
| 2962 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2963 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2964 | |
| 2965 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2966 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2967 | 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] | 2968 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2969 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2970 | if (buffer == NULL) |
| 2971 | goto onError; |
| 2972 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2973 | if (unicode == NULL) |
| 2974 | goto onError; |
| 2975 | if (!PyUnicode_Check(unicode)) { |
| 2976 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2977 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2978 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2979 | Py_DECREF(unicode); |
| 2980 | goto onError; |
| 2981 | } |
| 2982 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2983 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2985 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2986 | Py_XDECREF(buffer); |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2990 | PyObject * |
| 2991 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2992 | const char *encoding, |
| 2993 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2994 | { |
| 2995 | PyObject *v; |
| 2996 | |
| 2997 | if (!PyUnicode_Check(unicode)) { |
| 2998 | PyErr_BadArgument(); |
| 2999 | goto onError; |
| 3000 | } |
| 3001 | |
| 3002 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3003 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3004 | |
| 3005 | /* Decode via the codec registry */ |
| 3006 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3007 | if (v == NULL) |
| 3008 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3009 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3011 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3012 | return NULL; |
| 3013 | } |
| 3014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3015 | PyObject * |
| 3016 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3017 | const char *encoding, |
| 3018 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3019 | { |
| 3020 | PyObject *v; |
| 3021 | |
| 3022 | if (!PyUnicode_Check(unicode)) { |
| 3023 | PyErr_BadArgument(); |
| 3024 | goto onError; |
| 3025 | } |
| 3026 | |
| 3027 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3028 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3029 | |
| 3030 | /* Decode via the codec registry */ |
| 3031 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3032 | if (v == NULL) |
| 3033 | goto onError; |
| 3034 | if (!PyUnicode_Check(v)) { |
| 3035 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3036 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3037 | Py_TYPE(v)->tp_name); |
| 3038 | Py_DECREF(v); |
| 3039 | goto onError; |
| 3040 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3041 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3042 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3043 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3044 | return NULL; |
| 3045 | } |
| 3046 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3047 | PyObject * |
| 3048 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3049 | Py_ssize_t size, |
| 3050 | const char *encoding, |
| 3051 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 | { |
| 3053 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3054 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | unicode = PyUnicode_FromUnicode(s, size); |
| 3056 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3057 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3058 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3059 | Py_DECREF(unicode); |
| 3060 | return v; |
| 3061 | } |
| 3062 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3063 | PyObject * |
| 3064 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3065 | const char *encoding, |
| 3066 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3067 | { |
| 3068 | PyObject *v; |
| 3069 | |
| 3070 | if (!PyUnicode_Check(unicode)) { |
| 3071 | PyErr_BadArgument(); |
| 3072 | goto onError; |
| 3073 | } |
| 3074 | |
| 3075 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3076 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3077 | |
| 3078 | /* Encode via the codec registry */ |
| 3079 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3080 | if (v == NULL) |
| 3081 | goto onError; |
| 3082 | return v; |
| 3083 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3084 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3085 | return NULL; |
| 3086 | } |
| 3087 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3088 | static size_t |
| 3089 | wcstombs_errorpos(const wchar_t *wstr) |
| 3090 | { |
| 3091 | size_t len; |
| 3092 | #if SIZEOF_WCHAR_T == 2 |
| 3093 | wchar_t buf[3]; |
| 3094 | #else |
| 3095 | wchar_t buf[2]; |
| 3096 | #endif |
| 3097 | char outbuf[MB_LEN_MAX]; |
| 3098 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3099 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3100 | #if SIZEOF_WCHAR_T == 2 |
| 3101 | buf[2] = 0; |
| 3102 | #else |
| 3103 | buf[1] = 0; |
| 3104 | #endif |
| 3105 | start = wstr; |
| 3106 | while (*wstr != L'\0') |
| 3107 | { |
| 3108 | previous = wstr; |
| 3109 | #if SIZEOF_WCHAR_T == 2 |
| 3110 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3111 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3112 | { |
| 3113 | buf[0] = wstr[0]; |
| 3114 | buf[1] = wstr[1]; |
| 3115 | wstr += 2; |
| 3116 | } |
| 3117 | else { |
| 3118 | buf[0] = *wstr; |
| 3119 | buf[1] = 0; |
| 3120 | wstr++; |
| 3121 | } |
| 3122 | #else |
| 3123 | buf[0] = *wstr; |
| 3124 | wstr++; |
| 3125 | #endif |
| 3126 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3127 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3128 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3132 | return 0; |
| 3133 | } |
| 3134 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3135 | static int |
| 3136 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3137 | { |
| 3138 | if (errors == NULL) { |
| 3139 | *surrogateescape = 0; |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
| 3143 | if (strcmp(errors, "strict") == 0) { |
| 3144 | *surrogateescape = 0; |
| 3145 | return 0; |
| 3146 | } |
| 3147 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3148 | *surrogateescape = 1; |
| 3149 | return 0; |
| 3150 | } |
| 3151 | PyErr_Format(PyExc_ValueError, |
| 3152 | "only 'strict' and 'surrogateescape' error handlers " |
| 3153 | "are supported, not '%s'", |
| 3154 | errors); |
| 3155 | return -1; |
| 3156 | } |
| 3157 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3158 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3159 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3160 | { |
| 3161 | Py_ssize_t wlen, wlen2; |
| 3162 | wchar_t *wstr; |
| 3163 | PyObject *bytes = NULL; |
| 3164 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3165 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3166 | PyObject *exc; |
| 3167 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3168 | int surrogateescape; |
| 3169 | |
| 3170 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3171 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3172 | |
| 3173 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3174 | if (wstr == NULL) |
| 3175 | return NULL; |
| 3176 | |
| 3177 | wlen2 = wcslen(wstr); |
| 3178 | if (wlen2 != wlen) { |
| 3179 | PyMem_Free(wstr); |
| 3180 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3181 | return NULL; |
| 3182 | } |
| 3183 | |
| 3184 | if (surrogateescape) { |
| 3185 | /* locale encoding with surrogateescape */ |
| 3186 | char *str; |
| 3187 | |
| 3188 | str = _Py_wchar2char(wstr, &error_pos); |
| 3189 | if (str == NULL) { |
| 3190 | if (error_pos == (size_t)-1) { |
| 3191 | PyErr_NoMemory(); |
| 3192 | PyMem_Free(wstr); |
| 3193 | return NULL; |
| 3194 | } |
| 3195 | else { |
| 3196 | goto encode_error; |
| 3197 | } |
| 3198 | } |
| 3199 | PyMem_Free(wstr); |
| 3200 | |
| 3201 | bytes = PyBytes_FromString(str); |
| 3202 | PyMem_Free(str); |
| 3203 | } |
| 3204 | else { |
| 3205 | size_t len, len2; |
| 3206 | |
| 3207 | len = wcstombs(NULL, wstr, 0); |
| 3208 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3209 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3210 | goto encode_error; |
| 3211 | } |
| 3212 | |
| 3213 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3214 | if (bytes == NULL) { |
| 3215 | PyMem_Free(wstr); |
| 3216 | return NULL; |
| 3217 | } |
| 3218 | |
| 3219 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3220 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3221 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3222 | goto encode_error; |
| 3223 | } |
| 3224 | PyMem_Free(wstr); |
| 3225 | } |
| 3226 | return bytes; |
| 3227 | |
| 3228 | encode_error: |
| 3229 | errmsg = strerror(errno); |
| 3230 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3231 | |
| 3232 | if (error_pos == (size_t)-1) |
| 3233 | error_pos = wcstombs_errorpos(wstr); |
| 3234 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3235 | PyMem_Free(wstr); |
| 3236 | Py_XDECREF(bytes); |
| 3237 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3238 | if (errmsg != NULL) { |
| 3239 | size_t errlen; |
| 3240 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3241 | if (wstr != NULL) { |
| 3242 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3243 | PyMem_Free(wstr); |
| 3244 | } else |
| 3245 | errmsg = NULL; |
| 3246 | } |
| 3247 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3248 | reason = PyUnicode_FromString( |
| 3249 | "wcstombs() encountered an unencodable " |
| 3250 | "wide character"); |
| 3251 | if (reason == NULL) |
| 3252 | return NULL; |
| 3253 | |
| 3254 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3255 | "locale", unicode, |
| 3256 | (Py_ssize_t)error_pos, |
| 3257 | (Py_ssize_t)(error_pos+1), |
| 3258 | reason); |
| 3259 | Py_DECREF(reason); |
| 3260 | if (exc != NULL) { |
| 3261 | PyCodec_StrictErrors(exc); |
| 3262 | Py_XDECREF(exc); |
| 3263 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3264 | return NULL; |
| 3265 | } |
| 3266 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3267 | PyObject * |
| 3268 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3269 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3270 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3271 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3272 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3273 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3274 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3275 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3276 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3277 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3278 | the Python codec requires to encode at least its own filename. Use the C |
| 3279 | version of the locale codec until the codec registry is initialized and |
| 3280 | the Python codec is loaded. |
| 3281 | |
| 3282 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3283 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3284 | subinterpreters. */ |
| 3285 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3286 | return PyUnicode_AsEncodedString(unicode, |
| 3287 | Py_FileSystemDefaultEncoding, |
| 3288 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3289 | } |
| 3290 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3291 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3292 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3293 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3296 | PyObject * |
| 3297 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3298 | const char *encoding, |
| 3299 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | { |
| 3301 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3302 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3304 | if (!PyUnicode_Check(unicode)) { |
| 3305 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3306 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3307 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3308 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3309 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3310 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3311 | if ((strcmp(lower, "utf-8") == 0) || |
| 3312 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3313 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3314 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3315 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3316 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3317 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3318 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3319 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3320 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3321 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3322 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3323 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3324 | else if (strcmp(lower, "mbcs") == 0) |
| 3325 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3326 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3327 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3328 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3329 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | |
| 3331 | /* Encode via the codec registry */ |
| 3332 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3333 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3334 | return NULL; |
| 3335 | |
| 3336 | /* The normal path */ |
| 3337 | if (PyBytes_Check(v)) |
| 3338 | return v; |
| 3339 | |
| 3340 | /* 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] | 3341 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3342 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3343 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3344 | |
| 3345 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3346 | "encoder %s returned bytearray instead of bytes", |
| 3347 | encoding); |
| 3348 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3349 | Py_DECREF(v); |
| 3350 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3351 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3352 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3353 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3354 | Py_DECREF(v); |
| 3355 | return b; |
| 3356 | } |
| 3357 | |
| 3358 | PyErr_Format(PyExc_TypeError, |
| 3359 | "encoder did not return a bytes object (type=%.400s)", |
| 3360 | Py_TYPE(v)->tp_name); |
| 3361 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3362 | return NULL; |
| 3363 | } |
| 3364 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3365 | PyObject * |
| 3366 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3367 | const char *encoding, |
| 3368 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3369 | { |
| 3370 | PyObject *v; |
| 3371 | |
| 3372 | if (!PyUnicode_Check(unicode)) { |
| 3373 | PyErr_BadArgument(); |
| 3374 | goto onError; |
| 3375 | } |
| 3376 | |
| 3377 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3378 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3379 | |
| 3380 | /* Encode via the codec registry */ |
| 3381 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3382 | if (v == NULL) |
| 3383 | goto onError; |
| 3384 | if (!PyUnicode_Check(v)) { |
| 3385 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3386 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3387 | Py_TYPE(v)->tp_name); |
| 3388 | Py_DECREF(v); |
| 3389 | goto onError; |
| 3390 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3391 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3392 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3393 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3394 | return NULL; |
| 3395 | } |
| 3396 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3397 | static size_t |
| 3398 | mbstowcs_errorpos(const char *str, size_t len) |
| 3399 | { |
| 3400 | #ifdef HAVE_MBRTOWC |
| 3401 | const char *start = str; |
| 3402 | mbstate_t mbs; |
| 3403 | size_t converted; |
| 3404 | wchar_t ch; |
| 3405 | |
| 3406 | memset(&mbs, 0, sizeof mbs); |
| 3407 | while (len) |
| 3408 | { |
| 3409 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3410 | if (converted == 0) |
| 3411 | /* Reached end of string */ |
| 3412 | break; |
| 3413 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3414 | /* Conversion error or incomplete character */ |
| 3415 | return str - start; |
| 3416 | } |
| 3417 | else { |
| 3418 | str += converted; |
| 3419 | len -= converted; |
| 3420 | } |
| 3421 | } |
| 3422 | /* failed to find the undecodable byte sequence */ |
| 3423 | return 0; |
| 3424 | #endif |
| 3425 | return 0; |
| 3426 | } |
| 3427 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3428 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3429 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3430 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3431 | { |
| 3432 | wchar_t smallbuf[256]; |
| 3433 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3434 | wchar_t *wstr; |
| 3435 | size_t wlen, wlen2; |
| 3436 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3437 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3438 | size_t error_pos; |
| 3439 | char *errmsg; |
| 3440 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3441 | |
| 3442 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3443 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3444 | |
| 3445 | if (str[len] != '\0' || len != strlen(str)) { |
| 3446 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3447 | return NULL; |
| 3448 | } |
| 3449 | |
| 3450 | if (surrogateescape) |
| 3451 | { |
| 3452 | wstr = _Py_char2wchar(str, &wlen); |
| 3453 | if (wstr == NULL) { |
| 3454 | if (wlen == (size_t)-1) |
| 3455 | PyErr_NoMemory(); |
| 3456 | else |
| 3457 | PyErr_SetFromErrno(PyExc_OSError); |
| 3458 | return NULL; |
| 3459 | } |
| 3460 | |
| 3461 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3462 | PyMem_Free(wstr); |
| 3463 | } |
| 3464 | else { |
| 3465 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3466 | wlen = mbstowcs(NULL, str, 0); |
| 3467 | #else |
| 3468 | wlen = len; |
| 3469 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3470 | if (wlen == (size_t)-1) |
| 3471 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3472 | if (wlen+1 <= smallbuf_len) { |
| 3473 | wstr = smallbuf; |
| 3474 | } |
| 3475 | else { |
| 3476 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3477 | return PyErr_NoMemory(); |
| 3478 | |
| 3479 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3480 | if (!wstr) |
| 3481 | return PyErr_NoMemory(); |
| 3482 | } |
| 3483 | |
| 3484 | /* This shouldn't fail now */ |
| 3485 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3486 | if (wlen2 == (size_t)-1) { |
| 3487 | if (wstr != smallbuf) |
| 3488 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3489 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3490 | } |
| 3491 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3492 | assert(wlen2 == wlen); |
| 3493 | #endif |
| 3494 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3495 | if (wstr != smallbuf) |
| 3496 | PyMem_Free(wstr); |
| 3497 | } |
| 3498 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3499 | |
| 3500 | decode_error: |
| 3501 | errmsg = strerror(errno); |
| 3502 | assert(errmsg != NULL); |
| 3503 | |
| 3504 | error_pos = mbstowcs_errorpos(str, len); |
| 3505 | if (errmsg != NULL) { |
| 3506 | size_t errlen; |
| 3507 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3508 | if (wstr != NULL) { |
| 3509 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3510 | PyMem_Free(wstr); |
| 3511 | } else |
| 3512 | errmsg = NULL; |
| 3513 | } |
| 3514 | if (errmsg == NULL) |
| 3515 | reason = PyUnicode_FromString( |
| 3516 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3517 | if (reason == NULL) |
| 3518 | return NULL; |
| 3519 | |
| 3520 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3521 | "locale", str, len, |
| 3522 | (Py_ssize_t)error_pos, |
| 3523 | (Py_ssize_t)(error_pos+1), |
| 3524 | reason); |
| 3525 | Py_DECREF(reason); |
| 3526 | if (exc != NULL) { |
| 3527 | PyCodec_StrictErrors(exc); |
| 3528 | Py_XDECREF(exc); |
| 3529 | } |
| 3530 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3534 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3535 | { |
| 3536 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3537 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3542 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3543 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3544 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3545 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3546 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3547 | PyObject* |
| 3548 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3549 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3550 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3551 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3552 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3553 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3554 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3555 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3556 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3557 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3558 | the Python codec requires to encode at least its own filename. Use the C |
| 3559 | version of the locale codec until the codec registry is initialized and |
| 3560 | the Python codec is loaded. |
| 3561 | |
| 3562 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3563 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3564 | subinterpreters. */ |
| 3565 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3566 | return PyUnicode_Decode(s, size, |
| 3567 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3568 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3569 | } |
| 3570 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3571 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3572 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3573 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3576 | |
| 3577 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3578 | _PyUnicode_HasNULChars(PyObject* s) |
| 3579 | { |
| 3580 | static PyObject *nul = NULL; |
| 3581 | |
| 3582 | if (nul == NULL) |
| 3583 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3584 | if (nul == NULL) |
| 3585 | return -1; |
| 3586 | return PyUnicode_Contains(s, nul); |
| 3587 | } |
| 3588 | |
| 3589 | |
| 3590 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3591 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3592 | { |
| 3593 | PyObject *output = NULL; |
| 3594 | Py_ssize_t size; |
| 3595 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3596 | if (arg == NULL) { |
| 3597 | Py_DECREF(*(PyObject**)addr); |
| 3598 | return 1; |
| 3599 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3600 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3601 | output = arg; |
| 3602 | Py_INCREF(output); |
| 3603 | } |
| 3604 | else { |
| 3605 | arg = PyUnicode_FromObject(arg); |
| 3606 | if (!arg) |
| 3607 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3608 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3609 | Py_DECREF(arg); |
| 3610 | if (!output) |
| 3611 | return 0; |
| 3612 | if (!PyBytes_Check(output)) { |
| 3613 | Py_DECREF(output); |
| 3614 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3615 | return 0; |
| 3616 | } |
| 3617 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3618 | size = PyBytes_GET_SIZE(output); |
| 3619 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3620 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3621 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3622 | Py_DECREF(output); |
| 3623 | return 0; |
| 3624 | } |
| 3625 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3626 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3630 | int |
| 3631 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3632 | { |
| 3633 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3634 | if (arg == NULL) { |
| 3635 | Py_DECREF(*(PyObject**)addr); |
| 3636 | return 1; |
| 3637 | } |
| 3638 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3639 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3640 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3641 | output = arg; |
| 3642 | Py_INCREF(output); |
| 3643 | } |
| 3644 | else { |
| 3645 | arg = PyBytes_FromObject(arg); |
| 3646 | if (!arg) |
| 3647 | return 0; |
| 3648 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3649 | PyBytes_GET_SIZE(arg)); |
| 3650 | Py_DECREF(arg); |
| 3651 | if (!output) |
| 3652 | return 0; |
| 3653 | if (!PyUnicode_Check(output)) { |
| 3654 | Py_DECREF(output); |
| 3655 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3656 | return 0; |
| 3657 | } |
| 3658 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3659 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3660 | Py_DECREF(output); |
| 3661 | return 0; |
| 3662 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3663 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3664 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3665 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3666 | Py_DECREF(output); |
| 3667 | return 0; |
| 3668 | } |
| 3669 | *(PyObject**)addr = output; |
| 3670 | return Py_CLEANUP_SUPPORTED; |
| 3671 | } |
| 3672 | |
| 3673 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3674 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3675 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3676 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3677 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3678 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3679 | if (!PyUnicode_Check(unicode)) { |
| 3680 | PyErr_BadArgument(); |
| 3681 | return NULL; |
| 3682 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3683 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3684 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3685 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3686 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3687 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3688 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3689 | if (bytes == NULL) |
| 3690 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3691 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3692 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3693 | Py_DECREF(bytes); |
| 3694 | return NULL; |
| 3695 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3696 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3697 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3698 | PyBytes_AS_STRING(bytes), |
| 3699 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3700 | Py_DECREF(bytes); |
| 3701 | } |
| 3702 | |
| 3703 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3704 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3705 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3709 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3710 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3711 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3712 | } |
| 3713 | |
| 3714 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3715 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3716 | #endif |
| 3717 | |
| 3718 | |
| 3719 | Py_UNICODE * |
| 3720 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3721 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3722 | const unsigned char *one_byte; |
| 3723 | #if SIZEOF_WCHAR_T == 4 |
| 3724 | const Py_UCS2 *two_bytes; |
| 3725 | #else |
| 3726 | const Py_UCS4 *four_bytes; |
| 3727 | const Py_UCS4 *ucs4_end; |
| 3728 | Py_ssize_t num_surrogates; |
| 3729 | #endif |
| 3730 | wchar_t *w; |
| 3731 | wchar_t *wchar_end; |
| 3732 | |
| 3733 | if (!PyUnicode_Check(unicode)) { |
| 3734 | PyErr_BadArgument(); |
| 3735 | return NULL; |
| 3736 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3737 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3738 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3739 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3740 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 | |
| 3742 | #ifdef Py_DEBUG |
| 3743 | ++unicode_as_unicode_calls; |
| 3744 | #endif |
| 3745 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3746 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3747 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3748 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3749 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3750 | num_surrogates = 0; |
| 3751 | |
| 3752 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3753 | if (*four_bytes > 0xFFFF) |
| 3754 | ++num_surrogates; |
| 3755 | } |
| 3756 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3757 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3758 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3759 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3760 | PyErr_NoMemory(); |
| 3761 | return NULL; |
| 3762 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3763 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3765 | w = _PyUnicode_WSTR(unicode); |
| 3766 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3767 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3768 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3769 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3770 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3771 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3772 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3773 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3774 | } |
| 3775 | else |
| 3776 | *w = *four_bytes; |
| 3777 | |
| 3778 | if (w > wchar_end) { |
| 3779 | assert(0 && "Miscalculated string end"); |
| 3780 | } |
| 3781 | } |
| 3782 | *w = 0; |
| 3783 | #else |
| 3784 | /* sizeof(wchar_t) == 4 */ |
| 3785 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3786 | "should share memory already."); |
| 3787 | return NULL; |
| 3788 | #endif |
| 3789 | } |
| 3790 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3791 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3792 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3793 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3794 | PyErr_NoMemory(); |
| 3795 | return NULL; |
| 3796 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3797 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3798 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3799 | w = _PyUnicode_WSTR(unicode); |
| 3800 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3801 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3802 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3803 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3804 | for (; w < wchar_end; ++one_byte, ++w) |
| 3805 | *w = *one_byte; |
| 3806 | /* null-terminate the wstr */ |
| 3807 | *w = 0; |
| 3808 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3809 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3810 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3811 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3813 | *w = *two_bytes; |
| 3814 | /* null-terminate the wstr */ |
| 3815 | *w = 0; |
| 3816 | #else |
| 3817 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3818 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3819 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3820 | Py_FatalError("Impossible unicode object state, wstr " |
| 3821 | "and str should share memory already."); |
| 3822 | return NULL; |
| 3823 | #endif |
| 3824 | } |
| 3825 | else { |
| 3826 | assert(0 && "This should never happen."); |
| 3827 | } |
| 3828 | } |
| 3829 | } |
| 3830 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3831 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3832 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3835 | Py_UNICODE * |
| 3836 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3837 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3838 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3842 | Py_ssize_t |
| 3843 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3844 | { |
| 3845 | if (!PyUnicode_Check(unicode)) { |
| 3846 | PyErr_BadArgument(); |
| 3847 | goto onError; |
| 3848 | } |
| 3849 | return PyUnicode_GET_SIZE(unicode); |
| 3850 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3851 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3852 | return -1; |
| 3853 | } |
| 3854 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3855 | Py_ssize_t |
| 3856 | PyUnicode_GetLength(PyObject *unicode) |
| 3857 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3858 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | PyErr_BadArgument(); |
| 3860 | return -1; |
| 3861 | } |
| 3862 | |
| 3863 | return PyUnicode_GET_LENGTH(unicode); |
| 3864 | } |
| 3865 | |
| 3866 | Py_UCS4 |
| 3867 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3868 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3869 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3870 | PyErr_BadArgument(); |
| 3871 | return (Py_UCS4)-1; |
| 3872 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3873 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3874 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3875 | return (Py_UCS4)-1; |
| 3876 | } |
| 3877 | return PyUnicode_READ_CHAR(unicode, index); |
| 3878 | } |
| 3879 | |
| 3880 | int |
| 3881 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3882 | { |
| 3883 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3884 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3885 | return -1; |
| 3886 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3887 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3888 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3889 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3890 | return -1; |
| 3891 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3892 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3893 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3894 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3895 | index, ch); |
| 3896 | return 0; |
| 3897 | } |
| 3898 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3899 | const char * |
| 3900 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3901 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3902 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3905 | /* create or adjust a UnicodeDecodeError */ |
| 3906 | static void |
| 3907 | make_decode_exception(PyObject **exceptionObject, |
| 3908 | const char *encoding, |
| 3909 | const char *input, Py_ssize_t length, |
| 3910 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3911 | const char *reason) |
| 3912 | { |
| 3913 | if (*exceptionObject == NULL) { |
| 3914 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3915 | encoding, input, length, startpos, endpos, reason); |
| 3916 | } |
| 3917 | else { |
| 3918 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3919 | goto onError; |
| 3920 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3921 | goto onError; |
| 3922 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3923 | goto onError; |
| 3924 | } |
| 3925 | return; |
| 3926 | |
| 3927 | onError: |
| 3928 | Py_DECREF(*exceptionObject); |
| 3929 | *exceptionObject = NULL; |
| 3930 | } |
| 3931 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | /* error handling callback helper: |
| 3933 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3934 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3935 | and adjust various state variables. |
| 3936 | return 0 on success, -1 on error |
| 3937 | */ |
| 3938 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3939 | static int |
| 3940 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3941 | const char *encoding, const char *reason, |
| 3942 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3943 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3944 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3946 | 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] | 3947 | |
| 3948 | PyObject *restuple = NULL; |
| 3949 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3950 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3951 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3952 | Py_ssize_t requiredsize; |
| 3953 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3954 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3955 | int res = -1; |
| 3956 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3957 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3958 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3959 | else |
| 3960 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3961 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3962 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | *errorHandler = PyCodec_LookupError(errors); |
| 3964 | if (*errorHandler == NULL) |
| 3965 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3968 | make_decode_exception(exceptionObject, |
| 3969 | encoding, |
| 3970 | *input, *inend - *input, |
| 3971 | *startinpos, *endinpos, |
| 3972 | reason); |
| 3973 | if (*exceptionObject == NULL) |
| 3974 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3975 | |
| 3976 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3977 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3978 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3979 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3980 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3981 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3982 | } |
| 3983 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3984 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3985 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3986 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3987 | |
| 3988 | /* Copy back the bytes variables, which might have been modified by the |
| 3989 | callback */ |
| 3990 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3991 | if (!inputobj) |
| 3992 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3993 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3994 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3995 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3996 | *input = PyBytes_AS_STRING(inputobj); |
| 3997 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3998 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3999 | /* we can DECREF safely, as the exception has another reference, |
| 4000 | so the object won't go away. */ |
| 4001 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4002 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4003 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4004 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4005 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4006 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4007 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4008 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4009 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4010 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4011 | /* need more space? (at least enough for what we |
| 4012 | have+the replacement+the rest of the string (starting |
| 4013 | at the new input position), so we won't have to check space |
| 4014 | when there are no errors in the rest of the string) */ |
| 4015 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4016 | requiredsize = *outpos + replen + insize-newpos; |
| 4017 | if (requiredsize > outsize) { |
| 4018 | if (requiredsize<2*outsize) |
| 4019 | requiredsize = 2*outsize; |
| 4020 | if (unicode_resize(output, requiredsize) < 0) |
| 4021 | goto onError; |
| 4022 | } |
| 4023 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4024 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4025 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4026 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4027 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4028 | else { |
| 4029 | wchar_t *repwstr; |
| 4030 | Py_ssize_t repwlen; |
| 4031 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4032 | if (repwstr == NULL) |
| 4033 | goto onError; |
| 4034 | /* need more space? (at least enough for what we |
| 4035 | have+the replacement+the rest of the string (starting |
| 4036 | at the new input position), so we won't have to check space |
| 4037 | when there are no errors in the rest of the string) */ |
| 4038 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4039 | if (requiredsize > outsize) { |
| 4040 | if (requiredsize < 2*outsize) |
| 4041 | requiredsize = 2*outsize; |
| 4042 | if (unicode_resize(output, requiredsize) < 0) |
| 4043 | goto onError; |
| 4044 | } |
| 4045 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4046 | *outpos += repwlen; |
| 4047 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4048 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4049 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4050 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4051 | /* we made it! */ |
| 4052 | res = 0; |
| 4053 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4054 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4055 | Py_XDECREF(restuple); |
| 4056 | return res; |
| 4057 | } |
| 4058 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4059 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4060 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4061 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4062 | |
| 4063 | /* Three simple macros defining base-64. */ |
| 4064 | |
| 4065 | /* Is c a base-64 character? */ |
| 4066 | |
| 4067 | #define IS_BASE64(c) \ |
| 4068 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4069 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4070 | ((c) >= '0' && (c) <= '9') || \ |
| 4071 | (c) == '+' || (c) == '/') |
| 4072 | |
| 4073 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4074 | |
| 4075 | #define FROM_BASE64(c) \ |
| 4076 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4077 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4078 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4079 | (c) == '+' ? 62 : 63) |
| 4080 | |
| 4081 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4082 | |
| 4083 | #define TO_BASE64(n) \ |
| 4084 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4085 | |
| 4086 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4087 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4088 | * byte not decoding to itself is the + which begins a base64 |
| 4089 | * string. */ |
| 4090 | |
| 4091 | #define DECODE_DIRECT(c) \ |
| 4092 | ((c) <= 127 && (c) != '+') |
| 4093 | |
| 4094 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4095 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4096 | * the above). See RFC2152. This array identifies these different |
| 4097 | * sets: |
| 4098 | * 0 : "Set D" |
| 4099 | * alphanumeric and '(),-./:? |
| 4100 | * 1 : "Set O" |
| 4101 | * !"#$%&*;<=>@[]^_`{|} |
| 4102 | * 2 : "whitespace" |
| 4103 | * ht nl cr sp |
| 4104 | * 3 : special (must be base64 encoded) |
| 4105 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4106 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4107 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4108 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4109 | char utf7_category[128] = { |
| 4110 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4111 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4112 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4113 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4114 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4115 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4116 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4117 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4118 | /* @ A B C D E F G H I J K L M N O */ |
| 4119 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4120 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4121 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4122 | /* ` a b c d e f g h i j k l m n o */ |
| 4123 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4124 | /* p q r s t u v w x y z { | } ~ del */ |
| 4125 | 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] | 4126 | }; |
| 4127 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4128 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4129 | * answer depends on whether we are encoding set O as itself, and also |
| 4130 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4131 | * clear that the answers to these questions vary between |
| 4132 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4133 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4134 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4135 | ((c) < 128 && (c) > 0 && \ |
| 4136 | ((utf7_category[(c)] == 0) || \ |
| 4137 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4138 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4139 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4140 | PyObject * |
| 4141 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4142 | Py_ssize_t size, |
| 4143 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4144 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4145 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4146 | } |
| 4147 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4148 | /* The decoder. The only state we preserve is our read position, |
| 4149 | * i.e. how many characters we have consumed. So if we end in the |
| 4150 | * middle of a shift sequence we have to back off the read position |
| 4151 | * and the output to the beginning of the sequence, otherwise we lose |
| 4152 | * all the shift state (seen bits, number of bits seen, high |
| 4153 | * surrogate). */ |
| 4154 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4155 | PyObject * |
| 4156 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4157 | Py_ssize_t size, |
| 4158 | const char *errors, |
| 4159 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4160 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4161 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4162 | Py_ssize_t startinpos; |
| 4163 | Py_ssize_t endinpos; |
| 4164 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4165 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4166 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4167 | const char *errmsg = ""; |
| 4168 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4169 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4170 | unsigned int base64bits = 0; |
| 4171 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4172 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4173 | PyObject *errorHandler = NULL; |
| 4174 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4175 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4176 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4177 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4178 | if (!unicode) |
| 4179 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4180 | if (size == 0) { |
| 4181 | if (consumed) |
| 4182 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4183 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4184 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4185 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4186 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4187 | e = s + size; |
| 4188 | |
| 4189 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4190 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4191 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4192 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4193 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4194 | if (inShift) { /* in a base-64 section */ |
| 4195 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4196 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4197 | base64bits += 6; |
| 4198 | s++; |
| 4199 | if (base64bits >= 16) { |
| 4200 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4201 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4202 | base64bits -= 16; |
| 4203 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4204 | if (surrogate) { |
| 4205 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4206 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4207 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4208 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4209 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4210 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4211 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4212 | } |
| 4213 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4214 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4215 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4216 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4217 | } |
| 4218 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4219 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4220 | /* first surrogate */ |
| 4221 | surrogate = outCh; |
| 4222 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4223 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4224 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4225 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | } |
| 4229 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4230 | inShift = 0; |
| 4231 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4232 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4233 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4234 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4235 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4236 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4237 | if (base64bits > 0) { /* left-over bits */ |
| 4238 | if (base64bits >= 6) { |
| 4239 | /* We've seen at least one base-64 character */ |
| 4240 | errmsg = "partial character in shift sequence"; |
| 4241 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4242 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4243 | else { |
| 4244 | /* Some bits remain; they should be zero */ |
| 4245 | if (base64buffer != 0) { |
| 4246 | errmsg = "non-zero padding bits in shift sequence"; |
| 4247 | goto utf7Error; |
| 4248 | } |
| 4249 | } |
| 4250 | } |
| 4251 | if (ch != '-') { |
| 4252 | /* '-' is absorbed; other terminating |
| 4253 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4254 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4255 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4256 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4260 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4261 | s++; /* consume '+' */ |
| 4262 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4263 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4264 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4265 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4266 | } |
| 4267 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4268 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4269 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4270 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4271 | } |
| 4272 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4273 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4274 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4275 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4276 | s++; |
| 4277 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4278 | else { |
| 4279 | startinpos = s-starts; |
| 4280 | s++; |
| 4281 | errmsg = "unexpected special character"; |
| 4282 | goto utf7Error; |
| 4283 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4284 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4285 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4286 | endinpos = s-starts; |
| 4287 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4288 | errors, &errorHandler, |
| 4289 | "utf7", errmsg, |
| 4290 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4291 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4292 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4295 | /* end of string */ |
| 4296 | |
| 4297 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4298 | /* if we're in an inconsistent state, that's an error */ |
| 4299 | if (surrogate || |
| 4300 | (base64bits >= 6) || |
| 4301 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4302 | endinpos = size; |
| 4303 | if (unicode_decode_call_errorhandler( |
| 4304 | errors, &errorHandler, |
| 4305 | "utf7", "unterminated shift sequence", |
| 4306 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4307 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4308 | goto onError; |
| 4309 | if (s < e) |
| 4310 | goto restart; |
| 4311 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4312 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4313 | |
| 4314 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4315 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4316 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4317 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4318 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4319 | } |
| 4320 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4321 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4322 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4323 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4324 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4325 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4326 | goto onError; |
| 4327 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4328 | Py_XDECREF(errorHandler); |
| 4329 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4330 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4331 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4332 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4333 | Py_XDECREF(errorHandler); |
| 4334 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4335 | Py_DECREF(unicode); |
| 4336 | return NULL; |
| 4337 | } |
| 4338 | |
| 4339 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4340 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4341 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4342 | int base64SetO, |
| 4343 | int base64WhiteSpace, |
| 4344 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4345 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4346 | int kind; |
| 4347 | void *data; |
| 4348 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4349 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4350 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4351 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4352 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4353 | unsigned int base64bits = 0; |
| 4354 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4355 | char * out; |
| 4356 | char * start; |
| 4357 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4358 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4359 | return NULL; |
| 4360 | kind = PyUnicode_KIND(str); |
| 4361 | data = PyUnicode_DATA(str); |
| 4362 | len = PyUnicode_GET_LENGTH(str); |
| 4363 | |
| 4364 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4365 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4366 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4367 | /* It might be possible to tighten this worst case */ |
| 4368 | allocated = 8 * len; |
| 4369 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4370 | return PyErr_NoMemory(); |
| 4371 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4372 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4373 | if (v == NULL) |
| 4374 | return NULL; |
| 4375 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4376 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4377 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4378 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | if (inShift) { |
| 4381 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4382 | /* shifting out */ |
| 4383 | if (base64bits) { /* output remaining bits */ |
| 4384 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4385 | base64buffer = 0; |
| 4386 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4387 | } |
| 4388 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4389 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4390 | so no '-' is required, except if the character is itself a '-' */ |
| 4391 | if (IS_BASE64(ch) || ch == '-') { |
| 4392 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4393 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4394 | *out++ = (char) ch; |
| 4395 | } |
| 4396 | else { |
| 4397 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4398 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4399 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4400 | else { /* not in a shift sequence */ |
| 4401 | if (ch == '+') { |
| 4402 | *out++ = '+'; |
| 4403 | *out++ = '-'; |
| 4404 | } |
| 4405 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4406 | *out++ = (char) ch; |
| 4407 | } |
| 4408 | else { |
| 4409 | *out++ = '+'; |
| 4410 | inShift = 1; |
| 4411 | goto encode_char; |
| 4412 | } |
| 4413 | } |
| 4414 | continue; |
| 4415 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4416 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4417 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4418 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4419 | /* code first surrogate */ |
| 4420 | base64bits += 16; |
| 4421 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4422 | while (base64bits >= 6) { |
| 4423 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4424 | base64bits -= 6; |
| 4425 | } |
| 4426 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4427 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4428 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4429 | base64bits += 16; |
| 4430 | base64buffer = (base64buffer << 16) | ch; |
| 4431 | while (base64bits >= 6) { |
| 4432 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4433 | base64bits -= 6; |
| 4434 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4435 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4436 | if (base64bits) |
| 4437 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4438 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4439 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4440 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4441 | return NULL; |
| 4442 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4443 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4444 | PyObject * |
| 4445 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4446 | Py_ssize_t size, |
| 4447 | int base64SetO, |
| 4448 | int base64WhiteSpace, |
| 4449 | const char *errors) |
| 4450 | { |
| 4451 | PyObject *result; |
| 4452 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4453 | if (tmp == NULL) |
| 4454 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4455 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4456 | base64WhiteSpace, errors); |
| 4457 | Py_DECREF(tmp); |
| 4458 | return result; |
| 4459 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4460 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | #undef IS_BASE64 |
| 4462 | #undef FROM_BASE64 |
| 4463 | #undef TO_BASE64 |
| 4464 | #undef DECODE_DIRECT |
| 4465 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4467 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4468 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4469 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4470 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4471 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4472 | illegal prefix. See RFC 3629 for details */ |
| 4473 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4474 | 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] | 4475 | 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] | 4476 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4477 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4478 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4479 | 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] | 4480 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4481 | 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] | 4482 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4483 | 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] | 4484 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4485 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4486 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4487 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4488 | 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] | 4489 | }; |
| 4490 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4491 | PyObject * |
| 4492 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4493 | Py_ssize_t size, |
| 4494 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4495 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4496 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4497 | } |
| 4498 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4499 | #include "stringlib/ucs1lib.h" |
| 4500 | #include "stringlib/codecs.h" |
| 4501 | #include "stringlib/undef.h" |
| 4502 | |
| 4503 | #include "stringlib/ucs2lib.h" |
| 4504 | #include "stringlib/codecs.h" |
| 4505 | #include "stringlib/undef.h" |
| 4506 | |
| 4507 | #include "stringlib/ucs4lib.h" |
| 4508 | #include "stringlib/codecs.h" |
| 4509 | #include "stringlib/undef.h" |
| 4510 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4511 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4512 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4513 | |
| 4514 | /* Mask to quickly check whether a C 'long' contains a |
| 4515 | non-ASCII, UTF8-encoded char. */ |
| 4516 | #if (SIZEOF_LONG == 8) |
| 4517 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4518 | #elif (SIZEOF_LONG == 4) |
| 4519 | # define ASCII_CHAR_MASK 0x80808080L |
| 4520 | #else |
| 4521 | # error C 'long' size should be either 4 or 8! |
| 4522 | #endif |
| 4523 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4524 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4525 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4526 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4527 | 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] | 4528 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4529 | */ |
| 4530 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4531 | 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] | 4532 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4533 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4534 | const unsigned char *end = p + string_size; |
| 4535 | 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] | 4536 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4537 | assert(unicode_size != NULL); |
| 4538 | |
| 4539 | /* By having a cascade of independent loops which fallback onto each |
| 4540 | other, we minimize the amount of work done in the average loop |
| 4541 | iteration, and we also maximize the CPU's ability to predict |
| 4542 | branches correctly (because a given condition will have always the |
| 4543 | same boolean outcome except perhaps in the last iteration of the |
| 4544 | corresponding loop). |
| 4545 | In the general case this brings us rather close to decoding |
| 4546 | performance pre-PEP 393, despite the two-pass decoding. |
| 4547 | |
| 4548 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4549 | character has been encountered. It is actually a pessimization (by |
| 4550 | a significant factor) to use this loop on text with many non-ASCII |
| 4551 | characters, and it is important to avoid bad performance on valid |
| 4552 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4553 | */ |
| 4554 | |
| 4555 | /* ASCII */ |
| 4556 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4557 | /* Only check value if it's not a ASCII char... */ |
| 4558 | if (*p < 0x80) { |
| 4559 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4560 | an explanation. */ |
| 4561 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4562 | /* Help register allocation */ |
| 4563 | register const unsigned char *_p = p; |
| 4564 | while (_p < aligned_end) { |
| 4565 | unsigned long value = *(unsigned long *) _p; |
| 4566 | if (value & ASCII_CHAR_MASK) |
| 4567 | break; |
| 4568 | _p += SIZEOF_LONG; |
| 4569 | char_count += SIZEOF_LONG; |
| 4570 | } |
| 4571 | p = _p; |
| 4572 | if (p == end) |
| 4573 | break; |
| 4574 | } |
| 4575 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4576 | if (*p < 0x80) |
| 4577 | ++char_count; |
| 4578 | else |
| 4579 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4580 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4581 | *unicode_size = char_count; |
| 4582 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4583 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4584 | _ucs1loop: |
| 4585 | for (; p < end; ++p) { |
| 4586 | if (*p < 0xc4) |
| 4587 | char_count += ((*p & 0xc0) != 0x80); |
| 4588 | else |
| 4589 | goto _ucs2loop; |
| 4590 | } |
| 4591 | *unicode_size = char_count; |
| 4592 | return 255; |
| 4593 | |
| 4594 | _ucs2loop: |
| 4595 | for (; p < end; ++p) { |
| 4596 | if (*p < 0xf0) |
| 4597 | char_count += ((*p & 0xc0) != 0x80); |
| 4598 | else |
| 4599 | goto _ucs4loop; |
| 4600 | } |
| 4601 | *unicode_size = char_count; |
| 4602 | return 65535; |
| 4603 | |
| 4604 | _ucs4loop: |
| 4605 | for (; p < end; ++p) { |
| 4606 | char_count += ((*p & 0xc0) != 0x80); |
| 4607 | } |
| 4608 | *unicode_size = char_count; |
| 4609 | return 65537; |
| 4610 | } |
| 4611 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4612 | /* 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] | 4613 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4614 | 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] | 4615 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4616 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4617 | do { \ |
| 4618 | Py_ssize_t pos = index; \ |
| 4619 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4620 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4621 | goto onError; \ |
| 4622 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4623 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4624 | } while (0) |
| 4625 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4626 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4627 | decode_utf8_errors(const char *starts, |
| 4628 | Py_ssize_t size, |
| 4629 | const char *errors, |
| 4630 | Py_ssize_t *consumed, |
| 4631 | const char *s, |
| 4632 | PyObject *unicode, |
| 4633 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4634 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4635 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4636 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4637 | Py_ssize_t startinpos; |
| 4638 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4639 | const char *e = starts + size; |
| 4640 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4641 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4642 | PyObject *errorHandler = NULL; |
| 4643 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4644 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4645 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4646 | |
| 4647 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4648 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4649 | |
| 4650 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4651 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4652 | input will consist of an overwhelming majority of ASCII |
| 4653 | characters, we try to optimize for this case by checking |
| 4654 | as many characters as a C 'long' can contain. |
| 4655 | First, check if we can do an aligned read, as most CPUs have |
| 4656 | a penalty for unaligned reads. |
| 4657 | */ |
| 4658 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4659 | /* Help register allocation */ |
| 4660 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4661 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4662 | while (_s < aligned_end) { |
| 4663 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4664 | and do a fast unrolled copy if it only contains ASCII |
| 4665 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4666 | unsigned long value = *(unsigned long *) _s; |
| 4667 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4668 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4669 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4670 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4671 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4672 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4673 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4674 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4675 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4676 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4677 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4678 | #endif |
| 4679 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4680 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4681 | } |
| 4682 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4683 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4684 | if (s == e) |
| 4685 | break; |
| 4686 | ch = (unsigned char)*s; |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4691 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4692 | s++; |
| 4693 | continue; |
| 4694 | } |
| 4695 | |
| 4696 | n = utf8_code_length[ch]; |
| 4697 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4698 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4699 | if (consumed) |
| 4700 | break; |
| 4701 | else { |
| 4702 | errmsg = "unexpected end of data"; |
| 4703 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4704 | endinpos = startinpos+1; |
| 4705 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4706 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4707 | goto utf8Error; |
| 4708 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4709 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | |
| 4711 | switch (n) { |
| 4712 | |
| 4713 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4714 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4715 | startinpos = s-starts; |
| 4716 | endinpos = startinpos+1; |
| 4717 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | |
| 4719 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4720 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4721 | startinpos = s-starts; |
| 4722 | endinpos = startinpos+1; |
| 4723 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4724 | |
| 4725 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4726 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4727 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4728 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4729 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4730 | goto utf8Error; |
| 4731 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4732 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4733 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4734 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4735 | break; |
| 4736 | |
| 4737 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4738 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4739 | will result in surrogates in range d800-dfff. Surrogates are |
| 4740 | not valid UTF-8 so they are rejected. |
| 4741 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4742 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4743 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4744 | (s[2] & 0xc0) != 0x80 || |
| 4745 | ((unsigned char)s[0] == 0xE0 && |
| 4746 | (unsigned char)s[1] < 0xA0) || |
| 4747 | ((unsigned char)s[0] == 0xED && |
| 4748 | (unsigned char)s[1] > 0x9F)) { |
| 4749 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4750 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4751 | endinpos = startinpos + 1; |
| 4752 | |
| 4753 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4754 | continuation byte is s[2], so increment endinpos by 1, |
| 4755 | if not, s[1] is invalid and endinpos doesn't need to |
| 4756 | be incremented. */ |
| 4757 | if ((s[1] & 0xC0) == 0x80) |
| 4758 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4759 | goto utf8Error; |
| 4760 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4761 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4762 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4763 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4764 | break; |
| 4765 | |
| 4766 | case 4: |
| 4767 | if ((s[1] & 0xc0) != 0x80 || |
| 4768 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4769 | (s[3] & 0xc0) != 0x80 || |
| 4770 | ((unsigned char)s[0] == 0xF0 && |
| 4771 | (unsigned char)s[1] < 0x90) || |
| 4772 | ((unsigned char)s[0] == 0xF4 && |
| 4773 | (unsigned char)s[1] > 0x8F)) { |
| 4774 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4775 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4776 | endinpos = startinpos + 1; |
| 4777 | if ((s[1] & 0xC0) == 0x80) { |
| 4778 | endinpos++; |
| 4779 | if ((s[2] & 0xC0) == 0x80) |
| 4780 | endinpos++; |
| 4781 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4782 | goto utf8Error; |
| 4783 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4784 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4785 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4786 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4787 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4788 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4789 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4790 | } |
| 4791 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4792 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4793 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4795 | if (unicode_decode_call_errorhandler( |
| 4796 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4797 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4798 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4799 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4800 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4801 | /* Update data because unicode_decode_call_errorhandler might have |
| 4802 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4803 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4804 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4805 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4806 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4807 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4808 | /* Adjust length and ready string when it contained errors and |
| 4809 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4810 | if (unicode_resize(&unicode, i) < 0) |
| 4811 | goto onError; |
| 4812 | unicode_adjust_maxchar(&unicode); |
| 4813 | if (unicode == NULL) |
| 4814 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4815 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4816 | Py_XDECREF(errorHandler); |
| 4817 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4818 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4819 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4820 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4821 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4822 | Py_XDECREF(errorHandler); |
| 4823 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4824 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | return NULL; |
| 4826 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4827 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4828 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4829 | PyObject * |
| 4830 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4831 | Py_ssize_t size, |
| 4832 | const char *errors, |
| 4833 | Py_ssize_t *consumed) |
| 4834 | { |
| 4835 | Py_UCS4 maxchar = 0; |
| 4836 | Py_ssize_t unicode_size; |
| 4837 | int has_errors = 0; |
| 4838 | PyObject *unicode; |
| 4839 | int kind; |
| 4840 | void *data; |
| 4841 | const char *starts = s; |
| 4842 | const char *e; |
| 4843 | Py_ssize_t i; |
| 4844 | |
| 4845 | if (size == 0) { |
| 4846 | if (consumed) |
| 4847 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4848 | Py_INCREF(unicode_empty); |
| 4849 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4850 | } |
| 4851 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4852 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4853 | |
| 4854 | /* When the string is ASCII only, just use memcpy and return. |
| 4855 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4856 | sequence at the end of the ASCII block. */ |
| 4857 | if (maxchar < 128 && size == unicode_size) { |
| 4858 | if (consumed) |
| 4859 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 4860 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4864 | if (!unicode) |
| 4865 | return NULL; |
| 4866 | kind = PyUnicode_KIND(unicode); |
| 4867 | data = PyUnicode_DATA(unicode); |
| 4868 | |
| 4869 | /* Unpack UTF-8 encoded data */ |
| 4870 | i = 0; |
| 4871 | e = starts + size; |
| 4872 | switch (kind) { |
| 4873 | case PyUnicode_1BYTE_KIND: |
| 4874 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4875 | break; |
| 4876 | case PyUnicode_2BYTE_KIND: |
| 4877 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4878 | break; |
| 4879 | case PyUnicode_4BYTE_KIND: |
| 4880 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4881 | break; |
| 4882 | } |
| 4883 | if (!has_errors) { |
| 4884 | /* Ensure the unicode size calculation was correct */ |
| 4885 | assert(i == unicode_size); |
| 4886 | assert(s == e); |
| 4887 | if (consumed) |
| 4888 | *consumed = size; |
| 4889 | return unicode; |
| 4890 | } |
| 4891 | |
| 4892 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4893 | code below refits and resizes as necessary. */ |
| 4894 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 4895 | } |
| 4896 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4897 | #ifdef __APPLE__ |
| 4898 | |
| 4899 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4900 | used to decode the command line arguments on Mac OS X. */ |
| 4901 | |
| 4902 | wchar_t* |
| 4903 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4904 | { |
| 4905 | int n; |
| 4906 | const char *e; |
| 4907 | wchar_t *unicode, *p; |
| 4908 | |
| 4909 | /* Note: size will always be longer than the resulting Unicode |
| 4910 | character count */ |
| 4911 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4912 | PyErr_NoMemory(); |
| 4913 | return NULL; |
| 4914 | } |
| 4915 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4916 | if (!unicode) |
| 4917 | return NULL; |
| 4918 | |
| 4919 | /* Unpack UTF-8 encoded data */ |
| 4920 | p = unicode; |
| 4921 | e = s + size; |
| 4922 | while (s < e) { |
| 4923 | Py_UCS4 ch = (unsigned char)*s; |
| 4924 | |
| 4925 | if (ch < 0x80) { |
| 4926 | *p++ = (wchar_t)ch; |
| 4927 | s++; |
| 4928 | continue; |
| 4929 | } |
| 4930 | |
| 4931 | n = utf8_code_length[ch]; |
| 4932 | if (s + n > e) { |
| 4933 | goto surrogateescape; |
| 4934 | } |
| 4935 | |
| 4936 | switch (n) { |
| 4937 | case 0: |
| 4938 | case 1: |
| 4939 | goto surrogateescape; |
| 4940 | |
| 4941 | case 2: |
| 4942 | if ((s[1] & 0xc0) != 0x80) |
| 4943 | goto surrogateescape; |
| 4944 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4945 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4946 | *p++ = (wchar_t)ch; |
| 4947 | break; |
| 4948 | |
| 4949 | case 3: |
| 4950 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4951 | will result in surrogates in range d800-dfff. Surrogates are |
| 4952 | not valid UTF-8 so they are rejected. |
| 4953 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4954 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4955 | if ((s[1] & 0xc0) != 0x80 || |
| 4956 | (s[2] & 0xc0) != 0x80 || |
| 4957 | ((unsigned char)s[0] == 0xE0 && |
| 4958 | (unsigned char)s[1] < 0xA0) || |
| 4959 | ((unsigned char)s[0] == 0xED && |
| 4960 | (unsigned char)s[1] > 0x9F)) { |
| 4961 | |
| 4962 | goto surrogateescape; |
| 4963 | } |
| 4964 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4965 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4966 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4967 | break; |
| 4968 | |
| 4969 | case 4: |
| 4970 | if ((s[1] & 0xc0) != 0x80 || |
| 4971 | (s[2] & 0xc0) != 0x80 || |
| 4972 | (s[3] & 0xc0) != 0x80 || |
| 4973 | ((unsigned char)s[0] == 0xF0 && |
| 4974 | (unsigned char)s[1] < 0x90) || |
| 4975 | ((unsigned char)s[0] == 0xF4 && |
| 4976 | (unsigned char)s[1] > 0x8F)) { |
| 4977 | goto surrogateescape; |
| 4978 | } |
| 4979 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4980 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4981 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4982 | |
| 4983 | #if SIZEOF_WCHAR_T == 4 |
| 4984 | *p++ = (wchar_t)ch; |
| 4985 | #else |
| 4986 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4987 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4988 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4989 | #endif |
| 4990 | break; |
| 4991 | } |
| 4992 | s += n; |
| 4993 | continue; |
| 4994 | |
| 4995 | surrogateescape: |
| 4996 | *p++ = 0xDC00 + ch; |
| 4997 | s++; |
| 4998 | } |
| 4999 | *p = L'\0'; |
| 5000 | return unicode; |
| 5001 | } |
| 5002 | |
| 5003 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5005 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5006 | |
| 5007 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5008 | and allocate exactly as much space needed at the end. Else allocate the |
| 5009 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5010 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5011 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5012 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5013 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5014 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5015 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5016 | void *data; |
| 5017 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5019 | if (!PyUnicode_Check(unicode)) { |
| 5020 | PyErr_BadArgument(); |
| 5021 | return NULL; |
| 5022 | } |
| 5023 | |
| 5024 | if (PyUnicode_READY(unicode) == -1) |
| 5025 | return NULL; |
| 5026 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5027 | if (PyUnicode_UTF8(unicode)) |
| 5028 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5029 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5030 | |
| 5031 | kind = PyUnicode_KIND(unicode); |
| 5032 | data = PyUnicode_DATA(unicode); |
| 5033 | size = PyUnicode_GET_LENGTH(unicode); |
| 5034 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5035 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5036 | default: |
| 5037 | assert(0); |
| 5038 | case PyUnicode_1BYTE_KIND: |
| 5039 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5040 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5041 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5042 | case PyUnicode_2BYTE_KIND: |
| 5043 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5044 | case PyUnicode_4BYTE_KIND: |
| 5045 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5046 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5049 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5050 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5051 | Py_ssize_t size, |
| 5052 | const char *errors) |
| 5053 | { |
| 5054 | PyObject *v, *unicode; |
| 5055 | |
| 5056 | unicode = PyUnicode_FromUnicode(s, size); |
| 5057 | if (unicode == NULL) |
| 5058 | return NULL; |
| 5059 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5060 | Py_DECREF(unicode); |
| 5061 | return v; |
| 5062 | } |
| 5063 | |
| 5064 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5065 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5066 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5067 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5071 | |
| 5072 | PyObject * |
| 5073 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5074 | Py_ssize_t size, |
| 5075 | const char *errors, |
| 5076 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5077 | { |
| 5078 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5079 | } |
| 5080 | |
| 5081 | PyObject * |
| 5082 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5083 | Py_ssize_t size, |
| 5084 | const char *errors, |
| 5085 | int *byteorder, |
| 5086 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5087 | { |
| 5088 | const char *starts = s; |
| 5089 | Py_ssize_t startinpos; |
| 5090 | Py_ssize_t endinpos; |
| 5091 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5092 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5093 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5094 | int bo = 0; /* assume native ordering by default */ |
| 5095 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5096 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5097 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5098 | int iorder[] = {0, 1, 2, 3}; |
| 5099 | #else |
| 5100 | int iorder[] = {3, 2, 1, 0}; |
| 5101 | #endif |
| 5102 | PyObject *errorHandler = NULL; |
| 5103 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5104 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5105 | q = (unsigned char *)s; |
| 5106 | e = q + size; |
| 5107 | |
| 5108 | if (byteorder) |
| 5109 | bo = *byteorder; |
| 5110 | |
| 5111 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5112 | byte order setting accordingly. In native mode, the leading BOM |
| 5113 | mark is skipped, in all other modes, it is copied to the output |
| 5114 | stream as-is (giving a ZWNBSP character). */ |
| 5115 | if (bo == 0) { |
| 5116 | if (size >= 4) { |
| 5117 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5118 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5119 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5120 | if (bom == 0x0000FEFF) { |
| 5121 | q += 4; |
| 5122 | bo = -1; |
| 5123 | } |
| 5124 | else if (bom == 0xFFFE0000) { |
| 5125 | q += 4; |
| 5126 | bo = 1; |
| 5127 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5129 | if (bom == 0x0000FEFF) { |
| 5130 | q += 4; |
| 5131 | bo = 1; |
| 5132 | } |
| 5133 | else if (bom == 0xFFFE0000) { |
| 5134 | q += 4; |
| 5135 | bo = -1; |
| 5136 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5137 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5138 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | if (bo == -1) { |
| 5142 | /* force LE */ |
| 5143 | iorder[0] = 0; |
| 5144 | iorder[1] = 1; |
| 5145 | iorder[2] = 2; |
| 5146 | iorder[3] = 3; |
| 5147 | } |
| 5148 | else if (bo == 1) { |
| 5149 | /* force BE */ |
| 5150 | iorder[0] = 3; |
| 5151 | iorder[1] = 2; |
| 5152 | iorder[2] = 1; |
| 5153 | iorder[3] = 0; |
| 5154 | } |
| 5155 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5156 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5157 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5158 | if (!unicode) |
| 5159 | return NULL; |
| 5160 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5161 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5162 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5163 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5164 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5165 | Py_UCS4 ch; |
| 5166 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5167 | if (e-q<4) { |
| 5168 | if (consumed) |
| 5169 | break; |
| 5170 | errmsg = "truncated data"; |
| 5171 | startinpos = ((const char *)q)-starts; |
| 5172 | endinpos = ((const char *)e)-starts; |
| 5173 | goto utf32Error; |
| 5174 | /* The remaining input chars are ignored if the callback |
| 5175 | chooses to skip the input */ |
| 5176 | } |
| 5177 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5178 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | if (ch >= 0x110000) |
| 5181 | { |
| 5182 | errmsg = "codepoint not in range(0x110000)"; |
| 5183 | startinpos = ((const char *)q)-starts; |
| 5184 | endinpos = startinpos+4; |
| 5185 | goto utf32Error; |
| 5186 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5187 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5188 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | q += 4; |
| 5190 | continue; |
| 5191 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5192 | if (unicode_decode_call_errorhandler( |
| 5193 | errors, &errorHandler, |
| 5194 | "utf32", errmsg, |
| 5195 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5196 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5197 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | if (byteorder) |
| 5201 | *byteorder = bo; |
| 5202 | |
| 5203 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5204 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5205 | |
| 5206 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5207 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5208 | goto onError; |
| 5209 | |
| 5210 | Py_XDECREF(errorHandler); |
| 5211 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5212 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5214 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5215 | Py_DECREF(unicode); |
| 5216 | Py_XDECREF(errorHandler); |
| 5217 | Py_XDECREF(exc); |
| 5218 | return NULL; |
| 5219 | } |
| 5220 | |
| 5221 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5222 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5223 | const char *errors, |
| 5224 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5225 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5226 | int kind; |
| 5227 | void *data; |
| 5228 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5229 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5230 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5231 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5232 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5233 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5234 | int iorder[] = {0, 1, 2, 3}; |
| 5235 | #else |
| 5236 | int iorder[] = {3, 2, 1, 0}; |
| 5237 | #endif |
| 5238 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5239 | #define STORECHAR(CH) \ |
| 5240 | do { \ |
| 5241 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5242 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5243 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5244 | p[iorder[0]] = (CH) & 0xff; \ |
| 5245 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5246 | } while(0) |
| 5247 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5248 | if (!PyUnicode_Check(str)) { |
| 5249 | PyErr_BadArgument(); |
| 5250 | return NULL; |
| 5251 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5252 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5253 | return NULL; |
| 5254 | kind = PyUnicode_KIND(str); |
| 5255 | data = PyUnicode_DATA(str); |
| 5256 | len = PyUnicode_GET_LENGTH(str); |
| 5257 | |
| 5258 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5259 | bytesize = nsize * 4; |
| 5260 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5261 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5262 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5263 | if (v == NULL) |
| 5264 | return NULL; |
| 5265 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5266 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5267 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5268 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5269 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5270 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5271 | |
| 5272 | if (byteorder == -1) { |
| 5273 | /* force LE */ |
| 5274 | iorder[0] = 0; |
| 5275 | iorder[1] = 1; |
| 5276 | iorder[2] = 2; |
| 5277 | iorder[3] = 3; |
| 5278 | } |
| 5279 | else if (byteorder == 1) { |
| 5280 | /* force BE */ |
| 5281 | iorder[0] = 3; |
| 5282 | iorder[1] = 2; |
| 5283 | iorder[2] = 1; |
| 5284 | iorder[3] = 0; |
| 5285 | } |
| 5286 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5287 | for (i = 0; i < len; i++) |
| 5288 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5289 | |
| 5290 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5291 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5292 | #undef STORECHAR |
| 5293 | } |
| 5294 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5295 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5296 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5297 | Py_ssize_t size, |
| 5298 | const char *errors, |
| 5299 | int byteorder) |
| 5300 | { |
| 5301 | PyObject *result; |
| 5302 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5303 | if (tmp == NULL) |
| 5304 | return NULL; |
| 5305 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5306 | Py_DECREF(tmp); |
| 5307 | return result; |
| 5308 | } |
| 5309 | |
| 5310 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5311 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5312 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5313 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5317 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5318 | PyObject * |
| 5319 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5320 | Py_ssize_t size, |
| 5321 | const char *errors, |
| 5322 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5324 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5325 | } |
| 5326 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5327 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5328 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5329 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5330 | rare in most input. |
| 5331 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5332 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5333 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5334 | #if (SIZEOF_LONG == 8) |
| 5335 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5336 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5337 | #elif (SIZEOF_LONG == 4) |
| 5338 | # define FAST_CHAR_MASK 0x80008000L |
| 5339 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5340 | #else |
| 5341 | # error C 'long' size should be either 4 or 8! |
| 5342 | #endif |
| 5343 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5344 | PyObject * |
| 5345 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5346 | Py_ssize_t size, |
| 5347 | const char *errors, |
| 5348 | int *byteorder, |
| 5349 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5350 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5351 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5352 | Py_ssize_t startinpos; |
| 5353 | Py_ssize_t endinpos; |
| 5354 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5355 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5356 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5357 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5358 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5359 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5360 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5361 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5362 | int ihi = 1, ilo = 0; |
| 5363 | #else |
| 5364 | int ihi = 0, ilo = 1; |
| 5365 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5366 | PyObject *errorHandler = NULL; |
| 5367 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | |
| 5369 | /* Note: size will always be longer than the resulting Unicode |
| 5370 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5371 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | if (!unicode) |
| 5373 | return NULL; |
| 5374 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5375 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5376 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5378 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5379 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | |
| 5381 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5382 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5384 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5385 | byte order setting accordingly. In native mode, the leading BOM |
| 5386 | mark is skipped, in all other modes, it is copied to the output |
| 5387 | stream as-is (giving a ZWNBSP character). */ |
| 5388 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5389 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5390 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5391 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 | if (bom == 0xFEFF) { |
| 5393 | q += 2; |
| 5394 | bo = -1; |
| 5395 | } |
| 5396 | else if (bom == 0xFFFE) { |
| 5397 | q += 2; |
| 5398 | bo = 1; |
| 5399 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5400 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5401 | if (bom == 0xFEFF) { |
| 5402 | q += 2; |
| 5403 | bo = 1; |
| 5404 | } |
| 5405 | else if (bom == 0xFFFE) { |
| 5406 | q += 2; |
| 5407 | bo = -1; |
| 5408 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5409 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5410 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5411 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5413 | if (bo == -1) { |
| 5414 | /* force LE */ |
| 5415 | ihi = 1; |
| 5416 | ilo = 0; |
| 5417 | } |
| 5418 | else if (bo == 1) { |
| 5419 | /* force BE */ |
| 5420 | ihi = 0; |
| 5421 | ilo = 1; |
| 5422 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5423 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5424 | native_ordering = ilo < ihi; |
| 5425 | #else |
| 5426 | native_ordering = ilo > ihi; |
| 5427 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5428 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5429 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5430 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5431 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5432 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5433 | reads are more expensive, better to defer to another iteration. */ |
| 5434 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5435 | /* Fast path for runs of non-surrogate chars. */ |
| 5436 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5437 | int kind = PyUnicode_KIND(unicode); |
| 5438 | void *data = PyUnicode_DATA(unicode); |
| 5439 | while (_q < aligned_end) { |
| 5440 | unsigned long block = * (unsigned long *) _q; |
| 5441 | unsigned short *pblock = (unsigned short*)█ |
| 5442 | Py_UCS4 maxch; |
| 5443 | if (native_ordering) { |
| 5444 | /* Can use buffer directly */ |
| 5445 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5446 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5447 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5448 | else { |
| 5449 | /* Need to byte-swap */ |
| 5450 | unsigned char *_p = (unsigned char*)pblock; |
| 5451 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5452 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5453 | _p[0] = _q[1]; |
| 5454 | _p[1] = _q[0]; |
| 5455 | _p[2] = _q[3]; |
| 5456 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5457 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5458 | _p[4] = _q[5]; |
| 5459 | _p[5] = _q[4]; |
| 5460 | _p[6] = _q[7]; |
| 5461 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5462 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5463 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5464 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5465 | #if SIZEOF_LONG == 8 |
| 5466 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5467 | #endif |
| 5468 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5469 | if (unicode_widen(&unicode, maxch) < 0) |
| 5470 | goto onError; |
| 5471 | kind = PyUnicode_KIND(unicode); |
| 5472 | data = PyUnicode_DATA(unicode); |
| 5473 | } |
| 5474 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5475 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5476 | #if SIZEOF_LONG == 8 |
| 5477 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5478 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5479 | #endif |
| 5480 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5481 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5482 | q = _q; |
| 5483 | if (q >= e) |
| 5484 | break; |
| 5485 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5486 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5487 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5488 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5489 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5490 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5491 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5492 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5493 | continue; |
| 5494 | } |
| 5495 | |
| 5496 | /* UTF-16 code pair: */ |
| 5497 | if (q > e) { |
| 5498 | errmsg = "unexpected end of data"; |
| 5499 | startinpos = (((const char *)q) - 2) - starts; |
| 5500 | endinpos = ((const char *)e) + 1 - starts; |
| 5501 | goto utf16Error; |
| 5502 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5503 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5504 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5505 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5506 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5507 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5508 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5509 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5510 | continue; |
| 5511 | } |
| 5512 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5513 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5514 | startinpos = (((const char *)q)-4)-starts; |
| 5515 | endinpos = startinpos+2; |
| 5516 | goto utf16Error; |
| 5517 | } |
| 5518 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5519 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5520 | errmsg = "illegal encoding"; |
| 5521 | startinpos = (((const char *)q)-2)-starts; |
| 5522 | endinpos = startinpos+2; |
| 5523 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5524 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5525 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5526 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5527 | errors, |
| 5528 | &errorHandler, |
| 5529 | "utf16", errmsg, |
| 5530 | &starts, |
| 5531 | (const char **)&e, |
| 5532 | &startinpos, |
| 5533 | &endinpos, |
| 5534 | &exc, |
| 5535 | (const char **)&q, |
| 5536 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5537 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5538 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5539 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5540 | /* remaining byte at the end? (size should be even) */ |
| 5541 | if (e == q) { |
| 5542 | if (!consumed) { |
| 5543 | errmsg = "truncated data"; |
| 5544 | startinpos = ((const char *)q) - starts; |
| 5545 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5546 | if (unicode_decode_call_errorhandler( |
| 5547 | errors, |
| 5548 | &errorHandler, |
| 5549 | "utf16", errmsg, |
| 5550 | &starts, |
| 5551 | (const char **)&e, |
| 5552 | &startinpos, |
| 5553 | &endinpos, |
| 5554 | &exc, |
| 5555 | (const char **)&q, |
| 5556 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5557 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5558 | goto onError; |
| 5559 | /* The remaining input chars are ignored if the callback |
| 5560 | chooses to skip the input */ |
| 5561 | } |
| 5562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | |
| 5564 | if (byteorder) |
| 5565 | *byteorder = bo; |
| 5566 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5567 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5568 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5571 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | goto onError; |
| 5573 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5574 | Py_XDECREF(errorHandler); |
| 5575 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5576 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5578 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5580 | Py_XDECREF(errorHandler); |
| 5581 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | return NULL; |
| 5583 | } |
| 5584 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5585 | #undef FAST_CHAR_MASK |
| 5586 | #undef SWAPPED_FAST_CHAR_MASK |
| 5587 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5588 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5589 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5590 | const char *errors, |
| 5591 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5593 | int kind; |
| 5594 | void *data; |
| 5595 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5596 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5597 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5598 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5599 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5600 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5601 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5602 | int ihi = 1, ilo = 0; |
| 5603 | #else |
| 5604 | int ihi = 0, ilo = 1; |
| 5605 | #endif |
| 5606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | #define STORECHAR(CH) \ |
| 5608 | do { \ |
| 5609 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5610 | p[ilo] = (CH) & 0xff; \ |
| 5611 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5612 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5614 | if (!PyUnicode_Check(str)) { |
| 5615 | PyErr_BadArgument(); |
| 5616 | return NULL; |
| 5617 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5618 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5619 | return NULL; |
| 5620 | kind = PyUnicode_KIND(str); |
| 5621 | data = PyUnicode_DATA(str); |
| 5622 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5623 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5624 | pairs = 0; |
| 5625 | if (kind == PyUnicode_4BYTE_KIND) |
| 5626 | for (i = 0; i < len; i++) |
| 5627 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5628 | pairs++; |
| 5629 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5630 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5631 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5632 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5633 | bytesize = nsize * 2; |
| 5634 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5635 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5636 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5637 | if (v == NULL) |
| 5638 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5640 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5642 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5643 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5644 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5645 | |
| 5646 | if (byteorder == -1) { |
| 5647 | /* force LE */ |
| 5648 | ihi = 1; |
| 5649 | ilo = 0; |
| 5650 | } |
| 5651 | else if (byteorder == 1) { |
| 5652 | /* force BE */ |
| 5653 | ihi = 0; |
| 5654 | ilo = 1; |
| 5655 | } |
| 5656 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5657 | for (i = 0; i < len; i++) { |
| 5658 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5659 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5660 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5661 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5662 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5663 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5664 | STORECHAR(ch); |
| 5665 | if (ch2) |
| 5666 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5667 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5668 | |
| 5669 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5670 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5671 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | } |
| 5673 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5674 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5675 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5676 | Py_ssize_t size, |
| 5677 | const char *errors, |
| 5678 | int byteorder) |
| 5679 | { |
| 5680 | PyObject *result; |
| 5681 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5682 | if (tmp == NULL) |
| 5683 | return NULL; |
| 5684 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5685 | Py_DECREF(tmp); |
| 5686 | return result; |
| 5687 | } |
| 5688 | |
| 5689 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5690 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5692 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5697 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5698 | if all the escapes in the string make it still a valid ASCII string. |
| 5699 | Returns -1 if any escapes were found which cause the string to |
| 5700 | pop out of ASCII range. Otherwise returns the length of the |
| 5701 | required buffer to hold the string. |
| 5702 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5703 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5705 | { |
| 5706 | const unsigned char *p = (const unsigned char *)s; |
| 5707 | const unsigned char *end = p + size; |
| 5708 | Py_ssize_t length = 0; |
| 5709 | |
| 5710 | if (size < 0) |
| 5711 | return -1; |
| 5712 | |
| 5713 | for (; p < end; ++p) { |
| 5714 | if (*p > 127) { |
| 5715 | /* Non-ASCII */ |
| 5716 | return -1; |
| 5717 | } |
| 5718 | else if (*p != '\\') { |
| 5719 | /* Normal character */ |
| 5720 | ++length; |
| 5721 | } |
| 5722 | else { |
| 5723 | /* Backslash-escape, check next char */ |
| 5724 | ++p; |
| 5725 | /* Escape sequence reaches till end of string or |
| 5726 | non-ASCII follow-up. */ |
| 5727 | if (p >= end || *p > 127) |
| 5728 | return -1; |
| 5729 | switch (*p) { |
| 5730 | case '\n': |
| 5731 | /* backslash + \n result in zero characters */ |
| 5732 | break; |
| 5733 | case '\\': case '\'': case '\"': |
| 5734 | case 'b': case 'f': case 't': |
| 5735 | case 'n': case 'r': case 'v': case 'a': |
| 5736 | ++length; |
| 5737 | break; |
| 5738 | case '0': case '1': case '2': case '3': |
| 5739 | case '4': case '5': case '6': case '7': |
| 5740 | case 'x': case 'u': case 'U': case 'N': |
| 5741 | /* these do not guarantee ASCII characters */ |
| 5742 | return -1; |
| 5743 | default: |
| 5744 | /* count the backslash + the other character */ |
| 5745 | length += 2; |
| 5746 | } |
| 5747 | } |
| 5748 | } |
| 5749 | return length; |
| 5750 | } |
| 5751 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5752 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5754 | PyObject * |
| 5755 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5756 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5757 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5759 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5760 | Py_ssize_t startinpos; |
| 5761 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5762 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5763 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5765 | char* message; |
| 5766 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5767 | PyObject *errorHandler = NULL; |
| 5768 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5769 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5770 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5771 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5772 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5773 | |
| 5774 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5775 | either the string is pure ASCII with named escapes like \n, etc. |
| 5776 | and we determined it's exact size (common case) |
| 5777 | or it contains \x, \u, ... escape sequences. then we create a |
| 5778 | 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] | 5779 | if (len >= 0) { |
| 5780 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5781 | if (!v) |
| 5782 | goto onError; |
| 5783 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5784 | } |
| 5785 | else { |
| 5786 | /* Escaped strings will always be longer than the resulting |
| 5787 | Unicode string, so we start with size here and then reduce the |
| 5788 | length after conversion to the true value. |
| 5789 | (but if the error callback returns a long replacement string |
| 5790 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5791 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5792 | if (!v) |
| 5793 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5794 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5798 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5799 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | while (s < end) { |
| 5803 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5804 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5805 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5807 | /* The only case in which i == ascii_length is a backslash |
| 5808 | followed by a newline. */ |
| 5809 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5812 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5813 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5814 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5815 | continue; |
| 5816 | } |
| 5817 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5818 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | /* \ - Escapes */ |
| 5820 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5821 | c = *s++; |
| 5822 | if (s > end) |
| 5823 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5824 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5825 | /* The only case in which i == ascii_length is a backslash |
| 5826 | followed by a newline. */ |
| 5827 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5828 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5829 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5831 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5832 | #define WRITECHAR(ch) \ |
| 5833 | do { \ |
| 5834 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5835 | goto onError; \ |
| 5836 | }while(0) |
| 5837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5839 | case '\\': WRITECHAR('\\'); break; |
| 5840 | case '\'': WRITECHAR('\''); break; |
| 5841 | case '\"': WRITECHAR('\"'); break; |
| 5842 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5843 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5844 | case 'f': WRITECHAR('\014'); break; |
| 5845 | case 't': WRITECHAR('\t'); break; |
| 5846 | case 'n': WRITECHAR('\n'); break; |
| 5847 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5848 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5849 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5850 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5851 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5852 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5853 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | case '0': case '1': case '2': case '3': |
| 5855 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5856 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5857 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5858 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5859 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5860 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5862 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | break; |
| 5864 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | /* hex escapes */ |
| 5866 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5868 | digits = 2; |
| 5869 | message = "truncated \\xXX escape"; |
| 5870 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5874 | digits = 4; |
| 5875 | message = "truncated \\uXXXX escape"; |
| 5876 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5879 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5880 | digits = 8; |
| 5881 | message = "truncated \\UXXXXXXXX escape"; |
| 5882 | hexescape: |
| 5883 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5884 | if (s+digits>end) { |
| 5885 | endinpos = size; |
| 5886 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5887 | errors, &errorHandler, |
| 5888 | "unicodeescape", "end of string in escape sequence", |
| 5889 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5890 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5891 | goto onError; |
| 5892 | goto nextByte; |
| 5893 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5894 | for (j = 0; j < digits; ++j) { |
| 5895 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5896 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5897 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5898 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5899 | errors, &errorHandler, |
| 5900 | "unicodeescape", message, |
| 5901 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5902 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5903 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5904 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5905 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5906 | } |
| 5907 | chr = (chr<<4) & ~0xF; |
| 5908 | if (c >= '0' && c <= '9') |
| 5909 | chr += c - '0'; |
| 5910 | else if (c >= 'a' && c <= 'f') |
| 5911 | chr += 10 + c - 'a'; |
| 5912 | else |
| 5913 | chr += 10 + c - 'A'; |
| 5914 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5915 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5916 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5917 | /* _decoding_error will have already written into the |
| 5918 | target buffer. */ |
| 5919 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5920 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5921 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5922 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5923 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5924 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5925 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5926 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5927 | errors, &errorHandler, |
| 5928 | "unicodeescape", "illegal Unicode character", |
| 5929 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5930 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5931 | goto onError; |
| 5932 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5933 | break; |
| 5934 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5936 | case 'N': |
| 5937 | message = "malformed \\N character escape"; |
| 5938 | if (ucnhash_CAPI == NULL) { |
| 5939 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5940 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5941 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5942 | if (ucnhash_CAPI == NULL) |
| 5943 | goto ucnhashError; |
| 5944 | } |
| 5945 | if (*s == '{') { |
| 5946 | const char *start = s+1; |
| 5947 | /* look for the closing brace */ |
| 5948 | while (*s != '}' && s < end) |
| 5949 | s++; |
| 5950 | if (s > start && s < end && *s == '}') { |
| 5951 | /* found a name. look it up in the unicode database */ |
| 5952 | message = "unknown Unicode character name"; |
| 5953 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5954 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5955 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5956 | goto store; |
| 5957 | } |
| 5958 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5959 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5960 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5961 | errors, &errorHandler, |
| 5962 | "unicodeescape", message, |
| 5963 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5964 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5965 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5966 | break; |
| 5967 | |
| 5968 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5969 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5970 | message = "\\ at end of string"; |
| 5971 | s--; |
| 5972 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5973 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5974 | errors, &errorHandler, |
| 5975 | "unicodeescape", message, |
| 5976 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5977 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5978 | goto onError; |
| 5979 | } |
| 5980 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5981 | WRITECHAR('\\'); |
| 5982 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5983 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5984 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5987 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5989 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5990 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5991 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5992 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5993 | Py_XDECREF(errorHandler); |
| 5994 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5995 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5996 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5997 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5998 | PyErr_SetString( |
| 5999 | PyExc_UnicodeError, |
| 6000 | "\\N escapes not supported (can't load unicodedata module)" |
| 6001 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6002 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6003 | Py_XDECREF(errorHandler); |
| 6004 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6005 | return NULL; |
| 6006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6009 | Py_XDECREF(errorHandler); |
| 6010 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | return NULL; |
| 6012 | } |
| 6013 | |
| 6014 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6015 | |
| 6016 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6017 | appropriate. |
| 6018 | |
| 6019 | */ |
| 6020 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6021 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6022 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6024 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6025 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6027 | int kind; |
| 6028 | void *data; |
| 6029 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6031 | /* Initial allocation is based on the longest-possible unichr |
| 6032 | escape. |
| 6033 | |
| 6034 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6035 | unichr, so in this case it's the longest unichr escape. In |
| 6036 | narrow (UTF-16) builds this is five chars per source unichr |
| 6037 | since there are two unichrs in the surrogate pair, so in narrow |
| 6038 | (UTF-16) builds it's not the longest unichr escape. |
| 6039 | |
| 6040 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6041 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6042 | escape. |
| 6043 | */ |
| 6044 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6045 | if (!PyUnicode_Check(unicode)) { |
| 6046 | PyErr_BadArgument(); |
| 6047 | return NULL; |
| 6048 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6049 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6050 | return NULL; |
| 6051 | len = PyUnicode_GET_LENGTH(unicode); |
| 6052 | kind = PyUnicode_KIND(unicode); |
| 6053 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6054 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6055 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6056 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6057 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6058 | } |
| 6059 | |
| 6060 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6061 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6062 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6063 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6064 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6065 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6066 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6067 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6068 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6069 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | if (repr == NULL) |
| 6071 | return NULL; |
| 6072 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6073 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6075 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6076 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6077 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6078 | /* Escape backslashes */ |
| 6079 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | *p++ = '\\'; |
| 6081 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6082 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6083 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6084 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6085 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6086 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6087 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6088 | *p++ = '\\'; |
| 6089 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6090 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6091 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6092 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6093 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6094 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6095 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6096 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6097 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6098 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6099 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6100 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6102 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | *p++ = '\\'; |
| 6104 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6105 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6106 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6107 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6108 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6110 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6111 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6112 | else if (ch == '\t') { |
| 6113 | *p++ = '\\'; |
| 6114 | *p++ = 't'; |
| 6115 | } |
| 6116 | else if (ch == '\n') { |
| 6117 | *p++ = '\\'; |
| 6118 | *p++ = 'n'; |
| 6119 | } |
| 6120 | else if (ch == '\r') { |
| 6121 | *p++ = '\\'; |
| 6122 | *p++ = 'r'; |
| 6123 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6124 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6125 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6126 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6128 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6129 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6130 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6131 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | /* Copy everything else as-is */ |
| 6134 | else |
| 6135 | *p++ = (char) ch; |
| 6136 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6138 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6139 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6140 | return NULL; |
| 6141 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6144 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6145 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6146 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6148 | PyObject *result; |
| 6149 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6150 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6152 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6153 | Py_DECREF(tmp); |
| 6154 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6158 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6159 | PyObject * |
| 6160 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6161 | Py_ssize_t size, |
| 6162 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6164 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6165 | Py_ssize_t startinpos; |
| 6166 | Py_ssize_t endinpos; |
| 6167 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6168 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | const char *end; |
| 6170 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6171 | PyObject *errorHandler = NULL; |
| 6172 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6174 | /* Escaped strings will always be longer than the resulting |
| 6175 | 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] | 6176 | length after conversion to the true value. (But decoding error |
| 6177 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6178 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6182 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6183 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | end = s + size; |
| 6185 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6186 | unsigned char c; |
| 6187 | Py_UCS4 x; |
| 6188 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6189 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6192 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6193 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6194 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6195 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6196 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6197 | startinpos = s-starts; |
| 6198 | |
| 6199 | /* \u-escapes are only interpreted iff the number of leading |
| 6200 | backslashes if odd */ |
| 6201 | bs = s; |
| 6202 | for (;s < end;) { |
| 6203 | if (*s != '\\') |
| 6204 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6205 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6206 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6207 | } |
| 6208 | if (((s - bs) & 1) == 0 || |
| 6209 | s >= end || |
| 6210 | (*s != 'u' && *s != 'U')) { |
| 6211 | continue; |
| 6212 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6213 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | count = *s=='u' ? 4 : 8; |
| 6215 | s++; |
| 6216 | |
| 6217 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6218 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6219 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6220 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6221 | endinpos = s-starts; |
| 6222 | if (unicode_decode_call_errorhandler( |
| 6223 | errors, &errorHandler, |
| 6224 | "rawunicodeescape", "truncated \\uXXXX", |
| 6225 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6226 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | goto onError; |
| 6228 | goto nextByte; |
| 6229 | } |
| 6230 | x = (x<<4) & ~0xF; |
| 6231 | if (c >= '0' && c <= '9') |
| 6232 | x += c - '0'; |
| 6233 | else if (c >= 'a' && c <= 'f') |
| 6234 | x += 10 + c - 'a'; |
| 6235 | else |
| 6236 | x += 10 + c - 'A'; |
| 6237 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6238 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6239 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6240 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6241 | } else { |
| 6242 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6243 | if (unicode_decode_call_errorhandler( |
| 6244 | errors, &errorHandler, |
| 6245 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6246 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6247 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6249 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | nextByte: |
| 6251 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6253 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6254 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6255 | Py_XDECREF(errorHandler); |
| 6256 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6257 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6258 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6261 | Py_XDECREF(errorHandler); |
| 6262 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6263 | return NULL; |
| 6264 | } |
| 6265 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6266 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6267 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6268 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6270 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | char *p; |
| 6272 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6273 | Py_ssize_t expandsize, pos; |
| 6274 | int kind; |
| 6275 | void *data; |
| 6276 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6278 | if (!PyUnicode_Check(unicode)) { |
| 6279 | PyErr_BadArgument(); |
| 6280 | return NULL; |
| 6281 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6282 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6283 | return NULL; |
| 6284 | kind = PyUnicode_KIND(unicode); |
| 6285 | data = PyUnicode_DATA(unicode); |
| 6286 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6287 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6288 | bytes, and 1 byte characters 4. */ |
| 6289 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6290 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6291 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6293 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6294 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | if (repr == NULL) |
| 6296 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6297 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6298 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6300 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6301 | for (pos = 0; pos < len; pos++) { |
| 6302 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6304 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6305 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6306 | *p++ = '\\'; |
| 6307 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6308 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6309 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6310 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6311 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6312 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6313 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6314 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6315 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6316 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6318 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6319 | *p++ = '\\'; |
| 6320 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6321 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6322 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6323 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6324 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6325 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6326 | /* Copy everything else as-is */ |
| 6327 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6328 | *p++ = (char) ch; |
| 6329 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6330 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6331 | assert(p > q); |
| 6332 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6333 | return NULL; |
| 6334 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6337 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6338 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6339 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6340 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6341 | PyObject *result; |
| 6342 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6343 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6344 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6345 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6346 | Py_DECREF(tmp); |
| 6347 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6350 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6351 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6352 | PyObject * |
| 6353 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6354 | Py_ssize_t size, |
| 6355 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6356 | { |
| 6357 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6358 | Py_ssize_t startinpos; |
| 6359 | Py_ssize_t endinpos; |
| 6360 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6361 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6362 | const char *end; |
| 6363 | const char *reason; |
| 6364 | PyObject *errorHandler = NULL; |
| 6365 | PyObject *exc = NULL; |
| 6366 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6367 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6368 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6369 | 1)) |
| 6370 | return NULL; |
| 6371 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6372 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6373 | 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] | 6374 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6376 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6377 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6378 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6379 | end = s + size; |
| 6380 | |
| 6381 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6382 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6383 | Py_UCS4 ch; |
| 6384 | /* We copy the raw representation one byte at a time because the |
| 6385 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6386 | ((char *) &uch)[0] = s[0]; |
| 6387 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6388 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6389 | ((char *) &uch)[2] = s[2]; |
| 6390 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6391 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6392 | ch = uch; |
| 6393 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6394 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6395 | some malformed UCS-4 data. */ |
| 6396 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6397 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6398 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6399 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6400 | end-s < Py_UNICODE_SIZE |
| 6401 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6402 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6403 | startinpos = s - starts; |
| 6404 | if (end-s < Py_UNICODE_SIZE) { |
| 6405 | endinpos = end-starts; |
| 6406 | reason = "truncated input"; |
| 6407 | } |
| 6408 | else { |
| 6409 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6410 | reason = "illegal code point (> 0x10FFFF)"; |
| 6411 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6412 | if (unicode_decode_call_errorhandler( |
| 6413 | errors, &errorHandler, |
| 6414 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6415 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6416 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6417 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6418 | continue; |
| 6419 | } |
| 6420 | |
| 6421 | s += Py_UNICODE_SIZE; |
| 6422 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6423 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6424 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6425 | Py_UNICODE uch2; |
| 6426 | ((char *) &uch2)[0] = s[0]; |
| 6427 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6428 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6429 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6430 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6431 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6432 | } |
| 6433 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6434 | #endif |
| 6435 | |
| 6436 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6437 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6440 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6441 | goto onError; |
| 6442 | Py_XDECREF(errorHandler); |
| 6443 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6444 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6447 | Py_XDECREF(v); |
| 6448 | Py_XDECREF(errorHandler); |
| 6449 | Py_XDECREF(exc); |
| 6450 | return NULL; |
| 6451 | } |
| 6452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6454 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6455 | PyObject * |
| 6456 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6457 | Py_ssize_t size, |
| 6458 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6461 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6464 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6465 | static void |
| 6466 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6467 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6468 | PyObject *unicode, |
| 6469 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6470 | const char *reason) |
| 6471 | { |
| 6472 | if (*exceptionObject == NULL) { |
| 6473 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6474 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6475 | encoding, unicode, startpos, endpos, reason); |
| 6476 | } |
| 6477 | else { |
| 6478 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6479 | goto onError; |
| 6480 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6481 | goto onError; |
| 6482 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6483 | goto onError; |
| 6484 | return; |
| 6485 | onError: |
| 6486 | Py_DECREF(*exceptionObject); |
| 6487 | *exceptionObject = NULL; |
| 6488 | } |
| 6489 | } |
| 6490 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6491 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6492 | static void |
| 6493 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6494 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6495 | PyObject *unicode, |
| 6496 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6497 | const char *reason) |
| 6498 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6499 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6500 | encoding, unicode, startpos, endpos, reason); |
| 6501 | if (*exceptionObject != NULL) |
| 6502 | PyCodec_StrictErrors(*exceptionObject); |
| 6503 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6504 | |
| 6505 | /* error handling callback helper: |
| 6506 | build arguments, call the callback and check the arguments, |
| 6507 | put the result into newpos and return the replacement string, which |
| 6508 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6509 | static PyObject * |
| 6510 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6511 | PyObject **errorHandler, |
| 6512 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6513 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6514 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6515 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6516 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6517 | 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] | 6518 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6519 | PyObject *restuple; |
| 6520 | PyObject *resunicode; |
| 6521 | |
| 6522 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6524 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6525 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6528 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6529 | return NULL; |
| 6530 | len = PyUnicode_GET_LENGTH(unicode); |
| 6531 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6532 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6533 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6534 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6536 | |
| 6537 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6539 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6540 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6541 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6542 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | Py_DECREF(restuple); |
| 6544 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6545 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6546 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6547 | &resunicode, newpos)) { |
| 6548 | Py_DECREF(restuple); |
| 6549 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6550 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6551 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6552 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6553 | Py_DECREF(restuple); |
| 6554 | return NULL; |
| 6555 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6556 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6557 | *newpos = len + *newpos; |
| 6558 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6559 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6560 | Py_DECREF(restuple); |
| 6561 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6562 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6563 | Py_INCREF(resunicode); |
| 6564 | Py_DECREF(restuple); |
| 6565 | return resunicode; |
| 6566 | } |
| 6567 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6568 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6569 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6570 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6571 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6572 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6573 | /* input state */ |
| 6574 | Py_ssize_t pos=0, size; |
| 6575 | int kind; |
| 6576 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6577 | /* output object */ |
| 6578 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6579 | /* pointer into the output */ |
| 6580 | char *str; |
| 6581 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6582 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6583 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6584 | 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] | 6585 | PyObject *errorHandler = NULL; |
| 6586 | PyObject *exc = NULL; |
| 6587 | /* the following variable is used for caching string comparisons |
| 6588 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6589 | int known_errorHandler = -1; |
| 6590 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6591 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6592 | return NULL; |
| 6593 | size = PyUnicode_GET_LENGTH(unicode); |
| 6594 | kind = PyUnicode_KIND(unicode); |
| 6595 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6596 | /* allocate enough for a simple encoding without |
| 6597 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6598 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6599 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6600 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6601 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6602 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6603 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6604 | ressize = size; |
| 6605 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6606 | while (pos < size) { |
| 6607 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6608 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6609 | /* can we encode this? */ |
| 6610 | if (c<limit) { |
| 6611 | /* no overflow check, because we know that the space is enough */ |
| 6612 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6613 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6614 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6616 | Py_ssize_t requiredsize; |
| 6617 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6618 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6620 | Py_ssize_t collstart = pos; |
| 6621 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6622 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6623 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6624 | ++collend; |
| 6625 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6626 | if (known_errorHandler==-1) { |
| 6627 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6628 | known_errorHandler = 1; |
| 6629 | else if (!strcmp(errors, "replace")) |
| 6630 | known_errorHandler = 2; |
| 6631 | else if (!strcmp(errors, "ignore")) |
| 6632 | known_errorHandler = 3; |
| 6633 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6634 | known_errorHandler = 4; |
| 6635 | else |
| 6636 | known_errorHandler = 0; |
| 6637 | } |
| 6638 | switch (known_errorHandler) { |
| 6639 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6640 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6641 | goto onError; |
| 6642 | case 2: /* replace */ |
| 6643 | while (collstart++<collend) |
| 6644 | *str++ = '?'; /* fall through */ |
| 6645 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6646 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6647 | break; |
| 6648 | case 4: /* xmlcharrefreplace */ |
| 6649 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6650 | /* determine replacement size */ |
| 6651 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6652 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6653 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6654 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6655 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6656 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6657 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6658 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6659 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6660 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6661 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6662 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6663 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6664 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6665 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6666 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6668 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6669 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6670 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6671 | if (requiredsize > ressize) { |
| 6672 | if (requiredsize<2*ressize) |
| 6673 | requiredsize = 2*ressize; |
| 6674 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6675 | goto onError; |
| 6676 | str = PyBytes_AS_STRING(res) + respos; |
| 6677 | ressize = requiredsize; |
| 6678 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6679 | /* generate replacement */ |
| 6680 | for (i = collstart; i < collend; ++i) { |
| 6681 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6682 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6683 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6684 | break; |
| 6685 | default: |
| 6686 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6687 | encoding, reason, unicode, &exc, |
| 6688 | collstart, collend, &newpos); |
| 6689 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6690 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6691 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6692 | if (PyBytes_Check(repunicode)) { |
| 6693 | /* Directly copy bytes result to output. */ |
| 6694 | repsize = PyBytes_Size(repunicode); |
| 6695 | if (repsize > 1) { |
| 6696 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6697 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6698 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6699 | Py_DECREF(repunicode); |
| 6700 | goto onError; |
| 6701 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6702 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6703 | ressize += repsize-1; |
| 6704 | } |
| 6705 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6706 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6707 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6708 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6709 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6710 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6711 | /* need more space? (at least enough for what we |
| 6712 | have+the replacement+the rest of the string, so |
| 6713 | we won't have to check space for encodable characters) */ |
| 6714 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6715 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6716 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6717 | if (requiredsize > ressize) { |
| 6718 | if (requiredsize<2*ressize) |
| 6719 | requiredsize = 2*ressize; |
| 6720 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6721 | Py_DECREF(repunicode); |
| 6722 | goto onError; |
| 6723 | } |
| 6724 | str = PyBytes_AS_STRING(res) + respos; |
| 6725 | ressize = requiredsize; |
| 6726 | } |
| 6727 | /* check if there is anything unencodable in the replacement |
| 6728 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6729 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6730 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6731 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6732 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6733 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6734 | Py_DECREF(repunicode); |
| 6735 | goto onError; |
| 6736 | } |
| 6737 | *str = (char)c; |
| 6738 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6739 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6740 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6741 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6742 | } |
| 6743 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6744 | /* Resize if we allocated to much */ |
| 6745 | size = str - PyBytes_AS_STRING(res); |
| 6746 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6747 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6748 | if (_PyBytes_Resize(&res, size) < 0) |
| 6749 | goto onError; |
| 6750 | } |
| 6751 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6752 | Py_XDECREF(errorHandler); |
| 6753 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6754 | return res; |
| 6755 | |
| 6756 | onError: |
| 6757 | Py_XDECREF(res); |
| 6758 | Py_XDECREF(errorHandler); |
| 6759 | Py_XDECREF(exc); |
| 6760 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6763 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6764 | PyObject * |
| 6765 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6766 | Py_ssize_t size, |
| 6767 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6769 | PyObject *result; |
| 6770 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6771 | if (unicode == NULL) |
| 6772 | return NULL; |
| 6773 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6774 | Py_DECREF(unicode); |
| 6775 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | } |
| 6777 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6778 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6779 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | { |
| 6781 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | PyErr_BadArgument(); |
| 6783 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6785 | if (PyUnicode_READY(unicode) == -1) |
| 6786 | return NULL; |
| 6787 | /* Fast path: if it is a one-byte string, construct |
| 6788 | bytes object directly. */ |
| 6789 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6790 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6791 | PyUnicode_GET_LENGTH(unicode)); |
| 6792 | /* Non-Latin-1 characters present. Defer to above function to |
| 6793 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6794 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | PyObject* |
| 6798 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6799 | { |
| 6800 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6805 | PyObject * |
| 6806 | PyUnicode_DecodeASCII(const char *s, |
| 6807 | Py_ssize_t size, |
| 6808 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6809 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6810 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6811 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6812 | int kind; |
| 6813 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6814 | Py_ssize_t startinpos; |
| 6815 | Py_ssize_t endinpos; |
| 6816 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6817 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6818 | int has_error; |
| 6819 | const unsigned char *p = (const unsigned char *)s; |
| 6820 | const unsigned char *end = p + size; |
| 6821 | 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] | 6822 | PyObject *errorHandler = NULL; |
| 6823 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6824 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6825 | if (size == 0) { |
| 6826 | Py_INCREF(unicode_empty); |
| 6827 | return unicode_empty; |
| 6828 | } |
| 6829 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6831 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6832 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6833 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6834 | has_error = 0; |
| 6835 | while (p < end && !has_error) { |
| 6836 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6837 | an explanation. */ |
| 6838 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6839 | /* Help register allocation */ |
| 6840 | register const unsigned char *_p = p; |
| 6841 | while (_p < aligned_end) { |
| 6842 | unsigned long value = *(unsigned long *) _p; |
| 6843 | if (value & ASCII_CHAR_MASK) { |
| 6844 | has_error = 1; |
| 6845 | break; |
| 6846 | } |
| 6847 | _p += SIZEOF_LONG; |
| 6848 | } |
| 6849 | if (_p == end) |
| 6850 | break; |
| 6851 | if (has_error) |
| 6852 | break; |
| 6853 | p = _p; |
| 6854 | } |
| 6855 | if (*p & 0x80) { |
| 6856 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6857 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6858 | } |
| 6859 | else { |
| 6860 | ++p; |
| 6861 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6862 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6863 | if (!has_error) |
| 6864 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6865 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6866 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6868 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6869 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6870 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6871 | kind = PyUnicode_KIND(v); |
| 6872 | data = PyUnicode_DATA(v); |
| 6873 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6874 | e = s + size; |
| 6875 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6876 | register unsigned char c = (unsigned char)*s; |
| 6877 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6878 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6879 | ++s; |
| 6880 | } |
| 6881 | else { |
| 6882 | startinpos = s-starts; |
| 6883 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6884 | if (unicode_decode_call_errorhandler( |
| 6885 | errors, &errorHandler, |
| 6886 | "ascii", "ordinal not in range(128)", |
| 6887 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6888 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6890 | kind = PyUnicode_KIND(v); |
| 6891 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6894 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6895 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6896 | Py_XDECREF(errorHandler); |
| 6897 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6898 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6899 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6902 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6903 | Py_XDECREF(errorHandler); |
| 6904 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | return NULL; |
| 6906 | } |
| 6907 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6908 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6909 | PyObject * |
| 6910 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6911 | Py_ssize_t size, |
| 6912 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6914 | PyObject *result; |
| 6915 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6916 | if (unicode == NULL) |
| 6917 | return NULL; |
| 6918 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6919 | Py_DECREF(unicode); |
| 6920 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6923 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6924 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6925 | { |
| 6926 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6927 | PyErr_BadArgument(); |
| 6928 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6930 | if (PyUnicode_READY(unicode) == -1) |
| 6931 | return NULL; |
| 6932 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6933 | directly. Else defer to above function to raise the exception. */ |
| 6934 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6935 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6936 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6937 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6938 | } |
| 6939 | |
| 6940 | PyObject * |
| 6941 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6942 | { |
| 6943 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6946 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6947 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6948 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6949 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6950 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6951 | #define NEED_RETRY |
| 6952 | #endif |
| 6953 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6954 | #ifndef WC_ERR_INVALID_CHARS |
| 6955 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6956 | #endif |
| 6957 | |
| 6958 | static char* |
| 6959 | code_page_name(UINT code_page, PyObject **obj) |
| 6960 | { |
| 6961 | *obj = NULL; |
| 6962 | if (code_page == CP_ACP) |
| 6963 | return "mbcs"; |
| 6964 | if (code_page == CP_UTF7) |
| 6965 | return "CP_UTF7"; |
| 6966 | if (code_page == CP_UTF8) |
| 6967 | return "CP_UTF8"; |
| 6968 | |
| 6969 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6970 | if (*obj == NULL) |
| 6971 | return NULL; |
| 6972 | return PyBytes_AS_STRING(*obj); |
| 6973 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6974 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6975 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6976 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6977 | { |
| 6978 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6980 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6981 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6982 | return 0; |
| 6983 | |
| 6984 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6985 | if (prev == curr) |
| 6986 | return 1; |
| 6987 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6988 | as it assumes an incomplete character consists of a single |
| 6989 | byte. */ |
| 6990 | if (curr - prev == 2) |
| 6991 | return 1; |
| 6992 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6993 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6994 | return 0; |
| 6995 | } |
| 6996 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6997 | static DWORD |
| 6998 | decode_code_page_flags(UINT code_page) |
| 6999 | { |
| 7000 | if (code_page == CP_UTF7) { |
| 7001 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7002 | return 0; |
| 7003 | } |
| 7004 | else |
| 7005 | return MB_ERR_INVALID_CHARS; |
| 7006 | } |
| 7007 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7008 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7009 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7010 | * mode. |
| 7011 | * |
| 7012 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7013 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7015 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7016 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7017 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7018 | const char *in, |
| 7019 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7020 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7021 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7022 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7023 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7024 | |
| 7025 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 | assert(insize > 0); |
| 7027 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7028 | if (outsize <= 0) |
| 7029 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7030 | |
| 7031 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7032 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7033 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7034 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7035 | if (*v == NULL) |
| 7036 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7038 | } |
| 7039 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7040 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7041 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7042 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7043 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7045 | } |
| 7046 | |
| 7047 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7049 | if (outsize <= 0) |
| 7050 | goto error; |
| 7051 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7052 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7053 | error: |
| 7054 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7055 | return -2; |
| 7056 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7057 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7060 | /* |
| 7061 | * Decode a byte string from a code page into unicode object with an error |
| 7062 | * handler. |
| 7063 | * |
| 7064 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7065 | * UnicodeDecodeError exception and returns -1 on error. |
| 7066 | */ |
| 7067 | static int |
| 7068 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7069 | PyObject **v, |
| 7070 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | const char *errors) |
| 7072 | { |
| 7073 | const char *startin = in; |
| 7074 | const char *endin = in + size; |
| 7075 | const DWORD flags = decode_code_page_flags(code_page); |
| 7076 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7077 | 2000 English version of the message. */ |
| 7078 | const char *reason = "No mapping for the Unicode character exists " |
| 7079 | "in the target code page."; |
| 7080 | /* each step cannot decode more than 1 character, but a character can be |
| 7081 | represented as a surrogate pair */ |
| 7082 | wchar_t buffer[2], *startout, *out; |
| 7083 | int insize, outsize; |
| 7084 | PyObject *errorHandler = NULL; |
| 7085 | PyObject *exc = NULL; |
| 7086 | PyObject *encoding_obj = NULL; |
| 7087 | char *encoding; |
| 7088 | DWORD err; |
| 7089 | int ret = -1; |
| 7090 | |
| 7091 | assert(size > 0); |
| 7092 | |
| 7093 | encoding = code_page_name(code_page, &encoding_obj); |
| 7094 | if (encoding == NULL) |
| 7095 | return -1; |
| 7096 | |
| 7097 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7098 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7099 | UnicodeDecodeError. */ |
| 7100 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7101 | if (exc != NULL) { |
| 7102 | PyCodec_StrictErrors(exc); |
| 7103 | Py_CLEAR(exc); |
| 7104 | } |
| 7105 | goto error; |
| 7106 | } |
| 7107 | |
| 7108 | if (*v == NULL) { |
| 7109 | /* Create unicode object */ |
| 7110 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7111 | PyErr_NoMemory(); |
| 7112 | goto error; |
| 7113 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7114 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7115 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7116 | if (*v == NULL) |
| 7117 | goto error; |
| 7118 | startout = PyUnicode_AS_UNICODE(*v); |
| 7119 | } |
| 7120 | else { |
| 7121 | /* Extend unicode object */ |
| 7122 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7123 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7124 | PyErr_NoMemory(); |
| 7125 | goto error; |
| 7126 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7127 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7128 | goto error; |
| 7129 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7130 | } |
| 7131 | |
| 7132 | /* Decode the byte string character per character */ |
| 7133 | out = startout; |
| 7134 | while (in < endin) |
| 7135 | { |
| 7136 | /* Decode a character */ |
| 7137 | insize = 1; |
| 7138 | do |
| 7139 | { |
| 7140 | outsize = MultiByteToWideChar(code_page, flags, |
| 7141 | in, insize, |
| 7142 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7143 | if (outsize > 0) |
| 7144 | break; |
| 7145 | err = GetLastError(); |
| 7146 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7147 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7148 | { |
| 7149 | PyErr_SetFromWindowsErr(0); |
| 7150 | goto error; |
| 7151 | } |
| 7152 | insize++; |
| 7153 | } |
| 7154 | /* 4=maximum length of a UTF-8 sequence */ |
| 7155 | while (insize <= 4 && (in + insize) <= endin); |
| 7156 | |
| 7157 | if (outsize <= 0) { |
| 7158 | Py_ssize_t startinpos, endinpos, outpos; |
| 7159 | |
| 7160 | startinpos = in - startin; |
| 7161 | endinpos = startinpos + 1; |
| 7162 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7163 | if (unicode_decode_call_errorhandler( |
| 7164 | errors, &errorHandler, |
| 7165 | encoding, reason, |
| 7166 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7167 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7168 | { |
| 7169 | goto error; |
| 7170 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7171 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | } |
| 7173 | else { |
| 7174 | in += insize; |
| 7175 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7176 | out += outsize; |
| 7177 | } |
| 7178 | } |
| 7179 | |
| 7180 | /* write a NUL character at the end */ |
| 7181 | *out = 0; |
| 7182 | |
| 7183 | /* Extend unicode object */ |
| 7184 | outsize = out - startout; |
| 7185 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7186 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7187 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7188 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7189 | |
| 7190 | error: |
| 7191 | Py_XDECREF(encoding_obj); |
| 7192 | Py_XDECREF(errorHandler); |
| 7193 | Py_XDECREF(exc); |
| 7194 | return ret; |
| 7195 | } |
| 7196 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | static PyObject * |
| 7198 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7199 | const char *s, Py_ssize_t size, |
| 7200 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7201 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7202 | PyObject *v = NULL; |
| 7203 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7204 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | if (code_page < 0) { |
| 7206 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7207 | return NULL; |
| 7208 | } |
| 7209 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7210 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7211 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7212 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7213 | do |
| 7214 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7215 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7216 | if (size > INT_MAX) { |
| 7217 | chunk_size = INT_MAX; |
| 7218 | final = 0; |
| 7219 | done = 0; |
| 7220 | } |
| 7221 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7222 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7223 | { |
| 7224 | chunk_size = (int)size; |
| 7225 | final = (consumed == NULL); |
| 7226 | done = 1; |
| 7227 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7228 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7229 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7230 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7231 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7232 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7233 | if (chunk_size == 0 && done) { |
| 7234 | if (v != NULL) |
| 7235 | break; |
| 7236 | Py_INCREF(unicode_empty); |
| 7237 | return unicode_empty; |
| 7238 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7239 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7240 | |
| 7241 | converted = decode_code_page_strict(code_page, &v, |
| 7242 | s, chunk_size); |
| 7243 | if (converted == -2) |
| 7244 | converted = decode_code_page_errors(code_page, &v, |
| 7245 | s, chunk_size, |
| 7246 | errors); |
| 7247 | assert(converted != 0); |
| 7248 | |
| 7249 | if (converted < 0) { |
| 7250 | Py_XDECREF(v); |
| 7251 | return NULL; |
| 7252 | } |
| 7253 | |
| 7254 | if (consumed) |
| 7255 | *consumed += converted; |
| 7256 | |
| 7257 | s += converted; |
| 7258 | size -= converted; |
| 7259 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7260 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7261 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7264 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7265 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7266 | const char *s, |
| 7267 | Py_ssize_t size, |
| 7268 | const char *errors, |
| 7269 | Py_ssize_t *consumed) |
| 7270 | { |
| 7271 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7272 | } |
| 7273 | |
| 7274 | PyObject * |
| 7275 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7276 | Py_ssize_t size, |
| 7277 | const char *errors, |
| 7278 | Py_ssize_t *consumed) |
| 7279 | { |
| 7280 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7281 | } |
| 7282 | |
| 7283 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7284 | PyUnicode_DecodeMBCS(const char *s, |
| 7285 | Py_ssize_t size, |
| 7286 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7287 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7288 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7289 | } |
| 7290 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7291 | static DWORD |
| 7292 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7293 | { |
| 7294 | if (code_page == CP_UTF8) { |
| 7295 | if (winver.dwMajorVersion >= 6) |
| 7296 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7297 | and later */ |
| 7298 | return WC_ERR_INVALID_CHARS; |
| 7299 | else |
| 7300 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7301 | return 0; |
| 7302 | } |
| 7303 | else if (code_page == CP_UTF7) { |
| 7304 | /* CP_UTF7 only supports flags=0 */ |
| 7305 | return 0; |
| 7306 | } |
| 7307 | else { |
| 7308 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7309 | return 0; |
| 7310 | else |
| 7311 | return WC_NO_BEST_FIT_CHARS; |
| 7312 | } |
| 7313 | } |
| 7314 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7315 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7317 | * mode. |
| 7318 | * |
| 7319 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7320 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7321 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7322 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7323 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7324 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7325 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7326 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7327 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7329 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7330 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7331 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7332 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7333 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7334 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7335 | /* Create a substring so that we can get the UTF-16 representation |
| 7336 | of just the slice under consideration. */ |
| 7337 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7338 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7339 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7340 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7341 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7342 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7343 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7344 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7345 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7346 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7347 | if (substring == NULL) |
| 7348 | return -1; |
| 7349 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7350 | if (p == NULL) { |
| 7351 | Py_DECREF(substring); |
| 7352 | return -1; |
| 7353 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7354 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7355 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7356 | outsize = WideCharToMultiByte(code_page, flags, |
| 7357 | p, size, |
| 7358 | NULL, 0, |
| 7359 | NULL, pusedDefaultChar); |
| 7360 | if (outsize <= 0) |
| 7361 | goto error; |
| 7362 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7363 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7364 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7365 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7366 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7367 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7368 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7369 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7370 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7371 | if (*outbytes == NULL) { |
| 7372 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7373 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7374 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7375 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7376 | } |
| 7377 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7379 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7380 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7381 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7382 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7383 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7384 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7385 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7386 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7387 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7388 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7389 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7393 | outsize = WideCharToMultiByte(code_page, flags, |
| 7394 | p, size, |
| 7395 | out, outsize, |
| 7396 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7397 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7398 | if (outsize <= 0) |
| 7399 | goto error; |
| 7400 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7401 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7402 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7403 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7404 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7405 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7406 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7407 | return -2; |
| 7408 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7409 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7412 | /* |
| 7413 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7414 | * error handler. |
| 7415 | * |
| 7416 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7417 | * -1 on other error. |
| 7418 | */ |
| 7419 | static int |
| 7420 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7421 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7422 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7423 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7424 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7425 | Py_ssize_t pos = unicode_offset; |
| 7426 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7427 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7428 | 2000 English version of the message. */ |
| 7429 | const char *reason = "invalid character"; |
| 7430 | /* 4=maximum length of a UTF-8 sequence */ |
| 7431 | char buffer[4]; |
| 7432 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7433 | Py_ssize_t outsize; |
| 7434 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7435 | PyObject *errorHandler = NULL; |
| 7436 | PyObject *exc = NULL; |
| 7437 | PyObject *encoding_obj = NULL; |
| 7438 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7439 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7440 | PyObject *rep; |
| 7441 | int ret = -1; |
| 7442 | |
| 7443 | assert(insize > 0); |
| 7444 | |
| 7445 | encoding = code_page_name(code_page, &encoding_obj); |
| 7446 | if (encoding == NULL) |
| 7447 | return -1; |
| 7448 | |
| 7449 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7450 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7451 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7452 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7453 | if (exc != NULL) { |
| 7454 | PyCodec_StrictErrors(exc); |
| 7455 | Py_DECREF(exc); |
| 7456 | } |
| 7457 | Py_XDECREF(encoding_obj); |
| 7458 | return -1; |
| 7459 | } |
| 7460 | |
| 7461 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7462 | pusedDefaultChar = &usedDefaultChar; |
| 7463 | else |
| 7464 | pusedDefaultChar = NULL; |
| 7465 | |
| 7466 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7467 | PyErr_NoMemory(); |
| 7468 | goto error; |
| 7469 | } |
| 7470 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7471 | |
| 7472 | if (*outbytes == NULL) { |
| 7473 | /* Create string object */ |
| 7474 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7475 | if (*outbytes == NULL) |
| 7476 | goto error; |
| 7477 | out = PyBytes_AS_STRING(*outbytes); |
| 7478 | } |
| 7479 | else { |
| 7480 | /* Extend string object */ |
| 7481 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7482 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7483 | PyErr_NoMemory(); |
| 7484 | goto error; |
| 7485 | } |
| 7486 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7487 | goto error; |
| 7488 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7489 | } |
| 7490 | |
| 7491 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7492 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7493 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7494 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7495 | wchar_t chars[2]; |
| 7496 | int charsize; |
| 7497 | if (ch < 0x10000) { |
| 7498 | chars[0] = (wchar_t)ch; |
| 7499 | charsize = 1; |
| 7500 | } |
| 7501 | else { |
| 7502 | ch -= 0x10000; |
| 7503 | chars[0] = 0xd800 + (ch >> 10); |
| 7504 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7505 | charsize = 2; |
| 7506 | } |
| 7507 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7508 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7509 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7510 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7511 | NULL, pusedDefaultChar); |
| 7512 | if (outsize > 0) { |
| 7513 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7514 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7515 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7516 | memcpy(out, buffer, outsize); |
| 7517 | out += outsize; |
| 7518 | continue; |
| 7519 | } |
| 7520 | } |
| 7521 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7522 | PyErr_SetFromWindowsErr(0); |
| 7523 | goto error; |
| 7524 | } |
| 7525 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7526 | rep = unicode_encode_call_errorhandler( |
| 7527 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7528 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7529 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7530 | if (rep == NULL) |
| 7531 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7532 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7533 | |
| 7534 | if (PyBytes_Check(rep)) { |
| 7535 | outsize = PyBytes_GET_SIZE(rep); |
| 7536 | if (outsize != 1) { |
| 7537 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7538 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7539 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7540 | Py_DECREF(rep); |
| 7541 | goto error; |
| 7542 | } |
| 7543 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7544 | } |
| 7545 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7546 | out += outsize; |
| 7547 | } |
| 7548 | else { |
| 7549 | Py_ssize_t i; |
| 7550 | enum PyUnicode_Kind kind; |
| 7551 | void *data; |
| 7552 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7553 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7554 | Py_DECREF(rep); |
| 7555 | goto error; |
| 7556 | } |
| 7557 | |
| 7558 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7559 | if (outsize != 1) { |
| 7560 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7561 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7562 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7563 | Py_DECREF(rep); |
| 7564 | goto error; |
| 7565 | } |
| 7566 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7567 | } |
| 7568 | kind = PyUnicode_KIND(rep); |
| 7569 | data = PyUnicode_DATA(rep); |
| 7570 | for (i=0; i < outsize; i++) { |
| 7571 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7572 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7573 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7574 | encoding, unicode, |
| 7575 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7576 | "unable to encode error handler result to ASCII"); |
| 7577 | Py_DECREF(rep); |
| 7578 | goto error; |
| 7579 | } |
| 7580 | *out = (unsigned char)ch; |
| 7581 | out++; |
| 7582 | } |
| 7583 | } |
| 7584 | Py_DECREF(rep); |
| 7585 | } |
| 7586 | /* write a NUL byte */ |
| 7587 | *out = 0; |
| 7588 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7589 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7590 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7591 | goto error; |
| 7592 | ret = 0; |
| 7593 | |
| 7594 | error: |
| 7595 | Py_XDECREF(encoding_obj); |
| 7596 | Py_XDECREF(errorHandler); |
| 7597 | Py_XDECREF(exc); |
| 7598 | return ret; |
| 7599 | } |
| 7600 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7601 | static PyObject * |
| 7602 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7603 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7604 | const char *errors) |
| 7605 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7606 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7607 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7608 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7609 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7610 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7611 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7612 | return NULL; |
| 7613 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7614 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7615 | if (code_page < 0) { |
| 7616 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7617 | return NULL; |
| 7618 | } |
| 7619 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7620 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7621 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7622 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7623 | offset = 0; |
| 7624 | do |
| 7625 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7626 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7627 | /* 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] | 7628 | chunks. */ |
| 7629 | if (len > INT_MAX/2) { |
| 7630 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7631 | done = 0; |
| 7632 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7633 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7634 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7635 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7636 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7637 | done = 1; |
| 7638 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7639 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7640 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7641 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7642 | errors); |
| 7643 | if (ret == -2) |
| 7644 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7645 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7646 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7647 | if (ret < 0) { |
| 7648 | Py_XDECREF(outbytes); |
| 7649 | return NULL; |
| 7650 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7651 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7652 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7653 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7654 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7655 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7656 | return outbytes; |
| 7657 | } |
| 7658 | |
| 7659 | PyObject * |
| 7660 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7661 | Py_ssize_t size, |
| 7662 | const char *errors) |
| 7663 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7664 | PyObject *unicode, *res; |
| 7665 | unicode = PyUnicode_FromUnicode(p, size); |
| 7666 | if (unicode == NULL) |
| 7667 | return NULL; |
| 7668 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7669 | Py_DECREF(unicode); |
| 7670 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7671 | } |
| 7672 | |
| 7673 | PyObject * |
| 7674 | PyUnicode_EncodeCodePage(int code_page, |
| 7675 | PyObject *unicode, |
| 7676 | const char *errors) |
| 7677 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7678 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7679 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7680 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7681 | PyObject * |
| 7682 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7683 | { |
| 7684 | if (!PyUnicode_Check(unicode)) { |
| 7685 | PyErr_BadArgument(); |
| 7686 | return NULL; |
| 7687 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7688 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7689 | } |
| 7690 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7691 | #undef NEED_RETRY |
| 7692 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7693 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7696 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7697 | PyObject * |
| 7698 | PyUnicode_DecodeCharmap(const char *s, |
| 7699 | Py_ssize_t size, |
| 7700 | PyObject *mapping, |
| 7701 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7702 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7703 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7704 | Py_ssize_t startinpos; |
| 7705 | Py_ssize_t endinpos; |
| 7706 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7707 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7708 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7709 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7710 | PyObject *errorHandler = NULL; |
| 7711 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7712 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | /* Default to Latin-1 */ |
| 7714 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7715 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7717 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7718 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7719 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7721 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7722 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7723 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7724 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7725 | Py_ssize_t maplen; |
| 7726 | enum PyUnicode_Kind kind; |
| 7727 | void *data; |
| 7728 | Py_UCS4 x; |
| 7729 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7730 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7731 | return NULL; |
| 7732 | |
| 7733 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7734 | data = PyUnicode_DATA(mapping); |
| 7735 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7736 | while (s < e) { |
| 7737 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7740 | x = PyUnicode_READ(kind, data, ch); |
| 7741 | else |
| 7742 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7744 | if (x == 0xfffe) |
| 7745 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7746 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7747 | startinpos = s-starts; |
| 7748 | endinpos = startinpos+1; |
| 7749 | if (unicode_decode_call_errorhandler( |
| 7750 | errors, &errorHandler, |
| 7751 | "charmap", "character maps to <undefined>", |
| 7752 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7753 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7754 | goto onError; |
| 7755 | } |
| 7756 | continue; |
| 7757 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7758 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7759 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7760 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7762 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7763 | } |
| 7764 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7765 | while (s < e) { |
| 7766 | unsigned char ch = *s; |
| 7767 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7770 | w = PyLong_FromLong((long)ch); |
| 7771 | if (w == NULL) |
| 7772 | goto onError; |
| 7773 | x = PyObject_GetItem(mapping, w); |
| 7774 | Py_DECREF(w); |
| 7775 | if (x == NULL) { |
| 7776 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7777 | /* No mapping found means: mapping is undefined. */ |
| 7778 | PyErr_Clear(); |
| 7779 | x = Py_None; |
| 7780 | Py_INCREF(x); |
| 7781 | } else |
| 7782 | goto onError; |
| 7783 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7784 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | /* Apply mapping */ |
| 7786 | if (PyLong_Check(x)) { |
| 7787 | long value = PyLong_AS_LONG(x); |
| 7788 | if (value < 0 || value > 65535) { |
| 7789 | PyErr_SetString(PyExc_TypeError, |
| 7790 | "character mapping must be in range(65536)"); |
| 7791 | Py_DECREF(x); |
| 7792 | goto onError; |
| 7793 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7794 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7795 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7796 | } |
| 7797 | else if (x == Py_None) { |
| 7798 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7799 | startinpos = s-starts; |
| 7800 | endinpos = startinpos+1; |
| 7801 | if (unicode_decode_call_errorhandler( |
| 7802 | errors, &errorHandler, |
| 7803 | "charmap", "character maps to <undefined>", |
| 7804 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7805 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 | Py_DECREF(x); |
| 7807 | goto onError; |
| 7808 | } |
| 7809 | Py_DECREF(x); |
| 7810 | continue; |
| 7811 | } |
| 7812 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7813 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7814 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7815 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7816 | goto onError; |
| 7817 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7818 | |
| 7819 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7821 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7822 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7823 | goto onError; |
| 7824 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7825 | else if (targetsize > 1) { |
| 7826 | /* 1-n mapping */ |
| 7827 | if (targetsize > extrachars) { |
| 7828 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7829 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7830 | (targetsize << 2); |
| 7831 | extrachars += needed; |
| 7832 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7833 | if (unicode_resize(&v, |
| 7834 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7835 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 | Py_DECREF(x); |
| 7837 | goto onError; |
| 7838 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7839 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7840 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7841 | goto onError; |
| 7842 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7843 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | extrachars -= targetsize; |
| 7845 | } |
| 7846 | /* 1-0 mapping: skip the character */ |
| 7847 | } |
| 7848 | else { |
| 7849 | /* wrong return value */ |
| 7850 | PyErr_SetString(PyExc_TypeError, |
| 7851 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7852 | Py_DECREF(x); |
| 7853 | goto onError; |
| 7854 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | Py_DECREF(x); |
| 7856 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7857 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7858 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7859 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7860 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7861 | Py_XDECREF(errorHandler); |
| 7862 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7863 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7864 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7865 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7866 | Py_XDECREF(errorHandler); |
| 7867 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | Py_XDECREF(v); |
| 7869 | return NULL; |
| 7870 | } |
| 7871 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7872 | /* Charmap encoding: the lookup table */ |
| 7873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7874 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7875 | PyObject_HEAD |
| 7876 | unsigned char level1[32]; |
| 7877 | int count2, count3; |
| 7878 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7879 | }; |
| 7880 | |
| 7881 | static PyObject* |
| 7882 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7883 | { |
| 7884 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7885 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7887 | } |
| 7888 | |
| 7889 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7890 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7891 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7892 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7893 | }; |
| 7894 | |
| 7895 | static void |
| 7896 | encoding_map_dealloc(PyObject* o) |
| 7897 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7898 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7899 | } |
| 7900 | |
| 7901 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7902 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | "EncodingMap", /*tp_name*/ |
| 7904 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7905 | 0, /*tp_itemsize*/ |
| 7906 | /* methods */ |
| 7907 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7908 | 0, /*tp_print*/ |
| 7909 | 0, /*tp_getattr*/ |
| 7910 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7911 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7912 | 0, /*tp_repr*/ |
| 7913 | 0, /*tp_as_number*/ |
| 7914 | 0, /*tp_as_sequence*/ |
| 7915 | 0, /*tp_as_mapping*/ |
| 7916 | 0, /*tp_hash*/ |
| 7917 | 0, /*tp_call*/ |
| 7918 | 0, /*tp_str*/ |
| 7919 | 0, /*tp_getattro*/ |
| 7920 | 0, /*tp_setattro*/ |
| 7921 | 0, /*tp_as_buffer*/ |
| 7922 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7923 | 0, /*tp_doc*/ |
| 7924 | 0, /*tp_traverse*/ |
| 7925 | 0, /*tp_clear*/ |
| 7926 | 0, /*tp_richcompare*/ |
| 7927 | 0, /*tp_weaklistoffset*/ |
| 7928 | 0, /*tp_iter*/ |
| 7929 | 0, /*tp_iternext*/ |
| 7930 | encoding_map_methods, /*tp_methods*/ |
| 7931 | 0, /*tp_members*/ |
| 7932 | 0, /*tp_getset*/ |
| 7933 | 0, /*tp_base*/ |
| 7934 | 0, /*tp_dict*/ |
| 7935 | 0, /*tp_descr_get*/ |
| 7936 | 0, /*tp_descr_set*/ |
| 7937 | 0, /*tp_dictoffset*/ |
| 7938 | 0, /*tp_init*/ |
| 7939 | 0, /*tp_alloc*/ |
| 7940 | 0, /*tp_new*/ |
| 7941 | 0, /*tp_free*/ |
| 7942 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7943 | }; |
| 7944 | |
| 7945 | PyObject* |
| 7946 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7947 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7948 | PyObject *result; |
| 7949 | struct encoding_map *mresult; |
| 7950 | int i; |
| 7951 | int need_dict = 0; |
| 7952 | unsigned char level1[32]; |
| 7953 | unsigned char level2[512]; |
| 7954 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7955 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7956 | int kind; |
| 7957 | void *data; |
| 7958 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7960 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7961 | PyErr_BadArgument(); |
| 7962 | return NULL; |
| 7963 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7964 | kind = PyUnicode_KIND(string); |
| 7965 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7966 | memset(level1, 0xFF, sizeof level1); |
| 7967 | memset(level2, 0xFF, sizeof level2); |
| 7968 | |
| 7969 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7970 | or if there are non-BMP characters, we need to use |
| 7971 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7972 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7973 | need_dict = 1; |
| 7974 | for (i = 1; i < 256; i++) { |
| 7975 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7976 | ch = PyUnicode_READ(kind, data, i); |
| 7977 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7978 | need_dict = 1; |
| 7979 | break; |
| 7980 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7981 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7982 | /* unmapped character */ |
| 7983 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7984 | l1 = ch >> 11; |
| 7985 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7986 | if (level1[l1] == 0xFF) |
| 7987 | level1[l1] = count2++; |
| 7988 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7989 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7990 | } |
| 7991 | |
| 7992 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7993 | need_dict = 1; |
| 7994 | |
| 7995 | if (need_dict) { |
| 7996 | PyObject *result = PyDict_New(); |
| 7997 | PyObject *key, *value; |
| 7998 | if (!result) |
| 7999 | return NULL; |
| 8000 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8001 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8002 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8003 | if (!key || !value) |
| 8004 | goto failed1; |
| 8005 | if (PyDict_SetItem(result, key, value) == -1) |
| 8006 | goto failed1; |
| 8007 | Py_DECREF(key); |
| 8008 | Py_DECREF(value); |
| 8009 | } |
| 8010 | return result; |
| 8011 | failed1: |
| 8012 | Py_XDECREF(key); |
| 8013 | Py_XDECREF(value); |
| 8014 | Py_DECREF(result); |
| 8015 | return NULL; |
| 8016 | } |
| 8017 | |
| 8018 | /* Create a three-level trie */ |
| 8019 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8020 | 16*count2 + 128*count3 - 1); |
| 8021 | if (!result) |
| 8022 | return PyErr_NoMemory(); |
| 8023 | PyObject_Init(result, &EncodingMapType); |
| 8024 | mresult = (struct encoding_map*)result; |
| 8025 | mresult->count2 = count2; |
| 8026 | mresult->count3 = count3; |
| 8027 | mlevel1 = mresult->level1; |
| 8028 | mlevel2 = mresult->level23; |
| 8029 | mlevel3 = mresult->level23 + 16*count2; |
| 8030 | memcpy(mlevel1, level1, 32); |
| 8031 | memset(mlevel2, 0xFF, 16*count2); |
| 8032 | memset(mlevel3, 0, 128*count3); |
| 8033 | count3 = 0; |
| 8034 | for (i = 1; i < 256; i++) { |
| 8035 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8036 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8037 | /* unmapped character */ |
| 8038 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8039 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8040 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8041 | i2 = 16*mlevel1[o1] + o2; |
| 8042 | if (mlevel2[i2] == 0xFF) |
| 8043 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8044 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8045 | i3 = 128*mlevel2[i2] + o3; |
| 8046 | mlevel3[i3] = i; |
| 8047 | } |
| 8048 | return result; |
| 8049 | } |
| 8050 | |
| 8051 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8052 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8053 | { |
| 8054 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8055 | int l1 = c>>11; |
| 8056 | int l2 = (c>>7) & 0xF; |
| 8057 | int l3 = c & 0x7F; |
| 8058 | int i; |
| 8059 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8060 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8061 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8062 | if (c == 0) |
| 8063 | return 0; |
| 8064 | /* level 1*/ |
| 8065 | i = map->level1[l1]; |
| 8066 | if (i == 0xFF) { |
| 8067 | return -1; |
| 8068 | } |
| 8069 | /* level 2*/ |
| 8070 | i = map->level23[16*i+l2]; |
| 8071 | if (i == 0xFF) { |
| 8072 | return -1; |
| 8073 | } |
| 8074 | /* level 3 */ |
| 8075 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8076 | if (i == 0) { |
| 8077 | return -1; |
| 8078 | } |
| 8079 | return i; |
| 8080 | } |
| 8081 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8082 | /* Lookup the character ch in the mapping. If the character |
| 8083 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8084 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8085 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8086 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8087 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8088 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8089 | PyObject *x; |
| 8090 | |
| 8091 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8093 | x = PyObject_GetItem(mapping, w); |
| 8094 | Py_DECREF(w); |
| 8095 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8096 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8097 | /* No mapping found means: mapping is undefined. */ |
| 8098 | PyErr_Clear(); |
| 8099 | x = Py_None; |
| 8100 | Py_INCREF(x); |
| 8101 | return x; |
| 8102 | } else |
| 8103 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8105 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8106 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8107 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8108 | long value = PyLong_AS_LONG(x); |
| 8109 | if (value < 0 || value > 255) { |
| 8110 | PyErr_SetString(PyExc_TypeError, |
| 8111 | "character mapping must be in range(256)"); |
| 8112 | Py_DECREF(x); |
| 8113 | return NULL; |
| 8114 | } |
| 8115 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8117 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | /* wrong return value */ |
| 8121 | PyErr_Format(PyExc_TypeError, |
| 8122 | "character mapping must return integer, bytes or None, not %.400s", |
| 8123 | x->ob_type->tp_name); |
| 8124 | Py_DECREF(x); |
| 8125 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | } |
| 8127 | } |
| 8128 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8129 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8130 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8131 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8132 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8133 | /* exponentially overallocate to minimize reallocations */ |
| 8134 | if (requiredsize < 2*outsize) |
| 8135 | requiredsize = 2*outsize; |
| 8136 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8137 | return -1; |
| 8138 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8139 | } |
| 8140 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8141 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8142 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8143 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8144 | /* 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] | 8145 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8146 | space is available. Return a new reference to the object that |
| 8147 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8148 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8149 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8150 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8151 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8152 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8153 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8154 | PyObject *rep; |
| 8155 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8156 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8157 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8158 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8159 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8161 | if (res == -1) |
| 8162 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8163 | if (outsize<requiredsize) |
| 8164 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8165 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8166 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | outstart[(*outpos)++] = (char)res; |
| 8168 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
| 8171 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8172 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8174 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | Py_DECREF(rep); |
| 8176 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8177 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8178 | if (PyLong_Check(rep)) { |
| 8179 | Py_ssize_t requiredsize = *outpos+1; |
| 8180 | if (outsize<requiredsize) |
| 8181 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8182 | Py_DECREF(rep); |
| 8183 | return enc_EXCEPTION; |
| 8184 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8185 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8186 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8187 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8188 | else { |
| 8189 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8190 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8191 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8192 | if (outsize<requiredsize) |
| 8193 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8194 | Py_DECREF(rep); |
| 8195 | return enc_EXCEPTION; |
| 8196 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8197 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8198 | memcpy(outstart + *outpos, repchars, repsize); |
| 8199 | *outpos += repsize; |
| 8200 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8201 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8202 | Py_DECREF(rep); |
| 8203 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
| 8206 | /* handle an error in PyUnicode_EncodeCharmap |
| 8207 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8208 | static int |
| 8209 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8210 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8212 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8213 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8214 | { |
| 8215 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8216 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8217 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8218 | enum PyUnicode_Kind kind; |
| 8219 | void *data; |
| 8220 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8221 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8222 | Py_ssize_t collstartpos = *inpos; |
| 8223 | Py_ssize_t collendpos = *inpos+1; |
| 8224 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8225 | char *encoding = "charmap"; |
| 8226 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8227 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8228 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8229 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8230 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8231 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8232 | return -1; |
| 8233 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8234 | /* find all unencodable characters */ |
| 8235 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8236 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8237 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8238 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8239 | val = encoding_map_lookup(ch, mapping); |
| 8240 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8241 | break; |
| 8242 | ++collendpos; |
| 8243 | continue; |
| 8244 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8245 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8246 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8247 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | if (rep==NULL) |
| 8249 | return -1; |
| 8250 | else if (rep!=Py_None) { |
| 8251 | Py_DECREF(rep); |
| 8252 | break; |
| 8253 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8254 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8255 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8256 | } |
| 8257 | /* cache callback name lookup |
| 8258 | * (if not done yet, i.e. it's the first error) */ |
| 8259 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8261 | *known_errorHandler = 1; |
| 8262 | else if (!strcmp(errors, "replace")) |
| 8263 | *known_errorHandler = 2; |
| 8264 | else if (!strcmp(errors, "ignore")) |
| 8265 | *known_errorHandler = 3; |
| 8266 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8267 | *known_errorHandler = 4; |
| 8268 | else |
| 8269 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8270 | } |
| 8271 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8272 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8273 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8274 | return -1; |
| 8275 | case 2: /* replace */ |
| 8276 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | x = charmapencode_output('?', mapping, res, respos); |
| 8278 | if (x==enc_EXCEPTION) { |
| 8279 | return -1; |
| 8280 | } |
| 8281 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8282 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | return -1; |
| 8284 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8285 | } |
| 8286 | /* fall through */ |
| 8287 | case 3: /* ignore */ |
| 8288 | *inpos = collendpos; |
| 8289 | break; |
| 8290 | case 4: /* xmlcharrefreplace */ |
| 8291 | /* generate replacement (temporarily (mis)uses p) */ |
| 8292 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8293 | char buffer[2+29+1+1]; |
| 8294 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8295 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8296 | for (cp = buffer; *cp; ++cp) { |
| 8297 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8298 | if (x==enc_EXCEPTION) |
| 8299 | return -1; |
| 8300 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8301 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | return -1; |
| 8303 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8304 | } |
| 8305 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8306 | *inpos = collendpos; |
| 8307 | break; |
| 8308 | default: |
| 8309 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8310 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8311 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8312 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8313 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8314 | if (PyBytes_Check(repunicode)) { |
| 8315 | /* Directly copy bytes result to output. */ |
| 8316 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8317 | Py_ssize_t requiredsize; |
| 8318 | repsize = PyBytes_Size(repunicode); |
| 8319 | requiredsize = *respos + repsize; |
| 8320 | if (requiredsize > outsize) |
| 8321 | /* Make room for all additional bytes. */ |
| 8322 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8323 | Py_DECREF(repunicode); |
| 8324 | return -1; |
| 8325 | } |
| 8326 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8327 | PyBytes_AsString(repunicode), repsize); |
| 8328 | *respos += repsize; |
| 8329 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8330 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8331 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8332 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8333 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8334 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8335 | Py_DECREF(repunicode); |
| 8336 | return -1; |
| 8337 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8338 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8339 | data = PyUnicode_DATA(repunicode); |
| 8340 | kind = PyUnicode_KIND(repunicode); |
| 8341 | for (index = 0; index < repsize; index++) { |
| 8342 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8343 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8345 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | return -1; |
| 8347 | } |
| 8348 | else if (x==enc_FAILED) { |
| 8349 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8350 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8351 | return -1; |
| 8352 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8353 | } |
| 8354 | *inpos = newpos; |
| 8355 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8356 | } |
| 8357 | return 0; |
| 8358 | } |
| 8359 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8360 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8361 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8362 | PyObject *mapping, |
| 8363 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8364 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8365 | /* output object */ |
| 8366 | PyObject *res = NULL; |
| 8367 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8368 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8369 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8371 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8372 | PyObject *errorHandler = NULL; |
| 8373 | PyObject *exc = NULL; |
| 8374 | /* the following variable is used for caching string comparisons |
| 8375 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8376 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8377 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8378 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8379 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8380 | return NULL; |
| 8381 | size = PyUnicode_GET_LENGTH(unicode); |
| 8382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | /* Default to Latin-1 */ |
| 8384 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8385 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8386 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8387 | /* allocate enough for a simple encoding without |
| 8388 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8389 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8390 | if (res == NULL) |
| 8391 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8392 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8393 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8394 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8395 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8396 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8398 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8399 | if (x==enc_EXCEPTION) /* error */ |
| 8400 | goto onError; |
| 8401 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8402 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8403 | &exc, |
| 8404 | &known_errorHandler, &errorHandler, errors, |
| 8405 | &res, &respos)) { |
| 8406 | goto onError; |
| 8407 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8408 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8409 | else |
| 8410 | /* done with this character => adjust input position */ |
| 8411 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8414 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8415 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8416 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8417 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8418 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8419 | Py_XDECREF(exc); |
| 8420 | Py_XDECREF(errorHandler); |
| 8421 | return res; |
| 8422 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8423 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8424 | Py_XDECREF(res); |
| 8425 | Py_XDECREF(exc); |
| 8426 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | return NULL; |
| 8428 | } |
| 8429 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8430 | /* Deprecated */ |
| 8431 | PyObject * |
| 8432 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8433 | Py_ssize_t size, |
| 8434 | PyObject *mapping, |
| 8435 | const char *errors) |
| 8436 | { |
| 8437 | PyObject *result; |
| 8438 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8439 | if (unicode == NULL) |
| 8440 | return NULL; |
| 8441 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8442 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8443 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8444 | } |
| 8445 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8446 | PyObject * |
| 8447 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8448 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8449 | { |
| 8450 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | PyErr_BadArgument(); |
| 8452 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8454 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8455 | } |
| 8456 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8458 | static void |
| 8459 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8461 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8462 | const char *reason) |
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 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8465 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8466 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | } |
| 8468 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8470 | goto onError; |
| 8471 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8472 | goto onError; |
| 8473 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8474 | goto onError; |
| 8475 | return; |
| 8476 | onError: |
| 8477 | Py_DECREF(*exceptionObject); |
| 8478 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | } |
| 8480 | } |
| 8481 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8482 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8483 | static void |
| 8484 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8485 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8486 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8487 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8488 | { |
| 8489 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8490 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8491 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8492 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8493 | } |
| 8494 | |
| 8495 | /* error handling callback helper: |
| 8496 | build arguments, call the callback and check the arguments, |
| 8497 | put the result into newpos and return the replacement string, which |
| 8498 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8499 | static PyObject * |
| 8500 | unicode_translate_call_errorhandler(const char *errors, |
| 8501 | PyObject **errorHandler, |
| 8502 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8503 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8504 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8505 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8506 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8507 | 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] | 8508 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8509 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8510 | PyObject *restuple; |
| 8511 | PyObject *resunicode; |
| 8512 | |
| 8513 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8514 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8515 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8516 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8520 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8521 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8522 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8523 | |
| 8524 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8525 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8526 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8528 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8529 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8530 | Py_DECREF(restuple); |
| 8531 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8532 | } |
| 8533 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8534 | &resunicode, &i_newpos)) { |
| 8535 | Py_DECREF(restuple); |
| 8536 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8537 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8538 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8539 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8540 | else |
| 8541 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8543 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8544 | Py_DECREF(restuple); |
| 8545 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8546 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8547 | Py_INCREF(resunicode); |
| 8548 | Py_DECREF(restuple); |
| 8549 | return resunicode; |
| 8550 | } |
| 8551 | |
| 8552 | /* Lookup the character ch in the mapping and put the result in result, |
| 8553 | which must be decrefed by the caller. |
| 8554 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8555 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8556 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8557 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8558 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8559 | PyObject *x; |
| 8560 | |
| 8561 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8563 | x = PyObject_GetItem(mapping, w); |
| 8564 | Py_DECREF(w); |
| 8565 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8566 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8567 | /* No mapping found means: use 1:1 mapping. */ |
| 8568 | PyErr_Clear(); |
| 8569 | *result = NULL; |
| 8570 | return 0; |
| 8571 | } else |
| 8572 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8573 | } |
| 8574 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8575 | *result = x; |
| 8576 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8577 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8578 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | long value = PyLong_AS_LONG(x); |
| 8580 | long max = PyUnicode_GetMax(); |
| 8581 | if (value < 0 || value > max) { |
| 8582 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8583 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | Py_DECREF(x); |
| 8585 | return -1; |
| 8586 | } |
| 8587 | *result = x; |
| 8588 | return 0; |
| 8589 | } |
| 8590 | else if (PyUnicode_Check(x)) { |
| 8591 | *result = x; |
| 8592 | return 0; |
| 8593 | } |
| 8594 | else { |
| 8595 | /* wrong return value */ |
| 8596 | PyErr_SetString(PyExc_TypeError, |
| 8597 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8598 | Py_DECREF(x); |
| 8599 | return -1; |
| 8600 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8601 | } |
| 8602 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8603 | if not reallocate and adjust various state variables. |
| 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_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8610 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | /* exponentially overallocate to minimize reallocations */ |
| 8612 | if (requiredsize < 2 * oldsize) |
| 8613 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8614 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8615 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8616 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8617 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8618 | } |
| 8619 | return 0; |
| 8620 | } |
| 8621 | /* lookup the character, put the result in the output string and adjust |
| 8622 | various state variables. Return a new reference to the object that |
| 8623 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8624 | undefined (in which case no character was written). |
| 8625 | The called must decref result. |
| 8626 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8627 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8628 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8629 | PyObject *mapping, Py_UCS4 **output, |
| 8630 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8631 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8632 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8633 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8634 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8635 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8636 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8638 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8639 | } |
| 8640 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8641 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8642 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8643 | /* 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] | 8644 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8645 | } |
| 8646 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8647 | Py_ssize_t repsize; |
| 8648 | if (PyUnicode_READY(*res) == -1) |
| 8649 | return -1; |
| 8650 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | if (repsize==1) { |
| 8652 | /* 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] | 8653 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8654 | } |
| 8655 | else if (repsize!=0) { |
| 8656 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | Py_ssize_t requiredsize = *opos + |
| 8658 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8659 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8660 | Py_ssize_t i; |
| 8661 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | for(i = 0; i < repsize; i++) |
| 8664 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8665 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8666 | } |
| 8667 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8668 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8669 | return 0; |
| 8670 | } |
| 8671 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8672 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8673 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8674 | PyObject *mapping, |
| 8675 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8676 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | /* input object */ |
| 8678 | char *idata; |
| 8679 | Py_ssize_t size, i; |
| 8680 | int kind; |
| 8681 | /* output buffer */ |
| 8682 | Py_UCS4 *output = NULL; |
| 8683 | Py_ssize_t osize; |
| 8684 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8685 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8686 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8687 | char *reason = "character maps to <undefined>"; |
| 8688 | PyObject *errorHandler = NULL; |
| 8689 | PyObject *exc = NULL; |
| 8690 | /* the following variable is used for caching string comparisons |
| 8691 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8692 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8693 | int known_errorHandler = -1; |
| 8694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8696 | PyErr_BadArgument(); |
| 8697 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8698 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8700 | if (PyUnicode_READY(input) == -1) |
| 8701 | return NULL; |
| 8702 | idata = (char*)PyUnicode_DATA(input); |
| 8703 | kind = PyUnicode_KIND(input); |
| 8704 | size = PyUnicode_GET_LENGTH(input); |
| 8705 | i = 0; |
| 8706 | |
| 8707 | if (size == 0) { |
| 8708 | Py_INCREF(input); |
| 8709 | return input; |
| 8710 | } |
| 8711 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8712 | /* allocate enough for a simple 1:1 translation without |
| 8713 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | osize = size; |
| 8715 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8716 | opos = 0; |
| 8717 | if (output == NULL) { |
| 8718 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8722 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8723 | /* try to encode it */ |
| 8724 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8725 | if (charmaptranslate_output(input, i, mapping, |
| 8726 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | Py_XDECREF(x); |
| 8728 | goto onError; |
| 8729 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8730 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8731 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8732 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8733 | else { /* untranslatable character */ |
| 8734 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8735 | Py_ssize_t repsize; |
| 8736 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8738 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | Py_ssize_t collstart = i; |
| 8740 | Py_ssize_t collend = i+1; |
| 8741 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8743 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | while (collend < size) { |
| 8745 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8746 | goto onError; |
| 8747 | Py_XDECREF(x); |
| 8748 | if (x!=Py_None) |
| 8749 | break; |
| 8750 | ++collend; |
| 8751 | } |
| 8752 | /* cache callback name lookup |
| 8753 | * (if not done yet, i.e. it's the first error) */ |
| 8754 | if (known_errorHandler==-1) { |
| 8755 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8756 | known_errorHandler = 1; |
| 8757 | else if (!strcmp(errors, "replace")) |
| 8758 | known_errorHandler = 2; |
| 8759 | else if (!strcmp(errors, "ignore")) |
| 8760 | known_errorHandler = 3; |
| 8761 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8762 | known_errorHandler = 4; |
| 8763 | else |
| 8764 | known_errorHandler = 0; |
| 8765 | } |
| 8766 | switch (known_errorHandler) { |
| 8767 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8768 | raise_translate_exception(&exc, input, collstart, |
| 8769 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8770 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8771 | case 2: /* replace */ |
| 8772 | /* 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] | 8773 | for (coll = collstart; coll<collend; coll++) |
| 8774 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8775 | /* fall through */ |
| 8776 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8777 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8778 | break; |
| 8779 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8780 | /* generate replacement (temporarily (mis)uses i) */ |
| 8781 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8782 | char buffer[2+29+1+1]; |
| 8783 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8785 | if (charmaptranslate_makespace(&output, &osize, |
| 8786 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8787 | goto onError; |
| 8788 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8789 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8790 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8792 | break; |
| 8793 | default: |
| 8794 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8795 | reason, input, &exc, |
| 8796 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8797 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8798 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8799 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8800 | Py_DECREF(repunicode); |
| 8801 | goto onError; |
| 8802 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8803 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8804 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8805 | if (charmaptranslate_makespace(&output, &osize, |
| 8806 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8807 | Py_DECREF(repunicode); |
| 8808 | goto onError; |
| 8809 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8810 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8811 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8812 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8813 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8814 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8815 | } |
| 8816 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8817 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8818 | if (!res) |
| 8819 | goto onError; |
| 8820 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8821 | Py_XDECREF(exc); |
| 8822 | Py_XDECREF(errorHandler); |
| 8823 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8824 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8825 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8826 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8827 | Py_XDECREF(exc); |
| 8828 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8829 | return NULL; |
| 8830 | } |
| 8831 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8833 | PyObject * |
| 8834 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8835 | Py_ssize_t size, |
| 8836 | PyObject *mapping, |
| 8837 | const char *errors) |
| 8838 | { |
| 8839 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8840 | if (!unicode) |
| 8841 | return NULL; |
| 8842 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8843 | } |
| 8844 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8845 | PyObject * |
| 8846 | PyUnicode_Translate(PyObject *str, |
| 8847 | PyObject *mapping, |
| 8848 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8849 | { |
| 8850 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8852 | str = PyUnicode_FromObject(str); |
| 8853 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8854 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8855 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8856 | Py_DECREF(str); |
| 8857 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8858 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8859 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 | Py_XDECREF(str); |
| 8861 | return NULL; |
| 8862 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8864 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8865 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8866 | { |
| 8867 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8868 | called as a callback from fixup() which does it already. */ |
| 8869 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8870 | const int kind = PyUnicode_KIND(self); |
| 8871 | void *data = PyUnicode_DATA(self); |
| 8872 | Py_UCS4 maxchar = 0, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8873 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | Py_ssize_t i; |
| 8875 | |
| 8876 | for (i = 0; i < len; ++i) { |
| 8877 | ch = PyUnicode_READ(kind, data, i); |
| 8878 | fixed = 0; |
| 8879 | if (ch > 127) { |
| 8880 | if (Py_UNICODE_ISSPACE(ch)) |
| 8881 | fixed = ' '; |
| 8882 | else { |
| 8883 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8884 | if (decimal >= 0) |
| 8885 | fixed = '0' + decimal; |
| 8886 | } |
| 8887 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8888 | modified = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8889 | if (fixed > maxchar) |
| 8890 | maxchar = fixed; |
| 8891 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8892 | } |
| 8893 | else if (ch > maxchar) |
| 8894 | maxchar = ch; |
| 8895 | } |
| 8896 | else if (ch > maxchar) |
| 8897 | maxchar = ch; |
| 8898 | } |
| 8899 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8900 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | } |
| 8902 | |
| 8903 | PyObject * |
| 8904 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8905 | { |
| 8906 | if (!PyUnicode_Check(unicode)) { |
| 8907 | PyErr_BadInternalCall(); |
| 8908 | return NULL; |
| 8909 | } |
| 8910 | if (PyUnicode_READY(unicode) == -1) |
| 8911 | return NULL; |
| 8912 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8913 | /* If the string is already ASCII, just return the same string */ |
| 8914 | Py_INCREF(unicode); |
| 8915 | return unicode; |
| 8916 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8917 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8918 | } |
| 8919 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8920 | PyObject * |
| 8921 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8922 | Py_ssize_t length) |
| 8923 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8924 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8925 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8926 | Py_UCS4 maxchar; |
| 8927 | enum PyUnicode_Kind kind; |
| 8928 | void *data; |
| 8929 | |
| 8930 | maxchar = 0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8931 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8932 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8933 | if (ch > 127) { |
| 8934 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8935 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8936 | ch = '0' + decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8937 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8938 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8939 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8940 | |
| 8941 | /* Copy to a new string */ |
| 8942 | decimal = PyUnicode_New(length, maxchar); |
| 8943 | if (decimal == NULL) |
| 8944 | return decimal; |
| 8945 | kind = PyUnicode_KIND(decimal); |
| 8946 | data = PyUnicode_DATA(decimal); |
| 8947 | /* Iterate over code points */ |
| 8948 | for (i = 0; i < length; i++) { |
| 8949 | Py_UNICODE ch = s[i]; |
| 8950 | if (ch > 127) { |
| 8951 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8952 | if (decimal >= 0) |
| 8953 | ch = '0' + decimal; |
| 8954 | } |
| 8955 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8956 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8957 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8958 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8959 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8960 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8961 | int |
| 8962 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8963 | Py_ssize_t length, |
| 8964 | char *output, |
| 8965 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8966 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8967 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8968 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8969 | enum PyUnicode_Kind kind; |
| 8970 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8971 | |
| 8972 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8973 | PyErr_BadArgument(); |
| 8974 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8975 | } |
| 8976 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8977 | unicode = PyUnicode_FromUnicode(s, length); |
| 8978 | if (unicode == NULL) |
| 8979 | return -1; |
| 8980 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8981 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8982 | Py_DECREF(unicode); |
| 8983 | return -1; |
| 8984 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8985 | kind = PyUnicode_KIND(unicode); |
| 8986 | data = PyUnicode_DATA(unicode); |
| 8987 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8988 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8989 | PyObject *exc; |
| 8990 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8991 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8992 | Py_ssize_t startpos; |
| 8993 | |
| 8994 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8995 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8996 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8997 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8998 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8999 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9000 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9001 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9002 | if (decimal >= 0) { |
| 9003 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9004 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9005 | continue; |
| 9006 | } |
| 9007 | if (0 < ch && ch < 256) { |
| 9008 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9009 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9010 | continue; |
| 9011 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9012 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9013 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9014 | exc = NULL; |
| 9015 | raise_encode_exception(&exc, "decimal", unicode, |
| 9016 | startpos, startpos+1, |
| 9017 | "invalid decimal Unicode string"); |
| 9018 | Py_XDECREF(exc); |
| 9019 | Py_DECREF(unicode); |
| 9020 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9021 | } |
| 9022 | /* 0-terminate the output string */ |
| 9023 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9024 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9025 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9026 | } |
| 9027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | /* --- Helpers ------------------------------------------------------------ */ |
| 9029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9031 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9032 | Py_ssize_t start, |
| 9033 | Py_ssize_t end) |
| 9034 | { |
| 9035 | int kind1, kind2, kind; |
| 9036 | void *buf1, *buf2; |
| 9037 | Py_ssize_t len1, len2, result; |
| 9038 | |
| 9039 | kind1 = PyUnicode_KIND(s1); |
| 9040 | kind2 = PyUnicode_KIND(s2); |
| 9041 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9042 | buf1 = PyUnicode_DATA(s1); |
| 9043 | buf2 = PyUnicode_DATA(s2); |
| 9044 | if (kind1 != kind) |
| 9045 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9046 | if (!buf1) |
| 9047 | return -2; |
| 9048 | if (kind2 != kind) |
| 9049 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9050 | if (!buf2) { |
| 9051 | if (kind1 != kind) PyMem_Free(buf1); |
| 9052 | return -2; |
| 9053 | } |
| 9054 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9055 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9056 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9057 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9058 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9059 | case PyUnicode_1BYTE_KIND: |
| 9060 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9061 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9062 | else |
| 9063 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9064 | break; |
| 9065 | case PyUnicode_2BYTE_KIND: |
| 9066 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9067 | break; |
| 9068 | case PyUnicode_4BYTE_KIND: |
| 9069 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9070 | break; |
| 9071 | default: |
| 9072 | assert(0); result = -2; |
| 9073 | } |
| 9074 | } |
| 9075 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9076 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9077 | case PyUnicode_1BYTE_KIND: |
| 9078 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9079 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9080 | else |
| 9081 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9082 | break; |
| 9083 | case PyUnicode_2BYTE_KIND: |
| 9084 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9085 | break; |
| 9086 | case PyUnicode_4BYTE_KIND: |
| 9087 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9088 | break; |
| 9089 | default: |
| 9090 | assert(0); result = -2; |
| 9091 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9092 | } |
| 9093 | |
| 9094 | if (kind1 != kind) |
| 9095 | PyMem_Free(buf1); |
| 9096 | if (kind2 != kind) |
| 9097 | PyMem_Free(buf2); |
| 9098 | |
| 9099 | return result; |
| 9100 | } |
| 9101 | |
| 9102 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9103 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9104 | Py_ssize_t n_buffer, |
| 9105 | void *digits, Py_ssize_t n_digits, |
| 9106 | Py_ssize_t min_width, |
| 9107 | const char *grouping, |
| 9108 | const char *thousands_sep) |
| 9109 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9110 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9111 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9112 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9113 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9114 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9115 | min_width, grouping, thousands_sep); |
| 9116 | else |
| 9117 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9118 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9119 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 | case PyUnicode_2BYTE_KIND: |
| 9121 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9122 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9123 | min_width, grouping, thousands_sep); |
| 9124 | case PyUnicode_4BYTE_KIND: |
| 9125 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9126 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9127 | min_width, grouping, thousands_sep); |
| 9128 | } |
| 9129 | assert(0); |
| 9130 | return -1; |
| 9131 | } |
| 9132 | |
| 9133 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9134 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9135 | #define ADJUST_INDICES(start, end, len) \ |
| 9136 | if (end > len) \ |
| 9137 | end = len; \ |
| 9138 | else if (end < 0) { \ |
| 9139 | end += len; \ |
| 9140 | if (end < 0) \ |
| 9141 | end = 0; \ |
| 9142 | } \ |
| 9143 | if (start < 0) { \ |
| 9144 | start += len; \ |
| 9145 | if (start < 0) \ |
| 9146 | start = 0; \ |
| 9147 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9149 | Py_ssize_t |
| 9150 | PyUnicode_Count(PyObject *str, |
| 9151 | PyObject *substr, |
| 9152 | Py_ssize_t start, |
| 9153 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9154 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9155 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9156 | PyObject* str_obj; |
| 9157 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9158 | int kind1, kind2, kind; |
| 9159 | void *buf1 = NULL, *buf2 = NULL; |
| 9160 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9161 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9162 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9163 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9164 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9165 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9166 | if (!sub_obj) { |
| 9167 | Py_DECREF(str_obj); |
| 9168 | return -1; |
| 9169 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9170 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9171 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9172 | Py_DECREF(str_obj); |
| 9173 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9174 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9176 | kind1 = PyUnicode_KIND(str_obj); |
| 9177 | kind2 = PyUnicode_KIND(sub_obj); |
| 9178 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9179 | buf1 = PyUnicode_DATA(str_obj); |
| 9180 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9181 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9182 | if (!buf1) |
| 9183 | goto onError; |
| 9184 | buf2 = PyUnicode_DATA(sub_obj); |
| 9185 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9186 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9187 | if (!buf2) |
| 9188 | goto onError; |
| 9189 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9190 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9191 | |
| 9192 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9193 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9195 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9196 | result = asciilib_count( |
| 9197 | ((Py_UCS1*)buf1) + start, end - start, |
| 9198 | buf2, len2, PY_SSIZE_T_MAX |
| 9199 | ); |
| 9200 | else |
| 9201 | result = ucs1lib_count( |
| 9202 | ((Py_UCS1*)buf1) + start, end - start, |
| 9203 | buf2, len2, PY_SSIZE_T_MAX |
| 9204 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9205 | break; |
| 9206 | case PyUnicode_2BYTE_KIND: |
| 9207 | result = ucs2lib_count( |
| 9208 | ((Py_UCS2*)buf1) + start, end - start, |
| 9209 | buf2, len2, PY_SSIZE_T_MAX |
| 9210 | ); |
| 9211 | break; |
| 9212 | case PyUnicode_4BYTE_KIND: |
| 9213 | result = ucs4lib_count( |
| 9214 | ((Py_UCS4*)buf1) + start, end - start, |
| 9215 | buf2, len2, PY_SSIZE_T_MAX |
| 9216 | ); |
| 9217 | break; |
| 9218 | default: |
| 9219 | assert(0); result = 0; |
| 9220 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9221 | |
| 9222 | Py_DECREF(sub_obj); |
| 9223 | Py_DECREF(str_obj); |
| 9224 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9225 | if (kind1 != kind) |
| 9226 | PyMem_Free(buf1); |
| 9227 | if (kind2 != kind) |
| 9228 | PyMem_Free(buf2); |
| 9229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9230 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9231 | onError: |
| 9232 | Py_DECREF(sub_obj); |
| 9233 | Py_DECREF(str_obj); |
| 9234 | if (kind1 != kind && buf1) |
| 9235 | PyMem_Free(buf1); |
| 9236 | if (kind2 != kind && buf2) |
| 9237 | PyMem_Free(buf2); |
| 9238 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | } |
| 9240 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9241 | Py_ssize_t |
| 9242 | PyUnicode_Find(PyObject *str, |
| 9243 | PyObject *sub, |
| 9244 | Py_ssize_t start, |
| 9245 | Py_ssize_t end, |
| 9246 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9248 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9251 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9252 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9253 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9254 | if (!sub) { |
| 9255 | Py_DECREF(str); |
| 9256 | return -2; |
| 9257 | } |
| 9258 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9259 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9260 | Py_DECREF(str); |
| 9261 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9262 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9263 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9264 | result = any_find_slice(direction, |
| 9265 | str, sub, start, end |
| 9266 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9268 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9269 | Py_DECREF(sub); |
| 9270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9271 | return result; |
| 9272 | } |
| 9273 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9274 | Py_ssize_t |
| 9275 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9276 | Py_ssize_t start, Py_ssize_t end, |
| 9277 | int direction) |
| 9278 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9279 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9280 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | if (PyUnicode_READY(str) == -1) |
| 9282 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9283 | if (start < 0 || end < 0) { |
| 9284 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9285 | return -2; |
| 9286 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9287 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9288 | end = PyUnicode_GET_LENGTH(str); |
| 9289 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9290 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9291 | kind, end-start, ch, direction); |
| 9292 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9293 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9294 | else |
| 9295 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9296 | } |
| 9297 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9298 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9299 | tailmatch(PyObject *self, |
| 9300 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9301 | Py_ssize_t start, |
| 9302 | Py_ssize_t end, |
| 9303 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9304 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9305 | int kind_self; |
| 9306 | int kind_sub; |
| 9307 | void *data_self; |
| 9308 | void *data_sub; |
| 9309 | Py_ssize_t offset; |
| 9310 | Py_ssize_t i; |
| 9311 | Py_ssize_t end_sub; |
| 9312 | |
| 9313 | if (PyUnicode_READY(self) == -1 || |
| 9314 | PyUnicode_READY(substring) == -1) |
| 9315 | return 0; |
| 9316 | |
| 9317 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9318 | return 1; |
| 9319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9320 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9321 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9322 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9323 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9324 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9325 | kind_self = PyUnicode_KIND(self); |
| 9326 | data_self = PyUnicode_DATA(self); |
| 9327 | kind_sub = PyUnicode_KIND(substring); |
| 9328 | data_sub = PyUnicode_DATA(substring); |
| 9329 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9330 | |
| 9331 | if (direction > 0) |
| 9332 | offset = end; |
| 9333 | else |
| 9334 | offset = start; |
| 9335 | |
| 9336 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9337 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9338 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9339 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9340 | /* If both are of the same kind, memcmp is sufficient */ |
| 9341 | if (kind_self == kind_sub) { |
| 9342 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9343 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9344 | data_sub, |
| 9345 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9346 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | } |
| 9348 | /* otherwise we have to compare each character by first accesing it */ |
| 9349 | else { |
| 9350 | /* We do not need to compare 0 and len(substring)-1 because |
| 9351 | the if statement above ensured already that they are equal |
| 9352 | when we end up here. */ |
| 9353 | // TODO: honor direction and do a forward or backwards search |
| 9354 | for (i = 1; i < end_sub; ++i) { |
| 9355 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9356 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9357 | return 0; |
| 9358 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9359 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9360 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
| 9363 | return 0; |
| 9364 | } |
| 9365 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9366 | Py_ssize_t |
| 9367 | PyUnicode_Tailmatch(PyObject *str, |
| 9368 | PyObject *substr, |
| 9369 | Py_ssize_t start, |
| 9370 | Py_ssize_t end, |
| 9371 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9373 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | str = PyUnicode_FromObject(str); |
| 9376 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9377 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9378 | substr = PyUnicode_FromObject(substr); |
| 9379 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9380 | Py_DECREF(str); |
| 9381 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9382 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9383 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9384 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9385 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9386 | Py_DECREF(str); |
| 9387 | Py_DECREF(substr); |
| 9388 | return result; |
| 9389 | } |
| 9390 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9391 | /* Apply fixfct filter to the Unicode object self and return a |
| 9392 | reference to the modified object */ |
| 9393 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9394 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9395 | fixup(PyObject *self, |
| 9396 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9397 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9398 | PyObject *u; |
| 9399 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9400 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9401 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9402 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9403 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9404 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9405 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9406 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9407 | /* fix functions return the new maximum character in a string, |
| 9408 | if the kind of the resulting unicode object does not change, |
| 9409 | everything is fine. Otherwise we need to change the string kind |
| 9410 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9411 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9412 | |
| 9413 | if (maxchar_new == 0) { |
| 9414 | /* no changes */; |
| 9415 | if (PyUnicode_CheckExact(self)) { |
| 9416 | Py_DECREF(u); |
| 9417 | Py_INCREF(self); |
| 9418 | return self; |
| 9419 | } |
| 9420 | else |
| 9421 | return u; |
| 9422 | } |
| 9423 | |
| 9424 | if (maxchar_new <= 127) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | maxchar_new = 127; |
| 9426 | else if (maxchar_new <= 255) |
| 9427 | maxchar_new = 255; |
| 9428 | else if (maxchar_new <= 65535) |
| 9429 | maxchar_new = 65535; |
| 9430 | else |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 9431 | maxchar_new = MAX_UNICODE; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9432 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9433 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9434 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9435 | |
| 9436 | /* In case the maximum character changed, we need to |
| 9437 | convert the string to the new category. */ |
| 9438 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9439 | if (v == NULL) { |
| 9440 | Py_DECREF(u); |
| 9441 | return NULL; |
| 9442 | } |
| 9443 | if (maxchar_new > maxchar_old) { |
| 9444 | /* If the maxchar increased so that the kind changed, not all |
| 9445 | characters are representable anymore and we need to fix the |
| 9446 | string again. This only happens in very few cases. */ |
| 9447 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9448 | maxchar_old = fixfct(v); |
| 9449 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | } |
| 9451 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9452 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9453 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9454 | Py_DECREF(u); |
| 9455 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9456 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9457 | } |
| 9458 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9459 | static PyObject * |
| 9460 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9461 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9462 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9463 | char *resdata, *data = PyUnicode_DATA(self); |
| 9464 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9465 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9466 | res = PyUnicode_New(len, 127); |
| 9467 | if (res == NULL) |
| 9468 | return NULL; |
| 9469 | resdata = PyUnicode_DATA(res); |
| 9470 | if (lower) |
| 9471 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9472 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9473 | _Py_bytes_upper(resdata, data, len); |
| 9474 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9477 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9478 | 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] | 9479 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9480 | Py_ssize_t j; |
| 9481 | int final_sigma; |
| 9482 | Py_UCS4 c; |
| 9483 | /* 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] | 9484 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9485 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9486 | |
| 9487 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9488 | */ |
| 9489 | for (j = i - 1; j >= 0; j--) { |
| 9490 | c = PyUnicode_READ(kind, data, j); |
| 9491 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9492 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9493 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9494 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9495 | if (final_sigma) { |
| 9496 | for (j = i + 1; j < length; j++) { |
| 9497 | c = PyUnicode_READ(kind, data, j); |
| 9498 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9499 | break; |
| 9500 | } |
| 9501 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9502 | } |
| 9503 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | } |
| 9505 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9506 | static int |
| 9507 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9508 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9509 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9510 | /* Obscure special case. */ |
| 9511 | if (c == 0x3A3) { |
| 9512 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9513 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9514 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9515 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9516 | } |
| 9517 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9518 | static Py_ssize_t |
| 9519 | 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] | 9520 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9521 | Py_ssize_t i, k = 0; |
| 9522 | int n_res, j; |
| 9523 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9524 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9525 | c = PyUnicode_READ(kind, data, 0); |
| 9526 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9527 | for (j = 0; j < n_res; j++) { |
| 9528 | if (mapped[j] > *maxchar) |
| 9529 | *maxchar = mapped[j]; |
| 9530 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9532 | for (i = 1; i < length; i++) { |
| 9533 | c = PyUnicode_READ(kind, data, i); |
| 9534 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9535 | for (j = 0; j < n_res; j++) { |
| 9536 | if (mapped[j] > *maxchar) |
| 9537 | *maxchar = mapped[j]; |
| 9538 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9539 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9540 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9541 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9542 | } |
| 9543 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9544 | static Py_ssize_t |
| 9545 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9546 | Py_ssize_t i, k = 0; |
| 9547 | |
| 9548 | for (i = 0; i < length; i++) { |
| 9549 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9550 | int n_res, j; |
| 9551 | if (Py_UNICODE_ISUPPER(c)) { |
| 9552 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9553 | } |
| 9554 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9555 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9556 | } |
| 9557 | else { |
| 9558 | n_res = 1; |
| 9559 | mapped[0] = c; |
| 9560 | } |
| 9561 | for (j = 0; j < n_res; j++) { |
| 9562 | if (mapped[j] > *maxchar) |
| 9563 | *maxchar = mapped[j]; |
| 9564 | res[k++] = mapped[j]; |
| 9565 | } |
| 9566 | } |
| 9567 | return k; |
| 9568 | } |
| 9569 | |
| 9570 | static Py_ssize_t |
| 9571 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9572 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9573 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9574 | Py_ssize_t i, k = 0; |
| 9575 | |
| 9576 | for (i = 0; i < length; i++) { |
| 9577 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9578 | int n_res, j; |
| 9579 | if (lower) |
| 9580 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9581 | else |
| 9582 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9583 | for (j = 0; j < n_res; j++) { |
| 9584 | if (mapped[j] > *maxchar) |
| 9585 | *maxchar = mapped[j]; |
| 9586 | res[k++] = mapped[j]; |
| 9587 | } |
| 9588 | } |
| 9589 | return k; |
| 9590 | } |
| 9591 | |
| 9592 | static Py_ssize_t |
| 9593 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9594 | { |
| 9595 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9596 | } |
| 9597 | |
| 9598 | static Py_ssize_t |
| 9599 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9600 | { |
| 9601 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9602 | } |
| 9603 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9604 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9605 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9606 | { |
| 9607 | Py_ssize_t i, k = 0; |
| 9608 | |
| 9609 | for (i = 0; i < length; i++) { |
| 9610 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9611 | Py_UCS4 mapped[3]; |
| 9612 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9613 | for (j = 0; j < n_res; j++) { |
| 9614 | if (mapped[j] > *maxchar) |
| 9615 | *maxchar = mapped[j]; |
| 9616 | res[k++] = mapped[j]; |
| 9617 | } |
| 9618 | } |
| 9619 | return k; |
| 9620 | } |
| 9621 | |
| 9622 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9623 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9624 | { |
| 9625 | Py_ssize_t i, k = 0; |
| 9626 | int previous_is_cased; |
| 9627 | |
| 9628 | previous_is_cased = 0; |
| 9629 | for (i = 0; i < length; i++) { |
| 9630 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9631 | Py_UCS4 mapped[3]; |
| 9632 | int n_res, j; |
| 9633 | |
| 9634 | if (previous_is_cased) |
| 9635 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9636 | else |
| 9637 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9638 | |
| 9639 | for (j = 0; j < n_res; j++) { |
| 9640 | if (mapped[j] > *maxchar) |
| 9641 | *maxchar = mapped[j]; |
| 9642 | res[k++] = mapped[j]; |
| 9643 | } |
| 9644 | |
| 9645 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9646 | } |
| 9647 | return k; |
| 9648 | } |
| 9649 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9650 | static PyObject * |
| 9651 | case_operation(PyObject *self, |
| 9652 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9653 | { |
| 9654 | PyObject *res = NULL; |
| 9655 | Py_ssize_t length, newlength = 0; |
| 9656 | int kind, outkind; |
| 9657 | void *data, *outdata; |
| 9658 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9659 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9660 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9661 | |
| 9662 | kind = PyUnicode_KIND(self); |
| 9663 | data = PyUnicode_DATA(self); |
| 9664 | length = PyUnicode_GET_LENGTH(self); |
| 9665 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9666 | if (tmp == NULL) |
| 9667 | return PyErr_NoMemory(); |
| 9668 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9669 | res = PyUnicode_New(newlength, maxchar); |
| 9670 | if (res == NULL) |
| 9671 | goto leave; |
| 9672 | tmpend = tmp + newlength; |
| 9673 | outdata = PyUnicode_DATA(res); |
| 9674 | outkind = PyUnicode_KIND(res); |
| 9675 | switch (outkind) { |
| 9676 | case PyUnicode_1BYTE_KIND: |
| 9677 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9678 | break; |
| 9679 | case PyUnicode_2BYTE_KIND: |
| 9680 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9681 | break; |
| 9682 | case PyUnicode_4BYTE_KIND: |
| 9683 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9684 | break; |
| 9685 | default: |
| 9686 | assert(0); |
| 9687 | break; |
| 9688 | } |
| 9689 | leave: |
| 9690 | PyMem_FREE(tmp); |
| 9691 | return res; |
| 9692 | } |
| 9693 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9694 | PyObject * |
| 9695 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9696 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9697 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9698 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9699 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9700 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9701 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9702 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9703 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9704 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9705 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9706 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9707 | int use_memcpy; |
| 9708 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9709 | PyObject *last_obj; |
| 9710 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9711 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9712 | fseq = PySequence_Fast(seq, ""); |
| 9713 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9714 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9715 | } |
| 9716 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9717 | /* NOTE: the following code can't call back into Python code, |
| 9718 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9719 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9720 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9721 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9722 | /* If empty sequence, return u"". */ |
| 9723 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9724 | Py_DECREF(fseq); |
| 9725 | Py_INCREF(unicode_empty); |
| 9726 | res = unicode_empty; |
| 9727 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9728 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9729 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9730 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9731 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9732 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9733 | if (seqlen == 1) { |
| 9734 | if (PyUnicode_CheckExact(items[0])) { |
| 9735 | res = items[0]; |
| 9736 | Py_INCREF(res); |
| 9737 | Py_DECREF(fseq); |
| 9738 | return res; |
| 9739 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9740 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9741 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9742 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9743 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9744 | /* Set up sep and seplen */ |
| 9745 | if (separator == NULL) { |
| 9746 | /* fall back to a blank space separator */ |
| 9747 | sep = PyUnicode_FromOrdinal(' '); |
| 9748 | if (!sep) |
| 9749 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9750 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9751 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9752 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9753 | else { |
| 9754 | if (!PyUnicode_Check(separator)) { |
| 9755 | PyErr_Format(PyExc_TypeError, |
| 9756 | "separator: expected str instance," |
| 9757 | " %.80s found", |
| 9758 | Py_TYPE(separator)->tp_name); |
| 9759 | goto onError; |
| 9760 | } |
| 9761 | if (PyUnicode_READY(separator)) |
| 9762 | goto onError; |
| 9763 | sep = separator; |
| 9764 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9765 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9766 | /* inc refcount to keep this code path symmetric with the |
| 9767 | above case of a blank separator */ |
| 9768 | Py_INCREF(sep); |
| 9769 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9770 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9771 | } |
| 9772 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9773 | /* There are at least two things to join, or else we have a subclass |
| 9774 | * of str in the sequence. |
| 9775 | * Do a pre-pass to figure out the total amount of space we'll |
| 9776 | * need (sz), and see whether all argument are strings. |
| 9777 | */ |
| 9778 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9779 | #ifdef Py_DEBUG |
| 9780 | use_memcpy = 0; |
| 9781 | #else |
| 9782 | use_memcpy = 1; |
| 9783 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9784 | for (i = 0; i < seqlen; i++) { |
| 9785 | const Py_ssize_t old_sz = sz; |
| 9786 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9787 | if (!PyUnicode_Check(item)) { |
| 9788 | PyErr_Format(PyExc_TypeError, |
| 9789 | "sequence item %zd: expected str instance," |
| 9790 | " %.80s found", |
| 9791 | i, Py_TYPE(item)->tp_name); |
| 9792 | goto onError; |
| 9793 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9794 | if (PyUnicode_READY(item) == -1) |
| 9795 | goto onError; |
| 9796 | sz += PyUnicode_GET_LENGTH(item); |
| 9797 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9798 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9799 | if (i != 0) |
| 9800 | sz += seplen; |
| 9801 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9802 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9803 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9804 | goto onError; |
| 9805 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9806 | if (use_memcpy && last_obj != NULL) { |
| 9807 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9808 | use_memcpy = 0; |
| 9809 | } |
| 9810 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9811 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9812 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9813 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9814 | if (res == NULL) |
| 9815 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9816 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9817 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9818 | #ifdef Py_DEBUG |
| 9819 | use_memcpy = 0; |
| 9820 | #else |
| 9821 | if (use_memcpy) { |
| 9822 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9823 | kind = PyUnicode_KIND(res); |
| 9824 | if (seplen != 0) |
| 9825 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9826 | } |
| 9827 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9828 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9829 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9830 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9831 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9832 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9833 | if (use_memcpy) { |
| 9834 | Py_MEMCPY(res_data, |
| 9835 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9836 | kind * seplen); |
| 9837 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9838 | } |
| 9839 | else { |
| 9840 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9841 | res_offset += seplen; |
| 9842 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9843 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9844 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9845 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9846 | if (use_memcpy) { |
| 9847 | Py_MEMCPY(res_data, |
| 9848 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9849 | kind * itemlen); |
| 9850 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9851 | } |
| 9852 | else { |
| 9853 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9854 | res_offset += itemlen; |
| 9855 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9856 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9857 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9858 | if (use_memcpy) |
| 9859 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9860 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9861 | else |
| 9862 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9863 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9864 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9866 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9868 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9869 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9870 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9871 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9872 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9873 | return NULL; |
| 9874 | } |
| 9875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9876 | #define FILL(kind, data, value, start, length) \ |
| 9877 | do { \ |
| 9878 | Py_ssize_t i_ = 0; \ |
| 9879 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9880 | switch ((kind)) { \ |
| 9881 | case PyUnicode_1BYTE_KIND: { \ |
| 9882 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9883 | memset(to_, (unsigned char)value, length); \ |
| 9884 | break; \ |
| 9885 | } \ |
| 9886 | case PyUnicode_2BYTE_KIND: { \ |
| 9887 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9888 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9889 | break; \ |
| 9890 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9891 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9893 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9894 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9895 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | } \ |
| 9897 | } \ |
| 9898 | } while (0) |
| 9899 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9900 | Py_ssize_t |
| 9901 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9902 | Py_UCS4 fill_char) |
| 9903 | { |
| 9904 | Py_ssize_t maxlen; |
| 9905 | enum PyUnicode_Kind kind; |
| 9906 | void *data; |
| 9907 | |
| 9908 | if (!PyUnicode_Check(unicode)) { |
| 9909 | PyErr_BadInternalCall(); |
| 9910 | return -1; |
| 9911 | } |
| 9912 | if (PyUnicode_READY(unicode) == -1) |
| 9913 | return -1; |
| 9914 | if (unicode_check_modifiable(unicode)) |
| 9915 | return -1; |
| 9916 | |
| 9917 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9918 | PyErr_SetString(PyExc_ValueError, |
| 9919 | "fill character is bigger than " |
| 9920 | "the string maximum character"); |
| 9921 | return -1; |
| 9922 | } |
| 9923 | |
| 9924 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9925 | length = Py_MIN(maxlen, length); |
| 9926 | if (length <= 0) |
| 9927 | return 0; |
| 9928 | |
| 9929 | kind = PyUnicode_KIND(unicode); |
| 9930 | data = PyUnicode_DATA(unicode); |
| 9931 | FILL(kind, data, fill_char, start, length); |
| 9932 | return length; |
| 9933 | } |
| 9934 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9935 | static PyObject * |
| 9936 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9937 | Py_ssize_t left, |
| 9938 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9941 | PyObject *u; |
| 9942 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9943 | int kind; |
| 9944 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9945 | |
| 9946 | if (left < 0) |
| 9947 | left = 0; |
| 9948 | if (right < 0) |
| 9949 | right = 0; |
| 9950 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9951 | if (left == 0 && right == 0) |
| 9952 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9953 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9954 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9955 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9956 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9957 | return NULL; |
| 9958 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9960 | if (fill > maxchar) |
| 9961 | maxchar = fill; |
| 9962 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9963 | if (!u) |
| 9964 | return NULL; |
| 9965 | |
| 9966 | kind = PyUnicode_KIND(u); |
| 9967 | data = PyUnicode_DATA(u); |
| 9968 | if (left) |
| 9969 | FILL(kind, data, fill, 0, left); |
| 9970 | if (right) |
| 9971 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9972 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9973 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9974 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9975 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9978 | PyObject * |
| 9979 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9980 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9981 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9982 | |
| 9983 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9984 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9985 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9986 | if (PyUnicode_READY(string) == -1) { |
| 9987 | Py_DECREF(string); |
| 9988 | return NULL; |
| 9989 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9991 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9992 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9993 | if (PyUnicode_IS_ASCII(string)) |
| 9994 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9995 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9996 | PyUnicode_GET_LENGTH(string), keepends); |
| 9997 | else |
| 9998 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9999 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10000 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10001 | break; |
| 10002 | case PyUnicode_2BYTE_KIND: |
| 10003 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10004 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10005 | PyUnicode_GET_LENGTH(string), keepends); |
| 10006 | break; |
| 10007 | case PyUnicode_4BYTE_KIND: |
| 10008 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10009 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10010 | PyUnicode_GET_LENGTH(string), keepends); |
| 10011 | break; |
| 10012 | default: |
| 10013 | assert(0); |
| 10014 | list = 0; |
| 10015 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10016 | Py_DECREF(string); |
| 10017 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10018 | } |
| 10019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10020 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10021 | split(PyObject *self, |
| 10022 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10023 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10024 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10025 | int kind1, kind2, kind; |
| 10026 | void *buf1, *buf2; |
| 10027 | Py_ssize_t len1, len2; |
| 10028 | PyObject* out; |
| 10029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10030 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10031 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10033 | if (PyUnicode_READY(self) == -1) |
| 10034 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10036 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10037 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10039 | if (PyUnicode_IS_ASCII(self)) |
| 10040 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10041 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10042 | PyUnicode_GET_LENGTH(self), maxcount |
| 10043 | ); |
| 10044 | else |
| 10045 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10046 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10047 | PyUnicode_GET_LENGTH(self), maxcount |
| 10048 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | case PyUnicode_2BYTE_KIND: |
| 10050 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10051 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | PyUnicode_GET_LENGTH(self), maxcount |
| 10053 | ); |
| 10054 | case PyUnicode_4BYTE_KIND: |
| 10055 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10056 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | PyUnicode_GET_LENGTH(self), maxcount |
| 10058 | ); |
| 10059 | default: |
| 10060 | assert(0); |
| 10061 | return NULL; |
| 10062 | } |
| 10063 | |
| 10064 | if (PyUnicode_READY(substring) == -1) |
| 10065 | return NULL; |
| 10066 | |
| 10067 | kind1 = PyUnicode_KIND(self); |
| 10068 | kind2 = PyUnicode_KIND(substring); |
| 10069 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10070 | buf1 = PyUnicode_DATA(self); |
| 10071 | buf2 = PyUnicode_DATA(substring); |
| 10072 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10073 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10074 | if (!buf1) |
| 10075 | return NULL; |
| 10076 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10077 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | if (!buf2) { |
| 10079 | if (kind1 != kind) PyMem_Free(buf1); |
| 10080 | return NULL; |
| 10081 | } |
| 10082 | len1 = PyUnicode_GET_LENGTH(self); |
| 10083 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10084 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10085 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10087 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10088 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10089 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10090 | else |
| 10091 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10092 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | break; |
| 10094 | case PyUnicode_2BYTE_KIND: |
| 10095 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10096 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | break; |
| 10098 | case PyUnicode_4BYTE_KIND: |
| 10099 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10100 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10101 | break; |
| 10102 | default: |
| 10103 | out = NULL; |
| 10104 | } |
| 10105 | if (kind1 != kind) |
| 10106 | PyMem_Free(buf1); |
| 10107 | if (kind2 != kind) |
| 10108 | PyMem_Free(buf2); |
| 10109 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10110 | } |
| 10111 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10112 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10113 | rsplit(PyObject *self, |
| 10114 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10115 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10116 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | int kind1, kind2, kind; |
| 10118 | void *buf1, *buf2; |
| 10119 | Py_ssize_t len1, len2; |
| 10120 | PyObject* out; |
| 10121 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10122 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10123 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10124 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | if (PyUnicode_READY(self) == -1) |
| 10126 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10129 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10131 | if (PyUnicode_IS_ASCII(self)) |
| 10132 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10133 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10134 | PyUnicode_GET_LENGTH(self), maxcount |
| 10135 | ); |
| 10136 | else |
| 10137 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10138 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10139 | PyUnicode_GET_LENGTH(self), maxcount |
| 10140 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | case PyUnicode_2BYTE_KIND: |
| 10142 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10143 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10144 | PyUnicode_GET_LENGTH(self), maxcount |
| 10145 | ); |
| 10146 | case PyUnicode_4BYTE_KIND: |
| 10147 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10148 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10149 | PyUnicode_GET_LENGTH(self), maxcount |
| 10150 | ); |
| 10151 | default: |
| 10152 | assert(0); |
| 10153 | return NULL; |
| 10154 | } |
| 10155 | |
| 10156 | if (PyUnicode_READY(substring) == -1) |
| 10157 | return NULL; |
| 10158 | |
| 10159 | kind1 = PyUnicode_KIND(self); |
| 10160 | kind2 = PyUnicode_KIND(substring); |
| 10161 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10162 | buf1 = PyUnicode_DATA(self); |
| 10163 | buf2 = PyUnicode_DATA(substring); |
| 10164 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10165 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | if (!buf1) |
| 10167 | return NULL; |
| 10168 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10169 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | if (!buf2) { |
| 10171 | if (kind1 != kind) PyMem_Free(buf1); |
| 10172 | return NULL; |
| 10173 | } |
| 10174 | len1 = PyUnicode_GET_LENGTH(self); |
| 10175 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10176 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10177 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10179 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10180 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10181 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10182 | else |
| 10183 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10184 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10185 | break; |
| 10186 | case PyUnicode_2BYTE_KIND: |
| 10187 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10188 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10189 | break; |
| 10190 | case PyUnicode_4BYTE_KIND: |
| 10191 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10192 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10193 | break; |
| 10194 | default: |
| 10195 | out = NULL; |
| 10196 | } |
| 10197 | if (kind1 != kind) |
| 10198 | PyMem_Free(buf1); |
| 10199 | if (kind2 != kind) |
| 10200 | PyMem_Free(buf2); |
| 10201 | return out; |
| 10202 | } |
| 10203 | |
| 10204 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10205 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10206 | 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] | 10207 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10208 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10209 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10210 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10211 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10212 | else |
| 10213 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10214 | case PyUnicode_2BYTE_KIND: |
| 10215 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10216 | case PyUnicode_4BYTE_KIND: |
| 10217 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10218 | } |
| 10219 | assert(0); |
| 10220 | return -1; |
| 10221 | } |
| 10222 | |
| 10223 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10224 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10225 | 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] | 10226 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10227 | switch (kind) { |
| 10228 | case PyUnicode_1BYTE_KIND: |
| 10229 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10230 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10231 | else |
| 10232 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10233 | case PyUnicode_2BYTE_KIND: |
| 10234 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10235 | case PyUnicode_4BYTE_KIND: |
| 10236 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10237 | } |
| 10238 | assert(0); |
| 10239 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10240 | } |
| 10241 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10242 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | replace(PyObject *self, PyObject *str1, |
| 10244 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10245 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | PyObject *u; |
| 10247 | char *sbuf = PyUnicode_DATA(self); |
| 10248 | char *buf1 = PyUnicode_DATA(str1); |
| 10249 | char *buf2 = PyUnicode_DATA(str2); |
| 10250 | int srelease = 0, release1 = 0, release2 = 0; |
| 10251 | int skind = PyUnicode_KIND(self); |
| 10252 | int kind1 = PyUnicode_KIND(str1); |
| 10253 | int kind2 = PyUnicode_KIND(str2); |
| 10254 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10255 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10256 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10257 | int mayshrink; |
| 10258 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10259 | |
| 10260 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10261 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10262 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10263 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10264 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10265 | if (str1 == str2) |
| 10266 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | if (skind < kind1) |
| 10268 | /* substring too wide to be present */ |
| 10269 | goto nothing; |
| 10270 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10271 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10272 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10273 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10274 | result string. */ |
| 10275 | mayshrink = (maxchar_str2 < maxchar); |
| 10276 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10278 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10279 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10281 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10283 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10284 | Py_UCS4 u1, u2; |
| 10285 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10286 | Py_ssize_t index, pos; |
| 10287 | char *src; |
| 10288 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10290 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10291 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10292 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10294 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10295 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10296 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10297 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10299 | |
| 10300 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10301 | index = 0; |
| 10302 | src = sbuf; |
| 10303 | while (--maxcount) |
| 10304 | { |
| 10305 | pos++; |
| 10306 | src += pos * PyUnicode_KIND(self); |
| 10307 | slen -= pos; |
| 10308 | index += pos; |
| 10309 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10310 | if (pos < 0) |
| 10311 | break; |
| 10312 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10313 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10314 | } |
| 10315 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10316 | int rkind = skind; |
| 10317 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10318 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10320 | if (kind1 < rkind) { |
| 10321 | /* widen substring */ |
| 10322 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10323 | if (!buf1) goto error; |
| 10324 | release1 = 1; |
| 10325 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10326 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10327 | if (i < 0) |
| 10328 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10329 | if (rkind > kind2) { |
| 10330 | /* widen replacement */ |
| 10331 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10332 | if (!buf2) goto error; |
| 10333 | release2 = 1; |
| 10334 | } |
| 10335 | else if (rkind < kind2) { |
| 10336 | /* widen self and buf1 */ |
| 10337 | rkind = kind2; |
| 10338 | if (release1) PyMem_Free(buf1); |
| 10339 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10340 | if (!sbuf) goto error; |
| 10341 | srelease = 1; |
| 10342 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10343 | if (!buf1) goto error; |
| 10344 | release1 = 1; |
| 10345 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10346 | u = PyUnicode_New(slen, maxchar); |
| 10347 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10348 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10349 | assert(PyUnicode_KIND(u) == rkind); |
| 10350 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10351 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10352 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10353 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10354 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10355 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10356 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10358 | |
| 10359 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10360 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10361 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10362 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10363 | if (i == -1) |
| 10364 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10365 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10366 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10367 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10368 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10369 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10371 | } |
| 10372 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10373 | Py_ssize_t n, i, j, ires; |
| 10374 | Py_ssize_t product, new_size; |
| 10375 | int rkind = skind; |
| 10376 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10379 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10380 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10381 | if (!buf1) goto error; |
| 10382 | release1 = 1; |
| 10383 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10384 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10385 | if (n == 0) |
| 10386 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10387 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10388 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10389 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10390 | if (!buf2) goto error; |
| 10391 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10392 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10393 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10394 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | rkind = kind2; |
| 10396 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10397 | if (!sbuf) goto error; |
| 10398 | srelease = 1; |
| 10399 | if (release1) PyMem_Free(buf1); |
| 10400 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10401 | if (!buf1) goto error; |
| 10402 | release1 = 1; |
| 10403 | } |
| 10404 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10405 | PyUnicode_GET_LENGTH(str1))); */ |
| 10406 | product = n * (len2-len1); |
| 10407 | if ((product / (len2-len1)) != n) { |
| 10408 | PyErr_SetString(PyExc_OverflowError, |
| 10409 | "replace string is too long"); |
| 10410 | goto error; |
| 10411 | } |
| 10412 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10413 | if (new_size == 0) { |
| 10414 | Py_INCREF(unicode_empty); |
| 10415 | u = unicode_empty; |
| 10416 | goto done; |
| 10417 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10419 | PyErr_SetString(PyExc_OverflowError, |
| 10420 | "replace string is too long"); |
| 10421 | goto error; |
| 10422 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10423 | u = PyUnicode_New(new_size, maxchar); |
| 10424 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10425 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10426 | assert(PyUnicode_KIND(u) == rkind); |
| 10427 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10428 | ires = i = 0; |
| 10429 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10430 | while (n-- > 0) { |
| 10431 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10432 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10433 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10434 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10435 | if (j == -1) |
| 10436 | break; |
| 10437 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10438 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10439 | memcpy(res + rkind * ires, |
| 10440 | sbuf + rkind * i, |
| 10441 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10442 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10443 | } |
| 10444 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10446 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10448 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10450 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10451 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10452 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10453 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10454 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10455 | memcpy(res + rkind * ires, |
| 10456 | sbuf + rkind * i, |
| 10457 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10458 | } |
| 10459 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10460 | /* interleave */ |
| 10461 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10462 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10464 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10466 | if (--n <= 0) |
| 10467 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10468 | memcpy(res + rkind * ires, |
| 10469 | sbuf + rkind * i, |
| 10470 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | ires++; |
| 10472 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10473 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10474 | memcpy(res + rkind * ires, |
| 10475 | sbuf + rkind * i, |
| 10476 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10477 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10478 | } |
| 10479 | |
| 10480 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10481 | unicode_adjust_maxchar(&u); |
| 10482 | if (u == NULL) |
| 10483 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10485 | |
| 10486 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10487 | if (srelease) |
| 10488 | PyMem_FREE(sbuf); |
| 10489 | if (release1) |
| 10490 | PyMem_FREE(buf1); |
| 10491 | if (release2) |
| 10492 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10493 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10495 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10496 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10497 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | if (srelease) |
| 10499 | PyMem_FREE(sbuf); |
| 10500 | if (release1) |
| 10501 | PyMem_FREE(buf1); |
| 10502 | if (release2) |
| 10503 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10504 | return unicode_result_unchanged(self); |
| 10505 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10506 | error: |
| 10507 | if (srelease && sbuf) |
| 10508 | PyMem_FREE(sbuf); |
| 10509 | if (release1 && buf1) |
| 10510 | PyMem_FREE(buf1); |
| 10511 | if (release2 && buf2) |
| 10512 | PyMem_FREE(buf2); |
| 10513 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10514 | } |
| 10515 | |
| 10516 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10518 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10519 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10520 | \n\ |
| 10521 | 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] | 10522 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10523 | |
| 10524 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10525 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10526 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10527 | if (PyUnicode_READY(self) == -1) |
| 10528 | return NULL; |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10529 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10532 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10533 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10534 | \n\ |
| 10535 | 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] | 10536 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10537 | |
| 10538 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10539 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10540 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10541 | if (PyUnicode_READY(self) == -1) |
| 10542 | return NULL; |
| 10543 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10544 | return unicode_result_unchanged(self); |
| 10545 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10546 | } |
| 10547 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10548 | PyDoc_STRVAR(casefold__doc__, |
| 10549 | "S.casefold() -> str\n\ |
| 10550 | \n\ |
| 10551 | Return a version of S suitable for caseless comparisons."); |
| 10552 | |
| 10553 | static PyObject * |
| 10554 | unicode_casefold(PyObject *self) |
| 10555 | { |
| 10556 | if (PyUnicode_READY(self) == -1) |
| 10557 | return NULL; |
| 10558 | if (PyUnicode_IS_ASCII(self)) |
| 10559 | return ascii_upper_or_lower(self, 1); |
| 10560 | return case_operation(self, do_casefold); |
| 10561 | } |
| 10562 | |
| 10563 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10564 | /* Argument converter. Coerces to a single unicode character */ |
| 10565 | |
| 10566 | static int |
| 10567 | convert_uc(PyObject *obj, void *addr) |
| 10568 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10569 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10570 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10571 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10572 | uniobj = PyUnicode_FromObject(obj); |
| 10573 | if (uniobj == NULL) { |
| 10574 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10575 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10576 | return 0; |
| 10577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10578 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10579 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10580 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10581 | Py_DECREF(uniobj); |
| 10582 | return 0; |
| 10583 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10585 | Py_DECREF(uniobj); |
| 10586 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10587 | } |
| 10588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10589 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10590 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10591 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10592 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10593 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10594 | |
| 10595 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10596 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10597 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10598 | Py_ssize_t marg, left; |
| 10599 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10600 | Py_UCS4 fillchar = ' '; |
| 10601 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10602 | 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] | 10603 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10605 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10606 | return NULL; |
| 10607 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10608 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10609 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10611 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10612 | left = marg / 2 + (marg & width & 1); |
| 10613 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10614 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10615 | } |
| 10616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10617 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10618 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10619 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10620 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10621 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10622 | int kind1, kind2; |
| 10623 | void *data1, *data2; |
| 10624 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10625 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10626 | kind1 = PyUnicode_KIND(str1); |
| 10627 | kind2 = PyUnicode_KIND(str2); |
| 10628 | data1 = PyUnicode_DATA(str1); |
| 10629 | data2 = PyUnicode_DATA(str2); |
| 10630 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10631 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10632 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10633 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10634 | Py_UCS4 c1, c2; |
| 10635 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10636 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10637 | |
| 10638 | if (c1 != c2) |
| 10639 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10640 | } |
| 10641 | |
| 10642 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10643 | } |
| 10644 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10645 | int |
| 10646 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10647 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10648 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10649 | if (PyUnicode_READY(left) == -1 || |
| 10650 | PyUnicode_READY(right) == -1) |
| 10651 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10652 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10654 | PyErr_Format(PyExc_TypeError, |
| 10655 | "Can't compare %.100s and %.100s", |
| 10656 | left->ob_type->tp_name, |
| 10657 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10658 | return -1; |
| 10659 | } |
| 10660 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10661 | int |
| 10662 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10663 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | Py_ssize_t i; |
| 10665 | int kind; |
| 10666 | void *data; |
| 10667 | Py_UCS4 chr; |
| 10668 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10669 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10670 | if (PyUnicode_READY(uni) == -1) |
| 10671 | return -1; |
| 10672 | kind = PyUnicode_KIND(uni); |
| 10673 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10674 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10675 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10676 | if (chr != str[i]) |
| 10677 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10678 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10679 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10680 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10681 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10682 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10683 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10684 | return 0; |
| 10685 | } |
| 10686 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10687 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10688 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10689 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10690 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10691 | PyObject * |
| 10692 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10693 | { |
| 10694 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10695 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10696 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10697 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10698 | if (PyUnicode_READY(left) == -1 || |
| 10699 | PyUnicode_READY(right) == -1) |
| 10700 | return NULL; |
| 10701 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10702 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10703 | if (op == Py_EQ) { |
| 10704 | Py_INCREF(Py_False); |
| 10705 | return Py_False; |
| 10706 | } |
| 10707 | if (op == Py_NE) { |
| 10708 | Py_INCREF(Py_True); |
| 10709 | return Py_True; |
| 10710 | } |
| 10711 | } |
| 10712 | if (left == right) |
| 10713 | result = 0; |
| 10714 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10715 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10716 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10717 | /* Convert the return value to a Boolean */ |
| 10718 | switch (op) { |
| 10719 | case Py_EQ: |
| 10720 | v = TEST_COND(result == 0); |
| 10721 | break; |
| 10722 | case Py_NE: |
| 10723 | v = TEST_COND(result != 0); |
| 10724 | break; |
| 10725 | case Py_LE: |
| 10726 | v = TEST_COND(result <= 0); |
| 10727 | break; |
| 10728 | case Py_GE: |
| 10729 | v = TEST_COND(result >= 0); |
| 10730 | break; |
| 10731 | case Py_LT: |
| 10732 | v = TEST_COND(result == -1); |
| 10733 | break; |
| 10734 | case Py_GT: |
| 10735 | v = TEST_COND(result == 1); |
| 10736 | break; |
| 10737 | default: |
| 10738 | PyErr_BadArgument(); |
| 10739 | return NULL; |
| 10740 | } |
| 10741 | Py_INCREF(v); |
| 10742 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10743 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10744 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10745 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10746 | } |
| 10747 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10748 | int |
| 10749 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10750 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10751 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10752 | int kind1, kind2, kind; |
| 10753 | void *buf1, *buf2; |
| 10754 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10755 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10756 | |
| 10757 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10758 | sub = PyUnicode_FromObject(element); |
| 10759 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10760 | PyErr_Format(PyExc_TypeError, |
| 10761 | "'in <string>' requires string as left operand, not %s", |
| 10762 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10763 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10764 | } |
| 10765 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10766 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10767 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10768 | Py_DECREF(sub); |
| 10769 | return -1; |
| 10770 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10771 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10772 | Py_DECREF(sub); |
| 10773 | Py_DECREF(str); |
| 10774 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10775 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10776 | kind1 = PyUnicode_KIND(str); |
| 10777 | kind2 = PyUnicode_KIND(sub); |
| 10778 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10779 | buf1 = PyUnicode_DATA(str); |
| 10780 | buf2 = PyUnicode_DATA(sub); |
| 10781 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10782 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10783 | if (!buf1) { |
| 10784 | Py_DECREF(sub); |
| 10785 | return -1; |
| 10786 | } |
| 10787 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10788 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10789 | if (!buf2) { |
| 10790 | Py_DECREF(sub); |
| 10791 | if (kind1 != kind) PyMem_Free(buf1); |
| 10792 | return -1; |
| 10793 | } |
| 10794 | len1 = PyUnicode_GET_LENGTH(str); |
| 10795 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10796 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10797 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10798 | case PyUnicode_1BYTE_KIND: |
| 10799 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10800 | break; |
| 10801 | case PyUnicode_2BYTE_KIND: |
| 10802 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10803 | break; |
| 10804 | case PyUnicode_4BYTE_KIND: |
| 10805 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10806 | break; |
| 10807 | default: |
| 10808 | result = -1; |
| 10809 | assert(0); |
| 10810 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10811 | |
| 10812 | Py_DECREF(str); |
| 10813 | Py_DECREF(sub); |
| 10814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10815 | if (kind1 != kind) |
| 10816 | PyMem_Free(buf1); |
| 10817 | if (kind2 != kind) |
| 10818 | PyMem_Free(buf2); |
| 10819 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10820 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10821 | } |
| 10822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10823 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10824 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10825 | PyObject * |
| 10826 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10827 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10828 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10829 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10830 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10831 | |
| 10832 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10833 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10834 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10835 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10836 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10837 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10838 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10839 | |
| 10840 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10841 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10842 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10844 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10845 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10846 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10847 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10848 | } |
| 10849 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10850 | u_len = PyUnicode_GET_LENGTH(u); |
| 10851 | v_len = PyUnicode_GET_LENGTH(v); |
| 10852 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10853 | PyErr_SetString(PyExc_OverflowError, |
| 10854 | "strings are too large to concat"); |
| 10855 | goto onError; |
| 10856 | } |
| 10857 | new_len = u_len + v_len; |
| 10858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10859 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10860 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10861 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10862 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10863 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10864 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10866 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10867 | copy_characters(w, 0, u, 0, u_len); |
| 10868 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | Py_DECREF(u); |
| 10870 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10871 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10872 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10873 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10874 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10875 | Py_XDECREF(u); |
| 10876 | Py_XDECREF(v); |
| 10877 | return NULL; |
| 10878 | } |
| 10879 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10880 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10881 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10882 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10883 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10884 | Py_UCS4 maxchar, maxchar2; |
| 10885 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10886 | |
| 10887 | if (p_left == NULL) { |
| 10888 | if (!PyErr_Occurred()) |
| 10889 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10890 | return; |
| 10891 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10892 | left = *p_left; |
| 10893 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10894 | if (!PyErr_Occurred()) |
| 10895 | PyErr_BadInternalCall(); |
| 10896 | goto error; |
| 10897 | } |
| 10898 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10899 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10900 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10901 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10902 | goto error; |
| 10903 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10904 | /* Shortcuts */ |
| 10905 | if (left == unicode_empty) { |
| 10906 | Py_DECREF(left); |
| 10907 | Py_INCREF(right); |
| 10908 | *p_left = right; |
| 10909 | return; |
| 10910 | } |
| 10911 | if (right == unicode_empty) |
| 10912 | return; |
| 10913 | |
| 10914 | left_len = PyUnicode_GET_LENGTH(left); |
| 10915 | right_len = PyUnicode_GET_LENGTH(right); |
| 10916 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10917 | PyErr_SetString(PyExc_OverflowError, |
| 10918 | "strings are too large to concat"); |
| 10919 | goto error; |
| 10920 | } |
| 10921 | new_len = left_len + right_len; |
| 10922 | |
| 10923 | if (unicode_modifiable(left) |
| 10924 | && PyUnicode_CheckExact(right) |
| 10925 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10926 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10927 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10928 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10929 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10930 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10931 | { |
| 10932 | /* append inplace */ |
| 10933 | if (unicode_resize(p_left, new_len) != 0) { |
| 10934 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10935 | * deallocated so it cannot be put back into |
| 10936 | * 'variable'. The MemoryError is raised when there |
| 10937 | * is no value in 'variable', which might (very |
| 10938 | * remotely) be a cause of incompatibilities. |
| 10939 | */ |
| 10940 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10941 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10942 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10943 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10944 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10945 | else { |
| 10946 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10947 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
| 10948 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10949 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10950 | /* Concat the two Unicode strings */ |
| 10951 | res = PyUnicode_New(new_len, maxchar); |
| 10952 | if (res == NULL) |
| 10953 | goto error; |
| 10954 | copy_characters(res, 0, left, 0, left_len); |
| 10955 | copy_characters(res, left_len, right, 0, right_len); |
| 10956 | Py_DECREF(left); |
| 10957 | *p_left = res; |
| 10958 | } |
| 10959 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10960 | return; |
| 10961 | |
| 10962 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10963 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10964 | } |
| 10965 | |
| 10966 | void |
| 10967 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10968 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10969 | PyUnicode_Append(pleft, right); |
| 10970 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10971 | } |
| 10972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10973 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10974 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10976 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10977 | 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] | 10978 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10979 | |
| 10980 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10981 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10983 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10984 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10985 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10987 | int kind1, kind2, kind; |
| 10988 | void *buf1, *buf2; |
| 10989 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10990 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10991 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10992 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10993 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10994 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10995 | kind1 = PyUnicode_KIND(self); |
| 10996 | kind2 = PyUnicode_KIND(substring); |
| 10997 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10998 | buf1 = PyUnicode_DATA(self); |
| 10999 | buf2 = PyUnicode_DATA(substring); |
| 11000 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11001 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11002 | if (!buf1) { |
| 11003 | Py_DECREF(substring); |
| 11004 | return NULL; |
| 11005 | } |
| 11006 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11007 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11008 | if (!buf2) { |
| 11009 | Py_DECREF(substring); |
| 11010 | if (kind1 != kind) PyMem_Free(buf1); |
| 11011 | return NULL; |
| 11012 | } |
| 11013 | len1 = PyUnicode_GET_LENGTH(self); |
| 11014 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11015 | |
| 11016 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11017 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | case PyUnicode_1BYTE_KIND: |
| 11019 | iresult = ucs1lib_count( |
| 11020 | ((Py_UCS1*)buf1) + start, end - start, |
| 11021 | buf2, len2, PY_SSIZE_T_MAX |
| 11022 | ); |
| 11023 | break; |
| 11024 | case PyUnicode_2BYTE_KIND: |
| 11025 | iresult = ucs2lib_count( |
| 11026 | ((Py_UCS2*)buf1) + start, end - start, |
| 11027 | buf2, len2, PY_SSIZE_T_MAX |
| 11028 | ); |
| 11029 | break; |
| 11030 | case PyUnicode_4BYTE_KIND: |
| 11031 | iresult = ucs4lib_count( |
| 11032 | ((Py_UCS4*)buf1) + start, end - start, |
| 11033 | buf2, len2, PY_SSIZE_T_MAX |
| 11034 | ); |
| 11035 | break; |
| 11036 | default: |
| 11037 | assert(0); iresult = 0; |
| 11038 | } |
| 11039 | |
| 11040 | result = PyLong_FromSsize_t(iresult); |
| 11041 | |
| 11042 | if (kind1 != kind) |
| 11043 | PyMem_Free(buf1); |
| 11044 | if (kind2 != kind) |
| 11045 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | |
| 11047 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | return result; |
| 11050 | } |
| 11051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11052 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11053 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11054 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11055 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11056 | 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] | 11057 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11058 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11059 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11060 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11061 | |
| 11062 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11063 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11065 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 | char *encoding = NULL; |
| 11067 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11068 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11069 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11070 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11072 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11073 | } |
| 11074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11075 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11076 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | \n\ |
| 11078 | 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] | 11079 | 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] | 11080 | |
| 11081 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11082 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11083 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11084 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11085 | Py_UCS4 ch; |
| 11086 | PyObject *u; |
| 11087 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11088 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11089 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11090 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11091 | |
| 11092 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11093 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11095 | if (PyUnicode_READY(self) == -1) |
| 11096 | return NULL; |
| 11097 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11098 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11099 | src_len = PyUnicode_GET_LENGTH(self); |
| 11100 | i = j = line_pos = 0; |
| 11101 | kind = PyUnicode_KIND(self); |
| 11102 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11103 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11104 | for (; i < src_len; i++) { |
| 11105 | ch = PyUnicode_READ(kind, src_data, i); |
| 11106 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11107 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11108 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11109 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11110 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11111 | goto overflow; |
| 11112 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11113 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11115 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11117 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11118 | goto overflow; |
| 11119 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11121 | if (ch == '\n' || ch == '\r') |
| 11122 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11124 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11125 | if (!found) |
| 11126 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11128 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11129 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11130 | if (!u) |
| 11131 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11132 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11133 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11134 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11136 | for (; i < src_len; i++) { |
| 11137 | ch = PyUnicode_READ(kind, src_data, i); |
| 11138 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11139 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11140 | incr = tabsize - (line_pos % tabsize); |
| 11141 | line_pos += incr; |
| 11142 | while (incr--) { |
| 11143 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11144 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11145 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11146 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11147 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11148 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11149 | line_pos++; |
| 11150 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11151 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11152 | if (ch == '\n' || ch == '\r') |
| 11153 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11155 | } |
| 11156 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11157 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11158 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11159 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11160 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11161 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | } |
| 11163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11164 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11165 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11166 | \n\ |
| 11167 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11168 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | arguments start and end are interpreted as in slice notation.\n\ |
| 11170 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11171 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | |
| 11173 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11174 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11176 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11177 | Py_ssize_t start; |
| 11178 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11179 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11181 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11182 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | if (PyUnicode_READY(self) == -1) |
| 11186 | return NULL; |
| 11187 | if (PyUnicode_READY(substring) == -1) |
| 11188 | return NULL; |
| 11189 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11190 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | |
| 11192 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11193 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11194 | if (result == -2) |
| 11195 | return NULL; |
| 11196 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11197 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11198 | } |
| 11199 | |
| 11200 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11201 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11203 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11204 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11206 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | } |
| 11208 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11209 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11210 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11211 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11212 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11214 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11215 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11217 | if (_PyUnicode_HASH(self) != -1) |
| 11218 | return _PyUnicode_HASH(self); |
| 11219 | if (PyUnicode_READY(self) == -1) |
| 11220 | return -1; |
| 11221 | len = PyUnicode_GET_LENGTH(self); |
| 11222 | |
| 11223 | /* The hash function as a macro, gets expanded three times below. */ |
| 11224 | #define HASH(P) \ |
| 11225 | x = (Py_uhash_t)*P << 7; \ |
| 11226 | while (--len >= 0) \ |
Gregory P. Smith | f5b62a9 | 2012-01-14 15:45:13 -0800 | [diff] [blame] | 11227 | x = (_PyHASH_MULTIPLIER*x) ^ (Py_uhash_t)*P++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11228 | |
| 11229 | switch (PyUnicode_KIND(self)) { |
| 11230 | case PyUnicode_1BYTE_KIND: { |
| 11231 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11232 | HASH(c); |
| 11233 | break; |
| 11234 | } |
| 11235 | case PyUnicode_2BYTE_KIND: { |
| 11236 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11237 | HASH(s); |
| 11238 | break; |
| 11239 | } |
| 11240 | default: { |
| 11241 | Py_UCS4 *l; |
| 11242 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11243 | "Impossible switch case in unicode_hash"); |
| 11244 | l = PyUnicode_4BYTE_DATA(self); |
| 11245 | HASH(l); |
| 11246 | break; |
| 11247 | } |
| 11248 | } |
| 11249 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11250 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11251 | if (x == -1) |
| 11252 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11253 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11254 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11258 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11259 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11261 | 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] | 11262 | |
| 11263 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11264 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11266 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11267 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11268 | Py_ssize_t start; |
| 11269 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11271 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11272 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11275 | if (PyUnicode_READY(self) == -1) |
| 11276 | return NULL; |
| 11277 | if (PyUnicode_READY(substring) == -1) |
| 11278 | return NULL; |
| 11279 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11280 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | |
| 11282 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11283 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11284 | if (result == -2) |
| 11285 | return NULL; |
| 11286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | if (result < 0) { |
| 11288 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11289 | return NULL; |
| 11290 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11291 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11292 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | } |
| 11294 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11295 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11296 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11297 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11298 | 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] | 11299 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | |
| 11301 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11302 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11304 | Py_ssize_t i, length; |
| 11305 | int kind; |
| 11306 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | int cased; |
| 11308 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11309 | if (PyUnicode_READY(self) == -1) |
| 11310 | return NULL; |
| 11311 | length = PyUnicode_GET_LENGTH(self); |
| 11312 | kind = PyUnicode_KIND(self); |
| 11313 | data = PyUnicode_DATA(self); |
| 11314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11316 | if (length == 1) |
| 11317 | return PyBool_FromLong( |
| 11318 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11320 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11321 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11322 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11325 | for (i = 0; i < length; i++) { |
| 11326 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11327 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11328 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11329 | return PyBool_FromLong(0); |
| 11330 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11331 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11333 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11334 | } |
| 11335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11336 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11337 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11339 | 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] | 11340 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | |
| 11342 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11343 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11344 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | Py_ssize_t i, length; |
| 11346 | int kind; |
| 11347 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 | int cased; |
| 11349 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11350 | if (PyUnicode_READY(self) == -1) |
| 11351 | return NULL; |
| 11352 | length = PyUnicode_GET_LENGTH(self); |
| 11353 | kind = PyUnicode_KIND(self); |
| 11354 | data = PyUnicode_DATA(self); |
| 11355 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11357 | if (length == 1) |
| 11358 | return PyBool_FromLong( |
| 11359 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11360 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11361 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11363 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11364 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11366 | for (i = 0; i < length; i++) { |
| 11367 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11368 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11369 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11370 | return PyBool_FromLong(0); |
| 11371 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11372 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11374 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | } |
| 11376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11377 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11378 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11380 | Return True if S is a titlecased string and there is at least one\n\ |
| 11381 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11382 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11383 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11384 | |
| 11385 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11386 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11388 | Py_ssize_t i, length; |
| 11389 | int kind; |
| 11390 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11391 | int cased, previous_is_cased; |
| 11392 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11393 | if (PyUnicode_READY(self) == -1) |
| 11394 | return NULL; |
| 11395 | length = PyUnicode_GET_LENGTH(self); |
| 11396 | kind = PyUnicode_KIND(self); |
| 11397 | data = PyUnicode_DATA(self); |
| 11398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11400 | if (length == 1) { |
| 11401 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11402 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11403 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11404 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11406 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11407 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11408 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11410 | cased = 0; |
| 11411 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11412 | for (i = 0; i < length; i++) { |
| 11413 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11414 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11415 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11416 | if (previous_is_cased) |
| 11417 | return PyBool_FromLong(0); |
| 11418 | previous_is_cased = 1; |
| 11419 | cased = 1; |
| 11420 | } |
| 11421 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11422 | if (!previous_is_cased) |
| 11423 | return PyBool_FromLong(0); |
| 11424 | previous_is_cased = 1; |
| 11425 | cased = 1; |
| 11426 | } |
| 11427 | else |
| 11428 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11430 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | } |
| 11432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11433 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11434 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11436 | Return True if all characters in S are whitespace\n\ |
| 11437 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | |
| 11439 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11440 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11442 | Py_ssize_t i, length; |
| 11443 | int kind; |
| 11444 | void *data; |
| 11445 | |
| 11446 | if (PyUnicode_READY(self) == -1) |
| 11447 | return NULL; |
| 11448 | length = PyUnicode_GET_LENGTH(self); |
| 11449 | kind = PyUnicode_KIND(self); |
| 11450 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | if (length == 1) |
| 11454 | return PyBool_FromLong( |
| 11455 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11457 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11459 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11461 | for (i = 0; i < length; i++) { |
| 11462 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11463 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11464 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11466 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | } |
| 11468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11469 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11471 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11472 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11473 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11474 | |
| 11475 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11476 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11477 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11478 | Py_ssize_t i, length; |
| 11479 | int kind; |
| 11480 | void *data; |
| 11481 | |
| 11482 | if (PyUnicode_READY(self) == -1) |
| 11483 | return NULL; |
| 11484 | length = PyUnicode_GET_LENGTH(self); |
| 11485 | kind = PyUnicode_KIND(self); |
| 11486 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11487 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11488 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 | if (length == 1) |
| 11490 | return PyBool_FromLong( |
| 11491 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11492 | |
| 11493 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11494 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11495 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11496 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11497 | for (i = 0; i < length; i++) { |
| 11498 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11500 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11501 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11502 | } |
| 11503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11504 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11505 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11506 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11507 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11508 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11509 | |
| 11510 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11511 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11512 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11513 | int kind; |
| 11514 | void *data; |
| 11515 | Py_ssize_t len, i; |
| 11516 | |
| 11517 | if (PyUnicode_READY(self) == -1) |
| 11518 | return NULL; |
| 11519 | |
| 11520 | kind = PyUnicode_KIND(self); |
| 11521 | data = PyUnicode_DATA(self); |
| 11522 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11523 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11524 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 | if (len == 1) { |
| 11526 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11527 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11528 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11529 | |
| 11530 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11531 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11532 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11533 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11534 | for (i = 0; i < len; i++) { |
| 11535 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11536 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11537 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11538 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11539 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11540 | } |
| 11541 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11542 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11543 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11544 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11545 | 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] | 11546 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | |
| 11548 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11549 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11550 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11551 | Py_ssize_t i, length; |
| 11552 | int kind; |
| 11553 | void *data; |
| 11554 | |
| 11555 | if (PyUnicode_READY(self) == -1) |
| 11556 | return NULL; |
| 11557 | length = PyUnicode_GET_LENGTH(self); |
| 11558 | kind = PyUnicode_KIND(self); |
| 11559 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11560 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | if (length == 1) |
| 11563 | return PyBool_FromLong( |
| 11564 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11566 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11567 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11568 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11569 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11570 | for (i = 0; i < length; i++) { |
| 11571 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11572 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11573 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11574 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11575 | } |
| 11576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11577 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11578 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11579 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11580 | Return True if all characters in S are digits\n\ |
| 11581 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | |
| 11583 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11584 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | Py_ssize_t i, length; |
| 11587 | int kind; |
| 11588 | void *data; |
| 11589 | |
| 11590 | if (PyUnicode_READY(self) == -1) |
| 11591 | return NULL; |
| 11592 | length = PyUnicode_GET_LENGTH(self); |
| 11593 | kind = PyUnicode_KIND(self); |
| 11594 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11596 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11597 | if (length == 1) { |
| 11598 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11599 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11600 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11601 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11602 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11603 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11604 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11606 | for (i = 0; i < length; i++) { |
| 11607 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11608 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11609 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11610 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | } |
| 11612 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11613 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11614 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11616 | 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] | 11617 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11618 | |
| 11619 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11620 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11621 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | Py_ssize_t i, length; |
| 11623 | int kind; |
| 11624 | void *data; |
| 11625 | |
| 11626 | if (PyUnicode_READY(self) == -1) |
| 11627 | return NULL; |
| 11628 | length = PyUnicode_GET_LENGTH(self); |
| 11629 | kind = PyUnicode_KIND(self); |
| 11630 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11632 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | if (length == 1) |
| 11634 | return PyBool_FromLong( |
| 11635 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11636 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11637 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11638 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11639 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | for (i = 0; i < length; i++) { |
| 11642 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11643 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11644 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11645 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11646 | } |
| 11647 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11648 | int |
| 11649 | PyUnicode_IsIdentifier(PyObject *self) |
| 11650 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11651 | int kind; |
| 11652 | void *data; |
| 11653 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11654 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11655 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11656 | if (PyUnicode_READY(self) == -1) { |
| 11657 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11658 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | } |
| 11660 | |
| 11661 | /* Special case for empty strings */ |
| 11662 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11663 | return 0; |
| 11664 | kind = PyUnicode_KIND(self); |
| 11665 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11666 | |
| 11667 | /* PEP 3131 says that the first character must be in |
| 11668 | XID_Start and subsequent characters in XID_Continue, |
| 11669 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11670 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11671 | letters, digits, underscore). However, given the current |
| 11672 | definition of XID_Start and XID_Continue, it is sufficient |
| 11673 | to check just for these, except that _ must be allowed |
| 11674 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11675 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11676 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11677 | return 0; |
| 11678 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11679 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11680 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11681 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11682 | return 1; |
| 11683 | } |
| 11684 | |
| 11685 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11686 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11687 | \n\ |
| 11688 | Return True if S is a valid identifier according\n\ |
| 11689 | to the language definition."); |
| 11690 | |
| 11691 | static PyObject* |
| 11692 | unicode_isidentifier(PyObject *self) |
| 11693 | { |
| 11694 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11695 | } |
| 11696 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11697 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11699 | \n\ |
| 11700 | Return True if all characters in S are considered\n\ |
| 11701 | printable in repr() or S is empty, False otherwise."); |
| 11702 | |
| 11703 | static PyObject* |
| 11704 | unicode_isprintable(PyObject *self) |
| 11705 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11706 | Py_ssize_t i, length; |
| 11707 | int kind; |
| 11708 | void *data; |
| 11709 | |
| 11710 | if (PyUnicode_READY(self) == -1) |
| 11711 | return NULL; |
| 11712 | length = PyUnicode_GET_LENGTH(self); |
| 11713 | kind = PyUnicode_KIND(self); |
| 11714 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11715 | |
| 11716 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11717 | if (length == 1) |
| 11718 | return PyBool_FromLong( |
| 11719 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11721 | for (i = 0; i < length; i++) { |
| 11722 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11723 | Py_RETURN_FALSE; |
| 11724 | } |
| 11725 | } |
| 11726 | Py_RETURN_TRUE; |
| 11727 | } |
| 11728 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11729 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11730 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | \n\ |
| 11732 | 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] | 11733 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | |
| 11735 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11736 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11738 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | } |
| 11740 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11741 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11742 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11744 | if (PyUnicode_READY(self) == -1) |
| 11745 | return -1; |
| 11746 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | } |
| 11748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11749 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11750 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11752 | 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] | 11753 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11754 | |
| 11755 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11756 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11757 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11758 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11759 | Py_UCS4 fillchar = ' '; |
| 11760 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11761 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11762 | return NULL; |
| 11763 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11764 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11765 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11766 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11767 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11768 | return unicode_result_unchanged(self); |
| 11769 | |
| 11770 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | } |
| 11772 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11773 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11774 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11775 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11776 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11777 | |
| 11778 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11779 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11781 | if (PyUnicode_READY(self) == -1) |
| 11782 | return NULL; |
| 11783 | if (PyUnicode_IS_ASCII(self)) |
| 11784 | return ascii_upper_or_lower(self, 1); |
| 11785 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11786 | } |
| 11787 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11788 | #define LEFTSTRIP 0 |
| 11789 | #define RIGHTSTRIP 1 |
| 11790 | #define BOTHSTRIP 2 |
| 11791 | |
| 11792 | /* Arrays indexed by above */ |
| 11793 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11794 | |
| 11795 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11796 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11797 | /* externally visible for str.strip(unicode) */ |
| 11798 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11799 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11801 | void *data; |
| 11802 | int kind; |
| 11803 | Py_ssize_t i, j, len; |
| 11804 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11806 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11807 | return NULL; |
| 11808 | |
| 11809 | kind = PyUnicode_KIND(self); |
| 11810 | data = PyUnicode_DATA(self); |
| 11811 | len = PyUnicode_GET_LENGTH(self); |
| 11812 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11813 | PyUnicode_DATA(sepobj), |
| 11814 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11815 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11816 | i = 0; |
| 11817 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11818 | while (i < len && |
| 11819 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11820 | i++; |
| 11821 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11822 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11823 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11824 | j = len; |
| 11825 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11826 | do { |
| 11827 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11828 | } while (j >= i && |
| 11829 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11830 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11831 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11832 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11833 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11834 | } |
| 11835 | |
| 11836 | PyObject* |
| 11837 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11838 | { |
| 11839 | unsigned char *data; |
| 11840 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11841 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11842 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11843 | if (PyUnicode_READY(self) == -1) |
| 11844 | return NULL; |
| 11845 | |
| 11846 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11847 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11848 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11849 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11850 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11851 | length = end - start; |
| 11852 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11853 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11854 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11855 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11856 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11857 | return NULL; |
| 11858 | } |
| 11859 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11860 | if (PyUnicode_IS_ASCII(self)) { |
| 11861 | kind = PyUnicode_KIND(self); |
| 11862 | data = PyUnicode_1BYTE_DATA(self); |
| 11863 | return unicode_fromascii(data + start, length); |
| 11864 | } |
| 11865 | else { |
| 11866 | kind = PyUnicode_KIND(self); |
| 11867 | data = PyUnicode_1BYTE_DATA(self); |
| 11868 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11869 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11870 | length); |
| 11871 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11872 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11873 | |
| 11874 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11875 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11876 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11877 | int kind; |
| 11878 | void *data; |
| 11879 | Py_ssize_t len, i, j; |
| 11880 | |
| 11881 | if (PyUnicode_READY(self) == -1) |
| 11882 | return NULL; |
| 11883 | |
| 11884 | kind = PyUnicode_KIND(self); |
| 11885 | data = PyUnicode_DATA(self); |
| 11886 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11887 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11888 | i = 0; |
| 11889 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11890 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11891 | i++; |
| 11892 | } |
| 11893 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11894 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11895 | j = len; |
| 11896 | if (striptype != LEFTSTRIP) { |
| 11897 | do { |
| 11898 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11900 | j++; |
| 11901 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11902 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11903 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | } |
| 11905 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11906 | |
| 11907 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11908 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11909 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11910 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11911 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11912 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11913 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11914 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11915 | if (sep != NULL && sep != Py_None) { |
| 11916 | if (PyUnicode_Check(sep)) |
| 11917 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11918 | else { |
| 11919 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11920 | "%s arg must be None or str", |
| 11921 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11922 | return NULL; |
| 11923 | } |
| 11924 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11925 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11926 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11927 | } |
| 11928 | |
| 11929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11930 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11931 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11932 | \n\ |
| 11933 | Return a copy of the string S with leading and trailing\n\ |
| 11934 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11935 | 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] | 11936 | |
| 11937 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11938 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11939 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11940 | if (PyTuple_GET_SIZE(args) == 0) |
| 11941 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11942 | else |
| 11943 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11944 | } |
| 11945 | |
| 11946 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11947 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11948 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11949 | \n\ |
| 11950 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11951 | 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] | 11952 | |
| 11953 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11954 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11955 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11956 | if (PyTuple_GET_SIZE(args) == 0) |
| 11957 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11958 | else |
| 11959 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11960 | } |
| 11961 | |
| 11962 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11963 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11964 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11965 | \n\ |
| 11966 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11967 | 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] | 11968 | |
| 11969 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11970 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11971 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11972 | if (PyTuple_GET_SIZE(args) == 0) |
| 11973 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11974 | else |
| 11975 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11976 | } |
| 11977 | |
| 11978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11980 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11981 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11982 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11984 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11985 | if (len < 1) { |
| 11986 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11987 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11988 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11989 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11990 | /* no repeat, return original string */ |
| 11991 | if (len == 1) |
| 11992 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11993 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11994 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11995 | return NULL; |
| 11996 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11997 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11998 | PyErr_SetString(PyExc_OverflowError, |
| 11999 | "repeated string is too long"); |
| 12000 | return NULL; |
| 12001 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12002 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12003 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12004 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12005 | if (!u) |
| 12006 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12007 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12009 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12010 | const int kind = PyUnicode_KIND(str); |
| 12011 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12012 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12013 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12014 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12015 | } |
| 12016 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12017 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12018 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12019 | ucs2[n] = fill_char; |
| 12020 | } else { |
| 12021 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12022 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12023 | for (n = 0; n < len; ++n) |
| 12024 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12025 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12026 | } |
| 12027 | else { |
| 12028 | /* number of characters copied this far */ |
| 12029 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12030 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12031 | char *to = (char *) PyUnicode_DATA(u); |
| 12032 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12033 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12034 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12035 | n = (done <= nchars-done) ? done : nchars-done; |
| 12036 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12037 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12039 | } |
| 12040 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12041 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12042 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12043 | } |
| 12044 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12045 | PyObject * |
| 12046 | PyUnicode_Replace(PyObject *obj, |
| 12047 | PyObject *subobj, |
| 12048 | PyObject *replobj, |
| 12049 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12050 | { |
| 12051 | PyObject *self; |
| 12052 | PyObject *str1; |
| 12053 | PyObject *str2; |
| 12054 | PyObject *result; |
| 12055 | |
| 12056 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12057 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12058 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12059 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12060 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12061 | Py_DECREF(self); |
| 12062 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12063 | } |
| 12064 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12065 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12066 | Py_DECREF(self); |
| 12067 | Py_DECREF(str1); |
| 12068 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12069 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12070 | if (PyUnicode_READY(self) == -1 || |
| 12071 | PyUnicode_READY(str1) == -1 || |
| 12072 | PyUnicode_READY(str2) == -1) |
| 12073 | result = NULL; |
| 12074 | else |
| 12075 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12076 | Py_DECREF(self); |
| 12077 | Py_DECREF(str1); |
| 12078 | Py_DECREF(str2); |
| 12079 | return result; |
| 12080 | } |
| 12081 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12082 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12083 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12084 | \n\ |
| 12085 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12086 | old replaced by new. If the optional argument count is\n\ |
| 12087 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12088 | |
| 12089 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12090 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12091 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12092 | PyObject *str1; |
| 12093 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12094 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12095 | PyObject *result; |
| 12096 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12097 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12098 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12099 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12100 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12102 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12103 | return NULL; |
| 12104 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12105 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12106 | Py_DECREF(str1); |
| 12107 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12108 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12109 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12110 | result = NULL; |
| 12111 | else |
| 12112 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12113 | |
| 12114 | Py_DECREF(str1); |
| 12115 | Py_DECREF(str2); |
| 12116 | return result; |
| 12117 | } |
| 12118 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12119 | static PyObject * |
| 12120 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12122 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12123 | Py_ssize_t isize; |
| 12124 | Py_ssize_t osize, squote, dquote, i, o; |
| 12125 | Py_UCS4 max, quote; |
| 12126 | int ikind, okind; |
| 12127 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12129 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12130 | return NULL; |
| 12131 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12132 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12133 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12134 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12135 | /* Compute length of output, quote characters, and |
| 12136 | maximum character */ |
| 12137 | osize = 2; /* quotes */ |
| 12138 | max = 127; |
| 12139 | squote = dquote = 0; |
| 12140 | ikind = PyUnicode_KIND(unicode); |
| 12141 | for (i = 0; i < isize; i++) { |
| 12142 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12143 | switch (ch) { |
| 12144 | case '\'': squote++; osize++; break; |
| 12145 | case '"': dquote++; osize++; break; |
| 12146 | case '\\': case '\t': case '\r': case '\n': |
| 12147 | osize += 2; break; |
| 12148 | default: |
| 12149 | /* Fast-path ASCII */ |
| 12150 | if (ch < ' ' || ch == 0x7f) |
| 12151 | osize += 4; /* \xHH */ |
| 12152 | else if (ch < 0x7f) |
| 12153 | osize++; |
| 12154 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12155 | osize++; |
| 12156 | max = ch > max ? ch : max; |
| 12157 | } |
| 12158 | else if (ch < 0x100) |
| 12159 | osize += 4; /* \xHH */ |
| 12160 | else if (ch < 0x10000) |
| 12161 | osize += 6; /* \uHHHH */ |
| 12162 | else |
| 12163 | osize += 10; /* \uHHHHHHHH */ |
| 12164 | } |
| 12165 | } |
| 12166 | |
| 12167 | quote = '\''; |
| 12168 | if (squote) { |
| 12169 | if (dquote) |
| 12170 | /* Both squote and dquote present. Use squote, |
| 12171 | and escape them */ |
| 12172 | osize += squote; |
| 12173 | else |
| 12174 | quote = '"'; |
| 12175 | } |
| 12176 | |
| 12177 | repr = PyUnicode_New(osize, max); |
| 12178 | if (repr == NULL) |
| 12179 | return NULL; |
| 12180 | okind = PyUnicode_KIND(repr); |
| 12181 | odata = PyUnicode_DATA(repr); |
| 12182 | |
| 12183 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12184 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12185 | |
| 12186 | for (i = 0, o = 1; i < isize; i++) { |
| 12187 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12188 | |
| 12189 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12190 | if ((ch == quote) || (ch == '\\')) { |
| 12191 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12192 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12193 | continue; |
| 12194 | } |
| 12195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12196 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12197 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12198 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12199 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12200 | } |
| 12201 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12202 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12203 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12204 | } |
| 12205 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12206 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12207 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12208 | } |
| 12209 | |
| 12210 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12211 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12212 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12213 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12214 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12215 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12216 | } |
| 12217 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12218 | /* Copy ASCII characters as-is */ |
| 12219 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12220 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12221 | } |
| 12222 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12223 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12224 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12225 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12226 | (categories Z* and C* except ASCII space) |
| 12227 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12228 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12229 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12230 | if (ch <= 0xff) { |
| 12231 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12232 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12233 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12234 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12235 | } |
| 12236 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12237 | else if (ch >= 0x10000) { |
| 12238 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12239 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12240 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12241 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12242 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12243 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12244 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12245 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12246 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12247 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12248 | } |
| 12249 | /* Map 16-bit characters to '\uxxxx' */ |
| 12250 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12251 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12252 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12253 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12254 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12255 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12256 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12257 | } |
| 12258 | } |
| 12259 | /* Copy characters as-is */ |
| 12260 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12261 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12262 | } |
| 12263 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12264 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12265 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12266 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12267 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | } |
| 12269 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12270 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12271 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | \n\ |
| 12273 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12274 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | arguments start and end are interpreted as in slice notation.\n\ |
| 12276 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12277 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | |
| 12279 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12280 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12282 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12283 | Py_ssize_t start; |
| 12284 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12285 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12286 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12287 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12288 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12289 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12290 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12291 | if (PyUnicode_READY(self) == -1) |
| 12292 | return NULL; |
| 12293 | if (PyUnicode_READY(substring) == -1) |
| 12294 | return NULL; |
| 12295 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12296 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 | |
| 12298 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12300 | if (result == -2) |
| 12301 | return NULL; |
| 12302 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12303 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12304 | } |
| 12305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12306 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12307 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12308 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12309 | 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] | 12310 | |
| 12311 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12313 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12314 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12315 | Py_ssize_t start; |
| 12316 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12317 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12318 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12319 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12320 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12321 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12323 | if (PyUnicode_READY(self) == -1) |
| 12324 | return NULL; |
| 12325 | if (PyUnicode_READY(substring) == -1) |
| 12326 | return NULL; |
| 12327 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12328 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12329 | |
| 12330 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12331 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12332 | if (result == -2) |
| 12333 | return NULL; |
| 12334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12335 | if (result < 0) { |
| 12336 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12337 | return NULL; |
| 12338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12340 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | } |
| 12342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12343 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12344 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12345 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12346 | 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] | 12347 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12348 | |
| 12349 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12350 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12351 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12352 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | Py_UCS4 fillchar = ' '; |
| 12354 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12355 | 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] | 12356 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12357 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12358 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12359 | return NULL; |
| 12360 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12361 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12362 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12364 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 | } |
| 12366 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12367 | PyObject * |
| 12368 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12369 | { |
| 12370 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12372 | s = PyUnicode_FromObject(s); |
| 12373 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12374 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12375 | if (sep != NULL) { |
| 12376 | sep = PyUnicode_FromObject(sep); |
| 12377 | if (sep == NULL) { |
| 12378 | Py_DECREF(s); |
| 12379 | return NULL; |
| 12380 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12381 | } |
| 12382 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12383 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12384 | |
| 12385 | Py_DECREF(s); |
| 12386 | Py_XDECREF(sep); |
| 12387 | return result; |
| 12388 | } |
| 12389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12390 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12391 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12392 | \n\ |
| 12393 | Return a list of the words in S, using sep as the\n\ |
| 12394 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12395 | 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] | 12396 | whitespace string is a separator and empty strings are\n\ |
| 12397 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12398 | |
| 12399 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12400 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12401 | { |
| 12402 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12403 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12404 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12405 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12406 | return NULL; |
| 12407 | |
| 12408 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12410 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12411 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12413 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12414 | } |
| 12415 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12416 | PyObject * |
| 12417 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12418 | { |
| 12419 | PyObject* str_obj; |
| 12420 | PyObject* sep_obj; |
| 12421 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 | int kind1, kind2, kind; |
| 12423 | void *buf1 = NULL, *buf2 = NULL; |
| 12424 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12425 | |
| 12426 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12427 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12428 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12429 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12430 | if (!sep_obj) { |
| 12431 | Py_DECREF(str_obj); |
| 12432 | return NULL; |
| 12433 | } |
| 12434 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12435 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12436 | Py_DECREF(str_obj); |
| 12437 | return NULL; |
| 12438 | } |
| 12439 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12440 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12441 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12442 | kind = Py_MAX(kind1, kind2); |
| 12443 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12444 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12445 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12446 | if (!buf1) |
| 12447 | goto onError; |
| 12448 | buf2 = PyUnicode_DATA(sep_obj); |
| 12449 | if (kind2 != kind) |
| 12450 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12451 | if (!buf2) |
| 12452 | goto onError; |
| 12453 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12454 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12455 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12456 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12457 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12458 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12459 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12460 | else |
| 12461 | 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] | 12462 | break; |
| 12463 | case PyUnicode_2BYTE_KIND: |
| 12464 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12465 | break; |
| 12466 | case PyUnicode_4BYTE_KIND: |
| 12467 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12468 | break; |
| 12469 | default: |
| 12470 | assert(0); |
| 12471 | out = 0; |
| 12472 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12473 | |
| 12474 | Py_DECREF(sep_obj); |
| 12475 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12476 | if (kind1 != kind) |
| 12477 | PyMem_Free(buf1); |
| 12478 | if (kind2 != kind) |
| 12479 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12480 | |
| 12481 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12482 | onError: |
| 12483 | Py_DECREF(sep_obj); |
| 12484 | Py_DECREF(str_obj); |
| 12485 | if (kind1 != kind && buf1) |
| 12486 | PyMem_Free(buf1); |
| 12487 | if (kind2 != kind && buf2) |
| 12488 | PyMem_Free(buf2); |
| 12489 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12490 | } |
| 12491 | |
| 12492 | |
| 12493 | PyObject * |
| 12494 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12495 | { |
| 12496 | PyObject* str_obj; |
| 12497 | PyObject* sep_obj; |
| 12498 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12499 | int kind1, kind2, kind; |
| 12500 | void *buf1 = NULL, *buf2 = NULL; |
| 12501 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12502 | |
| 12503 | str_obj = PyUnicode_FromObject(str_in); |
| 12504 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12505 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12506 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12507 | if (!sep_obj) { |
| 12508 | Py_DECREF(str_obj); |
| 12509 | return NULL; |
| 12510 | } |
| 12511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | kind1 = PyUnicode_KIND(str_in); |
| 12513 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12514 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | buf1 = PyUnicode_DATA(str_in); |
| 12516 | if (kind1 != kind) |
| 12517 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12518 | if (!buf1) |
| 12519 | goto onError; |
| 12520 | buf2 = PyUnicode_DATA(sep_obj); |
| 12521 | if (kind2 != kind) |
| 12522 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12523 | if (!buf2) |
| 12524 | goto onError; |
| 12525 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12526 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12527 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12528 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12529 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12530 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12531 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12532 | else |
| 12533 | 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] | 12534 | break; |
| 12535 | case PyUnicode_2BYTE_KIND: |
| 12536 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12537 | break; |
| 12538 | case PyUnicode_4BYTE_KIND: |
| 12539 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12540 | break; |
| 12541 | default: |
| 12542 | assert(0); |
| 12543 | out = 0; |
| 12544 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12545 | |
| 12546 | Py_DECREF(sep_obj); |
| 12547 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12548 | if (kind1 != kind) |
| 12549 | PyMem_Free(buf1); |
| 12550 | if (kind2 != kind) |
| 12551 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12552 | |
| 12553 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12554 | onError: |
| 12555 | Py_DECREF(sep_obj); |
| 12556 | Py_DECREF(str_obj); |
| 12557 | if (kind1 != kind && buf1) |
| 12558 | PyMem_Free(buf1); |
| 12559 | if (kind2 != kind && buf2) |
| 12560 | PyMem_Free(buf2); |
| 12561 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12562 | } |
| 12563 | |
| 12564 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12565 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12566 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12567 | 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] | 12568 | 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] | 12569 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12570 | |
| 12571 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12572 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12573 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12574 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12575 | } |
| 12576 | |
| 12577 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12578 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12579 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12580 | 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] | 12581 | 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] | 12582 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12583 | |
| 12584 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12585 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12586 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12587 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12588 | } |
| 12589 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12590 | PyObject * |
| 12591 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12592 | { |
| 12593 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12594 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12595 | s = PyUnicode_FromObject(s); |
| 12596 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12597 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12598 | if (sep != NULL) { |
| 12599 | sep = PyUnicode_FromObject(sep); |
| 12600 | if (sep == NULL) { |
| 12601 | Py_DECREF(s); |
| 12602 | return NULL; |
| 12603 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12604 | } |
| 12605 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12606 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12607 | |
| 12608 | Py_DECREF(s); |
| 12609 | Py_XDECREF(sep); |
| 12610 | return result; |
| 12611 | } |
| 12612 | |
| 12613 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12614 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12615 | \n\ |
| 12616 | Return a list of the words in S, using sep as the\n\ |
| 12617 | delimiter string, starting at the end of the string and\n\ |
| 12618 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12619 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12620 | is a separator."); |
| 12621 | |
| 12622 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12623 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12624 | { |
| 12625 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12626 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12627 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12628 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12629 | return NULL; |
| 12630 | |
| 12631 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12632 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12633 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12634 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12635 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12636 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12637 | } |
| 12638 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12639 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12640 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12641 | \n\ |
| 12642 | 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] | 12643 | 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] | 12644 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12645 | |
| 12646 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12647 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12648 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12649 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12650 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12651 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12652 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12653 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | return NULL; |
| 12655 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12656 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | } |
| 12658 | |
| 12659 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12660 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12661 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12662 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12663 | } |
| 12664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12665 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12666 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | \n\ |
| 12668 | 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] | 12669 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12670 | |
| 12671 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12672 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12673 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12674 | if (PyUnicode_READY(self) == -1) |
| 12675 | return NULL; |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12676 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12677 | } |
| 12678 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12679 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12680 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12681 | \n\ |
| 12682 | Return a translation table usable for str.translate().\n\ |
| 12683 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12684 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12685 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12686 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12687 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12688 | character at the same position in y. If there is a third argument, it\n\ |
| 12689 | must be a string, whose characters will be mapped to None in the result."); |
| 12690 | |
| 12691 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12692 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12693 | { |
| 12694 | PyObject *x, *y = NULL, *z = NULL; |
| 12695 | PyObject *new = NULL, *key, *value; |
| 12696 | Py_ssize_t i = 0; |
| 12697 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12698 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12699 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12700 | return NULL; |
| 12701 | new = PyDict_New(); |
| 12702 | if (!new) |
| 12703 | return NULL; |
| 12704 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12705 | int x_kind, y_kind, z_kind; |
| 12706 | void *x_data, *y_data, *z_data; |
| 12707 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12708 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12709 | if (!PyUnicode_Check(x)) { |
| 12710 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12711 | "be a string if there is a second argument"); |
| 12712 | goto err; |
| 12713 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12715 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12716 | "arguments must have equal length"); |
| 12717 | goto err; |
| 12718 | } |
| 12719 | /* 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] | 12720 | x_kind = PyUnicode_KIND(x); |
| 12721 | y_kind = PyUnicode_KIND(y); |
| 12722 | x_data = PyUnicode_DATA(x); |
| 12723 | y_data = PyUnicode_DATA(y); |
| 12724 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12725 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12726 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12727 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12728 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12729 | if (!value) { |
| 12730 | Py_DECREF(key); |
| 12731 | goto err; |
| 12732 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12733 | res = PyDict_SetItem(new, key, value); |
| 12734 | Py_DECREF(key); |
| 12735 | Py_DECREF(value); |
| 12736 | if (res < 0) |
| 12737 | goto err; |
| 12738 | } |
| 12739 | /* create entries for deleting chars in z */ |
| 12740 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12741 | z_kind = PyUnicode_KIND(z); |
| 12742 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12743 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12744 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12745 | if (!key) |
| 12746 | goto err; |
| 12747 | res = PyDict_SetItem(new, key, Py_None); |
| 12748 | Py_DECREF(key); |
| 12749 | if (res < 0) |
| 12750 | goto err; |
| 12751 | } |
| 12752 | } |
| 12753 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12754 | int kind; |
| 12755 | void *data; |
| 12756 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12757 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12758 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12759 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12760 | "to maketrans it must be a dict"); |
| 12761 | goto err; |
| 12762 | } |
| 12763 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12764 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12765 | if (PyUnicode_Check(key)) { |
| 12766 | /* convert string keys to integer keys */ |
| 12767 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12768 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12769 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12770 | "table must be of length 1"); |
| 12771 | goto err; |
| 12772 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12773 | kind = PyUnicode_KIND(key); |
| 12774 | data = PyUnicode_DATA(key); |
| 12775 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12776 | if (!newkey) |
| 12777 | goto err; |
| 12778 | res = PyDict_SetItem(new, newkey, value); |
| 12779 | Py_DECREF(newkey); |
| 12780 | if (res < 0) |
| 12781 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12782 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12783 | /* just keep integer keys */ |
| 12784 | if (PyDict_SetItem(new, key, value) < 0) |
| 12785 | goto err; |
| 12786 | } else { |
| 12787 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12788 | "be strings or integers"); |
| 12789 | goto err; |
| 12790 | } |
| 12791 | } |
| 12792 | } |
| 12793 | return new; |
| 12794 | err: |
| 12795 | Py_DECREF(new); |
| 12796 | return NULL; |
| 12797 | } |
| 12798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12799 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12800 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12801 | \n\ |
| 12802 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12803 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12804 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12805 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12806 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12807 | |
| 12808 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12809 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12810 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12811 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12812 | } |
| 12813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12814 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12815 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12816 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12817 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12818 | |
| 12819 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12820 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12821 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12822 | if (PyUnicode_READY(self) == -1) |
| 12823 | return NULL; |
| 12824 | if (PyUnicode_IS_ASCII(self)) |
| 12825 | return ascii_upper_or_lower(self, 0); |
| 12826 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12827 | } |
| 12828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12829 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12830 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12831 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12832 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12833 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12834 | |
| 12835 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12836 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12837 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12838 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12839 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12840 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12841 | int kind; |
| 12842 | void *data; |
| 12843 | Py_UCS4 chr; |
| 12844 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12845 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12846 | return NULL; |
| 12847 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12848 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12849 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12850 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12851 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12852 | return unicode_result_unchanged(self); |
| 12853 | |
| 12854 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12855 | |
| 12856 | u = pad(self, fill, 0, '0'); |
| 12857 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12858 | if (u == NULL) |
| 12859 | return NULL; |
| 12860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12861 | kind = PyUnicode_KIND(u); |
| 12862 | data = PyUnicode_DATA(u); |
| 12863 | chr = PyUnicode_READ(kind, data, fill); |
| 12864 | |
| 12865 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12866 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12867 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12868 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12869 | } |
| 12870 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12871 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12872 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12873 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12874 | |
| 12875 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12876 | static PyObject * |
| 12877 | unicode__decimal2ascii(PyObject *self) |
| 12878 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12879 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12880 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12881 | #endif |
| 12882 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12883 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12884 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12885 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12886 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12887 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12888 | With optional end, stop comparing S at that position.\n\ |
| 12889 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12890 | |
| 12891 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12892 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12893 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12894 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12895 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12896 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12897 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12898 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12899 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12900 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12901 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12902 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12903 | if (PyTuple_Check(subobj)) { |
| 12904 | Py_ssize_t i; |
| 12905 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12906 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12907 | if (substring == NULL) |
| 12908 | return NULL; |
| 12909 | result = tailmatch(self, substring, start, end, -1); |
| 12910 | Py_DECREF(substring); |
| 12911 | if (result) { |
| 12912 | Py_RETURN_TRUE; |
| 12913 | } |
| 12914 | } |
| 12915 | /* nothing matched */ |
| 12916 | Py_RETURN_FALSE; |
| 12917 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12918 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12919 | if (substring == NULL) { |
| 12920 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12921 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12922 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12923 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12924 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12925 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12926 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12927 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12928 | } |
| 12929 | |
| 12930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12931 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12932 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12933 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12934 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12935 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12936 | With optional end, stop comparing S at that position.\n\ |
| 12937 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12938 | |
| 12939 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12940 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12941 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12942 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12943 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12944 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12945 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12946 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12947 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12948 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12949 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12950 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12951 | if (PyTuple_Check(subobj)) { |
| 12952 | Py_ssize_t i; |
| 12953 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12954 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12955 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12956 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12957 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12958 | result = tailmatch(self, substring, start, end, +1); |
| 12959 | Py_DECREF(substring); |
| 12960 | if (result) { |
| 12961 | Py_RETURN_TRUE; |
| 12962 | } |
| 12963 | } |
| 12964 | Py_RETURN_FALSE; |
| 12965 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12966 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12967 | if (substring == NULL) { |
| 12968 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12969 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12970 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12971 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12972 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12973 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12974 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12975 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12976 | } |
| 12977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12978 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12979 | |
| 12980 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12981 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12982 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12983 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12984 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12985 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12986 | PyDoc_STRVAR(format_map__doc__, |
| 12987 | "S.format_map(mapping) -> str\n\ |
| 12988 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12989 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12990 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12991 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12992 | static PyObject * |
| 12993 | unicode__format__(PyObject* self, PyObject* args) |
| 12994 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12995 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12996 | |
| 12997 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12998 | return NULL; |
| 12999 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13000 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13001 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13002 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13003 | } |
| 13004 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13005 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13006 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13007 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13008 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13009 | |
| 13010 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13011 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13012 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13013 | Py_ssize_t size; |
| 13014 | |
| 13015 | /* If it's a compact object, account for base structure + |
| 13016 | character data. */ |
| 13017 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13018 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13019 | else if (PyUnicode_IS_COMPACT(v)) |
| 13020 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13021 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13022 | else { |
| 13023 | /* If it is a two-block object, account for base object, and |
| 13024 | for character block if present. */ |
| 13025 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13026 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13027 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13028 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13029 | } |
| 13030 | /* 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] | 13031 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13032 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13033 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13034 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13035 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13036 | |
| 13037 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13038 | } |
| 13039 | |
| 13040 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13041 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13042 | |
| 13043 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13044 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13045 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13046 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13047 | if (!copy) |
| 13048 | return NULL; |
| 13049 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13050 | } |
| 13051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13052 | static PyMethodDef unicode_methods[] = { |
| 13053 | |
| 13054 | /* Order is according to common usage: often used methods should |
| 13055 | appear first, since lookup is done sequentially. */ |
| 13056 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13057 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13058 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 13059 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13060 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13061 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13062 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13063 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13064 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13065 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13066 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13067 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13068 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13069 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13070 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13071 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13072 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13073 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13074 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13075 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13076 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13077 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13078 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13079 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13080 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13081 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13082 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13083 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13084 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13085 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13086 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13087 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13088 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13089 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13090 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13091 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13092 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13093 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13094 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13095 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13096 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13097 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13098 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13099 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13100 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13101 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13102 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13103 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13104 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13105 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13106 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13107 | #endif |
| 13108 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13109 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | {NULL, NULL} |
| 13111 | }; |
| 13112 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13113 | static PyObject * |
| 13114 | unicode_mod(PyObject *v, PyObject *w) |
| 13115 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13116 | if (!PyUnicode_Check(v)) |
| 13117 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13118 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13119 | } |
| 13120 | |
| 13121 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13122 | 0, /*nb_add*/ |
| 13123 | 0, /*nb_subtract*/ |
| 13124 | 0, /*nb_multiply*/ |
| 13125 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13126 | }; |
| 13127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13128 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13129 | (lenfunc) unicode_length, /* sq_length */ |
| 13130 | PyUnicode_Concat, /* sq_concat */ |
| 13131 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13132 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13133 | 0, /* sq_slice */ |
| 13134 | 0, /* sq_ass_item */ |
| 13135 | 0, /* sq_ass_slice */ |
| 13136 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13137 | }; |
| 13138 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13139 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13140 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13141 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13142 | if (PyUnicode_READY(self) == -1) |
| 13143 | return NULL; |
| 13144 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13145 | if (PyIndex_Check(item)) { |
| 13146 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13147 | if (i == -1 && PyErr_Occurred()) |
| 13148 | return NULL; |
| 13149 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13150 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13151 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13152 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13153 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13154 | PyObject *result; |
| 13155 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13156 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13157 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13158 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13159 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13160 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13161 | return NULL; |
| 13162 | } |
| 13163 | |
| 13164 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13165 | Py_INCREF(unicode_empty); |
| 13166 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13167 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13168 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13169 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13170 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13171 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13172 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13173 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13174 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13175 | src_kind = PyUnicode_KIND(self); |
| 13176 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13177 | if (!PyUnicode_IS_ASCII(self)) { |
| 13178 | kind_limit = kind_maxchar_limit(src_kind); |
| 13179 | max_char = 0; |
| 13180 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13181 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13182 | if (ch > max_char) { |
| 13183 | max_char = ch; |
| 13184 | if (max_char >= kind_limit) |
| 13185 | break; |
| 13186 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13187 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13188 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13189 | else |
| 13190 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13191 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13192 | if (result == NULL) |
| 13193 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13194 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13195 | dest_data = PyUnicode_DATA(result); |
| 13196 | |
| 13197 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13198 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13199 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13200 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13201 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13202 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13203 | } else { |
| 13204 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13205 | return NULL; |
| 13206 | } |
| 13207 | } |
| 13208 | |
| 13209 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13210 | (lenfunc)unicode_length, /* mp_length */ |
| 13211 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13212 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13213 | }; |
| 13214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13215 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13216 | /* Helpers for PyUnicode_Format() */ |
| 13217 | |
| 13218 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13219 | 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] | 13220 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13221 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13222 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13223 | (*p_argidx)++; |
| 13224 | if (arglen < 0) |
| 13225 | return args; |
| 13226 | else |
| 13227 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13228 | } |
| 13229 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13230 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13231 | return NULL; |
| 13232 | } |
| 13233 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13234 | /* 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] | 13235 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13236 | static PyObject * |
| 13237 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13239 | char *p; |
| 13240 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13241 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13243 | x = PyFloat_AsDouble(v); |
| 13244 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13245 | return NULL; |
| 13246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13247 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13248 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13249 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13250 | p = PyOS_double_to_string(x, type, prec, |
| 13251 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13252 | if (p == NULL) |
| 13253 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13254 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13255 | PyMem_Free(p); |
| 13256 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13257 | } |
| 13258 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13259 | static PyObject* |
| 13260 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13261 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13262 | char *buf; |
| 13263 | int len; |
| 13264 | PyObject *str; /* temporary string object. */ |
| 13265 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13266 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13267 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13268 | if (!str) |
| 13269 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13270 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13271 | Py_DECREF(str); |
| 13272 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13273 | } |
| 13274 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13275 | static Py_UCS4 |
| 13276 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13277 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13278 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13279 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13280 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13281 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13282 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13283 | goto onError; |
| 13284 | } |
| 13285 | else { |
| 13286 | /* Integer input truncated to a character */ |
| 13287 | long x; |
| 13288 | x = PyLong_AsLong(v); |
| 13289 | if (x == -1 && PyErr_Occurred()) |
| 13290 | goto onError; |
| 13291 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13292 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13293 | PyErr_SetString(PyExc_OverflowError, |
| 13294 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13295 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13296 | } |
| 13297 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13298 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13299 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13300 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13301 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13302 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13303 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13304 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13305 | } |
| 13306 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13307 | static int |
| 13308 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13309 | { |
| 13310 | int r; |
| 13311 | assert(count > 0); |
| 13312 | assert(PyUnicode_Check(obj)); |
| 13313 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13314 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13315 | if (repeated == NULL) |
| 13316 | return -1; |
| 13317 | r = _PyAccu_Accumulate(acc, repeated); |
| 13318 | Py_DECREF(repeated); |
| 13319 | return r; |
| 13320 | } |
| 13321 | else { |
| 13322 | do { |
| 13323 | if (_PyAccu_Accumulate(acc, obj)) |
| 13324 | return -1; |
| 13325 | } while (--count); |
| 13326 | return 0; |
| 13327 | } |
| 13328 | } |
| 13329 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13330 | PyObject * |
| 13331 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13332 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13333 | void *fmt; |
| 13334 | int fmtkind; |
| 13335 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13336 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13337 | int r; |
| 13338 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13339 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13340 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13341 | PyObject *temp = NULL; |
| 13342 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13343 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13344 | _PyAccu acc; |
| 13345 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13346 | |
| 13347 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13348 | return NULL; |
| 13349 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13350 | return NULL; |
| 13351 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13352 | return NULL; |
| 13353 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13354 | return NULL; |
| 13355 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13356 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13358 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13359 | PyErr_BadInternalCall(); |
| 13360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13361 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13362 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13363 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13364 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13365 | if (PyUnicode_READY(uformat) == -1) |
| 13366 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13367 | if (_PyAccu_Init(&acc)) |
| 13368 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13369 | fmt = PyUnicode_DATA(uformat); |
| 13370 | fmtkind = PyUnicode_KIND(uformat); |
| 13371 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13372 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13374 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13375 | arglen = PyTuple_Size(args); |
| 13376 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13377 | } |
| 13378 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13379 | arglen = -1; |
| 13380 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13381 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13382 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13383 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13384 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13385 | |
| 13386 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13387 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13388 | PyObject *nonfmt; |
| 13389 | Py_ssize_t nonfmtpos; |
| 13390 | nonfmtpos = fmtpos++; |
| 13391 | while (fmtcnt >= 0 && |
| 13392 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13393 | fmtpos++; |
| 13394 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13395 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13396 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13397 | if (nonfmt == NULL) |
| 13398 | goto onError; |
| 13399 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13400 | Py_DECREF(nonfmt); |
| 13401 | if (r) |
| 13402 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13403 | } |
| 13404 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13405 | /* Got a format specifier */ |
| 13406 | int flags = 0; |
| 13407 | Py_ssize_t width = -1; |
| 13408 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13409 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13410 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13411 | int isnumok; |
| 13412 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13413 | void *pbuf = NULL; |
| 13414 | Py_ssize_t pindex, len; |
| 13415 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13416 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13417 | fmtpos++; |
| 13418 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13419 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13420 | Py_ssize_t keylen; |
| 13421 | PyObject *key; |
| 13422 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13423 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13424 | if (dict == NULL) { |
| 13425 | PyErr_SetString(PyExc_TypeError, |
| 13426 | "format requires a mapping"); |
| 13427 | goto onError; |
| 13428 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13429 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13430 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13431 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13432 | /* Skip over balanced parentheses */ |
| 13433 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13434 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13435 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13436 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13437 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13438 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13439 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13440 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13441 | if (fmtcnt < 0 || pcount > 0) { |
| 13442 | PyErr_SetString(PyExc_ValueError, |
| 13443 | "incomplete format key"); |
| 13444 | goto onError; |
| 13445 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13446 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13447 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13448 | if (key == NULL) |
| 13449 | goto onError; |
| 13450 | if (args_owned) { |
| 13451 | Py_DECREF(args); |
| 13452 | args_owned = 0; |
| 13453 | } |
| 13454 | args = PyObject_GetItem(dict, key); |
| 13455 | Py_DECREF(key); |
| 13456 | if (args == NULL) { |
| 13457 | goto onError; |
| 13458 | } |
| 13459 | args_owned = 1; |
| 13460 | arglen = -1; |
| 13461 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13462 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13463 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13464 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13465 | case '-': flags |= F_LJUST; continue; |
| 13466 | case '+': flags |= F_SIGN; continue; |
| 13467 | case ' ': flags |= F_BLANK; continue; |
| 13468 | case '#': flags |= F_ALT; continue; |
| 13469 | case '0': flags |= F_ZERO; continue; |
| 13470 | } |
| 13471 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13472 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13473 | if (c == '*') { |
| 13474 | v = getnextarg(args, arglen, &argidx); |
| 13475 | if (v == NULL) |
| 13476 | goto onError; |
| 13477 | if (!PyLong_Check(v)) { |
| 13478 | PyErr_SetString(PyExc_TypeError, |
| 13479 | "* wants int"); |
| 13480 | goto onError; |
| 13481 | } |
| 13482 | width = PyLong_AsLong(v); |
| 13483 | if (width == -1 && PyErr_Occurred()) |
| 13484 | goto onError; |
| 13485 | if (width < 0) { |
| 13486 | flags |= F_LJUST; |
| 13487 | width = -width; |
| 13488 | } |
| 13489 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13490 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13491 | } |
| 13492 | else if (c >= '0' && c <= '9') { |
| 13493 | width = c - '0'; |
| 13494 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13495 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13496 | if (c < '0' || c > '9') |
| 13497 | break; |
| 13498 | if ((width*10) / 10 != width) { |
| 13499 | PyErr_SetString(PyExc_ValueError, |
| 13500 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13501 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13502 | } |
| 13503 | width = width*10 + (c - '0'); |
| 13504 | } |
| 13505 | } |
| 13506 | if (c == '.') { |
| 13507 | prec = 0; |
| 13508 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13509 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13510 | if (c == '*') { |
| 13511 | v = getnextarg(args, arglen, &argidx); |
| 13512 | if (v == NULL) |
| 13513 | goto onError; |
| 13514 | if (!PyLong_Check(v)) { |
| 13515 | PyErr_SetString(PyExc_TypeError, |
| 13516 | "* wants int"); |
| 13517 | goto onError; |
| 13518 | } |
| 13519 | prec = PyLong_AsLong(v); |
| 13520 | if (prec == -1 && PyErr_Occurred()) |
| 13521 | goto onError; |
| 13522 | if (prec < 0) |
| 13523 | prec = 0; |
| 13524 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13525 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13526 | } |
| 13527 | else if (c >= '0' && c <= '9') { |
| 13528 | prec = c - '0'; |
| 13529 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13530 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13531 | if (c < '0' || c > '9') |
| 13532 | break; |
| 13533 | if ((prec*10) / 10 != prec) { |
| 13534 | PyErr_SetString(PyExc_ValueError, |
| 13535 | "prec too big"); |
| 13536 | goto onError; |
| 13537 | } |
| 13538 | prec = prec*10 + (c - '0'); |
| 13539 | } |
| 13540 | } |
| 13541 | } /* prec */ |
| 13542 | if (fmtcnt >= 0) { |
| 13543 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13544 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13545 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13546 | } |
| 13547 | } |
| 13548 | if (fmtcnt < 0) { |
| 13549 | PyErr_SetString(PyExc_ValueError, |
| 13550 | "incomplete format"); |
| 13551 | goto onError; |
| 13552 | } |
| 13553 | if (c != '%') { |
| 13554 | v = getnextarg(args, arglen, &argidx); |
| 13555 | if (v == NULL) |
| 13556 | goto onError; |
| 13557 | } |
| 13558 | sign = 0; |
| 13559 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13560 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13561 | switch (c) { |
| 13562 | |
| 13563 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13564 | _PyAccu_Accumulate(&acc, percent); |
| 13565 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13566 | |
| 13567 | case 's': |
| 13568 | case 'r': |
| 13569 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13570 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13571 | temp = v; |
| 13572 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13573 | } |
| 13574 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | if (c == 's') |
| 13576 | temp = PyObject_Str(v); |
| 13577 | else if (c == 'r') |
| 13578 | temp = PyObject_Repr(v); |
| 13579 | else |
| 13580 | temp = PyObject_ASCII(v); |
| 13581 | if (temp == NULL) |
| 13582 | goto onError; |
| 13583 | if (PyUnicode_Check(temp)) |
| 13584 | /* nothing to do */; |
| 13585 | else { |
| 13586 | Py_DECREF(temp); |
| 13587 | PyErr_SetString(PyExc_TypeError, |
| 13588 | "%s argument has non-string str()"); |
| 13589 | goto onError; |
| 13590 | } |
| 13591 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13592 | if (PyUnicode_READY(temp) == -1) { |
| 13593 | Py_CLEAR(temp); |
| 13594 | goto onError; |
| 13595 | } |
| 13596 | pbuf = PyUnicode_DATA(temp); |
| 13597 | kind = PyUnicode_KIND(temp); |
| 13598 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13599 | if (prec >= 0 && len > prec) |
| 13600 | len = prec; |
| 13601 | break; |
| 13602 | |
| 13603 | case 'i': |
| 13604 | case 'd': |
| 13605 | case 'u': |
| 13606 | case 'o': |
| 13607 | case 'x': |
| 13608 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13609 | isnumok = 0; |
| 13610 | if (PyNumber_Check(v)) { |
| 13611 | PyObject *iobj=NULL; |
| 13612 | |
| 13613 | if (PyLong_Check(v)) { |
| 13614 | iobj = v; |
| 13615 | Py_INCREF(iobj); |
| 13616 | } |
| 13617 | else { |
| 13618 | iobj = PyNumber_Long(v); |
| 13619 | } |
| 13620 | if (iobj!=NULL) { |
| 13621 | if (PyLong_Check(iobj)) { |
| 13622 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13623 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13624 | Py_DECREF(iobj); |
| 13625 | if (!temp) |
| 13626 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13627 | if (PyUnicode_READY(temp) == -1) { |
| 13628 | Py_CLEAR(temp); |
| 13629 | goto onError; |
| 13630 | } |
| 13631 | pbuf = PyUnicode_DATA(temp); |
| 13632 | kind = PyUnicode_KIND(temp); |
| 13633 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13634 | sign = 1; |
| 13635 | } |
| 13636 | else { |
| 13637 | Py_DECREF(iobj); |
| 13638 | } |
| 13639 | } |
| 13640 | } |
| 13641 | if (!isnumok) { |
| 13642 | PyErr_Format(PyExc_TypeError, |
| 13643 | "%%%c format: a number is required, " |
| 13644 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13645 | goto onError; |
| 13646 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13647 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13648 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13649 | fillobj = zero; |
| 13650 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13651 | break; |
| 13652 | |
| 13653 | case 'e': |
| 13654 | case 'E': |
| 13655 | case 'f': |
| 13656 | case 'F': |
| 13657 | case 'g': |
| 13658 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13659 | temp = formatfloat(v, flags, prec, c); |
| 13660 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13661 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13662 | if (PyUnicode_READY(temp) == -1) { |
| 13663 | Py_CLEAR(temp); |
| 13664 | goto onError; |
| 13665 | } |
| 13666 | pbuf = PyUnicode_DATA(temp); |
| 13667 | kind = PyUnicode_KIND(temp); |
| 13668 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13669 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13670 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13671 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13672 | fillobj = zero; |
| 13673 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13674 | break; |
| 13675 | |
| 13676 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13677 | { |
| 13678 | Py_UCS4 ch = formatchar(v); |
| 13679 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13680 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13681 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13682 | if (temp == NULL) |
| 13683 | goto onError; |
| 13684 | pbuf = PyUnicode_DATA(temp); |
| 13685 | kind = PyUnicode_KIND(temp); |
| 13686 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13687 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13688 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13689 | |
| 13690 | default: |
| 13691 | PyErr_Format(PyExc_ValueError, |
| 13692 | "unsupported format character '%c' (0x%x) " |
| 13693 | "at index %zd", |
| 13694 | (31<=c && c<=126) ? (char)c : '?', |
| 13695 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13696 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13697 | goto onError; |
| 13698 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13699 | /* pbuf is initialized here. */ |
| 13700 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13701 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13702 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13703 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13704 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13705 | pindex++; |
| 13706 | } |
| 13707 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13708 | signobj = plus; |
| 13709 | len--; |
| 13710 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13711 | } |
| 13712 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13713 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13714 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13715 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13716 | else |
| 13717 | sign = 0; |
| 13718 | } |
| 13719 | if (width < len) |
| 13720 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13721 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13722 | if (fill != ' ') { |
| 13723 | assert(signobj != NULL); |
| 13724 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13725 | goto onError; |
| 13726 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13727 | if (width > len) |
| 13728 | width--; |
| 13729 | } |
| 13730 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13731 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13732 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13733 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13734 | second = get_latin1_char( |
| 13735 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13736 | pindex += 2; |
| 13737 | if (second == NULL || |
| 13738 | _PyAccu_Accumulate(&acc, zero) || |
| 13739 | _PyAccu_Accumulate(&acc, second)) |
| 13740 | goto onError; |
| 13741 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13742 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13743 | width -= 2; |
| 13744 | if (width < 0) |
| 13745 | width = 0; |
| 13746 | len -= 2; |
| 13747 | } |
| 13748 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13749 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13750 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13751 | goto onError; |
| 13752 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13753 | } |
| 13754 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13755 | if (sign) { |
| 13756 | assert(signobj != NULL); |
| 13757 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13758 | goto onError; |
| 13759 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13760 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13761 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13762 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13763 | second = get_latin1_char( |
| 13764 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13765 | pindex += 2; |
| 13766 | if (second == NULL || |
| 13767 | _PyAccu_Accumulate(&acc, zero) || |
| 13768 | _PyAccu_Accumulate(&acc, second)) |
| 13769 | goto onError; |
| 13770 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13771 | } |
| 13772 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13773 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13774 | if (temp != NULL) { |
| 13775 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13776 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13777 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13778 | else { |
| 13779 | const char *p = (const char *) pbuf; |
| 13780 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13781 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13782 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13783 | } |
| 13784 | if (v == NULL) |
| 13785 | goto onError; |
| 13786 | r = _PyAccu_Accumulate(&acc, v); |
| 13787 | Py_DECREF(v); |
| 13788 | if (r) |
| 13789 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13790 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13791 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13792 | if (dict && (argidx < arglen) && c != '%') { |
| 13793 | PyErr_SetString(PyExc_TypeError, |
| 13794 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13795 | goto onError; |
| 13796 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13797 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13798 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13799 | } /* until end */ |
| 13800 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13801 | PyErr_SetString(PyExc_TypeError, |
| 13802 | "not all arguments converted during string formatting"); |
| 13803 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13804 | } |
| 13805 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13806 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13807 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13808 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13809 | } |
| 13810 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13811 | Py_XDECREF(temp); |
| 13812 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13813 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13814 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13815 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13816 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13817 | Py_XDECREF(temp); |
| 13818 | Py_XDECREF(second); |
| 13819 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13820 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13821 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13822 | } |
| 13823 | return NULL; |
| 13824 | } |
| 13825 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13826 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13827 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13828 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13829 | static PyObject * |
| 13830 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13831 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13832 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13833 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13834 | char *encoding = NULL; |
| 13835 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13836 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13837 | if (type != &PyUnicode_Type) |
| 13838 | return unicode_subtype_new(type, args, kwds); |
| 13839 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13840 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13841 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13842 | if (x == NULL) { |
| 13843 | Py_INCREF(unicode_empty); |
| 13844 | return unicode_empty; |
| 13845 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13846 | if (encoding == NULL && errors == NULL) |
| 13847 | return PyObject_Str(x); |
| 13848 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13849 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13850 | } |
| 13851 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13852 | static PyObject * |
| 13853 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13854 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13855 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13856 | Py_ssize_t length, char_size; |
| 13857 | int share_wstr, share_utf8; |
| 13858 | unsigned int kind; |
| 13859 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13860 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13861 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13862 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13863 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13864 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13865 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13866 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13867 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13868 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13869 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13870 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13871 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13872 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13873 | if (self == NULL) { |
| 13874 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13875 | return NULL; |
| 13876 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13877 | kind = PyUnicode_KIND(unicode); |
| 13878 | length = PyUnicode_GET_LENGTH(unicode); |
| 13879 | |
| 13880 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13881 | #ifdef Py_DEBUG |
| 13882 | _PyUnicode_HASH(self) = -1; |
| 13883 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13884 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13885 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13886 | _PyUnicode_STATE(self).interned = 0; |
| 13887 | _PyUnicode_STATE(self).kind = kind; |
| 13888 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13889 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13890 | _PyUnicode_STATE(self).ready = 1; |
| 13891 | _PyUnicode_WSTR(self) = NULL; |
| 13892 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13893 | _PyUnicode_UTF8(self) = NULL; |
| 13894 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13895 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13896 | |
| 13897 | share_utf8 = 0; |
| 13898 | share_wstr = 0; |
| 13899 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13900 | char_size = 1; |
| 13901 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13902 | share_utf8 = 1; |
| 13903 | } |
| 13904 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13905 | char_size = 2; |
| 13906 | if (sizeof(wchar_t) == 2) |
| 13907 | share_wstr = 1; |
| 13908 | } |
| 13909 | else { |
| 13910 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13911 | char_size = 4; |
| 13912 | if (sizeof(wchar_t) == 4) |
| 13913 | share_wstr = 1; |
| 13914 | } |
| 13915 | |
| 13916 | /* Ensure we won't overflow the length. */ |
| 13917 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13918 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13919 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13920 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13921 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13922 | if (data == NULL) { |
| 13923 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13924 | goto onError; |
| 13925 | } |
| 13926 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13927 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13928 | if (share_utf8) { |
| 13929 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13930 | _PyUnicode_UTF8(self) = data; |
| 13931 | } |
| 13932 | if (share_wstr) { |
| 13933 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13934 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13935 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13936 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13937 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13938 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13939 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13940 | #ifdef Py_DEBUG |
| 13941 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13942 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13943 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13944 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13945 | |
| 13946 | onError: |
| 13947 | Py_DECREF(unicode); |
| 13948 | Py_DECREF(self); |
| 13949 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13950 | } |
| 13951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13952 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13953 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13954 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13955 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13956 | encoding defaults to the current default string encoding.\n\ |
| 13957 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13958 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13959 | static PyObject *unicode_iter(PyObject *seq); |
| 13960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13961 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13962 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13963 | "str", /* tp_name */ |
| 13964 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13965 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13966 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13967 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13968 | 0, /* tp_print */ |
| 13969 | 0, /* tp_getattr */ |
| 13970 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13971 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13972 | unicode_repr, /* tp_repr */ |
| 13973 | &unicode_as_number, /* tp_as_number */ |
| 13974 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13975 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13976 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13977 | 0, /* tp_call*/ |
| 13978 | (reprfunc) unicode_str, /* tp_str */ |
| 13979 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13980 | 0, /* tp_setattro */ |
| 13981 | 0, /* tp_as_buffer */ |
| 13982 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13983 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13984 | unicode_doc, /* tp_doc */ |
| 13985 | 0, /* tp_traverse */ |
| 13986 | 0, /* tp_clear */ |
| 13987 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13988 | 0, /* tp_weaklistoffset */ |
| 13989 | unicode_iter, /* tp_iter */ |
| 13990 | 0, /* tp_iternext */ |
| 13991 | unicode_methods, /* tp_methods */ |
| 13992 | 0, /* tp_members */ |
| 13993 | 0, /* tp_getset */ |
| 13994 | &PyBaseObject_Type, /* tp_base */ |
| 13995 | 0, /* tp_dict */ |
| 13996 | 0, /* tp_descr_get */ |
| 13997 | 0, /* tp_descr_set */ |
| 13998 | 0, /* tp_dictoffset */ |
| 13999 | 0, /* tp_init */ |
| 14000 | 0, /* tp_alloc */ |
| 14001 | unicode_new, /* tp_new */ |
| 14002 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14003 | }; |
| 14004 | |
| 14005 | /* Initialize the Unicode implementation */ |
| 14006 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14007 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14008 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14009 | int i; |
| 14010 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14011 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14012 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14013 | 0x000A, /* LINE FEED */ |
| 14014 | 0x000D, /* CARRIAGE RETURN */ |
| 14015 | 0x001C, /* FILE SEPARATOR */ |
| 14016 | 0x001D, /* GROUP SEPARATOR */ |
| 14017 | 0x001E, /* RECORD SEPARATOR */ |
| 14018 | 0x0085, /* NEXT LINE */ |
| 14019 | 0x2028, /* LINE SEPARATOR */ |
| 14020 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14021 | }; |
| 14022 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14023 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14024 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14025 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14026 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14027 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14028 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14029 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14030 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14031 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14032 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14033 | |
| 14034 | /* initialize the linebreak bloom filter */ |
| 14035 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14036 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14037 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14038 | |
| 14039 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14040 | |
| 14041 | #ifdef HAVE_MBCS |
| 14042 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14043 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14044 | PyErr_SetFromWindowsErr(0); |
| 14045 | return -1; |
| 14046 | } |
| 14047 | #endif |
| 14048 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14049 | } |
| 14050 | |
| 14051 | /* Finalize the Unicode implementation */ |
| 14052 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14053 | int |
| 14054 | PyUnicode_ClearFreeList(void) |
| 14055 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14056 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14057 | } |
| 14058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14059 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14060 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14061 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14062 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14063 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14064 | Py_XDECREF(unicode_empty); |
| 14065 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14066 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14067 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14068 | if (unicode_latin1[i]) { |
| 14069 | Py_DECREF(unicode_latin1[i]); |
| 14070 | unicode_latin1[i] = NULL; |
| 14071 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14072 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14073 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14074 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14075 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14076 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14077 | void |
| 14078 | PyUnicode_InternInPlace(PyObject **p) |
| 14079 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14080 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14081 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14082 | #ifdef Py_DEBUG |
| 14083 | assert(s != NULL); |
| 14084 | assert(_PyUnicode_CHECK(s)); |
| 14085 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14086 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14087 | return; |
| 14088 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14089 | /* If it's a subclass, we don't really know what putting |
| 14090 | it in the interned dict might do. */ |
| 14091 | if (!PyUnicode_CheckExact(s)) |
| 14092 | return; |
| 14093 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14094 | return; |
| 14095 | if (interned == NULL) { |
| 14096 | interned = PyDict_New(); |
| 14097 | if (interned == NULL) { |
| 14098 | PyErr_Clear(); /* Don't leave an exception */ |
| 14099 | return; |
| 14100 | } |
| 14101 | } |
| 14102 | /* It might be that the GetItem call fails even |
| 14103 | though the key is present in the dictionary, |
| 14104 | namely when this happens during a stack overflow. */ |
| 14105 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14106 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14107 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14108 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14109 | if (t) { |
| 14110 | Py_INCREF(t); |
| 14111 | Py_DECREF(*p); |
| 14112 | *p = t; |
| 14113 | return; |
| 14114 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14115 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14116 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14117 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14118 | PyErr_Clear(); |
| 14119 | PyThreadState_GET()->recursion_critical = 0; |
| 14120 | return; |
| 14121 | } |
| 14122 | PyThreadState_GET()->recursion_critical = 0; |
| 14123 | /* The two references in interned are not counted by refcnt. |
| 14124 | The deallocator will take care of this */ |
| 14125 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14126 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14127 | } |
| 14128 | |
| 14129 | void |
| 14130 | PyUnicode_InternImmortal(PyObject **p) |
| 14131 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14132 | PyUnicode_InternInPlace(p); |
| 14133 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14134 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14135 | Py_INCREF(*p); |
| 14136 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14137 | } |
| 14138 | |
| 14139 | PyObject * |
| 14140 | PyUnicode_InternFromString(const char *cp) |
| 14141 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14142 | PyObject *s = PyUnicode_FromString(cp); |
| 14143 | if (s == NULL) |
| 14144 | return NULL; |
| 14145 | PyUnicode_InternInPlace(&s); |
| 14146 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14147 | } |
| 14148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14149 | void |
| 14150 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14151 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14152 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14153 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14154 | Py_ssize_t i, n; |
| 14155 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14156 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14157 | if (interned == NULL || !PyDict_Check(interned)) |
| 14158 | return; |
| 14159 | keys = PyDict_Keys(interned); |
| 14160 | if (keys == NULL || !PyList_Check(keys)) { |
| 14161 | PyErr_Clear(); |
| 14162 | return; |
| 14163 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14164 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14165 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14166 | detector, interned unicode strings are not forcibly deallocated; |
| 14167 | rather, we give them their stolen references back, and then clear |
| 14168 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14169 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14170 | n = PyList_GET_SIZE(keys); |
| 14171 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14172 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14173 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14174 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14175 | if (PyUnicode_READY(s) == -1) { |
| 14176 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14177 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14178 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14179 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14180 | case SSTATE_NOT_INTERNED: |
| 14181 | /* XXX Shouldn't happen */ |
| 14182 | break; |
| 14183 | case SSTATE_INTERNED_IMMORTAL: |
| 14184 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14185 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14186 | break; |
| 14187 | case SSTATE_INTERNED_MORTAL: |
| 14188 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14189 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14190 | break; |
| 14191 | default: |
| 14192 | Py_FatalError("Inconsistent interned string state."); |
| 14193 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14194 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14195 | } |
| 14196 | fprintf(stderr, "total size of all interned strings: " |
| 14197 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14198 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14199 | Py_DECREF(keys); |
| 14200 | PyDict_Clear(interned); |
| 14201 | Py_DECREF(interned); |
| 14202 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14203 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14204 | |
| 14205 | |
| 14206 | /********************* Unicode Iterator **************************/ |
| 14207 | |
| 14208 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14209 | PyObject_HEAD |
| 14210 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14211 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14212 | } unicodeiterobject; |
| 14213 | |
| 14214 | static void |
| 14215 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14216 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14217 | _PyObject_GC_UNTRACK(it); |
| 14218 | Py_XDECREF(it->it_seq); |
| 14219 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14220 | } |
| 14221 | |
| 14222 | static int |
| 14223 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14224 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14225 | Py_VISIT(it->it_seq); |
| 14226 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14227 | } |
| 14228 | |
| 14229 | static PyObject * |
| 14230 | unicodeiter_next(unicodeiterobject *it) |
| 14231 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14232 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14233 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14234 | assert(it != NULL); |
| 14235 | seq = it->it_seq; |
| 14236 | if (seq == NULL) |
| 14237 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14238 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14240 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14241 | int kind = PyUnicode_KIND(seq); |
| 14242 | void *data = PyUnicode_DATA(seq); |
| 14243 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14244 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14245 | if (item != NULL) |
| 14246 | ++it->it_index; |
| 14247 | return item; |
| 14248 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14249 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14250 | Py_DECREF(seq); |
| 14251 | it->it_seq = NULL; |
| 14252 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14253 | } |
| 14254 | |
| 14255 | static PyObject * |
| 14256 | unicodeiter_len(unicodeiterobject *it) |
| 14257 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14258 | Py_ssize_t len = 0; |
| 14259 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14260 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14261 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14262 | } |
| 14263 | |
| 14264 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14265 | |
| 14266 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14267 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14268 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14269 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14270 | }; |
| 14271 | |
| 14272 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14273 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14274 | "str_iterator", /* tp_name */ |
| 14275 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14276 | 0, /* tp_itemsize */ |
| 14277 | /* methods */ |
| 14278 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14279 | 0, /* tp_print */ |
| 14280 | 0, /* tp_getattr */ |
| 14281 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14282 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14283 | 0, /* tp_repr */ |
| 14284 | 0, /* tp_as_number */ |
| 14285 | 0, /* tp_as_sequence */ |
| 14286 | 0, /* tp_as_mapping */ |
| 14287 | 0, /* tp_hash */ |
| 14288 | 0, /* tp_call */ |
| 14289 | 0, /* tp_str */ |
| 14290 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14291 | 0, /* tp_setattro */ |
| 14292 | 0, /* tp_as_buffer */ |
| 14293 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14294 | 0, /* tp_doc */ |
| 14295 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14296 | 0, /* tp_clear */ |
| 14297 | 0, /* tp_richcompare */ |
| 14298 | 0, /* tp_weaklistoffset */ |
| 14299 | PyObject_SelfIter, /* tp_iter */ |
| 14300 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14301 | unicodeiter_methods, /* tp_methods */ |
| 14302 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14303 | }; |
| 14304 | |
| 14305 | static PyObject * |
| 14306 | unicode_iter(PyObject *seq) |
| 14307 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14308 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14309 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14310 | if (!PyUnicode_Check(seq)) { |
| 14311 | PyErr_BadInternalCall(); |
| 14312 | return NULL; |
| 14313 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14314 | if (PyUnicode_READY(seq) == -1) |
| 14315 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14316 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14317 | if (it == NULL) |
| 14318 | return NULL; |
| 14319 | it->it_index = 0; |
| 14320 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14321 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14322 | _PyObject_GC_TRACK(it); |
| 14323 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14324 | } |
| 14325 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14326 | |
| 14327 | size_t |
| 14328 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14329 | { |
| 14330 | int res = 0; |
| 14331 | while(*u++) |
| 14332 | res++; |
| 14333 | return res; |
| 14334 | } |
| 14335 | |
| 14336 | Py_UNICODE* |
| 14337 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14338 | { |
| 14339 | Py_UNICODE *u = s1; |
| 14340 | while ((*u++ = *s2++)); |
| 14341 | return s1; |
| 14342 | } |
| 14343 | |
| 14344 | Py_UNICODE* |
| 14345 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14346 | { |
| 14347 | Py_UNICODE *u = s1; |
| 14348 | while ((*u++ = *s2++)) |
| 14349 | if (n-- == 0) |
| 14350 | break; |
| 14351 | return s1; |
| 14352 | } |
| 14353 | |
| 14354 | Py_UNICODE* |
| 14355 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14356 | { |
| 14357 | Py_UNICODE *u1 = s1; |
| 14358 | u1 += Py_UNICODE_strlen(u1); |
| 14359 | Py_UNICODE_strcpy(u1, s2); |
| 14360 | return s1; |
| 14361 | } |
| 14362 | |
| 14363 | int |
| 14364 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14365 | { |
| 14366 | while (*s1 && *s2 && *s1 == *s2) |
| 14367 | s1++, s2++; |
| 14368 | if (*s1 && *s2) |
| 14369 | return (*s1 < *s2) ? -1 : +1; |
| 14370 | if (*s1) |
| 14371 | return 1; |
| 14372 | if (*s2) |
| 14373 | return -1; |
| 14374 | return 0; |
| 14375 | } |
| 14376 | |
| 14377 | int |
| 14378 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14379 | { |
| 14380 | register Py_UNICODE u1, u2; |
| 14381 | for (; n != 0; n--) { |
| 14382 | u1 = *s1; |
| 14383 | u2 = *s2; |
| 14384 | if (u1 != u2) |
| 14385 | return (u1 < u2) ? -1 : +1; |
| 14386 | if (u1 == '\0') |
| 14387 | return 0; |
| 14388 | s1++; |
| 14389 | s2++; |
| 14390 | } |
| 14391 | return 0; |
| 14392 | } |
| 14393 | |
| 14394 | Py_UNICODE* |
| 14395 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14396 | { |
| 14397 | const Py_UNICODE *p; |
| 14398 | for (p = s; *p; p++) |
| 14399 | if (*p == c) |
| 14400 | return (Py_UNICODE*)p; |
| 14401 | return NULL; |
| 14402 | } |
| 14403 | |
| 14404 | Py_UNICODE* |
| 14405 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14406 | { |
| 14407 | const Py_UNICODE *p; |
| 14408 | p = s + Py_UNICODE_strlen(s); |
| 14409 | while (p != s) { |
| 14410 | p--; |
| 14411 | if (*p == c) |
| 14412 | return (Py_UNICODE*)p; |
| 14413 | } |
| 14414 | return NULL; |
| 14415 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14416 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14417 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14418 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14419 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14420 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14421 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14422 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14423 | if (!PyUnicode_Check(unicode)) { |
| 14424 | PyErr_BadArgument(); |
| 14425 | return NULL; |
| 14426 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14427 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14428 | if (u == NULL) |
| 14429 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14430 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14431 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14432 | PyErr_NoMemory(); |
| 14433 | return NULL; |
| 14434 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14435 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14436 | size *= sizeof(Py_UNICODE); |
| 14437 | copy = PyMem_Malloc(size); |
| 14438 | if (copy == NULL) { |
| 14439 | PyErr_NoMemory(); |
| 14440 | return NULL; |
| 14441 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14442 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14443 | return copy; |
| 14444 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14445 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14446 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14447 | to the string.Formatter class implemented in Python. */ |
| 14448 | |
| 14449 | static PyMethodDef _string_methods[] = { |
| 14450 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14451 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14452 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14453 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14454 | {NULL, NULL} |
| 14455 | }; |
| 14456 | |
| 14457 | static struct PyModuleDef _string_module = { |
| 14458 | PyModuleDef_HEAD_INIT, |
| 14459 | "_string", |
| 14460 | PyDoc_STR("string helper module"), |
| 14461 | 0, |
| 14462 | _string_methods, |
| 14463 | NULL, |
| 14464 | NULL, |
| 14465 | NULL, |
| 14466 | NULL |
| 14467 | }; |
| 14468 | |
| 14469 | PyMODINIT_FUNC |
| 14470 | PyInit__string(void) |
| 14471 | { |
| 14472 | return PyModule_Create(&_string_module); |
| 14473 | } |
| 14474 | |
| 14475 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14476 | #ifdef __cplusplus |
| 14477 | } |
| 14478 | #endif |