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" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> |
| 47 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 | |
Victor Stinner | ce5faf6 | 2011-10-05 00:42:43 +0200 | [diff] [blame] | 49 | #ifdef Py_DEBUG |
| 50 | # define DONT_MAKE_RESULT_READY |
| 51 | #endif |
| 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Endianness switches; defaults to little endian */ |
| 54 | |
| 55 | #ifdef WORDS_BIGENDIAN |
| 56 | # define BYTEORDER_IS_BIG_ENDIAN |
| 57 | #else |
| 58 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 59 | #endif |
| 60 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 61 | /* --- Globals ------------------------------------------------------------ |
| 62 | |
| 63 | The globals are initialized by the _PyUnicode_Init() API and should |
| 64 | not be used before calling that API. |
| 65 | |
| 66 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | |
| 69 | #ifdef __cplusplus |
| 70 | extern "C" { |
| 71 | #endif |
| 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 | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 121 | #define _PyUnicode_READY_REPLACE(p_obj) \ |
| 122 | (assert(_PyUnicode_CHECK(*p_obj)), \ |
| 123 | (PyUnicode_IS_READY(*p_obj) ? \ |
| 124 | 0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) |
| 125 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 126 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 127 | (assert(_PyUnicode_CHECK(op)), \ |
| 128 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 129 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 130 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 131 | (assert(_PyUnicode_CHECK(op)), \ |
| 132 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 133 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 134 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 135 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 136 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 137 | (assert(_PyUnicode_CHECK(op)), \ |
| 138 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 139 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 140 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 141 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 142 | /* true if the Unicode object has an allocated wstr memory block |
| 143 | (not shared with other data) */ |
| 144 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 145 | (assert(_PyUnicode_CHECK(op)), \ |
| 146 | (_PyUnicode_WSTR(op) && \ |
| 147 | (!PyUnicode_IS_READY(op) || \ |
| 148 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 149 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 150 | /* Generic helper macro to convert characters of different types. |
| 151 | from_type and to_type have to be valid type names, begin and end |
| 152 | are pointers to the source characters which should be of type |
| 153 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 154 | buffer where the result characters are written to. */ |
| 155 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 156 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 157 | to_type *_to = (to_type *) to; \ |
| 158 | const from_type *_iter = (begin); \ |
| 159 | const from_type *_end = (end); \ |
| 160 | Py_ssize_t n = (_end) - (_iter); \ |
| 161 | const from_type *_unrolled_end = \ |
| 162 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 163 | while (_iter < (_unrolled_end)) { \ |
| 164 | _to[0] = (to_type) _iter[0]; \ |
| 165 | _to[1] = (to_type) _iter[1]; \ |
| 166 | _to[2] = (to_type) _iter[2]; \ |
| 167 | _to[3] = (to_type) _iter[3]; \ |
| 168 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 169 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 170 | while (_iter < (_end)) \ |
| 171 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 172 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 173 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 174 | /* The Unicode string has been modified: reset the hash */ |
| 175 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 176 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 177 | /* This dictionary holds all interned unicode strings. Note that references |
| 178 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 179 | When the interned string reaches a refcnt of 0 the string deallocation |
| 180 | function will delete the reference from this dictionary. |
| 181 | |
| 182 | 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] | 183 | 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] | 184 | */ |
| 185 | static PyObject *interned; |
| 186 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 187 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 188 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 189 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 190 | /* List of static strings. */ |
| 191 | static _Py_Identifier *static_strings; |
| 192 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | /* Single character Unicode strings in the Latin-1 range are being |
| 194 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 195 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 196 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 197 | /* Fast detection of the most frequent whitespace characters */ |
| 198 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 199 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 200 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 201 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 202 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 203 | /* case 0x000C: * FORM FEED */ |
| 204 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 207 | /* case 0x001C: * FILE SEPARATOR */ |
| 208 | /* case 0x001D: * GROUP SEPARATOR */ |
| 209 | /* case 0x001E: * RECORD SEPARATOR */ |
| 210 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 211 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 213 | 1, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 217 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 223 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 224 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 228 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 229 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 230 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 231 | static void copy_characters( |
| 232 | PyObject *to, Py_ssize_t to_start, |
| 233 | PyObject *from, Py_ssize_t from_start, |
| 234 | Py_ssize_t how_many); |
Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 235 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 236 | static int unicode_is_singleton(PyObject *unicode); |
Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 237 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 238 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 239 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 240 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 241 | static PyObject * |
| 242 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 243 | static PyObject * |
| 244 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 245 | static PyObject * |
| 246 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 247 | |
| 248 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 249 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 250 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 251 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 252 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 254 | static void |
| 255 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 256 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 257 | PyObject *unicode, |
| 258 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 259 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 260 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 261 | /* Same for linebreaks */ |
| 262 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 263 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 264 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 265 | /* 0x000B, * LINE TABULATION */ |
| 266 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 267 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 268 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 270 | /* 0x001C, * FILE SEPARATOR */ |
| 271 | /* 0x001D, * GROUP SEPARATOR */ |
| 272 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | 0, 0, 0, 0, 1, 1, 1, 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 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 289 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 290 | 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] | 291 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 292 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 293 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 294 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 295 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 296 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 297 | /* This is actually an illegal character, so it should |
| 298 | not be passed to unichr. */ |
| 299 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 300 | #endif |
| 301 | } |
| 302 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 303 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 304 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 305 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 306 | { |
| 307 | PyASCIIObject *ascii; |
| 308 | unsigned int kind; |
| 309 | |
| 310 | assert(PyUnicode_Check(op)); |
| 311 | |
| 312 | ascii = (PyASCIIObject *)op; |
| 313 | kind = ascii->state.kind; |
| 314 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 315 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 316 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 317 | assert(ascii->state.ready == 1); |
| 318 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 319 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 320 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 321 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 322 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 323 | if (ascii->state.compact == 1) { |
| 324 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 325 | assert(kind == PyUnicode_1BYTE_KIND |
| 326 | || kind == PyUnicode_2BYTE_KIND |
| 327 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 328 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 329 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 330 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 331 | } |
| 332 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 333 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 334 | |
| 335 | data = unicode->data.any; |
| 336 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 337 | assert(ascii->length == 0); |
| 338 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 339 | assert(ascii->state.compact == 0); |
| 340 | assert(ascii->state.ascii == 0); |
| 341 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 342 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 343 | assert(ascii->wstr != NULL); |
| 344 | assert(data == NULL); |
| 345 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 346 | } |
| 347 | else { |
| 348 | assert(kind == PyUnicode_1BYTE_KIND |
| 349 | || kind == PyUnicode_2BYTE_KIND |
| 350 | || kind == PyUnicode_4BYTE_KIND); |
| 351 | assert(ascii->state.compact == 0); |
| 352 | assert(ascii->state.ready == 1); |
| 353 | assert(data != NULL); |
| 354 | if (ascii->state.ascii) { |
| 355 | assert (compact->utf8 == data); |
| 356 | assert (compact->utf8_length == ascii->length); |
| 357 | } |
| 358 | else |
| 359 | assert (compact->utf8 != data); |
| 360 | } |
| 361 | } |
| 362 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 363 | if ( |
| 364 | #if SIZEOF_WCHAR_T == 2 |
| 365 | kind == PyUnicode_2BYTE_KIND |
| 366 | #else |
| 367 | kind == PyUnicode_4BYTE_KIND |
| 368 | #endif |
| 369 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 370 | { |
| 371 | assert(ascii->wstr == data); |
| 372 | assert(compact->wstr_length == ascii->length); |
| 373 | } else |
| 374 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 375 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 376 | |
| 377 | if (compact->utf8 == NULL) |
| 378 | assert(compact->utf8_length == 0); |
| 379 | if (ascii->wstr == NULL) |
| 380 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 381 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 382 | /* check that the best kind is used */ |
| 383 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 384 | { |
| 385 | Py_ssize_t i; |
| 386 | Py_UCS4 maxchar = 0; |
| 387 | void *data = PyUnicode_DATA(ascii); |
| 388 | for (i=0; i < ascii->length; i++) |
| 389 | { |
| 390 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 391 | if (ch > maxchar) |
| 392 | maxchar = ch; |
| 393 | } |
| 394 | if (kind == PyUnicode_1BYTE_KIND) { |
| 395 | if (ascii->state.ascii == 0) |
| 396 | assert(maxchar >= 128); |
| 397 | else |
| 398 | assert(maxchar < 128); |
| 399 | } |
| 400 | else if (kind == PyUnicode_2BYTE_KIND) |
| 401 | assert(maxchar >= 0x100); |
| 402 | else |
| 403 | assert(maxchar >= 0x10000); |
| 404 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 405 | if (check_content && !unicode_is_singleton(op)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 406 | assert(ascii->hash == -1); |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 407 | return 1; |
| 408 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 409 | #endif |
| 410 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 411 | #ifdef HAVE_MBCS |
| 412 | static OSVERSIONINFOEX winver; |
| 413 | #endif |
| 414 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 415 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 416 | |
| 417 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 418 | to keep things simple, we use a single bitmask, using the least 5 |
| 419 | bits from each unicode characters as the bit index. */ |
| 420 | |
| 421 | /* the linebreak mask is set up by Unicode_Init below */ |
| 422 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 423 | #if LONG_BIT >= 128 |
| 424 | #define BLOOM_WIDTH 128 |
| 425 | #elif LONG_BIT >= 64 |
| 426 | #define BLOOM_WIDTH 64 |
| 427 | #elif LONG_BIT >= 32 |
| 428 | #define BLOOM_WIDTH 32 |
| 429 | #else |
| 430 | #error "LONG_BIT is smaller than 32" |
| 431 | #endif |
| 432 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 433 | #define BLOOM_MASK unsigned long |
| 434 | |
| 435 | static BLOOM_MASK bloom_linebreak; |
| 436 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 437 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 438 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 439 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 440 | #define BLOOM_LINEBREAK(ch) \ |
| 441 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 442 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 444 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 445 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 446 | { |
| 447 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 448 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 449 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 450 | Py_ssize_t i; |
| 451 | |
| 452 | mask = 0; |
| 453 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 454 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 455 | |
| 456 | return mask; |
| 457 | } |
| 458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 459 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 460 | (BLOOM(mask, chr) \ |
| 461 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 463 | /* Compilation of templated routines */ |
| 464 | |
| 465 | #include "stringlib/asciilib.h" |
| 466 | #include "stringlib/fastsearch.h" |
| 467 | #include "stringlib/partition.h" |
| 468 | #include "stringlib/split.h" |
| 469 | #include "stringlib/count.h" |
| 470 | #include "stringlib/find.h" |
| 471 | #include "stringlib/find_max_char.h" |
| 472 | #include "stringlib/localeutil.h" |
| 473 | #include "stringlib/undef.h" |
| 474 | |
| 475 | #include "stringlib/ucs1lib.h" |
| 476 | #include "stringlib/fastsearch.h" |
| 477 | #include "stringlib/partition.h" |
| 478 | #include "stringlib/split.h" |
| 479 | #include "stringlib/count.h" |
| 480 | #include "stringlib/find.h" |
| 481 | #include "stringlib/find_max_char.h" |
| 482 | #include "stringlib/localeutil.h" |
| 483 | #include "stringlib/undef.h" |
| 484 | |
| 485 | #include "stringlib/ucs2lib.h" |
| 486 | #include "stringlib/fastsearch.h" |
| 487 | #include "stringlib/partition.h" |
| 488 | #include "stringlib/split.h" |
| 489 | #include "stringlib/count.h" |
| 490 | #include "stringlib/find.h" |
| 491 | #include "stringlib/find_max_char.h" |
| 492 | #include "stringlib/localeutil.h" |
| 493 | #include "stringlib/undef.h" |
| 494 | |
| 495 | #include "stringlib/ucs4lib.h" |
| 496 | #include "stringlib/fastsearch.h" |
| 497 | #include "stringlib/partition.h" |
| 498 | #include "stringlib/split.h" |
| 499 | #include "stringlib/count.h" |
| 500 | #include "stringlib/find.h" |
| 501 | #include "stringlib/find_max_char.h" |
| 502 | #include "stringlib/localeutil.h" |
| 503 | #include "stringlib/undef.h" |
| 504 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 505 | #include "stringlib/unicodedefs.h" |
| 506 | #include "stringlib/fastsearch.h" |
| 507 | #include "stringlib/count.h" |
| 508 | #include "stringlib/find.h" |
| 509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 510 | /* --- Unicode Object ----------------------------------------------------- */ |
| 511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 512 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 513 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 514 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 515 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 516 | Py_ssize_t size, Py_UCS4 ch, |
| 517 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 518 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 519 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 520 | |
| 521 | switch (kind) { |
| 522 | case PyUnicode_1BYTE_KIND: |
| 523 | { |
| 524 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 525 | if (ch1 == ch) |
| 526 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 527 | else |
| 528 | return -1; |
| 529 | } |
| 530 | case PyUnicode_2BYTE_KIND: |
| 531 | { |
| 532 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 533 | if (ch2 == ch) |
| 534 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 535 | else |
| 536 | return -1; |
| 537 | } |
| 538 | case PyUnicode_4BYTE_KIND: |
| 539 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 540 | default: |
| 541 | assert(0); |
| 542 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 546 | static PyObject* |
| 547 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 548 | { |
| 549 | Py_ssize_t char_size; |
| 550 | Py_ssize_t struct_size; |
| 551 | Py_ssize_t new_size; |
| 552 | int share_wstr; |
| 553 | |
| 554 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 555 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 556 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 557 | struct_size = sizeof(PyASCIIObject); |
| 558 | else |
| 559 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 560 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 561 | |
| 562 | _Py_DEC_REFTOTAL; |
| 563 | _Py_ForgetReference(unicode); |
| 564 | |
| 565 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 566 | PyErr_NoMemory(); |
| 567 | return NULL; |
| 568 | } |
| 569 | new_size = (struct_size + (length + 1) * char_size); |
| 570 | |
| 571 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 572 | if (unicode == NULL) { |
| 573 | PyObject_Del(unicode); |
| 574 | PyErr_NoMemory(); |
| 575 | return NULL; |
| 576 | } |
| 577 | _Py_NewReference(unicode); |
| 578 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 579 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 580 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 581 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 582 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 583 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 584 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 585 | length, 0); |
| 586 | return unicode; |
| 587 | } |
| 588 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 589 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 590 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 592 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 593 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 594 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 595 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 596 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 597 | |
| 598 | if (PyUnicode_IS_READY(unicode)) { |
| 599 | Py_ssize_t char_size; |
| 600 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 601 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 602 | void *data; |
| 603 | |
| 604 | data = _PyUnicode_DATA_ANY(unicode); |
| 605 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 606 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 607 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 608 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 609 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 610 | { |
| 611 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 612 | _PyUnicode_UTF8(unicode) = NULL; |
| 613 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 614 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 615 | |
| 616 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 617 | PyErr_NoMemory(); |
| 618 | return -1; |
| 619 | } |
| 620 | new_size = (length + 1) * char_size; |
| 621 | |
| 622 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 623 | if (data == NULL) { |
| 624 | PyErr_NoMemory(); |
| 625 | return -1; |
| 626 | } |
| 627 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 628 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 629 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 630 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 631 | } |
| 632 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 633 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 634 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 635 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 636 | _PyUnicode_LENGTH(unicode) = length; |
| 637 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 638 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 639 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 640 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 641 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 642 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 643 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 644 | |
| 645 | /* check for integer overflow */ |
| 646 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 647 | PyErr_NoMemory(); |
| 648 | return -1; |
| 649 | } |
| 650 | wstr = _PyUnicode_WSTR(unicode); |
| 651 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 652 | if (!wstr) { |
| 653 | PyErr_NoMemory(); |
| 654 | return -1; |
| 655 | } |
| 656 | _PyUnicode_WSTR(unicode) = wstr; |
| 657 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 658 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 659 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 663 | static PyObject* |
| 664 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 665 | { |
| 666 | Py_ssize_t copy_length; |
| 667 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 668 | PyObject *copy; |
| 669 | assert(PyUnicode_IS_READY(unicode)); |
| 670 | |
| 671 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 672 | if (copy == NULL) |
| 673 | return NULL; |
| 674 | |
| 675 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 676 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 677 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 678 | } |
| 679 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 680 | PyObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 681 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 682 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 683 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 684 | if (w == NULL) |
| 685 | return NULL; |
| 686 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 687 | copy_length = Py_MIN(copy_length, length); |
| 688 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 689 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 690 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 694 | /* 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] | 695 | Ux0000 terminated; some code (e.g. new_identifier) |
| 696 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 697 | |
| 698 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 699 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 700 | |
| 701 | */ |
| 702 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 704 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 705 | #endif |
| 706 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 707 | static PyUnicodeObject * |
| 708 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 709 | { |
| 710 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 711 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 713 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 714 | if (length == 0 && unicode_empty != NULL) { |
| 715 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 716 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 719 | /* Ensure we won't overflow the size. */ |
| 720 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 721 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 722 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 723 | if (length < 0) { |
| 724 | PyErr_SetString(PyExc_SystemError, |
| 725 | "Negative size passed to _PyUnicode_New"); |
| 726 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 729 | #ifdef Py_DEBUG |
| 730 | ++unicode_old_new_calls; |
| 731 | #endif |
| 732 | |
| 733 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 734 | if (unicode == NULL) |
| 735 | return NULL; |
| 736 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 737 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 738 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 739 | PyErr_NoMemory(); |
| 740 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 741 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 742 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 743 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 744 | * the caller fails before initializing str -- unicode_resize() |
| 745 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 746 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 747 | * We don't want unicode_resize to read uninitialized memory in |
| 748 | * that case. |
| 749 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 750 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 751 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 752 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 753 | _PyUnicode_HASH(unicode) = -1; |
| 754 | _PyUnicode_STATE(unicode).interned = 0; |
| 755 | _PyUnicode_STATE(unicode).kind = 0; |
| 756 | _PyUnicode_STATE(unicode).compact = 0; |
| 757 | _PyUnicode_STATE(unicode).ready = 0; |
| 758 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 759 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 760 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 761 | _PyUnicode_UTF8(unicode) = NULL; |
| 762 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 763 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 764 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 765 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 766 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 767 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 768 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 769 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 770 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 771 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 774 | static const char* |
| 775 | unicode_kind_name(PyObject *unicode) |
| 776 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 777 | /* don't check consistency: unicode_kind_name() is called from |
| 778 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 779 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 780 | { |
| 781 | if (!PyUnicode_IS_READY(unicode)) |
| 782 | return "wstr"; |
| 783 | switch(PyUnicode_KIND(unicode)) |
| 784 | { |
| 785 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 786 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 787 | return "legacy ascii"; |
| 788 | else |
| 789 | return "legacy latin1"; |
| 790 | case PyUnicode_2BYTE_KIND: |
| 791 | return "legacy UCS2"; |
| 792 | case PyUnicode_4BYTE_KIND: |
| 793 | return "legacy UCS4"; |
| 794 | default: |
| 795 | return "<legacy invalid kind>"; |
| 796 | } |
| 797 | } |
| 798 | assert(PyUnicode_IS_READY(unicode)); |
| 799 | switch(PyUnicode_KIND(unicode)) |
| 800 | { |
| 801 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 802 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 803 | return "ascii"; |
| 804 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 805 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 806 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 807 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 808 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 809 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 810 | default: |
| 811 | return "<invalid compact kind>"; |
| 812 | } |
| 813 | } |
| 814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 815 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 816 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 817 | |
| 818 | /* Functions wrapping macros for use in debugger */ |
| 819 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 820 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | void *_PyUnicode_compact_data(void *unicode) { |
| 824 | return _PyUnicode_COMPACT_DATA(unicode); |
| 825 | } |
| 826 | void *_PyUnicode_data(void *unicode){ |
| 827 | printf("obj %p\n", unicode); |
| 828 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 829 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 830 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 831 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 832 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 833 | return PyUnicode_DATA(unicode); |
| 834 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 835 | |
| 836 | void |
| 837 | _PyUnicode_Dump(PyObject *op) |
| 838 | { |
| 839 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 840 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 841 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 842 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 843 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 844 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 845 | { |
| 846 | if (ascii->state.ascii) |
| 847 | data = (ascii + 1); |
| 848 | else |
| 849 | data = (compact + 1); |
| 850 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 851 | else |
| 852 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 853 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 854 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 855 | if (ascii->wstr == data) |
| 856 | printf("shared "); |
| 857 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 858 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 859 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 860 | printf(" (%zu), ", compact->wstr_length); |
| 861 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 862 | printf("shared "); |
| 863 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 864 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 865 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 866 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 867 | #endif |
| 868 | |
| 869 | PyObject * |
| 870 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 871 | { |
| 872 | PyObject *obj; |
| 873 | PyCompactUnicodeObject *unicode; |
| 874 | void *data; |
| 875 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 876 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 877 | Py_ssize_t char_size; |
| 878 | Py_ssize_t struct_size; |
| 879 | |
| 880 | /* Optimization for empty strings */ |
| 881 | if (size == 0 && unicode_empty != NULL) { |
| 882 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 883 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | #ifdef Py_DEBUG |
| 887 | ++unicode_new_new_calls; |
| 888 | #endif |
| 889 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 890 | is_ascii = 0; |
| 891 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 892 | struct_size = sizeof(PyCompactUnicodeObject); |
| 893 | if (maxchar < 128) { |
| 894 | kind_state = PyUnicode_1BYTE_KIND; |
| 895 | char_size = 1; |
| 896 | is_ascii = 1; |
| 897 | struct_size = sizeof(PyASCIIObject); |
| 898 | } |
| 899 | else if (maxchar < 256) { |
| 900 | kind_state = PyUnicode_1BYTE_KIND; |
| 901 | char_size = 1; |
| 902 | } |
| 903 | else if (maxchar < 65536) { |
| 904 | kind_state = PyUnicode_2BYTE_KIND; |
| 905 | char_size = 2; |
| 906 | if (sizeof(wchar_t) == 2) |
| 907 | is_sharing = 1; |
| 908 | } |
| 909 | else { |
| 910 | kind_state = PyUnicode_4BYTE_KIND; |
| 911 | char_size = 4; |
| 912 | if (sizeof(wchar_t) == 4) |
| 913 | is_sharing = 1; |
| 914 | } |
| 915 | |
| 916 | /* Ensure we won't overflow the size. */ |
| 917 | if (size < 0) { |
| 918 | PyErr_SetString(PyExc_SystemError, |
| 919 | "Negative size passed to PyUnicode_New"); |
| 920 | return NULL; |
| 921 | } |
| 922 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 923 | return PyErr_NoMemory(); |
| 924 | |
| 925 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 926 | * PyObject_New() so we are able to allocate space for the object and |
| 927 | * it's data buffer. |
| 928 | */ |
| 929 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 930 | if (obj == NULL) |
| 931 | return PyErr_NoMemory(); |
| 932 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 933 | if (obj == NULL) |
| 934 | return NULL; |
| 935 | |
| 936 | unicode = (PyCompactUnicodeObject *)obj; |
| 937 | if (is_ascii) |
| 938 | data = ((PyASCIIObject*)obj) + 1; |
| 939 | else |
| 940 | data = unicode + 1; |
| 941 | _PyUnicode_LENGTH(unicode) = size; |
| 942 | _PyUnicode_HASH(unicode) = -1; |
| 943 | _PyUnicode_STATE(unicode).interned = 0; |
| 944 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 945 | _PyUnicode_STATE(unicode).compact = 1; |
| 946 | _PyUnicode_STATE(unicode).ready = 1; |
| 947 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 948 | if (is_ascii) { |
| 949 | ((char*)data)[size] = 0; |
| 950 | _PyUnicode_WSTR(unicode) = NULL; |
| 951 | } |
| 952 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 953 | ((char*)data)[size] = 0; |
| 954 | _PyUnicode_WSTR(unicode) = NULL; |
| 955 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 956 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 957 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 958 | } |
| 959 | else { |
| 960 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 961 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 962 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 963 | ((Py_UCS2*)data)[size] = 0; |
| 964 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 965 | ((Py_UCS4*)data)[size] = 0; |
| 966 | if (is_sharing) { |
| 967 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 968 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 969 | } |
| 970 | else { |
| 971 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 972 | _PyUnicode_WSTR(unicode) = NULL; |
| 973 | } |
| 974 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 975 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 976 | return obj; |
| 977 | } |
| 978 | |
| 979 | #if SIZEOF_WCHAR_T == 2 |
| 980 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 981 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 982 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 983 | |
| 984 | This function assumes that unicode can hold one more code point than wstr |
| 985 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 986 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | 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] | 988 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 989 | { |
| 990 | const wchar_t *iter; |
| 991 | Py_UCS4 *ucs4_out; |
| 992 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 993 | assert(unicode != NULL); |
| 994 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 995 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 996 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 997 | |
| 998 | for (iter = begin; iter < end; ) { |
| 999 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1000 | _PyUnicode_GET_LENGTH(unicode))); |
| 1001 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1002 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1003 | { |
| 1004 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 1005 | iter += 2; |
| 1006 | } |
| 1007 | else { |
| 1008 | *ucs4_out++ = *iter; |
| 1009 | iter++; |
| 1010 | } |
| 1011 | } |
| 1012 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1013 | _PyUnicode_GET_LENGTH(unicode))); |
| 1014 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1015 | } |
| 1016 | #endif |
| 1017 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1018 | static int |
| 1019 | _PyUnicode_Dirty(PyObject *unicode) |
| 1020 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1021 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1022 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1023 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1024 | "Cannot modify a string having more than 1 reference"); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | _PyUnicode_DIRTY(unicode); |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1031 | static int |
| 1032 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1033 | PyObject *from, Py_ssize_t from_start, |
| 1034 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1035 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1036 | unsigned int from_kind, to_kind; |
| 1037 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1038 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1039 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1040 | assert(PyUnicode_Check(from)); |
| 1041 | assert(PyUnicode_Check(to)); |
| 1042 | assert(PyUnicode_IS_READY(from)); |
| 1043 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1045 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1046 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1047 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1049 | if (how_many == 0) |
| 1050 | return 0; |
| 1051 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1053 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1055 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1057 | #ifdef Py_DEBUG |
| 1058 | if (!check_maxchar |
| 1059 | && (from_kind > to_kind |
| 1060 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1061 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1062 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1063 | Py_UCS4 ch; |
| 1064 | Py_ssize_t i; |
| 1065 | for (i=0; i < how_many; i++) { |
| 1066 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1067 | assert(ch <= to_maxchar); |
| 1068 | } |
| 1069 | } |
| 1070 | #endif |
| 1071 | fast = (from_kind == to_kind); |
| 1072 | if (check_maxchar |
| 1073 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1074 | { |
| 1075 | /* deny latin1 => ascii */ |
| 1076 | fast = 0; |
| 1077 | } |
| 1078 | |
| 1079 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1080 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1081 | (char*)from_data + from_kind * from_start, |
| 1082 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1083 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1084 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1085 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1086 | { |
| 1087 | _PyUnicode_CONVERT_BYTES( |
| 1088 | Py_UCS1, Py_UCS2, |
| 1089 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1090 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1091 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1092 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1093 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1094 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1095 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1096 | { |
| 1097 | _PyUnicode_CONVERT_BYTES( |
| 1098 | Py_UCS1, Py_UCS4, |
| 1099 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1100 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1101 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1102 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1103 | } |
| 1104 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1105 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1106 | { |
| 1107 | _PyUnicode_CONVERT_BYTES( |
| 1108 | Py_UCS2, Py_UCS4, |
| 1109 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1110 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1111 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1112 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1113 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1114 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1115 | /* check if max_char(from substring) <= max_char(to) */ |
| 1116 | if (from_kind > to_kind |
| 1117 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1118 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1119 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1120 | /* slow path to check for character overflow */ |
| 1121 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1122 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1123 | Py_ssize_t i; |
| 1124 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1125 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1126 | for (i=0; i < how_many; i++) { |
| 1127 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1128 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1129 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1130 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1131 | #else |
| 1132 | if (!check_maxchar) { |
| 1133 | for (i=0; i < how_many; i++) { |
| 1134 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1135 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1136 | } |
| 1137 | } |
| 1138 | else { |
| 1139 | for (i=0; i < how_many; i++) { |
| 1140 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1141 | if (ch > to_maxchar) |
| 1142 | return 1; |
| 1143 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1144 | } |
| 1145 | } |
| 1146 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1147 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1148 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1149 | assert(0 && "inconsistent state"); |
| 1150 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1151 | } |
| 1152 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | static void |
| 1157 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1158 | PyObject *from, Py_ssize_t from_start, |
| 1159 | Py_ssize_t how_many) |
| 1160 | { |
| 1161 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1162 | } |
| 1163 | |
| 1164 | Py_ssize_t |
| 1165 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1166 | PyObject *from, Py_ssize_t from_start, |
| 1167 | Py_ssize_t how_many) |
| 1168 | { |
| 1169 | int err; |
| 1170 | |
| 1171 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1172 | PyErr_BadInternalCall(); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | |
| 1176 | if (PyUnicode_READY(from)) |
| 1177 | return -1; |
| 1178 | if (PyUnicode_READY(to)) |
| 1179 | return -1; |
| 1180 | |
| 1181 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1182 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1183 | PyErr_Format(PyExc_SystemError, |
| 1184 | "Cannot write %zi characters at %zi " |
| 1185 | "in a string of %zi characters", |
| 1186 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1187 | return -1; |
| 1188 | } |
| 1189 | |
| 1190 | if (how_many == 0) |
| 1191 | return 0; |
| 1192 | |
| 1193 | if (_PyUnicode_Dirty(to)) |
| 1194 | return -1; |
| 1195 | |
| 1196 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1197 | if (err) { |
| 1198 | PyErr_Format(PyExc_SystemError, |
| 1199 | "Cannot copy %s characters " |
| 1200 | "into a string of %s characters", |
| 1201 | unicode_kind_name(from), |
| 1202 | unicode_kind_name(to)); |
| 1203 | return -1; |
| 1204 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1205 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1208 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1209 | correct string length can be computed before converting a string to UCS4. |
| 1210 | This function counts single surrogates as a character and not as a pair. |
| 1211 | |
| 1212 | Return 0 on success, or -1 on error. */ |
| 1213 | static int |
| 1214 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1215 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | { |
| 1217 | const wchar_t *iter; |
| 1218 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1219 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1220 | *num_surrogates = 0; |
| 1221 | *maxchar = 0; |
| 1222 | |
| 1223 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1224 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1225 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1226 | #if SIZEOF_WCHAR_T != 2 |
| 1227 | if (*maxchar >= 0x10000) |
| 1228 | return 0; |
| 1229 | #endif |
| 1230 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1231 | #if SIZEOF_WCHAR_T == 2 |
| 1232 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1233 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1234 | { |
| 1235 | Py_UCS4 surrogate_val; |
| 1236 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1237 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1238 | ++(*num_surrogates); |
| 1239 | if (surrogate_val > *maxchar) |
| 1240 | *maxchar = surrogate_val; |
| 1241 | iter += 2; |
| 1242 | } |
| 1243 | else |
| 1244 | iter++; |
| 1245 | #else |
| 1246 | iter++; |
| 1247 | #endif |
| 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1253 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1254 | #endif |
| 1255 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1256 | static int |
| 1257 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1258 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1259 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1260 | wchar_t *end; |
| 1261 | Py_UCS4 maxchar = 0; |
| 1262 | Py_ssize_t num_surrogates; |
| 1263 | #if SIZEOF_WCHAR_T == 2 |
| 1264 | Py_ssize_t length_wo_surrogates; |
| 1265 | #endif |
| 1266 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1267 | assert(p_obj != NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1268 | unicode = *p_obj; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1269 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1270 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1271 | strings were created using _PyObject_New() and where no canonical |
| 1272 | representation (the str field) has been set yet aka strings |
| 1273 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1274 | assert(_PyUnicode_CHECK(unicode)); |
| 1275 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1276 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1277 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1278 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1279 | /* Actually, it should neither be interned nor be anything else: */ |
| 1280 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1281 | |
| 1282 | #ifdef Py_DEBUG |
| 1283 | ++unicode_ready_calls; |
| 1284 | #endif |
| 1285 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1286 | #ifdef Py_DEBUG |
| 1287 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1288 | #else |
| 1289 | if (replace && Py_REFCNT(unicode) != 1) |
| 1290 | replace = 0; |
| 1291 | #endif |
| 1292 | if (replace) { |
| 1293 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1294 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1295 | /* Optimization for empty strings */ |
| 1296 | if (len == 0) { |
| 1297 | Py_INCREF(unicode_empty); |
| 1298 | Py_DECREF(*p_obj); |
| 1299 | *p_obj = unicode_empty; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | if (len == 1 && wstr[0] < 256) { |
| 1303 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1304 | if (latin1_char == NULL) |
| 1305 | return -1; |
| 1306 | Py_DECREF(*p_obj); |
| 1307 | *p_obj = latin1_char; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | } |
| 1311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1312 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1313 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1314 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | |
| 1317 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1318 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1319 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | PyErr_NoMemory(); |
| 1321 | return -1; |
| 1322 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1323 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | _PyUnicode_WSTR(unicode), end, |
| 1325 | PyUnicode_1BYTE_DATA(unicode)); |
| 1326 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1327 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1328 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1329 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1330 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1331 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1332 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1333 | } |
| 1334 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1335 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1336 | _PyUnicode_UTF8(unicode) = NULL; |
| 1337 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | } |
| 1339 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1340 | _PyUnicode_WSTR(unicode) = NULL; |
| 1341 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1342 | } |
| 1343 | /* In this case we might have to convert down from 4-byte native |
| 1344 | wchar_t to 2-byte unicode. */ |
| 1345 | else if (maxchar < 65536) { |
| 1346 | assert(num_surrogates == 0 && |
| 1347 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1348 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1349 | #if SIZEOF_WCHAR_T == 2 |
| 1350 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1351 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1352 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1353 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1354 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1355 | _PyUnicode_UTF8(unicode) = NULL; |
| 1356 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1357 | #else |
| 1358 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1359 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1360 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1361 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1362 | PyErr_NoMemory(); |
| 1363 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1364 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1365 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1366 | _PyUnicode_WSTR(unicode), end, |
| 1367 | PyUnicode_2BYTE_DATA(unicode)); |
| 1368 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1369 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1370 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1371 | _PyUnicode_UTF8(unicode) = NULL; |
| 1372 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1373 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1374 | _PyUnicode_WSTR(unicode) = NULL; |
| 1375 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1376 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | } |
| 1378 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1379 | else { |
| 1380 | #if SIZEOF_WCHAR_T == 2 |
| 1381 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1382 | new normalized 4-byte version. */ |
| 1383 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1384 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1385 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1386 | PyErr_NoMemory(); |
| 1387 | return -1; |
| 1388 | } |
| 1389 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1390 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1391 | _PyUnicode_UTF8(unicode) = NULL; |
| 1392 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1393 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1394 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1395 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1397 | _PyUnicode_WSTR(unicode) = NULL; |
| 1398 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1399 | #else |
| 1400 | assert(num_surrogates == 0); |
| 1401 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1402 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1403 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1404 | _PyUnicode_UTF8(unicode) = NULL; |
| 1405 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1407 | #endif |
| 1408 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1409 | } |
| 1410 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1411 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1412 | return 0; |
| 1413 | } |
| 1414 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1415 | int |
| 1416 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1417 | { |
| 1418 | return unicode_ready(op, 1); |
| 1419 | } |
| 1420 | |
| 1421 | int |
| 1422 | _PyUnicode_Ready(PyObject *op) |
| 1423 | { |
| 1424 | return unicode_ready(&op, 0); |
| 1425 | } |
| 1426 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1427 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1428 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1430 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1431 | case SSTATE_NOT_INTERNED: |
| 1432 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1433 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1434 | case SSTATE_INTERNED_MORTAL: |
| 1435 | /* revive dead object temporarily for DelItem */ |
| 1436 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1437 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1438 | Py_FatalError( |
| 1439 | "deletion of interned string failed"); |
| 1440 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1441 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1442 | case SSTATE_INTERNED_IMMORTAL: |
| 1443 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1445 | default: |
| 1446 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1449 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1450 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1451 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1453 | |
| 1454 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1455 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1456 | } |
| 1457 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1458 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1459 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1460 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1464 | #ifdef Py_DEBUG |
| 1465 | static int |
| 1466 | unicode_is_singleton(PyObject *unicode) |
| 1467 | { |
| 1468 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1469 | if (unicode == unicode_empty) |
| 1470 | return 1; |
| 1471 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1472 | { |
| 1473 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1474 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1475 | return 1; |
| 1476 | } |
| 1477 | return 0; |
| 1478 | } |
| 1479 | #endif |
| 1480 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1481 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1482 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1483 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1484 | if (Py_REFCNT(unicode) != 1) |
| 1485 | return 0; |
| 1486 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1487 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1488 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1489 | /* singleton refcount is greater than 1 */ |
| 1490 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1491 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1492 | return 1; |
| 1493 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1494 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1495 | static int |
| 1496 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1497 | { |
| 1498 | PyObject *unicode; |
| 1499 | Py_ssize_t old_length; |
| 1500 | |
| 1501 | assert(p_unicode != NULL); |
| 1502 | unicode = *p_unicode; |
| 1503 | |
| 1504 | assert(unicode != NULL); |
| 1505 | assert(PyUnicode_Check(unicode)); |
| 1506 | assert(0 <= length); |
| 1507 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1508 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1509 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1510 | else |
| 1511 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1512 | if (old_length == length) |
| 1513 | return 0; |
| 1514 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1515 | if (length == 0) { |
| 1516 | Py_DECREF(*p_unicode); |
| 1517 | *p_unicode = unicode_empty; |
| 1518 | Py_INCREF(*p_unicode); |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1522 | if (!unicode_resizable(unicode)) { |
| 1523 | PyObject *copy = resize_copy(unicode, length); |
| 1524 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1525 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1526 | Py_DECREF(*p_unicode); |
| 1527 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1528 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1531 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1532 | *p_unicode = resize_compact(unicode, length); |
| 1533 | if (*p_unicode == NULL) |
| 1534 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1535 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1536 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1537 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1538 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1541 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1542 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1543 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1544 | PyObject *unicode; |
| 1545 | if (p_unicode == NULL) { |
| 1546 | PyErr_BadInternalCall(); |
| 1547 | return -1; |
| 1548 | } |
| 1549 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1550 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1551 | { |
| 1552 | PyErr_BadInternalCall(); |
| 1553 | return -1; |
| 1554 | } |
| 1555 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1556 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1557 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1558 | static int |
| 1559 | unicode_widen(PyObject **p_unicode, int maxchar) |
| 1560 | { |
| 1561 | PyObject *result; |
| 1562 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1563 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1564 | return 0; |
| 1565 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1566 | maxchar); |
| 1567 | if (result == NULL) |
| 1568 | return -1; |
| 1569 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1570 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1571 | Py_DECREF(*p_unicode); |
| 1572 | *p_unicode = result; |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | static int |
| 1577 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1578 | Py_UCS4 ch) |
| 1579 | { |
| 1580 | if (unicode_widen(p_unicode, ch) < 0) |
| 1581 | return -1; |
| 1582 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1583 | PyUnicode_DATA(*p_unicode), |
| 1584 | (*pos)++, ch); |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1588 | static PyObject* |
| 1589 | get_latin1_char(unsigned char ch) |
| 1590 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1591 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1592 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1593 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1594 | if (!unicode) |
| 1595 | return NULL; |
| 1596 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1597 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1598 | unicode_latin1[ch] = unicode; |
| 1599 | } |
| 1600 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1601 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1602 | } |
| 1603 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1604 | PyObject * |
| 1605 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1606 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1607 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1608 | Py_UCS4 maxchar = 0; |
| 1609 | Py_ssize_t num_surrogates; |
| 1610 | |
| 1611 | if (u == NULL) |
| 1612 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1613 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1614 | /* If the Unicode data is known at construction time, we can apply |
| 1615 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1617 | /* Optimization for empty strings */ |
| 1618 | if (size == 0 && unicode_empty != NULL) { |
| 1619 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1620 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1621 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1623 | /* Single character Unicode objects in the Latin-1 range are |
| 1624 | shared when using this constructor */ |
| 1625 | if (size == 1 && *u < 256) |
| 1626 | return get_latin1_char((unsigned char)*u); |
| 1627 | |
| 1628 | /* If not empty and not single character, copy the Unicode data |
| 1629 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1630 | if (find_maxchar_surrogates(u, u + size, |
| 1631 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1632 | return NULL; |
| 1633 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1634 | unicode = PyUnicode_New(size - num_surrogates, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1635 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1636 | if (!unicode) |
| 1637 | return NULL; |
| 1638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1639 | switch (PyUnicode_KIND(unicode)) { |
| 1640 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1641 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1642 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1643 | break; |
| 1644 | case PyUnicode_2BYTE_KIND: |
| 1645 | #if Py_UNICODE_SIZE == 2 |
| 1646 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1647 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1648 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1649 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1650 | #endif |
| 1651 | break; |
| 1652 | case PyUnicode_4BYTE_KIND: |
| 1653 | #if SIZEOF_WCHAR_T == 2 |
| 1654 | /* This is the only case which has to process surrogates, thus |
| 1655 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1656 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1657 | #else |
| 1658 | assert(num_surrogates == 0); |
| 1659 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1660 | #endif |
| 1661 | break; |
| 1662 | default: |
| 1663 | assert(0 && "Impossible state"); |
| 1664 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1665 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1666 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1667 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1670 | PyObject * |
| 1671 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1672 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1673 | if (size < 0) { |
| 1674 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1675 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1676 | return NULL; |
| 1677 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1678 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1679 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1680 | some optimizations which share commonly used objects. |
| 1681 | Also, this means the input must be UTF-8, so fall back to the |
| 1682 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1683 | if (u != NULL) { |
| 1684 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1685 | /* Optimization for empty strings */ |
| 1686 | if (size == 0 && unicode_empty != NULL) { |
| 1687 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1688 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1689 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1690 | |
| 1691 | /* Single characters are shared when using this constructor. |
| 1692 | Restrict to ASCII, since the input must be UTF-8. */ |
Victor Stinner | 9faa384 | 2011-10-23 20:06:00 +0200 | [diff] [blame] | 1693 | if (size == 1 && (unsigned char)*u < 128) |
| 1694 | return get_latin1_char((unsigned char)*u); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1695 | |
| 1696 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1699 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1702 | PyObject * |
| 1703 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1704 | { |
| 1705 | size_t size = strlen(u); |
| 1706 | if (size > PY_SSIZE_T_MAX) { |
| 1707 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1708 | return NULL; |
| 1709 | } |
| 1710 | |
| 1711 | return PyUnicode_FromStringAndSize(u, size); |
| 1712 | } |
| 1713 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1714 | PyObject * |
| 1715 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1716 | { |
| 1717 | if (!id->object) { |
| 1718 | id->object = PyUnicode_FromString(id->string); |
| 1719 | if (!id->object) |
| 1720 | return NULL; |
| 1721 | PyUnicode_InternInPlace(&id->object); |
| 1722 | assert(!id->next); |
| 1723 | id->next = static_strings; |
| 1724 | static_strings = id; |
| 1725 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1726 | return id->object; |
| 1727 | } |
| 1728 | |
| 1729 | void |
| 1730 | _PyUnicode_ClearStaticStrings() |
| 1731 | { |
| 1732 | _Py_Identifier *i; |
| 1733 | for (i = static_strings; i; i = i->next) { |
| 1734 | Py_DECREF(i->object); |
| 1735 | i->object = NULL; |
| 1736 | i->next = NULL; |
| 1737 | } |
| 1738 | } |
| 1739 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1740 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1741 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1742 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1743 | PyObject *res; |
| 1744 | #ifdef Py_DEBUG |
| 1745 | const unsigned char *p; |
| 1746 | const unsigned char *end = s + size; |
| 1747 | for (p=s; p < end; p++) { |
| 1748 | assert(*p < 128); |
| 1749 | } |
| 1750 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1751 | if (size == 1) |
| 1752 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1753 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1754 | if (!res) |
| 1755 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1756 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1757 | return res; |
| 1758 | } |
| 1759 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1760 | static Py_UCS4 |
| 1761 | kind_maxchar_limit(unsigned int kind) |
| 1762 | { |
| 1763 | switch(kind) { |
| 1764 | case PyUnicode_1BYTE_KIND: |
| 1765 | return 0x80; |
| 1766 | case PyUnicode_2BYTE_KIND: |
| 1767 | return 0x100; |
| 1768 | case PyUnicode_4BYTE_KIND: |
| 1769 | return 0x10000; |
| 1770 | default: |
| 1771 | assert(0 && "invalid kind"); |
| 1772 | return 0x10ffff; |
| 1773 | } |
| 1774 | } |
| 1775 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1776 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1777 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1778 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1779 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1780 | unsigned char max_char = 127; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1781 | |
| 1782 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1783 | if (size == 1) |
| 1784 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1785 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1786 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 | if (!res) |
| 1788 | return NULL; |
| 1789 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1790 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1791 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1794 | static PyObject* |
| 1795 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 | { |
| 1797 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1798 | Py_UCS2 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1799 | |
| 1800 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1801 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1802 | return get_latin1_char((unsigned char)u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1803 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1804 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1805 | if (!res) |
| 1806 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1807 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1808 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1809 | else { |
| 1810 | _PyUnicode_CONVERT_BYTES( |
| 1811 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1812 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1813 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1814 | return res; |
| 1815 | } |
| 1816 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1817 | static PyObject* |
| 1818 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1819 | { |
| 1820 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1821 | Py_UCS4 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1822 | |
| 1823 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1824 | if (size == 1 && u[0] < 256) |
| 1825 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1826 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1827 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1828 | if (!res) |
| 1829 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1830 | if (max_char < 256) |
| 1831 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1832 | PyUnicode_1BYTE_DATA(res)); |
| 1833 | else if (max_char < 0x10000) |
| 1834 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1835 | PyUnicode_2BYTE_DATA(res)); |
| 1836 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1837 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1838 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1839 | return res; |
| 1840 | } |
| 1841 | |
| 1842 | PyObject* |
| 1843 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1844 | { |
| 1845 | switch(kind) { |
| 1846 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1847 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1848 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1849 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1850 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1851 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1852 | default: |
| 1853 | assert(0 && "invalid kind"); |
| 1854 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1855 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1856 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1859 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1860 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1861 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1862 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1863 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1864 | { |
| 1865 | PyObject *unicode, *copy; |
| 1866 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1867 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1868 | unsigned int kind; |
| 1869 | |
| 1870 | assert(p_unicode != NULL); |
| 1871 | unicode = *p_unicode; |
| 1872 | assert(PyUnicode_IS_READY(unicode)); |
| 1873 | if (PyUnicode_IS_ASCII(unicode)) |
| 1874 | return; |
| 1875 | |
| 1876 | len = PyUnicode_GET_LENGTH(unicode); |
| 1877 | kind = PyUnicode_KIND(unicode); |
| 1878 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1879 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1880 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1881 | if (max_char >= 128) |
| 1882 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1883 | } |
| 1884 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1885 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1886 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1887 | if (max_char >= 256) |
| 1888 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1889 | } |
| 1890 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1891 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1892 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1893 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1894 | if (max_char >= 0x10000) |
| 1895 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1896 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1897 | copy = PyUnicode_New(len, max_char); |
| 1898 | copy_characters(copy, 0, unicode, 0, len); |
| 1899 | Py_DECREF(unicode); |
| 1900 | *p_unicode = copy; |
| 1901 | } |
| 1902 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1903 | PyObject* |
| 1904 | PyUnicode_Copy(PyObject *unicode) |
| 1905 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1906 | Py_ssize_t size; |
| 1907 | PyObject *copy; |
| 1908 | void *data; |
| 1909 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1910 | if (!PyUnicode_Check(unicode)) { |
| 1911 | PyErr_BadInternalCall(); |
| 1912 | return NULL; |
| 1913 | } |
| 1914 | if (PyUnicode_READY(unicode)) |
| 1915 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1916 | |
| 1917 | size = PyUnicode_GET_LENGTH(unicode); |
| 1918 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1919 | if (!copy) |
| 1920 | return NULL; |
| 1921 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1922 | |
| 1923 | data = PyUnicode_DATA(unicode); |
| 1924 | switch (PyUnicode_KIND(unicode)) |
| 1925 | { |
| 1926 | case PyUnicode_1BYTE_KIND: |
| 1927 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1928 | break; |
| 1929 | case PyUnicode_2BYTE_KIND: |
| 1930 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1931 | break; |
| 1932 | case PyUnicode_4BYTE_KIND: |
| 1933 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1934 | break; |
| 1935 | default: |
| 1936 | assert(0); |
| 1937 | break; |
| 1938 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1939 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1940 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1943 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1944 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1945 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | |
| 1947 | void* |
| 1948 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1949 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1950 | Py_ssize_t len; |
| 1951 | void *result; |
| 1952 | unsigned int skind; |
| 1953 | |
| 1954 | if (PyUnicode_READY(s)) |
| 1955 | return NULL; |
| 1956 | |
| 1957 | len = PyUnicode_GET_LENGTH(s); |
| 1958 | skind = PyUnicode_KIND(s); |
| 1959 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1960 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1961 | return NULL; |
| 1962 | } |
| 1963 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1964 | case PyUnicode_2BYTE_KIND: |
| 1965 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1966 | if (!result) |
| 1967 | return PyErr_NoMemory(); |
| 1968 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1969 | _PyUnicode_CONVERT_BYTES( |
| 1970 | Py_UCS1, Py_UCS2, |
| 1971 | PyUnicode_1BYTE_DATA(s), |
| 1972 | PyUnicode_1BYTE_DATA(s) + len, |
| 1973 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1974 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1975 | case PyUnicode_4BYTE_KIND: |
| 1976 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1977 | if (!result) |
| 1978 | return PyErr_NoMemory(); |
| 1979 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1980 | _PyUnicode_CONVERT_BYTES( |
| 1981 | Py_UCS2, Py_UCS4, |
| 1982 | PyUnicode_2BYTE_DATA(s), |
| 1983 | PyUnicode_2BYTE_DATA(s) + len, |
| 1984 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1985 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1986 | else { |
| 1987 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1988 | _PyUnicode_CONVERT_BYTES( |
| 1989 | Py_UCS1, Py_UCS4, |
| 1990 | PyUnicode_1BYTE_DATA(s), |
| 1991 | PyUnicode_1BYTE_DATA(s) + len, |
| 1992 | result); |
| 1993 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1994 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1995 | default: |
| 1996 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1998 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | return NULL; |
| 2000 | } |
| 2001 | |
| 2002 | static Py_UCS4* |
| 2003 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2004 | int copy_null) |
| 2005 | { |
| 2006 | int kind; |
| 2007 | void *data; |
| 2008 | Py_ssize_t len, targetlen; |
| 2009 | if (PyUnicode_READY(string) == -1) |
| 2010 | return NULL; |
| 2011 | kind = PyUnicode_KIND(string); |
| 2012 | data = PyUnicode_DATA(string); |
| 2013 | len = PyUnicode_GET_LENGTH(string); |
| 2014 | targetlen = len; |
| 2015 | if (copy_null) |
| 2016 | targetlen++; |
| 2017 | if (!target) { |
| 2018 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2019 | PyErr_NoMemory(); |
| 2020 | return NULL; |
| 2021 | } |
| 2022 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2023 | if (!target) { |
| 2024 | PyErr_NoMemory(); |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | } |
| 2028 | else { |
| 2029 | if (targetsize < targetlen) { |
| 2030 | PyErr_Format(PyExc_SystemError, |
| 2031 | "string is longer than the buffer"); |
| 2032 | if (copy_null && 0 < targetsize) |
| 2033 | target[0] = 0; |
| 2034 | return NULL; |
| 2035 | } |
| 2036 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2037 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2038 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2039 | _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] | 2040 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2041 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2042 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2043 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2044 | } |
| 2045 | else { |
| 2046 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2047 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2048 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2049 | if (copy_null) |
| 2050 | target[len] = 0; |
| 2051 | return target; |
| 2052 | } |
| 2053 | |
| 2054 | Py_UCS4* |
| 2055 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2056 | int copy_null) |
| 2057 | { |
| 2058 | if (target == NULL || targetsize < 1) { |
| 2059 | PyErr_BadInternalCall(); |
| 2060 | return NULL; |
| 2061 | } |
| 2062 | return as_ucs4(string, target, targetsize, copy_null); |
| 2063 | } |
| 2064 | |
| 2065 | Py_UCS4* |
| 2066 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2067 | { |
| 2068 | return as_ucs4(string, NULL, 0, 1); |
| 2069 | } |
| 2070 | |
| 2071 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2072 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2073 | PyObject * |
| 2074 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2075 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2076 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2077 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2078 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2079 | PyErr_BadInternalCall(); |
| 2080 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2083 | if (size == -1) { |
| 2084 | size = wcslen(w); |
| 2085 | } |
| 2086 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2087 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2090 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2091 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2092 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2093 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2094 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2095 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2096 | *fmt++ = '%'; |
| 2097 | if (width) { |
| 2098 | if (zeropad) |
| 2099 | *fmt++ = '0'; |
| 2100 | fmt += sprintf(fmt, "%d", width); |
| 2101 | } |
| 2102 | if (precision) |
| 2103 | fmt += sprintf(fmt, ".%d", precision); |
| 2104 | if (longflag) |
| 2105 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2106 | else if (longlongflag) { |
| 2107 | /* longlongflag should only ever be nonzero on machines with |
| 2108 | HAVE_LONG_LONG defined */ |
| 2109 | #ifdef HAVE_LONG_LONG |
| 2110 | char *f = PY_FORMAT_LONG_LONG; |
| 2111 | while (*f) |
| 2112 | *fmt++ = *f++; |
| 2113 | #else |
| 2114 | /* we shouldn't ever get here */ |
| 2115 | assert(0); |
| 2116 | *fmt++ = 'l'; |
| 2117 | #endif |
| 2118 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2119 | else if (size_tflag) { |
| 2120 | char *f = PY_FORMAT_SIZE_T; |
| 2121 | while (*f) |
| 2122 | *fmt++ = *f++; |
| 2123 | } |
| 2124 | *fmt++ = c; |
| 2125 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2128 | /* helper for PyUnicode_FromFormatV() */ |
| 2129 | |
| 2130 | static const char* |
| 2131 | parse_format_flags(const char *f, |
| 2132 | int *p_width, int *p_precision, |
| 2133 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2134 | { |
| 2135 | int width, precision, longflag, longlongflag, size_tflag; |
| 2136 | |
| 2137 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2138 | f++; |
| 2139 | width = 0; |
| 2140 | while (Py_ISDIGIT((unsigned)*f)) |
| 2141 | width = (width*10) + *f++ - '0'; |
| 2142 | precision = 0; |
| 2143 | if (*f == '.') { |
| 2144 | f++; |
| 2145 | while (Py_ISDIGIT((unsigned)*f)) |
| 2146 | precision = (precision*10) + *f++ - '0'; |
| 2147 | if (*f == '%') { |
| 2148 | /* "%.3%s" => f points to "3" */ |
| 2149 | f--; |
| 2150 | } |
| 2151 | } |
| 2152 | if (*f == '\0') { |
| 2153 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2154 | f--; |
| 2155 | } |
| 2156 | if (p_width != NULL) |
| 2157 | *p_width = width; |
| 2158 | if (p_precision != NULL) |
| 2159 | *p_precision = precision; |
| 2160 | |
| 2161 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2162 | longflag = 0; |
| 2163 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2164 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2165 | |
| 2166 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2167 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2168 | longflag = 1; |
| 2169 | ++f; |
| 2170 | } |
| 2171 | #ifdef HAVE_LONG_LONG |
| 2172 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2173 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2174 | longlongflag = 1; |
| 2175 | f += 2; |
| 2176 | } |
| 2177 | #endif |
| 2178 | } |
| 2179 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2180 | 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] | 2181 | size_tflag = 1; |
| 2182 | ++f; |
| 2183 | } |
| 2184 | if (p_longflag != NULL) |
| 2185 | *p_longflag = longflag; |
| 2186 | if (p_longlongflag != NULL) |
| 2187 | *p_longlongflag = longlongflag; |
| 2188 | if (p_size_tflag != NULL) |
| 2189 | *p_size_tflag = size_tflag; |
| 2190 | return f; |
| 2191 | } |
| 2192 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2193 | /* maximum number of characters required for output of %ld. 21 characters |
| 2194 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2195 | #define MAX_LONG_CHARS 21 |
| 2196 | /* maximum number of characters required for output of %lld. |
| 2197 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2198 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2199 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2200 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2201 | PyObject * |
| 2202 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2203 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2204 | va_list count; |
| 2205 | Py_ssize_t callcount = 0; |
| 2206 | PyObject **callresults = NULL; |
| 2207 | PyObject **callresult = NULL; |
| 2208 | Py_ssize_t n = 0; |
| 2209 | int width = 0; |
| 2210 | int precision = 0; |
| 2211 | int zeropad; |
| 2212 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2213 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2214 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2215 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2216 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2217 | Py_UCS4 argmaxchar; |
| 2218 | Py_ssize_t numbersize = 0; |
| 2219 | char *numberresults = NULL; |
| 2220 | char *numberresult = NULL; |
| 2221 | Py_ssize_t i; |
| 2222 | int kind; |
| 2223 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2224 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2225 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2226 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2227 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2228 | * 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] | 2229 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2230 | * also estimate a upper bound for all the number formats in the string, |
| 2231 | * 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] | 2232 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2233 | for (f = format; *f; f++) { |
| 2234 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2235 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2236 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2237 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2238 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2239 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2240 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2241 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2242 | #ifdef HAVE_LONG_LONG |
| 2243 | if (longlongflag) { |
| 2244 | if (width < MAX_LONG_LONG_CHARS) |
| 2245 | width = MAX_LONG_LONG_CHARS; |
| 2246 | } |
| 2247 | else |
| 2248 | #endif |
| 2249 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2250 | including sign. Decimal takes the most space. This |
| 2251 | isn't enough for octal. If a width is specified we |
| 2252 | need more (which we allocate later). */ |
| 2253 | if (width < MAX_LONG_CHARS) |
| 2254 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2255 | |
| 2256 | /* account for the size + '\0' to separate numbers |
| 2257 | inside of the numberresults buffer */ |
| 2258 | numbersize += (width + 1); |
| 2259 | } |
| 2260 | } |
| 2261 | else if ((unsigned char)*f > 127) { |
| 2262 | PyErr_Format(PyExc_ValueError, |
| 2263 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2264 | "string, got a non-ASCII byte: 0x%02x", |
| 2265 | (unsigned char)*f); |
| 2266 | return NULL; |
| 2267 | } |
| 2268 | } |
| 2269 | /* step 2: allocate memory for the results of |
| 2270 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2271 | if (callcount) { |
| 2272 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2273 | if (!callresults) { |
| 2274 | PyErr_NoMemory(); |
| 2275 | return NULL; |
| 2276 | } |
| 2277 | callresult = callresults; |
| 2278 | } |
| 2279 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2280 | if (numbersize) { |
| 2281 | numberresults = PyObject_Malloc(numbersize); |
| 2282 | if (!numberresults) { |
| 2283 | PyErr_NoMemory(); |
| 2284 | goto fail; |
| 2285 | } |
| 2286 | numberresult = numberresults; |
| 2287 | } |
| 2288 | |
| 2289 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2290 | for (f = format; *f; f++) { |
| 2291 | if (*f == '%') { |
| 2292 | const char* p; |
| 2293 | int longflag; |
| 2294 | int longlongflag; |
| 2295 | int size_tflag; |
| 2296 | int numprinted; |
| 2297 | |
| 2298 | p = f; |
| 2299 | zeropad = (f[1] == '0'); |
| 2300 | f = parse_format_flags(f, &width, &precision, |
| 2301 | &longflag, &longlongflag, &size_tflag); |
| 2302 | switch (*f) { |
| 2303 | case 'c': |
| 2304 | { |
| 2305 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2306 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2307 | n++; |
| 2308 | break; |
| 2309 | } |
| 2310 | case '%': |
| 2311 | n++; |
| 2312 | break; |
| 2313 | case 'i': |
| 2314 | case 'd': |
| 2315 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2316 | width, precision, *f); |
| 2317 | if (longflag) |
| 2318 | numprinted = sprintf(numberresult, fmt, |
| 2319 | va_arg(count, long)); |
| 2320 | #ifdef HAVE_LONG_LONG |
| 2321 | else if (longlongflag) |
| 2322 | numprinted = sprintf(numberresult, fmt, |
| 2323 | va_arg(count, PY_LONG_LONG)); |
| 2324 | #endif |
| 2325 | else if (size_tflag) |
| 2326 | numprinted = sprintf(numberresult, fmt, |
| 2327 | va_arg(count, Py_ssize_t)); |
| 2328 | else |
| 2329 | numprinted = sprintf(numberresult, fmt, |
| 2330 | va_arg(count, int)); |
| 2331 | n += numprinted; |
| 2332 | /* advance by +1 to skip over the '\0' */ |
| 2333 | numberresult += (numprinted + 1); |
| 2334 | assert(*(numberresult - 1) == '\0'); |
| 2335 | assert(*(numberresult - 2) != '\0'); |
| 2336 | assert(numprinted >= 0); |
| 2337 | assert(numberresult <= numberresults + numbersize); |
| 2338 | break; |
| 2339 | case 'u': |
| 2340 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2341 | width, precision, 'u'); |
| 2342 | if (longflag) |
| 2343 | numprinted = sprintf(numberresult, fmt, |
| 2344 | va_arg(count, unsigned long)); |
| 2345 | #ifdef HAVE_LONG_LONG |
| 2346 | else if (longlongflag) |
| 2347 | numprinted = sprintf(numberresult, fmt, |
| 2348 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2349 | #endif |
| 2350 | else if (size_tflag) |
| 2351 | numprinted = sprintf(numberresult, fmt, |
| 2352 | va_arg(count, size_t)); |
| 2353 | else |
| 2354 | numprinted = sprintf(numberresult, fmt, |
| 2355 | va_arg(count, unsigned int)); |
| 2356 | n += numprinted; |
| 2357 | numberresult += (numprinted + 1); |
| 2358 | assert(*(numberresult - 1) == '\0'); |
| 2359 | assert(*(numberresult - 2) != '\0'); |
| 2360 | assert(numprinted >= 0); |
| 2361 | assert(numberresult <= numberresults + numbersize); |
| 2362 | break; |
| 2363 | case 'x': |
| 2364 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2365 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2366 | n += numprinted; |
| 2367 | numberresult += (numprinted + 1); |
| 2368 | assert(*(numberresult - 1) == '\0'); |
| 2369 | assert(*(numberresult - 2) != '\0'); |
| 2370 | assert(numprinted >= 0); |
| 2371 | assert(numberresult <= numberresults + numbersize); |
| 2372 | break; |
| 2373 | case 'p': |
| 2374 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2375 | /* %p is ill-defined: ensure leading 0x. */ |
| 2376 | if (numberresult[1] == 'X') |
| 2377 | numberresult[1] = 'x'; |
| 2378 | else if (numberresult[1] != 'x') { |
| 2379 | memmove(numberresult + 2, numberresult, |
| 2380 | strlen(numberresult) + 1); |
| 2381 | numberresult[0] = '0'; |
| 2382 | numberresult[1] = 'x'; |
| 2383 | numprinted += 2; |
| 2384 | } |
| 2385 | n += numprinted; |
| 2386 | numberresult += (numprinted + 1); |
| 2387 | assert(*(numberresult - 1) == '\0'); |
| 2388 | assert(*(numberresult - 2) != '\0'); |
| 2389 | assert(numprinted >= 0); |
| 2390 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2391 | break; |
| 2392 | case 's': |
| 2393 | { |
| 2394 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2395 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2396 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2397 | if (!str) |
| 2398 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2399 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2400 | unicode objects, there is no need to call ready on them */ |
| 2401 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2402 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2403 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2404 | /* Remember the str and switch to the next slot */ |
| 2405 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | } |
| 2408 | case 'U': |
| 2409 | { |
| 2410 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2411 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2412 | if (PyUnicode_READY(obj) == -1) |
| 2413 | goto fail; |
| 2414 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2415 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2416 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2417 | break; |
| 2418 | } |
| 2419 | case 'V': |
| 2420 | { |
| 2421 | PyObject *obj = va_arg(count, PyObject *); |
| 2422 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2423 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2424 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2425 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2426 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2427 | if (PyUnicode_READY(obj) == -1) |
| 2428 | goto fail; |
| 2429 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2430 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2431 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2432 | *callresult++ = NULL; |
| 2433 | } |
| 2434 | else { |
| 2435 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2436 | if (!str_obj) |
| 2437 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2438 | if (PyUnicode_READY(str_obj)) { |
| 2439 | Py_DECREF(str_obj); |
| 2440 | goto fail; |
| 2441 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2442 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2443 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2444 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2445 | *callresult++ = str_obj; |
| 2446 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2447 | break; |
| 2448 | } |
| 2449 | case 'S': |
| 2450 | { |
| 2451 | PyObject *obj = va_arg(count, PyObject *); |
| 2452 | PyObject *str; |
| 2453 | assert(obj); |
| 2454 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2456 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2457 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2458 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2459 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2460 | /* Remember the str and switch to the next slot */ |
| 2461 | *callresult++ = str; |
| 2462 | break; |
| 2463 | } |
| 2464 | case 'R': |
| 2465 | { |
| 2466 | PyObject *obj = va_arg(count, PyObject *); |
| 2467 | PyObject *repr; |
| 2468 | assert(obj); |
| 2469 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2470 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2471 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2472 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2473 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2474 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2475 | /* Remember the repr and switch to the next slot */ |
| 2476 | *callresult++ = repr; |
| 2477 | break; |
| 2478 | } |
| 2479 | case 'A': |
| 2480 | { |
| 2481 | PyObject *obj = va_arg(count, PyObject *); |
| 2482 | PyObject *ascii; |
| 2483 | assert(obj); |
| 2484 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2485 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2486 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2487 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2488 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2489 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2490 | /* Remember the repr and switch to the next slot */ |
| 2491 | *callresult++ = ascii; |
| 2492 | break; |
| 2493 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2494 | default: |
| 2495 | /* if we stumble upon an unknown |
| 2496 | formatting code, copy the rest of |
| 2497 | the format string to the output |
| 2498 | string. (we cannot just skip the |
| 2499 | code, since there's no way to know |
| 2500 | what's in the argument list) */ |
| 2501 | n += strlen(p); |
| 2502 | goto expand; |
| 2503 | } |
| 2504 | } else |
| 2505 | n++; |
| 2506 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2507 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2509 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2510 | we don't have to resize the string. |
| 2511 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2512 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2513 | if (!string) |
| 2514 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2515 | kind = PyUnicode_KIND(string); |
| 2516 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2517 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2518 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2519 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2520 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2521 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2522 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2523 | |
| 2524 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2525 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2526 | /* checking for == because the last argument could be a empty |
| 2527 | string, which causes i to point to end, the assert at the end of |
| 2528 | the loop */ |
| 2529 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2530 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2531 | switch (*f) { |
| 2532 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2533 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2534 | const int ordinal = va_arg(vargs, int); |
| 2535 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2536 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2537 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2538 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2540 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2541 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2542 | case 'p': |
| 2543 | /* unused, since we already have the result */ |
| 2544 | if (*f == 'p') |
| 2545 | (void) va_arg(vargs, void *); |
| 2546 | else |
| 2547 | (void) va_arg(vargs, int); |
| 2548 | /* extract the result from numberresults and append. */ |
| 2549 | for (; *numberresult; ++i, ++numberresult) |
| 2550 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2551 | /* skip over the separating '\0' */ |
| 2552 | assert(*numberresult == '\0'); |
| 2553 | numberresult++; |
| 2554 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2555 | break; |
| 2556 | case 's': |
| 2557 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2558 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2559 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2560 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2561 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2562 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2563 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2564 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2565 | /* We're done with the unicode()/repr() => forget it */ |
| 2566 | Py_DECREF(*callresult); |
| 2567 | /* switch to next unicode()/repr() result */ |
| 2568 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2569 | break; |
| 2570 | } |
| 2571 | case 'U': |
| 2572 | { |
| 2573 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2574 | Py_ssize_t size; |
| 2575 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2576 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2577 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2578 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2579 | break; |
| 2580 | } |
| 2581 | case 'V': |
| 2582 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2583 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2584 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2585 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2586 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2587 | size = PyUnicode_GET_LENGTH(obj); |
| 2588 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2589 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2592 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2593 | assert(PyUnicode_KIND(*callresult) <= |
| 2594 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2595 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2596 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2597 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2598 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2599 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2600 | break; |
| 2601 | } |
| 2602 | case 'S': |
| 2603 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2604 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2605 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2606 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2607 | /* unused, since we already have the result */ |
| 2608 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2609 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2610 | copy_characters(string, i, *callresult, 0, size); |
| 2611 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2612 | /* We're done with the unicode()/repr() => forget it */ |
| 2613 | Py_DECREF(*callresult); |
| 2614 | /* switch to next unicode()/repr() result */ |
| 2615 | ++callresult; |
| 2616 | break; |
| 2617 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2618 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2619 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2622 | for (; *p; ++p, ++i) |
| 2623 | PyUnicode_WRITE(kind, data, i, *p); |
| 2624 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2625 | goto end; |
| 2626 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2627 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2628 | else { |
| 2629 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2630 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2631 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2633 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2634 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2635 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2636 | if (callresults) |
| 2637 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | if (numberresults) |
| 2639 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2640 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 2641 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2642 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2643 | if (callresults) { |
| 2644 | PyObject **callresult2 = callresults; |
| 2645 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2646 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2647 | ++callresult2; |
| 2648 | } |
| 2649 | PyObject_Free(callresults); |
| 2650 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2651 | if (numberresults) |
| 2652 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2653 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2656 | PyObject * |
| 2657 | PyUnicode_FromFormat(const char *format, ...) |
| 2658 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2659 | PyObject* ret; |
| 2660 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2661 | |
| 2662 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2663 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2664 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2665 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2666 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2667 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2668 | va_end(vargs); |
| 2669 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2672 | #ifdef HAVE_WCHAR_H |
| 2673 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2674 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2675 | convert a Unicode object to a wide character string. |
| 2676 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2677 | - 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] | 2678 | character) required to convert the unicode object. Ignore size argument. |
| 2679 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2680 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2681 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2682 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2683 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2684 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2685 | wchar_t *w, |
| 2686 | Py_ssize_t size) |
| 2687 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2688 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2689 | const wchar_t *wstr; |
| 2690 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2691 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2692 | if (wstr == NULL) |
| 2693 | return -1; |
| 2694 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2695 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2696 | if (size > res) |
| 2697 | size = res + 1; |
| 2698 | else |
| 2699 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2700 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2701 | return res; |
| 2702 | } |
| 2703 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2704 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2708 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2709 | wchar_t *w, |
| 2710 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2711 | { |
| 2712 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2713 | PyErr_BadInternalCall(); |
| 2714 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2715 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2716 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2719 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2720 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2721 | Py_ssize_t *size) |
| 2722 | { |
| 2723 | wchar_t* buffer; |
| 2724 | Py_ssize_t buflen; |
| 2725 | |
| 2726 | if (unicode == NULL) { |
| 2727 | PyErr_BadInternalCall(); |
| 2728 | return NULL; |
| 2729 | } |
| 2730 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2731 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2732 | if (buflen == -1) |
| 2733 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2734 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2735 | PyErr_NoMemory(); |
| 2736 | return NULL; |
| 2737 | } |
| 2738 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2739 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2740 | if (buffer == NULL) { |
| 2741 | PyErr_NoMemory(); |
| 2742 | return NULL; |
| 2743 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2744 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2745 | if (buflen == -1) |
| 2746 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2747 | if (size != NULL) |
| 2748 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2749 | return buffer; |
| 2750 | } |
| 2751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2752 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2754 | PyObject * |
| 2755 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2756 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2757 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2758 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2759 | PyErr_SetString(PyExc_ValueError, |
| 2760 | "chr() arg not in range(0x110000)"); |
| 2761 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2762 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2764 | if (ordinal < 256) |
| 2765 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2767 | v = PyUnicode_New(1, ordinal); |
| 2768 | if (v == NULL) |
| 2769 | return NULL; |
| 2770 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2771 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2772 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2775 | PyObject * |
| 2776 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2777 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2778 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2779 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2780 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2781 | if (PyUnicode_READY(obj)) |
| 2782 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2783 | Py_INCREF(obj); |
| 2784 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2785 | } |
| 2786 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2787 | /* For a Unicode subtype that's not a Unicode object, |
| 2788 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2789 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2790 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2791 | PyErr_Format(PyExc_TypeError, |
| 2792 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2793 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2794 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2797 | PyObject * |
| 2798 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2799 | const char *encoding, |
| 2800 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2801 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2802 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2803 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2805 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2806 | PyErr_BadInternalCall(); |
| 2807 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2809 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2810 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2811 | if (PyBytes_Check(obj)) { |
| 2812 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2813 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2814 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2815 | } |
| 2816 | else { |
| 2817 | v = PyUnicode_Decode( |
| 2818 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2819 | encoding, errors); |
| 2820 | } |
| 2821 | return v; |
| 2822 | } |
| 2823 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2824 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2825 | PyErr_SetString(PyExc_TypeError, |
| 2826 | "decoding str is not supported"); |
| 2827 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2828 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2829 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2830 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2831 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2832 | PyErr_Format(PyExc_TypeError, |
| 2833 | "coercing to str: need bytes, bytearray " |
| 2834 | "or buffer-like object, %.80s found", |
| 2835 | Py_TYPE(obj)->tp_name); |
| 2836 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2837 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2839 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2840 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2841 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2842 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2843 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2844 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2846 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2847 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2850 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2851 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2852 | 1 on success. */ |
| 2853 | static int |
| 2854 | normalize_encoding(const char *encoding, |
| 2855 | char *lower, |
| 2856 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2857 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2858 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2859 | char *l; |
| 2860 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2861 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2862 | if (encoding == NULL) { |
| 2863 | strcpy(lower, "utf-8"); |
| 2864 | return 1; |
| 2865 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2866 | e = encoding; |
| 2867 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2868 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2869 | while (*e) { |
| 2870 | if (l == l_end) |
| 2871 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2872 | if (Py_ISUPPER(*e)) { |
| 2873 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2874 | } |
| 2875 | else if (*e == '_') { |
| 2876 | *l++ = '-'; |
| 2877 | e++; |
| 2878 | } |
| 2879 | else { |
| 2880 | *l++ = *e++; |
| 2881 | } |
| 2882 | } |
| 2883 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2884 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2887 | PyObject * |
| 2888 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2889 | Py_ssize_t size, |
| 2890 | const char *encoding, |
| 2891 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2892 | { |
| 2893 | PyObject *buffer = NULL, *unicode; |
| 2894 | Py_buffer info; |
| 2895 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2896 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2897 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2898 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2899 | if ((strcmp(lower, "utf-8") == 0) || |
| 2900 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2901 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2902 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2903 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2904 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2905 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2906 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2907 | else if (strcmp(lower, "mbcs") == 0) |
| 2908 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2909 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2910 | else if (strcmp(lower, "ascii") == 0) |
| 2911 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2912 | else if (strcmp(lower, "utf-16") == 0) |
| 2913 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2914 | else if (strcmp(lower, "utf-32") == 0) |
| 2915 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2916 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2917 | |
| 2918 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2919 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2920 | 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] | 2921 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2922 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2923 | if (buffer == NULL) |
| 2924 | goto onError; |
| 2925 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2926 | if (unicode == NULL) |
| 2927 | goto onError; |
| 2928 | if (!PyUnicode_Check(unicode)) { |
| 2929 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2930 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2931 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2932 | Py_DECREF(unicode); |
| 2933 | goto onError; |
| 2934 | } |
| 2935 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2936 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2937 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2938 | Py_DECREF(unicode); |
| 2939 | return NULL; |
| 2940 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2941 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2942 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2943 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2944 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2945 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | Py_XDECREF(buffer); |
| 2947 | return NULL; |
| 2948 | } |
| 2949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2950 | PyObject * |
| 2951 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2952 | const char *encoding, |
| 2953 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2954 | { |
| 2955 | PyObject *v; |
| 2956 | |
| 2957 | if (!PyUnicode_Check(unicode)) { |
| 2958 | PyErr_BadArgument(); |
| 2959 | goto onError; |
| 2960 | } |
| 2961 | |
| 2962 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2963 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2964 | |
| 2965 | /* Decode via the codec registry */ |
| 2966 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2967 | if (v == NULL) |
| 2968 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2969 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2970 | return v; |
| 2971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2972 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2973 | return NULL; |
| 2974 | } |
| 2975 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2976 | PyObject * |
| 2977 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2978 | const char *encoding, |
| 2979 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2980 | { |
| 2981 | PyObject *v; |
| 2982 | |
| 2983 | if (!PyUnicode_Check(unicode)) { |
| 2984 | PyErr_BadArgument(); |
| 2985 | goto onError; |
| 2986 | } |
| 2987 | |
| 2988 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2989 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2990 | |
| 2991 | /* Decode via the codec registry */ |
| 2992 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2993 | if (v == NULL) |
| 2994 | goto onError; |
| 2995 | if (!PyUnicode_Check(v)) { |
| 2996 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2997 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2998 | Py_TYPE(v)->tp_name); |
| 2999 | Py_DECREF(v); |
| 3000 | goto onError; |
| 3001 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3002 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3003 | return v; |
| 3004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3005 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3006 | return NULL; |
| 3007 | } |
| 3008 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3009 | PyObject * |
| 3010 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3011 | Py_ssize_t size, |
| 3012 | const char *encoding, |
| 3013 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | { |
| 3015 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3017 | unicode = PyUnicode_FromUnicode(s, size); |
| 3018 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3019 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3021 | Py_DECREF(unicode); |
| 3022 | return v; |
| 3023 | } |
| 3024 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3025 | PyObject * |
| 3026 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3027 | const char *encoding, |
| 3028 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3029 | { |
| 3030 | PyObject *v; |
| 3031 | |
| 3032 | if (!PyUnicode_Check(unicode)) { |
| 3033 | PyErr_BadArgument(); |
| 3034 | goto onError; |
| 3035 | } |
| 3036 | |
| 3037 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3038 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3039 | |
| 3040 | /* Encode via the codec registry */ |
| 3041 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3042 | if (v == NULL) |
| 3043 | goto onError; |
| 3044 | return v; |
| 3045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3046 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3047 | return NULL; |
| 3048 | } |
| 3049 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3050 | PyObject * |
| 3051 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3052 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3053 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3054 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3055 | PyUnicode_GET_SIZE(unicode), |
| 3056 | NULL); |
| 3057 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3058 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3059 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3060 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3061 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3062 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3063 | the Python codec requires to encode at least its own filename. Use the C |
| 3064 | version of the locale codec until the codec registry is initialized and |
| 3065 | the Python codec is loaded. |
| 3066 | |
| 3067 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3068 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3069 | subinterpreters. */ |
| 3070 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3071 | return PyUnicode_AsEncodedString(unicode, |
| 3072 | Py_FileSystemDefaultEncoding, |
| 3073 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3074 | } |
| 3075 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3076 | /* locale encoding with surrogateescape */ |
| 3077 | wchar_t *wchar; |
| 3078 | char *bytes; |
| 3079 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3080 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3081 | |
| 3082 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3083 | if (wchar == NULL) |
| 3084 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3085 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3086 | if (bytes == NULL) { |
| 3087 | if (error_pos != (size_t)-1) { |
| 3088 | char *errmsg = strerror(errno); |
| 3089 | PyObject *exc = NULL; |
| 3090 | if (errmsg == NULL) |
| 3091 | errmsg = "Py_wchar2char() failed"; |
| 3092 | raise_encode_exception(&exc, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 3093 | "filesystemencoding", unicode, |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3094 | error_pos, error_pos+1, |
| 3095 | errmsg); |
| 3096 | Py_XDECREF(exc); |
| 3097 | } |
| 3098 | else |
| 3099 | PyErr_NoMemory(); |
| 3100 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3101 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3102 | } |
| 3103 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3104 | |
| 3105 | bytes_obj = PyBytes_FromString(bytes); |
| 3106 | PyMem_Free(bytes); |
| 3107 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3108 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3109 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3112 | PyObject * |
| 3113 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3114 | const char *encoding, |
| 3115 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | { |
| 3117 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3118 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3119 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3120 | if (!PyUnicode_Check(unicode)) { |
| 3121 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3122 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3123 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3124 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3125 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3126 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3127 | if ((strcmp(lower, "utf-8") == 0) || |
| 3128 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3129 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3130 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3131 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3132 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3133 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3134 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3135 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3136 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3137 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3138 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3139 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3140 | else if (strcmp(lower, "mbcs") == 0) |
| 3141 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3142 | PyUnicode_GET_SIZE(unicode), |
| 3143 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3144 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3145 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3146 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3148 | |
| 3149 | /* Encode via the codec registry */ |
| 3150 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3151 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3152 | return NULL; |
| 3153 | |
| 3154 | /* The normal path */ |
| 3155 | if (PyBytes_Check(v)) |
| 3156 | return v; |
| 3157 | |
| 3158 | /* 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] | 3159 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3160 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3161 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3162 | |
| 3163 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3164 | "encoder %s returned bytearray instead of bytes", |
| 3165 | encoding); |
| 3166 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3167 | Py_DECREF(v); |
| 3168 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3169 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3170 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3171 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3172 | Py_DECREF(v); |
| 3173 | return b; |
| 3174 | } |
| 3175 | |
| 3176 | PyErr_Format(PyExc_TypeError, |
| 3177 | "encoder did not return a bytes object (type=%.400s)", |
| 3178 | Py_TYPE(v)->tp_name); |
| 3179 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3180 | return NULL; |
| 3181 | } |
| 3182 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3183 | PyObject * |
| 3184 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3185 | const char *encoding, |
| 3186 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3187 | { |
| 3188 | PyObject *v; |
| 3189 | |
| 3190 | if (!PyUnicode_Check(unicode)) { |
| 3191 | PyErr_BadArgument(); |
| 3192 | goto onError; |
| 3193 | } |
| 3194 | |
| 3195 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3196 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3197 | |
| 3198 | /* Encode via the codec registry */ |
| 3199 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3200 | if (v == NULL) |
| 3201 | goto onError; |
| 3202 | if (!PyUnicode_Check(v)) { |
| 3203 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3204 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3205 | Py_TYPE(v)->tp_name); |
| 3206 | Py_DECREF(v); |
| 3207 | goto onError; |
| 3208 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3209 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3210 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3211 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3212 | return NULL; |
| 3213 | } |
| 3214 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3215 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3216 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3217 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3218 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3219 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3220 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3221 | PyObject* |
| 3222 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3223 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3224 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3225 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3226 | #elif defined(__APPLE__) |
| 3227 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3228 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3229 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3230 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3231 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3232 | the Python codec requires to encode at least its own filename. Use the C |
| 3233 | version of the locale codec until the codec registry is initialized and |
| 3234 | the Python codec is loaded. |
| 3235 | |
| 3236 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3237 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3238 | subinterpreters. */ |
| 3239 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3240 | return PyUnicode_Decode(s, size, |
| 3241 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3242 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3243 | } |
| 3244 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3245 | /* locale encoding with surrogateescape */ |
| 3246 | wchar_t *wchar; |
| 3247 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3248 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3249 | |
| 3250 | if (s[size] != '\0' || size != strlen(s)) { |
| 3251 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3255 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3256 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3257 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3258 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3259 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3260 | PyMem_Free(wchar); |
| 3261 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3262 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3263 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3266 | |
| 3267 | int |
| 3268 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3269 | { |
| 3270 | PyObject *output = NULL; |
| 3271 | Py_ssize_t size; |
| 3272 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3273 | if (arg == NULL) { |
| 3274 | Py_DECREF(*(PyObject**)addr); |
| 3275 | return 1; |
| 3276 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3277 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3278 | output = arg; |
| 3279 | Py_INCREF(output); |
| 3280 | } |
| 3281 | else { |
| 3282 | arg = PyUnicode_FromObject(arg); |
| 3283 | if (!arg) |
| 3284 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3285 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3286 | Py_DECREF(arg); |
| 3287 | if (!output) |
| 3288 | return 0; |
| 3289 | if (!PyBytes_Check(output)) { |
| 3290 | Py_DECREF(output); |
| 3291 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3292 | return 0; |
| 3293 | } |
| 3294 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3295 | size = PyBytes_GET_SIZE(output); |
| 3296 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3297 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3298 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3299 | Py_DECREF(output); |
| 3300 | return 0; |
| 3301 | } |
| 3302 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3303 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3307 | int |
| 3308 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3309 | { |
| 3310 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3311 | if (arg == NULL) { |
| 3312 | Py_DECREF(*(PyObject**)addr); |
| 3313 | return 1; |
| 3314 | } |
| 3315 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3316 | if (PyUnicode_READY(arg)) |
| 3317 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3318 | output = arg; |
| 3319 | Py_INCREF(output); |
| 3320 | } |
| 3321 | else { |
| 3322 | arg = PyBytes_FromObject(arg); |
| 3323 | if (!arg) |
| 3324 | return 0; |
| 3325 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3326 | PyBytes_GET_SIZE(arg)); |
| 3327 | Py_DECREF(arg); |
| 3328 | if (!output) |
| 3329 | return 0; |
| 3330 | if (!PyUnicode_Check(output)) { |
| 3331 | Py_DECREF(output); |
| 3332 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3333 | return 0; |
| 3334 | } |
| 3335 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3336 | if (PyUnicode_READY(output) < 0) { |
| 3337 | Py_DECREF(output); |
| 3338 | return 0; |
| 3339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3340 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3341 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3342 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3343 | Py_DECREF(output); |
| 3344 | return 0; |
| 3345 | } |
| 3346 | *(PyObject**)addr = output; |
| 3347 | return Py_CLEANUP_SUPPORTED; |
| 3348 | } |
| 3349 | |
| 3350 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3351 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3352 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3353 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3354 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3355 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3356 | if (!PyUnicode_Check(unicode)) { |
| 3357 | PyErr_BadArgument(); |
| 3358 | return NULL; |
| 3359 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3360 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3361 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3362 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3363 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3364 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3365 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3366 | if (bytes == NULL) |
| 3367 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3368 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3369 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3370 | Py_DECREF(bytes); |
| 3371 | return NULL; |
| 3372 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3373 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3374 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3375 | PyBytes_AS_STRING(bytes), |
| 3376 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3377 | Py_DECREF(bytes); |
| 3378 | } |
| 3379 | |
| 3380 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3381 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3382 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3386 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3387 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3388 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3389 | } |
| 3390 | |
| 3391 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3392 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3393 | #endif |
| 3394 | |
| 3395 | |
| 3396 | Py_UNICODE * |
| 3397 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3398 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3399 | const unsigned char *one_byte; |
| 3400 | #if SIZEOF_WCHAR_T == 4 |
| 3401 | const Py_UCS2 *two_bytes; |
| 3402 | #else |
| 3403 | const Py_UCS4 *four_bytes; |
| 3404 | const Py_UCS4 *ucs4_end; |
| 3405 | Py_ssize_t num_surrogates; |
| 3406 | #endif |
| 3407 | wchar_t *w; |
| 3408 | wchar_t *wchar_end; |
| 3409 | |
| 3410 | if (!PyUnicode_Check(unicode)) { |
| 3411 | PyErr_BadArgument(); |
| 3412 | return NULL; |
| 3413 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3414 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3415 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3416 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3417 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3418 | |
| 3419 | #ifdef Py_DEBUG |
| 3420 | ++unicode_as_unicode_calls; |
| 3421 | #endif |
| 3422 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3423 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3424 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3425 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3426 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3427 | num_surrogates = 0; |
| 3428 | |
| 3429 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3430 | if (*four_bytes > 0xFFFF) |
| 3431 | ++num_surrogates; |
| 3432 | } |
| 3433 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3434 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3435 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3436 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3437 | PyErr_NoMemory(); |
| 3438 | return NULL; |
| 3439 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3440 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3441 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3442 | w = _PyUnicode_WSTR(unicode); |
| 3443 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3444 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3445 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3446 | if (*four_bytes > 0xFFFF) { |
| 3447 | /* encode surrogate pair in this case */ |
| 3448 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3449 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3450 | } |
| 3451 | else |
| 3452 | *w = *four_bytes; |
| 3453 | |
| 3454 | if (w > wchar_end) { |
| 3455 | assert(0 && "Miscalculated string end"); |
| 3456 | } |
| 3457 | } |
| 3458 | *w = 0; |
| 3459 | #else |
| 3460 | /* sizeof(wchar_t) == 4 */ |
| 3461 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3462 | "should share memory already."); |
| 3463 | return NULL; |
| 3464 | #endif |
| 3465 | } |
| 3466 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3467 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3468 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3469 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3470 | PyErr_NoMemory(); |
| 3471 | return NULL; |
| 3472 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3473 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3474 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3475 | w = _PyUnicode_WSTR(unicode); |
| 3476 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3477 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3478 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3479 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3480 | for (; w < wchar_end; ++one_byte, ++w) |
| 3481 | *w = *one_byte; |
| 3482 | /* null-terminate the wstr */ |
| 3483 | *w = 0; |
| 3484 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3485 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3486 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3487 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3488 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3489 | *w = *two_bytes; |
| 3490 | /* null-terminate the wstr */ |
| 3491 | *w = 0; |
| 3492 | #else |
| 3493 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3494 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3495 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3496 | Py_FatalError("Impossible unicode object state, wstr " |
| 3497 | "and str should share memory already."); |
| 3498 | return NULL; |
| 3499 | #endif |
| 3500 | } |
| 3501 | else { |
| 3502 | assert(0 && "This should never happen."); |
| 3503 | } |
| 3504 | } |
| 3505 | } |
| 3506 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3507 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3508 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3511 | Py_UNICODE * |
| 3512 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3513 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3514 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3517 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3518 | Py_ssize_t |
| 3519 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3520 | { |
| 3521 | if (!PyUnicode_Check(unicode)) { |
| 3522 | PyErr_BadArgument(); |
| 3523 | goto onError; |
| 3524 | } |
| 3525 | return PyUnicode_GET_SIZE(unicode); |
| 3526 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3527 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3528 | return -1; |
| 3529 | } |
| 3530 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3531 | Py_ssize_t |
| 3532 | PyUnicode_GetLength(PyObject *unicode) |
| 3533 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3534 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3535 | PyErr_BadArgument(); |
| 3536 | return -1; |
| 3537 | } |
| 3538 | |
| 3539 | return PyUnicode_GET_LENGTH(unicode); |
| 3540 | } |
| 3541 | |
| 3542 | Py_UCS4 |
| 3543 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3544 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3545 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3546 | PyErr_BadArgument(); |
| 3547 | return (Py_UCS4)-1; |
| 3548 | } |
| 3549 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3550 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3551 | return (Py_UCS4)-1; |
| 3552 | } |
| 3553 | return PyUnicode_READ_CHAR(unicode, index); |
| 3554 | } |
| 3555 | |
| 3556 | int |
| 3557 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3558 | { |
| 3559 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3560 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3561 | return -1; |
| 3562 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3563 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3564 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3565 | return -1; |
| 3566 | } |
| 3567 | if (_PyUnicode_Dirty(unicode)) |
| 3568 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3569 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3570 | index, ch); |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3574 | const char * |
| 3575 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3576 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3577 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3580 | /* create or adjust a UnicodeDecodeError */ |
| 3581 | static void |
| 3582 | make_decode_exception(PyObject **exceptionObject, |
| 3583 | const char *encoding, |
| 3584 | const char *input, Py_ssize_t length, |
| 3585 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3586 | const char *reason) |
| 3587 | { |
| 3588 | if (*exceptionObject == NULL) { |
| 3589 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3590 | encoding, input, length, startpos, endpos, reason); |
| 3591 | } |
| 3592 | else { |
| 3593 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3594 | goto onError; |
| 3595 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3596 | goto onError; |
| 3597 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3598 | goto onError; |
| 3599 | } |
| 3600 | return; |
| 3601 | |
| 3602 | onError: |
| 3603 | Py_DECREF(*exceptionObject); |
| 3604 | *exceptionObject = NULL; |
| 3605 | } |
| 3606 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3607 | /* error handling callback helper: |
| 3608 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3609 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3610 | and adjust various state variables. |
| 3611 | return 0 on success, -1 on error |
| 3612 | */ |
| 3613 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3614 | static int |
| 3615 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3616 | const char *encoding, const char *reason, |
| 3617 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3618 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3619 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3621 | 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] | 3622 | |
| 3623 | PyObject *restuple = NULL; |
| 3624 | PyObject *repunicode = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3625 | Py_ssize_t outsize = PyUnicode_GET_LENGTH(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3626 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3627 | Py_ssize_t requiredsize; |
| 3628 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3629 | PyObject *inputobj = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3630 | Py_ssize_t replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3631 | int res = -1; |
| 3632 | |
| 3633 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3634 | *errorHandler = PyCodec_LookupError(errors); |
| 3635 | if (*errorHandler == NULL) |
| 3636 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3639 | make_decode_exception(exceptionObject, |
| 3640 | encoding, |
| 3641 | *input, *inend - *input, |
| 3642 | *startinpos, *endinpos, |
| 3643 | reason); |
| 3644 | if (*exceptionObject == NULL) |
| 3645 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3646 | |
| 3647 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3648 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3649 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3650 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3651 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3652 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3653 | } |
| 3654 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3655 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3656 | if (PyUnicode_READY(repunicode) < 0) |
| 3657 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3658 | |
| 3659 | /* Copy back the bytes variables, which might have been modified by the |
| 3660 | callback */ |
| 3661 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3662 | if (!inputobj) |
| 3663 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3664 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3665 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3666 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3667 | *input = PyBytes_AS_STRING(inputobj); |
| 3668 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3669 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3670 | /* we can DECREF safely, as the exception has another reference, |
| 3671 | so the object won't go away. */ |
| 3672 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3673 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3674 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3675 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3676 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3677 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3678 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3679 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3680 | |
| 3681 | /* need more space? (at least enough for what we |
| 3682 | have+the replacement+the rest of the string (starting |
| 3683 | at the new input position), so we won't have to check space |
| 3684 | when there are no errors in the rest of the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3685 | replen = PyUnicode_GET_LENGTH(repunicode); |
| 3686 | requiredsize = *outpos + replen + insize-newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3687 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3688 | if (requiredsize<2*outsize) |
| 3689 | requiredsize = 2*outsize; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3690 | if (unicode_resize(output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3691 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3692 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3693 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
| 3694 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3695 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3696 | *inptr = *input + newpos; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3697 | PyUnicode_CopyCharacters(*output, *outpos, repunicode, 0, replen); |
| 3698 | *outpos += replen; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3699 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3700 | /* we made it! */ |
| 3701 | res = 0; |
| 3702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3703 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3704 | Py_XDECREF(restuple); |
| 3705 | return res; |
| 3706 | } |
| 3707 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3708 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3709 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3710 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3711 | |
| 3712 | /* Three simple macros defining base-64. */ |
| 3713 | |
| 3714 | /* Is c a base-64 character? */ |
| 3715 | |
| 3716 | #define IS_BASE64(c) \ |
| 3717 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3718 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3719 | ((c) >= '0' && (c) <= '9') || \ |
| 3720 | (c) == '+' || (c) == '/') |
| 3721 | |
| 3722 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3723 | |
| 3724 | #define FROM_BASE64(c) \ |
| 3725 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3726 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3727 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3728 | (c) == '+' ? 62 : 63) |
| 3729 | |
| 3730 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3731 | |
| 3732 | #define TO_BASE64(n) \ |
| 3733 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3734 | |
| 3735 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3736 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3737 | * byte not decoding to itself is the + which begins a base64 |
| 3738 | * string. */ |
| 3739 | |
| 3740 | #define DECODE_DIRECT(c) \ |
| 3741 | ((c) <= 127 && (c) != '+') |
| 3742 | |
| 3743 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3744 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3745 | * the above). See RFC2152. This array identifies these different |
| 3746 | * sets: |
| 3747 | * 0 : "Set D" |
| 3748 | * alphanumeric and '(),-./:? |
| 3749 | * 1 : "Set O" |
| 3750 | * !"#$%&*;<=>@[]^_`{|} |
| 3751 | * 2 : "whitespace" |
| 3752 | * ht nl cr sp |
| 3753 | * 3 : special (must be base64 encoded) |
| 3754 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3755 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3756 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3757 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3758 | char utf7_category[128] = { |
| 3759 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3760 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3761 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3762 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3763 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3764 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3765 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3766 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3767 | /* @ A B C D E F G H I J K L M N O */ |
| 3768 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3769 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3770 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3771 | /* ` a b c d e f g h i j k l m n o */ |
| 3772 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3773 | /* p q r s t u v w x y z { | } ~ del */ |
| 3774 | 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] | 3775 | }; |
| 3776 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3777 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3778 | * answer depends on whether we are encoding set O as itself, and also |
| 3779 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3780 | * clear that the answers to these questions vary between |
| 3781 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3782 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3783 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3784 | ((c) < 128 && (c) > 0 && \ |
| 3785 | ((utf7_category[(c)] == 0) || \ |
| 3786 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3787 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3788 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3789 | PyObject * |
| 3790 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3791 | Py_ssize_t size, |
| 3792 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3793 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3794 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3795 | } |
| 3796 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3797 | /* The decoder. The only state we preserve is our read position, |
| 3798 | * i.e. how many characters we have consumed. So if we end in the |
| 3799 | * middle of a shift sequence we have to back off the read position |
| 3800 | * and the output to the beginning of the sequence, otherwise we lose |
| 3801 | * all the shift state (seen bits, number of bits seen, high |
| 3802 | * surrogate). */ |
| 3803 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3804 | PyObject * |
| 3805 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3806 | Py_ssize_t size, |
| 3807 | const char *errors, |
| 3808 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3809 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3810 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3811 | Py_ssize_t startinpos; |
| 3812 | Py_ssize_t endinpos; |
| 3813 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3814 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3815 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3816 | const char *errmsg = ""; |
| 3817 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3818 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3819 | unsigned int base64bits = 0; |
| 3820 | unsigned long base64buffer = 0; |
| 3821 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3822 | PyObject *errorHandler = NULL; |
| 3823 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3824 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3825 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 3826 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3827 | if (!unicode) |
| 3828 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3829 | if (size == 0) { |
| 3830 | if (consumed) |
| 3831 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3832 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3833 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3834 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3835 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3836 | e = s + size; |
| 3837 | |
| 3838 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3839 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3840 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3841 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3842 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3843 | if (inShift) { /* in a base-64 section */ |
| 3844 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3845 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3846 | base64bits += 6; |
| 3847 | s++; |
| 3848 | if (base64bits >= 16) { |
| 3849 | /* we have enough bits for a UTF-16 value */ |
| 3850 | Py_UNICODE outCh = (Py_UNICODE) |
| 3851 | (base64buffer >> (base64bits-16)); |
| 3852 | base64bits -= 16; |
| 3853 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3854 | if (surrogate) { |
| 3855 | /* expecting a second surrogate */ |
| 3856 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3857 | Py_UCS4 ch2 = (((surrogate & 0x3FF)<<10) |
| 3858 | | (outCh & 0x3FF)) + 0x10000; |
| 3859 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 3860 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3861 | surrogate = 0; |
| 3862 | } |
| 3863 | else { |
| 3864 | surrogate = 0; |
| 3865 | errmsg = "second surrogate missing"; |
| 3866 | goto utf7Error; |
| 3867 | } |
| 3868 | } |
| 3869 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3870 | /* first surrogate */ |
| 3871 | surrogate = outCh; |
| 3872 | } |
| 3873 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3874 | errmsg = "unexpected second surrogate"; |
| 3875 | goto utf7Error; |
| 3876 | } |
| 3877 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3878 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 3879 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3884 | inShift = 0; |
| 3885 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3886 | if (surrogate) { |
| 3887 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3888 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3889 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3890 | if (base64bits > 0) { /* left-over bits */ |
| 3891 | if (base64bits >= 6) { |
| 3892 | /* We've seen at least one base-64 character */ |
| 3893 | errmsg = "partial character in shift sequence"; |
| 3894 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3895 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3896 | else { |
| 3897 | /* Some bits remain; they should be zero */ |
| 3898 | if (base64buffer != 0) { |
| 3899 | errmsg = "non-zero padding bits in shift sequence"; |
| 3900 | goto utf7Error; |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | if (ch != '-') { |
| 3905 | /* '-' is absorbed; other terminating |
| 3906 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3907 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3908 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3909 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3913 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3914 | s++; /* consume '+' */ |
| 3915 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3916 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3917 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 3918 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3919 | } |
| 3920 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3921 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3922 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3923 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3924 | } |
| 3925 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3926 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3927 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3928 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3929 | s++; |
| 3930 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3931 | else { |
| 3932 | startinpos = s-starts; |
| 3933 | s++; |
| 3934 | errmsg = "unexpected special character"; |
| 3935 | goto utf7Error; |
| 3936 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3937 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3938 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3939 | endinpos = s-starts; |
| 3940 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3941 | errors, &errorHandler, |
| 3942 | "utf7", errmsg, |
| 3943 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3944 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3945 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3948 | /* end of string */ |
| 3949 | |
| 3950 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3951 | /* if we're in an inconsistent state, that's an error */ |
| 3952 | if (surrogate || |
| 3953 | (base64bits >= 6) || |
| 3954 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3955 | endinpos = size; |
| 3956 | if (unicode_decode_call_errorhandler( |
| 3957 | errors, &errorHandler, |
| 3958 | "utf7", "unterminated shift sequence", |
| 3959 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3960 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3961 | goto onError; |
| 3962 | if (s < e) |
| 3963 | goto restart; |
| 3964 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3965 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3966 | |
| 3967 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3968 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3969 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3970 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3971 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3972 | } |
| 3973 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3974 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3975 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3976 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3977 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3978 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3979 | goto onError; |
| 3980 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3981 | Py_XDECREF(errorHandler); |
| 3982 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3983 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3984 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3985 | Py_DECREF(unicode); |
| 3986 | return NULL; |
| 3987 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3988 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3989 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3990 | return unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3991 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3992 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3993 | Py_XDECREF(errorHandler); |
| 3994 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3995 | Py_DECREF(unicode); |
| 3996 | return NULL; |
| 3997 | } |
| 3998 | |
| 3999 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4000 | PyObject * |
| 4001 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4002 | Py_ssize_t size, |
| 4003 | int base64SetO, |
| 4004 | int base64WhiteSpace, |
| 4005 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4006 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4007 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4008 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 4009 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4010 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4011 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4012 | unsigned int base64bits = 0; |
| 4013 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4014 | char * out; |
| 4015 | char * start; |
| 4016 | |
| 4017 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4018 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4019 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 4020 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4021 | return PyErr_NoMemory(); |
| 4022 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4023 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4024 | if (v == NULL) |
| 4025 | return NULL; |
| 4026 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4027 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4028 | for (;i < size; ++i) { |
| 4029 | Py_UNICODE ch = s[i]; |
| 4030 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4031 | if (inShift) { |
| 4032 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4033 | /* shifting out */ |
| 4034 | if (base64bits) { /* output remaining bits */ |
| 4035 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4036 | base64buffer = 0; |
| 4037 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4038 | } |
| 4039 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4040 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4041 | so no '-' is required, except if the character is itself a '-' */ |
| 4042 | if (IS_BASE64(ch) || ch == '-') { |
| 4043 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4044 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4045 | *out++ = (char) ch; |
| 4046 | } |
| 4047 | else { |
| 4048 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4049 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4050 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4051 | else { /* not in a shift sequence */ |
| 4052 | if (ch == '+') { |
| 4053 | *out++ = '+'; |
| 4054 | *out++ = '-'; |
| 4055 | } |
| 4056 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4057 | *out++ = (char) ch; |
| 4058 | } |
| 4059 | else { |
| 4060 | *out++ = '+'; |
| 4061 | inShift = 1; |
| 4062 | goto encode_char; |
| 4063 | } |
| 4064 | } |
| 4065 | continue; |
| 4066 | encode_char: |
| 4067 | #ifdef Py_UNICODE_WIDE |
| 4068 | if (ch >= 0x10000) { |
| 4069 | /* code first surrogate */ |
| 4070 | base64bits += 16; |
| 4071 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4072 | while (base64bits >= 6) { |
| 4073 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4074 | base64bits -= 6; |
| 4075 | } |
| 4076 | /* prepare second surrogate */ |
| 4077 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4078 | } |
| 4079 | #endif |
| 4080 | base64bits += 16; |
| 4081 | base64buffer = (base64buffer << 16) | ch; |
| 4082 | while (base64bits >= 6) { |
| 4083 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4084 | base64bits -= 6; |
| 4085 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4086 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4087 | if (base64bits) |
| 4088 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4089 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4090 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4091 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4092 | return NULL; |
| 4093 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4096 | #undef IS_BASE64 |
| 4097 | #undef FROM_BASE64 |
| 4098 | #undef TO_BASE64 |
| 4099 | #undef DECODE_DIRECT |
| 4100 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4101 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4102 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4103 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4104 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4105 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4106 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4107 | illegal prefix. See RFC 3629 for details */ |
| 4108 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4109 | 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] | 4110 | 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] | 4111 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4112 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4113 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4114 | 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] | 4115 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4116 | 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] | 4117 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4118 | 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] | 4119 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4120 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4121 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4122 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4123 | 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] | 4124 | }; |
| 4125 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4126 | PyObject * |
| 4127 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4128 | Py_ssize_t size, |
| 4129 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4131 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4132 | } |
| 4133 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4134 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4135 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4136 | |
| 4137 | /* Mask to quickly check whether a C 'long' contains a |
| 4138 | non-ASCII, UTF8-encoded char. */ |
| 4139 | #if (SIZEOF_LONG == 8) |
| 4140 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4141 | #elif (SIZEOF_LONG == 4) |
| 4142 | # define ASCII_CHAR_MASK 0x80808080L |
| 4143 | #else |
| 4144 | # error C 'long' size should be either 4 or 8! |
| 4145 | #endif |
| 4146 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4147 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4148 | the size of the decoded unicode string and if any major errors were |
| 4149 | encountered. |
| 4150 | |
| 4151 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4152 | if the string contains surrogates, and if all continuation bytes are |
| 4153 | within the correct ranges, these checks are performed in |
| 4154 | PyUnicode_DecodeUTF8Stateful. |
| 4155 | |
| 4156 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4157 | will be bogus and you should not rely on useful information in them. |
| 4158 | */ |
| 4159 | static Py_UCS4 |
| 4160 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4161 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4162 | int *has_errors) |
| 4163 | { |
| 4164 | Py_ssize_t n; |
| 4165 | Py_ssize_t char_count = 0; |
| 4166 | Py_UCS4 max_char = 127, new_max; |
| 4167 | Py_UCS4 upper_bound; |
| 4168 | const unsigned char *p = (const unsigned char *)s; |
| 4169 | const unsigned char *end = p + string_size; |
| 4170 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4171 | int err = 0; |
| 4172 | |
| 4173 | for (; p < end && !err; ++p, ++char_count) { |
| 4174 | /* Only check value if it's not a ASCII char... */ |
| 4175 | if (*p < 0x80) { |
| 4176 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4177 | an explanation. */ |
| 4178 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4179 | /* Help register allocation */ |
| 4180 | register const unsigned char *_p = p; |
| 4181 | while (_p < aligned_end) { |
| 4182 | unsigned long value = *(unsigned long *) _p; |
| 4183 | if (value & ASCII_CHAR_MASK) |
| 4184 | break; |
| 4185 | _p += SIZEOF_LONG; |
| 4186 | char_count += SIZEOF_LONG; |
| 4187 | } |
| 4188 | p = _p; |
| 4189 | if (p == end) |
| 4190 | break; |
| 4191 | } |
| 4192 | } |
| 4193 | if (*p >= 0x80) { |
| 4194 | n = utf8_code_length[*p]; |
| 4195 | new_max = max_char; |
| 4196 | switch (n) { |
| 4197 | /* invalid start byte */ |
| 4198 | case 0: |
| 4199 | err = 1; |
| 4200 | break; |
| 4201 | case 2: |
| 4202 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4203 | Approximate the upper bound of the code point, |
| 4204 | if this flips over 255 we can be sure it will be more |
| 4205 | than 255 and the string will need 2 bytes per code coint, |
| 4206 | if it stays under or equal to 255, we can be sure 1 byte |
| 4207 | is enough. |
| 4208 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4209 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4210 | if (max_char < upper_bound) |
| 4211 | new_max = upper_bound; |
| 4212 | /* Ensure we track at least that we left ASCII space. */ |
| 4213 | if (new_max < 128) |
| 4214 | new_max = 128; |
| 4215 | break; |
| 4216 | case 3: |
| 4217 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4218 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4219 | if (max_char < 65535) |
| 4220 | new_max = 65535; |
| 4221 | break; |
| 4222 | case 4: |
| 4223 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4224 | new_max = 65537; |
| 4225 | break; |
| 4226 | /* Internal error, this should be caught by the first if */ |
| 4227 | case 1: |
| 4228 | default: |
| 4229 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4230 | err = 1; |
| 4231 | } |
| 4232 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4233 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4234 | --n; |
| 4235 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4236 | if (n >= 1) { |
| 4237 | const unsigned char *cont; |
| 4238 | if ((p + n) >= end) { |
| 4239 | if (consumed == 0) |
| 4240 | /* incomplete data, non-incremental decoding */ |
| 4241 | err = 1; |
| 4242 | break; |
| 4243 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4244 | for (cont = p + 1; cont <= (p + n); ++cont) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4245 | if ((*cont & 0xc0) != 0x80) { |
| 4246 | err = 1; |
| 4247 | break; |
| 4248 | } |
| 4249 | } |
| 4250 | p += n; |
| 4251 | } |
| 4252 | else |
| 4253 | err = 1; |
| 4254 | max_char = new_max; |
| 4255 | } |
| 4256 | } |
| 4257 | |
| 4258 | if (unicode_size) |
| 4259 | *unicode_size = char_count; |
| 4260 | if (has_errors) |
| 4261 | *has_errors = err; |
| 4262 | return max_char; |
| 4263 | } |
| 4264 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4265 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
| 4266 | in case of errors. Implicit parameters: unicode, kind, data, has_errors, |
| 4267 | onError. Potential resizing overallocates, so the result needs to shrink |
| 4268 | at the end. |
| 4269 | */ |
| 4270 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4271 | do { \ |
| 4272 | if (has_errors) { \ |
| 4273 | Py_ssize_t pos = index; \ |
| 4274 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4275 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4276 | goto onError; \ |
| 4277 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4278 | goto onError; \ |
| 4279 | } \ |
| 4280 | else \ |
| 4281 | PyUnicode_WRITE(kind, data, index, value); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4282 | } while (0) |
| 4283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4284 | PyObject * |
| 4285 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4286 | Py_ssize_t size, |
| 4287 | const char *errors, |
| 4288 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4289 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4290 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4291 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4292 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4293 | Py_ssize_t startinpos; |
| 4294 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4295 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4296 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4297 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4298 | PyObject *errorHandler = NULL; |
| 4299 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4300 | Py_UCS4 maxchar = 0; |
| 4301 | Py_ssize_t unicode_size; |
| 4302 | Py_ssize_t i; |
| 4303 | int kind; |
| 4304 | void *data; |
| 4305 | int has_errors; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4306 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4307 | if (size == 0) { |
| 4308 | if (consumed) |
| 4309 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4310 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4311 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4312 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4313 | consumed, &has_errors); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4314 | if (has_errors) |
| 4315 | /* maxchar and size computation might be incorrect; |
| 4316 | code below widens and resizes as necessary. */ |
| 4317 | unicode = PyUnicode_New(size, 127); |
| 4318 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4319 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4320 | if (!unicode) |
| 4321 | return NULL; |
| 4322 | /* When the string is ASCII only, just use memcpy and return. |
| 4323 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4324 | sequence at the end of the ASCII block. */ |
| 4325 | if (!has_errors && maxchar < 128 && size == unicode_size) { |
| 4326 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4327 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4328 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4329 | kind = PyUnicode_KIND(unicode); |
| 4330 | data = PyUnicode_DATA(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4332 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4333 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4334 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4335 | |
| 4336 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4337 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4338 | |
| 4339 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4340 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4341 | input will consist of an overwhelming majority of ASCII |
| 4342 | characters, we try to optimize for this case by checking |
| 4343 | as many characters as a C 'long' can contain. |
| 4344 | First, check if we can do an aligned read, as most CPUs have |
| 4345 | a penalty for unaligned reads. |
| 4346 | */ |
| 4347 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4348 | /* Help register allocation */ |
| 4349 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4350 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4351 | while (_s < aligned_end) { |
| 4352 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4353 | and do a fast unrolled copy if it only contains ASCII |
| 4354 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4355 | unsigned long value = *(unsigned long *) _s; |
| 4356 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4357 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4358 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4359 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4360 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4361 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4362 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4363 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4364 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4365 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4366 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4367 | #endif |
| 4368 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4369 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4370 | } |
| 4371 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4372 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4373 | if (s == e) |
| 4374 | break; |
| 4375 | ch = (unsigned char)*s; |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4380 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4381 | s++; |
| 4382 | continue; |
| 4383 | } |
| 4384 | |
| 4385 | n = utf8_code_length[ch]; |
| 4386 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4387 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4388 | if (consumed) |
| 4389 | break; |
| 4390 | else { |
| 4391 | errmsg = "unexpected end of data"; |
| 4392 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4393 | endinpos = startinpos+1; |
| 4394 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4395 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4396 | goto utf8Error; |
| 4397 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4398 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4399 | |
| 4400 | switch (n) { |
| 4401 | |
| 4402 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4403 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4404 | startinpos = s-starts; |
| 4405 | endinpos = startinpos+1; |
| 4406 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4407 | |
| 4408 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4409 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4410 | startinpos = s-starts; |
| 4411 | endinpos = startinpos+1; |
| 4412 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4413 | |
| 4414 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4415 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4416 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4417 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4418 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4419 | goto utf8Error; |
| 4420 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4422 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4423 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | break; |
| 4425 | |
| 4426 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4427 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4428 | will result in surrogates in range d800-dfff. Surrogates are |
| 4429 | not valid UTF-8 so they are rejected. |
| 4430 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4431 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4432 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4433 | (s[2] & 0xc0) != 0x80 || |
| 4434 | ((unsigned char)s[0] == 0xE0 && |
| 4435 | (unsigned char)s[1] < 0xA0) || |
| 4436 | ((unsigned char)s[0] == 0xED && |
| 4437 | (unsigned char)s[1] > 0x9F)) { |
| 4438 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4439 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4440 | endinpos = startinpos + 1; |
| 4441 | |
| 4442 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4443 | continuation byte is s[2], so increment endinpos by 1, |
| 4444 | if not, s[1] is invalid and endinpos doesn't need to |
| 4445 | be incremented. */ |
| 4446 | if ((s[1] & 0xC0) == 0x80) |
| 4447 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4448 | goto utf8Error; |
| 4449 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4450 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4451 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4452 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4453 | break; |
| 4454 | |
| 4455 | case 4: |
| 4456 | if ((s[1] & 0xc0) != 0x80 || |
| 4457 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4458 | (s[3] & 0xc0) != 0x80 || |
| 4459 | ((unsigned char)s[0] == 0xF0 && |
| 4460 | (unsigned char)s[1] < 0x90) || |
| 4461 | ((unsigned char)s[0] == 0xF4 && |
| 4462 | (unsigned char)s[1] > 0x8F)) { |
| 4463 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4464 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4465 | endinpos = startinpos + 1; |
| 4466 | if ((s[1] & 0xC0) == 0x80) { |
| 4467 | endinpos++; |
| 4468 | if ((s[2] & 0xC0) == 0x80) |
| 4469 | endinpos++; |
| 4470 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4471 | goto utf8Error; |
| 4472 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4473 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4474 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4475 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4476 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4477 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4478 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4479 | } |
| 4480 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4481 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4483 | utf8Error: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4484 | if (!has_errors) { |
| 4485 | PyObject *tmp; |
| 4486 | Py_ssize_t k; |
| 4487 | /* We encountered some error that wasn't detected in the original scan, |
| 4488 | e.g. an encoded surrogate character. The original maxchar computation may |
| 4489 | have been incorrect, so redo it now. */ |
| 4490 | for (k = 0, maxchar = 0; k < i; k++) |
| 4491 | maxchar = Py_MAX(maxchar, PyUnicode_READ(kind, data, k)); |
| 4492 | tmp = PyUnicode_New(PyUnicode_GET_LENGTH(unicode), maxchar); |
| 4493 | if (tmp == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4494 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4495 | PyUnicode_CopyCharacters(tmp, 0, unicode, 0, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4496 | Py_DECREF(unicode); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4497 | unicode = tmp; |
| 4498 | has_errors = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4499 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4500 | if (unicode_decode_call_errorhandler( |
| 4501 | errors, &errorHandler, |
| 4502 | "utf8", errmsg, |
| 4503 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4504 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4505 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4506 | /* Update data because unicode_decode_call_errorhandler might have |
| 4507 | re-created or resized the unicode object. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4508 | data = PyUnicode_DATA(unicode); |
| 4509 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4510 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4511 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4512 | /* Ensure the unicode_size calculation above was correct: */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4513 | assert(has_errors || i == unicode_size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4514 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4515 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4516 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4517 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4518 | /* Adjust length and ready string when it contained errors and |
| 4519 | is of the old resizable kind. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4520 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4521 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4522 | goto onError; |
| 4523 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4524 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 | Py_XDECREF(errorHandler); |
| 4526 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4527 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4528 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4529 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4530 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4531 | Py_XDECREF(errorHandler); |
| 4532 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4533 | Py_DECREF(unicode); |
| 4534 | return NULL; |
| 4535 | } |
| 4536 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4537 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4538 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4539 | #ifdef __APPLE__ |
| 4540 | |
| 4541 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4542 | used to decode the command line arguments on Mac OS X. */ |
| 4543 | |
| 4544 | wchar_t* |
| 4545 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4546 | { |
| 4547 | int n; |
| 4548 | const char *e; |
| 4549 | wchar_t *unicode, *p; |
| 4550 | |
| 4551 | /* Note: size will always be longer than the resulting Unicode |
| 4552 | character count */ |
| 4553 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4554 | PyErr_NoMemory(); |
| 4555 | return NULL; |
| 4556 | } |
| 4557 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4558 | if (!unicode) |
| 4559 | return NULL; |
| 4560 | |
| 4561 | /* Unpack UTF-8 encoded data */ |
| 4562 | p = unicode; |
| 4563 | e = s + size; |
| 4564 | while (s < e) { |
| 4565 | Py_UCS4 ch = (unsigned char)*s; |
| 4566 | |
| 4567 | if (ch < 0x80) { |
| 4568 | *p++ = (wchar_t)ch; |
| 4569 | s++; |
| 4570 | continue; |
| 4571 | } |
| 4572 | |
| 4573 | n = utf8_code_length[ch]; |
| 4574 | if (s + n > e) { |
| 4575 | goto surrogateescape; |
| 4576 | } |
| 4577 | |
| 4578 | switch (n) { |
| 4579 | case 0: |
| 4580 | case 1: |
| 4581 | goto surrogateescape; |
| 4582 | |
| 4583 | case 2: |
| 4584 | if ((s[1] & 0xc0) != 0x80) |
| 4585 | goto surrogateescape; |
| 4586 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4587 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4588 | *p++ = (wchar_t)ch; |
| 4589 | break; |
| 4590 | |
| 4591 | case 3: |
| 4592 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4593 | will result in surrogates in range d800-dfff. Surrogates are |
| 4594 | not valid UTF-8 so they are rejected. |
| 4595 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4596 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4597 | if ((s[1] & 0xc0) != 0x80 || |
| 4598 | (s[2] & 0xc0) != 0x80 || |
| 4599 | ((unsigned char)s[0] == 0xE0 && |
| 4600 | (unsigned char)s[1] < 0xA0) || |
| 4601 | ((unsigned char)s[0] == 0xED && |
| 4602 | (unsigned char)s[1] > 0x9F)) { |
| 4603 | |
| 4604 | goto surrogateescape; |
| 4605 | } |
| 4606 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4607 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4608 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4609 | break; |
| 4610 | |
| 4611 | case 4: |
| 4612 | if ((s[1] & 0xc0) != 0x80 || |
| 4613 | (s[2] & 0xc0) != 0x80 || |
| 4614 | (s[3] & 0xc0) != 0x80 || |
| 4615 | ((unsigned char)s[0] == 0xF0 && |
| 4616 | (unsigned char)s[1] < 0x90) || |
| 4617 | ((unsigned char)s[0] == 0xF4 && |
| 4618 | (unsigned char)s[1] > 0x8F)) { |
| 4619 | goto surrogateescape; |
| 4620 | } |
| 4621 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4622 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4623 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4624 | |
| 4625 | #if SIZEOF_WCHAR_T == 4 |
| 4626 | *p++ = (wchar_t)ch; |
| 4627 | #else |
| 4628 | /* compute and append the two surrogates: */ |
| 4629 | |
| 4630 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4631 | ch -= 0x10000; |
| 4632 | |
| 4633 | /* high surrogate = top 10 bits added to D800 */ |
| 4634 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4635 | |
| 4636 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4637 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4638 | #endif |
| 4639 | break; |
| 4640 | } |
| 4641 | s += n; |
| 4642 | continue; |
| 4643 | |
| 4644 | surrogateescape: |
| 4645 | *p++ = 0xDC00 + ch; |
| 4646 | s++; |
| 4647 | } |
| 4648 | *p = L'\0'; |
| 4649 | return unicode; |
| 4650 | } |
| 4651 | |
| 4652 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4653 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4654 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4655 | |
| 4656 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4657 | and allocate exactly as much space needed at the end. Else allocate the |
| 4658 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4659 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4660 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4661 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4662 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4663 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4664 | #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4665 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4666 | Py_ssize_t i; /* index into s of next input byte */ |
| 4667 | PyObject *result; /* result string object */ |
| 4668 | char *p; /* next free byte in output buffer */ |
| 4669 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4670 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4671 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4672 | PyObject *errorHandler = NULL; |
| 4673 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4674 | int kind; |
| 4675 | void *data; |
| 4676 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4678 | if (!PyUnicode_Check(unicode)) { |
| 4679 | PyErr_BadArgument(); |
| 4680 | return NULL; |
| 4681 | } |
| 4682 | |
| 4683 | if (PyUnicode_READY(unicode) == -1) |
| 4684 | return NULL; |
| 4685 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4686 | if (PyUnicode_UTF8(unicode)) |
| 4687 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4688 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4689 | |
| 4690 | kind = PyUnicode_KIND(unicode); |
| 4691 | data = PyUnicode_DATA(unicode); |
| 4692 | size = PyUnicode_GET_LENGTH(unicode); |
| 4693 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4694 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4695 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4696 | if (size <= MAX_SHORT_UNICHARS) { |
| 4697 | /* Write into the stack buffer; nallocated can't overflow. |
| 4698 | * At the end, we'll allocate exactly as much heap space as it |
| 4699 | * turns out we need. |
| 4700 | */ |
| 4701 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4702 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4703 | p = stackbuf; |
| 4704 | } |
| 4705 | else { |
| 4706 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4707 | nallocated = size * 4; |
| 4708 | if (nallocated / 4 != size) /* overflow! */ |
| 4709 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4710 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4711 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4712 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4713 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4714 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4715 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4716 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4717 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4718 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4719 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4720 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4721 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4723 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4724 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4725 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4726 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4727 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4728 | Py_ssize_t newpos; |
| 4729 | PyObject *rep; |
| 4730 | Py_ssize_t repsize, k, startpos; |
| 4731 | startpos = i-1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4732 | rep = unicode_encode_call_errorhandler( |
| 4733 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4734 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4735 | if (!rep) |
| 4736 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4738 | if (PyBytes_Check(rep)) |
| 4739 | repsize = PyBytes_GET_SIZE(rep); |
| 4740 | else |
| 4741 | repsize = PyUnicode_GET_SIZE(rep); |
| 4742 | |
| 4743 | if (repsize > 4) { |
| 4744 | Py_ssize_t offset; |
| 4745 | |
| 4746 | if (result == NULL) |
| 4747 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4748 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4749 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4751 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4752 | /* integer overflow */ |
| 4753 | PyErr_NoMemory(); |
| 4754 | goto error; |
| 4755 | } |
| 4756 | nallocated += repsize - 4; |
| 4757 | if (result != NULL) { |
| 4758 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4759 | goto error; |
| 4760 | } else { |
| 4761 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4762 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4763 | goto error; |
| 4764 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4765 | } |
| 4766 | p = PyBytes_AS_STRING(result) + offset; |
| 4767 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4768 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4769 | if (PyBytes_Check(rep)) { |
| 4770 | char *prep = PyBytes_AS_STRING(rep); |
| 4771 | for(k = repsize; k > 0; k--) |
| 4772 | *p++ = *prep++; |
| 4773 | } else /* rep is unicode */ { |
| 4774 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4775 | Py_UNICODE c; |
| 4776 | |
| 4777 | for(k=0; k<repsize; k++) { |
| 4778 | c = prep[k]; |
| 4779 | if (0x80 <= c) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 4780 | raise_encode_exception(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4781 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4782 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4783 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4784 | goto error; |
| 4785 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4786 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4787 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4788 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4789 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4790 | } else if (ch < 0x10000) { |
| 4791 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4792 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4793 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4794 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4795 | /* Encode UCS4 Unicode ordinals */ |
| 4796 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4797 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4798 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4799 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4800 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4801 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4802 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4803 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4804 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4805 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4806 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4807 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4808 | } |
| 4809 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4810 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4811 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4812 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4813 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4814 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4815 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4816 | Py_XDECREF(errorHandler); |
| 4817 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4818 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4819 | error: |
| 4820 | Py_XDECREF(errorHandler); |
| 4821 | Py_XDECREF(exc); |
| 4822 | Py_XDECREF(result); |
| 4823 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4824 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4825 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4828 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4829 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4830 | Py_ssize_t size, |
| 4831 | const char *errors) |
| 4832 | { |
| 4833 | PyObject *v, *unicode; |
| 4834 | |
| 4835 | unicode = PyUnicode_FromUnicode(s, size); |
| 4836 | if (unicode == NULL) |
| 4837 | return NULL; |
| 4838 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4839 | Py_DECREF(unicode); |
| 4840 | return v; |
| 4841 | } |
| 4842 | |
| 4843 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4844 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4846 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4849 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4850 | |
| 4851 | PyObject * |
| 4852 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4853 | Py_ssize_t size, |
| 4854 | const char *errors, |
| 4855 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4856 | { |
| 4857 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4858 | } |
| 4859 | |
| 4860 | PyObject * |
| 4861 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4862 | Py_ssize_t size, |
| 4863 | const char *errors, |
| 4864 | int *byteorder, |
| 4865 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4866 | { |
| 4867 | const char *starts = s; |
| 4868 | Py_ssize_t startinpos; |
| 4869 | Py_ssize_t endinpos; |
| 4870 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4871 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4872 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4873 | int bo = 0; /* assume native ordering by default */ |
| 4874 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4875 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4876 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4877 | int iorder[] = {0, 1, 2, 3}; |
| 4878 | #else |
| 4879 | int iorder[] = {3, 2, 1, 0}; |
| 4880 | #endif |
| 4881 | PyObject *errorHandler = NULL; |
| 4882 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4883 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4884 | q = (unsigned char *)s; |
| 4885 | e = q + size; |
| 4886 | |
| 4887 | if (byteorder) |
| 4888 | bo = *byteorder; |
| 4889 | |
| 4890 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4891 | byte order setting accordingly. In native mode, the leading BOM |
| 4892 | mark is skipped, in all other modes, it is copied to the output |
| 4893 | stream as-is (giving a ZWNBSP character). */ |
| 4894 | if (bo == 0) { |
| 4895 | if (size >= 4) { |
| 4896 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4897 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4898 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4899 | if (bom == 0x0000FEFF) { |
| 4900 | q += 4; |
| 4901 | bo = -1; |
| 4902 | } |
| 4903 | else if (bom == 0xFFFE0000) { |
| 4904 | q += 4; |
| 4905 | bo = 1; |
| 4906 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4907 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4908 | if (bom == 0x0000FEFF) { |
| 4909 | q += 4; |
| 4910 | bo = 1; |
| 4911 | } |
| 4912 | else if (bom == 0xFFFE0000) { |
| 4913 | q += 4; |
| 4914 | bo = -1; |
| 4915 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4916 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4917 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | if (bo == -1) { |
| 4921 | /* force LE */ |
| 4922 | iorder[0] = 0; |
| 4923 | iorder[1] = 1; |
| 4924 | iorder[2] = 2; |
| 4925 | iorder[3] = 3; |
| 4926 | } |
| 4927 | else if (bo == 1) { |
| 4928 | /* force BE */ |
| 4929 | iorder[0] = 3; |
| 4930 | iorder[1] = 2; |
| 4931 | iorder[2] = 1; |
| 4932 | iorder[3] = 0; |
| 4933 | } |
| 4934 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4935 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4936 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4937 | if (!unicode) |
| 4938 | return NULL; |
| 4939 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4940 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4941 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4942 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4943 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4944 | Py_UCS4 ch; |
| 4945 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4946 | if (e-q<4) { |
| 4947 | if (consumed) |
| 4948 | break; |
| 4949 | errmsg = "truncated data"; |
| 4950 | startinpos = ((const char *)q)-starts; |
| 4951 | endinpos = ((const char *)e)-starts; |
| 4952 | goto utf32Error; |
| 4953 | /* The remaining input chars are ignored if the callback |
| 4954 | chooses to skip the input */ |
| 4955 | } |
| 4956 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4957 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4958 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4959 | if (ch >= 0x110000) |
| 4960 | { |
| 4961 | errmsg = "codepoint not in range(0x110000)"; |
| 4962 | startinpos = ((const char *)q)-starts; |
| 4963 | endinpos = startinpos+4; |
| 4964 | goto utf32Error; |
| 4965 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4966 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4967 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4968 | q += 4; |
| 4969 | continue; |
| 4970 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | if (unicode_decode_call_errorhandler( |
| 4972 | errors, &errorHandler, |
| 4973 | "utf32", errmsg, |
| 4974 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4975 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | if (byteorder) |
| 4980 | *byteorder = bo; |
| 4981 | |
| 4982 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4983 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4984 | |
| 4985 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4986 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4987 | goto onError; |
| 4988 | |
| 4989 | Py_XDECREF(errorHandler); |
| 4990 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4991 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4992 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4993 | Py_DECREF(unicode); |
| 4994 | return NULL; |
| 4995 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4996 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4997 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4998 | return unicode; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4999 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5000 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5001 | Py_DECREF(unicode); |
| 5002 | Py_XDECREF(errorHandler); |
| 5003 | Py_XDECREF(exc); |
| 5004 | return NULL; |
| 5005 | } |
| 5006 | |
| 5007 | PyObject * |
| 5008 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5009 | Py_ssize_t size, |
| 5010 | const char *errors, |
| 5011 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5012 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5013 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5014 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5015 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5016 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5017 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5018 | #else |
| 5019 | const int pairs = 0; |
| 5020 | #endif |
| 5021 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5022 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5023 | int iorder[] = {0, 1, 2, 3}; |
| 5024 | #else |
| 5025 | int iorder[] = {3, 2, 1, 0}; |
| 5026 | #endif |
| 5027 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5028 | #define STORECHAR(CH) \ |
| 5029 | do { \ |
| 5030 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5031 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5032 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5033 | p[iorder[0]] = (CH) & 0xff; \ |
| 5034 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5035 | } while(0) |
| 5036 | |
| 5037 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5038 | so we need less space. */ |
| 5039 | #ifndef Py_UNICODE_WIDE |
| 5040 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5042 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5043 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5044 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5045 | nsize = (size - pairs + (byteorder == 0)); |
| 5046 | bytesize = nsize * 4; |
| 5047 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5049 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5050 | if (v == NULL) |
| 5051 | return NULL; |
| 5052 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5053 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5054 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5055 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5056 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5057 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5058 | |
| 5059 | if (byteorder == -1) { |
| 5060 | /* force LE */ |
| 5061 | iorder[0] = 0; |
| 5062 | iorder[1] = 1; |
| 5063 | iorder[2] = 2; |
| 5064 | iorder[3] = 3; |
| 5065 | } |
| 5066 | else if (byteorder == 1) { |
| 5067 | /* force BE */ |
| 5068 | iorder[0] = 3; |
| 5069 | iorder[1] = 2; |
| 5070 | iorder[2] = 1; |
| 5071 | iorder[3] = 0; |
| 5072 | } |
| 5073 | |
| 5074 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5076 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5077 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5078 | Py_UCS4 ch2 = *s; |
| 5079 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5080 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5081 | s++; |
| 5082 | size--; |
| 5083 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5084 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5085 | #endif |
| 5086 | STORECHAR(ch); |
| 5087 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5088 | |
| 5089 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5090 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | #undef STORECHAR |
| 5092 | } |
| 5093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5094 | PyObject * |
| 5095 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5096 | { |
| 5097 | if (!PyUnicode_Check(unicode)) { |
| 5098 | PyErr_BadArgument(); |
| 5099 | return NULL; |
| 5100 | } |
| 5101 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5102 | PyUnicode_GET_SIZE(unicode), |
| 5103 | NULL, |
| 5104 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5108 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5109 | PyObject * |
| 5110 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5111 | Py_ssize_t size, |
| 5112 | const char *errors, |
| 5113 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5114 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5115 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5116 | } |
| 5117 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5118 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5119 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5120 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5121 | rare in most input. |
| 5122 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5123 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5124 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5125 | #if (SIZEOF_LONG == 8) |
| 5126 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5127 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5128 | #elif (SIZEOF_LONG == 4) |
| 5129 | # define FAST_CHAR_MASK 0x80008000L |
| 5130 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5131 | #else |
| 5132 | # error C 'long' size should be either 4 or 8! |
| 5133 | #endif |
| 5134 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5135 | PyObject * |
| 5136 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5137 | Py_ssize_t size, |
| 5138 | const char *errors, |
| 5139 | int *byteorder, |
| 5140 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5141 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5142 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5143 | Py_ssize_t startinpos; |
| 5144 | Py_ssize_t endinpos; |
| 5145 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5146 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5147 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5148 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5149 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5150 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5151 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5152 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5153 | int ihi = 1, ilo = 0; |
| 5154 | #else |
| 5155 | int ihi = 0, ilo = 1; |
| 5156 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5157 | PyObject *errorHandler = NULL; |
| 5158 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5159 | |
| 5160 | /* Note: size will always be longer than the resulting Unicode |
| 5161 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5162 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5163 | if (!unicode) |
| 5164 | return NULL; |
| 5165 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5166 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5167 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5168 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5169 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5170 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | |
| 5172 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5173 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5175 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5176 | byte order setting accordingly. In native mode, the leading BOM |
| 5177 | mark is skipped, in all other modes, it is copied to the output |
| 5178 | stream as-is (giving a ZWNBSP character). */ |
| 5179 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5180 | if (size >= 2) { |
| 5181 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5182 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5183 | if (bom == 0xFEFF) { |
| 5184 | q += 2; |
| 5185 | bo = -1; |
| 5186 | } |
| 5187 | else if (bom == 0xFFFE) { |
| 5188 | q += 2; |
| 5189 | bo = 1; |
| 5190 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5191 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5192 | if (bom == 0xFEFF) { |
| 5193 | q += 2; |
| 5194 | bo = 1; |
| 5195 | } |
| 5196 | else if (bom == 0xFFFE) { |
| 5197 | q += 2; |
| 5198 | bo = -1; |
| 5199 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5200 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5201 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5202 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5204 | if (bo == -1) { |
| 5205 | /* force LE */ |
| 5206 | ihi = 1; |
| 5207 | ilo = 0; |
| 5208 | } |
| 5209 | else if (bo == 1) { |
| 5210 | /* force BE */ |
| 5211 | ihi = 0; |
| 5212 | ilo = 1; |
| 5213 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5214 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5215 | native_ordering = ilo < ihi; |
| 5216 | #else |
| 5217 | native_ordering = ilo > ihi; |
| 5218 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5219 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5220 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5221 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5222 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5223 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5224 | reads are more expensive, better to defer to another iteration. */ |
| 5225 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5226 | /* Fast path for runs of non-surrogate chars. */ |
| 5227 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5228 | int kind = PyUnicode_KIND(unicode); |
| 5229 | void *data = PyUnicode_DATA(unicode); |
| 5230 | while (_q < aligned_end) { |
| 5231 | unsigned long block = * (unsigned long *) _q; |
| 5232 | unsigned short *pblock = (unsigned short*)█ |
| 5233 | Py_UCS4 maxch; |
| 5234 | if (native_ordering) { |
| 5235 | /* Can use buffer directly */ |
| 5236 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5237 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5238 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5239 | else { |
| 5240 | /* Need to byte-swap */ |
| 5241 | unsigned char *_p = (unsigned char*)pblock; |
| 5242 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5243 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5244 | _p[0] = _q[1]; |
| 5245 | _p[1] = _q[0]; |
| 5246 | _p[2] = _q[3]; |
| 5247 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5248 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5249 | _p[4] = _q[5]; |
| 5250 | _p[5] = _q[4]; |
| 5251 | _p[6] = _q[7]; |
| 5252 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5253 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5254 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5255 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5256 | #if SIZEOF_LONG == 8 |
| 5257 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5258 | #endif |
| 5259 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5260 | if (unicode_widen(&unicode, maxch) < 0) |
| 5261 | goto onError; |
| 5262 | kind = PyUnicode_KIND(unicode); |
| 5263 | data = PyUnicode_DATA(unicode); |
| 5264 | } |
| 5265 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5266 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5267 | #if SIZEOF_LONG == 8 |
| 5268 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5269 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5270 | #endif |
| 5271 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5272 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5273 | q = _q; |
| 5274 | if (q >= e) |
| 5275 | break; |
| 5276 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5277 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5278 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5279 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5280 | |
| 5281 | if (ch < 0xD800 || ch > 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5282 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5283 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | continue; |
| 5285 | } |
| 5286 | |
| 5287 | /* UTF-16 code pair: */ |
| 5288 | if (q > e) { |
| 5289 | errmsg = "unexpected end of data"; |
| 5290 | startinpos = (((const char *)q) - 2) - starts; |
| 5291 | endinpos = ((const char *)e) + 1 - starts; |
| 5292 | goto utf16Error; |
| 5293 | } |
| 5294 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5295 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5296 | q += 2; |
| 5297 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5298 | if (unicode_putchar(&unicode, &outpos, |
| 5299 | (((ch & 0x3FF)<<10) | |
| 5300 | (ch2 & 0x3FF)) + 0x10000) < 0) |
| 5301 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5302 | continue; |
| 5303 | } |
| 5304 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5305 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5306 | startinpos = (((const char *)q)-4)-starts; |
| 5307 | endinpos = startinpos+2; |
| 5308 | goto utf16Error; |
| 5309 | } |
| 5310 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5311 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5312 | errmsg = "illegal encoding"; |
| 5313 | startinpos = (((const char *)q)-2)-starts; |
| 5314 | endinpos = startinpos+2; |
| 5315 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5317 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5318 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5319 | errors, |
| 5320 | &errorHandler, |
| 5321 | "utf16", errmsg, |
| 5322 | &starts, |
| 5323 | (const char **)&e, |
| 5324 | &startinpos, |
| 5325 | &endinpos, |
| 5326 | &exc, |
| 5327 | (const char **)&q, |
| 5328 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5329 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5330 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5332 | /* remaining byte at the end? (size should be even) */ |
| 5333 | if (e == q) { |
| 5334 | if (!consumed) { |
| 5335 | errmsg = "truncated data"; |
| 5336 | startinpos = ((const char *)q) - starts; |
| 5337 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5338 | if (unicode_decode_call_errorhandler( |
| 5339 | errors, |
| 5340 | &errorHandler, |
| 5341 | "utf16", errmsg, |
| 5342 | &starts, |
| 5343 | (const char **)&e, |
| 5344 | &startinpos, |
| 5345 | &endinpos, |
| 5346 | &exc, |
| 5347 | (const char **)&q, |
| 5348 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5349 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5350 | goto onError; |
| 5351 | /* The remaining input chars are ignored if the callback |
| 5352 | chooses to skip the input */ |
| 5353 | } |
| 5354 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5355 | |
| 5356 | if (byteorder) |
| 5357 | *byteorder = bo; |
| 5358 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5359 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5360 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5363 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5364 | goto onError; |
| 5365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5366 | Py_XDECREF(errorHandler); |
| 5367 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5368 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5369 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5371 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5373 | Py_XDECREF(errorHandler); |
| 5374 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | return NULL; |
| 5376 | } |
| 5377 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5378 | #undef FAST_CHAR_MASK |
| 5379 | #undef SWAPPED_FAST_CHAR_MASK |
| 5380 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5381 | PyObject * |
| 5382 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5383 | Py_ssize_t size, |
| 5384 | const char *errors, |
| 5385 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5387 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5388 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5389 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5390 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5391 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5392 | #else |
| 5393 | const int pairs = 0; |
| 5394 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5395 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5396 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5397 | int ihi = 1, ilo = 0; |
| 5398 | #else |
| 5399 | int ihi = 0, ilo = 1; |
| 5400 | #endif |
| 5401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5402 | #define STORECHAR(CH) \ |
| 5403 | do { \ |
| 5404 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5405 | p[ilo] = (CH) & 0xff; \ |
| 5406 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5407 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5409 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5410 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5411 | if (s[i] >= 0x10000) |
| 5412 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5413 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5414 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5415 | if (size > PY_SSIZE_T_MAX || |
| 5416 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5417 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5418 | nsize = size + pairs + (byteorder == 0); |
| 5419 | bytesize = nsize * 2; |
| 5420 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5422 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | if (v == NULL) |
| 5424 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5426 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5427 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5428 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5429 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5430 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5431 | |
| 5432 | if (byteorder == -1) { |
| 5433 | /* force LE */ |
| 5434 | ihi = 1; |
| 5435 | ilo = 0; |
| 5436 | } |
| 5437 | else if (byteorder == 1) { |
| 5438 | /* force BE */ |
| 5439 | ihi = 0; |
| 5440 | ilo = 1; |
| 5441 | } |
| 5442 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5443 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | Py_UNICODE ch = *s++; |
| 5445 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5446 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5447 | if (ch >= 0x10000) { |
| 5448 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5449 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5450 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5451 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5452 | STORECHAR(ch); |
| 5453 | if (ch2) |
| 5454 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5455 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5456 | |
| 5457 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5458 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5459 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5462 | PyObject * |
| 5463 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | { |
| 5465 | if (!PyUnicode_Check(unicode)) { |
| 5466 | PyErr_BadArgument(); |
| 5467 | return NULL; |
| 5468 | } |
| 5469 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5470 | PyUnicode_GET_SIZE(unicode), |
| 5471 | NULL, |
| 5472 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5476 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5477 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5478 | if all the escapes in the string make it still a valid ASCII string. |
| 5479 | Returns -1 if any escapes were found which cause the string to |
| 5480 | pop out of ASCII range. Otherwise returns the length of the |
| 5481 | required buffer to hold the string. |
| 5482 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5483 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5484 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5485 | { |
| 5486 | const unsigned char *p = (const unsigned char *)s; |
| 5487 | const unsigned char *end = p + size; |
| 5488 | Py_ssize_t length = 0; |
| 5489 | |
| 5490 | if (size < 0) |
| 5491 | return -1; |
| 5492 | |
| 5493 | for (; p < end; ++p) { |
| 5494 | if (*p > 127) { |
| 5495 | /* Non-ASCII */ |
| 5496 | return -1; |
| 5497 | } |
| 5498 | else if (*p != '\\') { |
| 5499 | /* Normal character */ |
| 5500 | ++length; |
| 5501 | } |
| 5502 | else { |
| 5503 | /* Backslash-escape, check next char */ |
| 5504 | ++p; |
| 5505 | /* Escape sequence reaches till end of string or |
| 5506 | non-ASCII follow-up. */ |
| 5507 | if (p >= end || *p > 127) |
| 5508 | return -1; |
| 5509 | switch (*p) { |
| 5510 | case '\n': |
| 5511 | /* backslash + \n result in zero characters */ |
| 5512 | break; |
| 5513 | case '\\': case '\'': case '\"': |
| 5514 | case 'b': case 'f': case 't': |
| 5515 | case 'n': case 'r': case 'v': case 'a': |
| 5516 | ++length; |
| 5517 | break; |
| 5518 | case '0': case '1': case '2': case '3': |
| 5519 | case '4': case '5': case '6': case '7': |
| 5520 | case 'x': case 'u': case 'U': case 'N': |
| 5521 | /* these do not guarantee ASCII characters */ |
| 5522 | return -1; |
| 5523 | default: |
| 5524 | /* count the backslash + the other character */ |
| 5525 | length += 2; |
| 5526 | } |
| 5527 | } |
| 5528 | } |
| 5529 | return length; |
| 5530 | } |
| 5531 | |
| 5532 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5533 | or treat string as ASCII. */ |
| 5534 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5535 | do { \ |
| 5536 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5537 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5538 | else \ |
| 5539 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5540 | } while (0) |
| 5541 | |
| 5542 | #define WRITE_WSTR(buf, index, value) \ |
| 5543 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5544 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5545 | |
| 5546 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5547 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5548 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5549 | PyObject * |
| 5550 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5551 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5552 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5554 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5555 | Py_ssize_t startinpos; |
| 5556 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5557 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5558 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5560 | char* message; |
| 5561 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5562 | PyObject *errorHandler = NULL; |
| 5563 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5564 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5565 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5566 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5567 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5568 | |
| 5569 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5570 | either the string is pure ASCII with named escapes like \n, etc. |
| 5571 | and we determined it's exact size (common case) |
| 5572 | or it contains \x, \u, ... escape sequences. then we create a |
| 5573 | 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] | 5574 | if (len >= 0) { |
| 5575 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5576 | if (!v) |
| 5577 | goto onError; |
| 5578 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5579 | } |
| 5580 | else { |
| 5581 | /* Escaped strings will always be longer than the resulting |
| 5582 | Unicode string, so we start with size here and then reduce the |
| 5583 | length after conversion to the true value. |
| 5584 | (but if the error callback returns a long replacement string |
| 5585 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5586 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5587 | if (!v) |
| 5588 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5589 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5590 | } |
| 5591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5593 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5594 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | while (s < end) { |
| 5598 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5599 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5600 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5602 | /* The only case in which i == ascii_length is a backslash |
| 5603 | followed by a newline. */ |
| 5604 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5607 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5608 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5609 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | continue; |
| 5611 | } |
| 5612 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5613 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | /* \ - Escapes */ |
| 5615 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5616 | c = *s++; |
| 5617 | if (s > end) |
| 5618 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5619 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5620 | /* The only case in which i == ascii_length is a backslash |
| 5621 | followed by a newline. */ |
| 5622 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5623 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5624 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5626 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5627 | #define WRITECHAR(ch) \ |
| 5628 | do { \ |
| 5629 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5630 | goto onError; \ |
| 5631 | }while(0) |
| 5632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5634 | case '\\': WRITECHAR('\\'); break; |
| 5635 | case '\'': WRITECHAR('\''); break; |
| 5636 | case '\"': WRITECHAR('\"'); break; |
| 5637 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5638 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5639 | case 'f': WRITECHAR('\014'); break; |
| 5640 | case 't': WRITECHAR('\t'); break; |
| 5641 | case 'n': WRITECHAR('\n'); break; |
| 5642 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5643 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5644 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5645 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5646 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5648 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | case '0': case '1': case '2': case '3': |
| 5650 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5651 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5652 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5653 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5654 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5655 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5657 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | break; |
| 5659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5660 | /* hex escapes */ |
| 5661 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5663 | digits = 2; |
| 5664 | message = "truncated \\xXX escape"; |
| 5665 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5667 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5669 | digits = 4; |
| 5670 | message = "truncated \\uXXXX escape"; |
| 5671 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5673 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5674 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5675 | digits = 8; |
| 5676 | message = "truncated \\UXXXXXXXX escape"; |
| 5677 | hexescape: |
| 5678 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5679 | if (s+digits>end) { |
| 5680 | endinpos = size; |
| 5681 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | errors, &errorHandler, |
| 5683 | "unicodeescape", "end of string in escape sequence", |
| 5684 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5685 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5686 | goto onError; |
| 5687 | goto nextByte; |
| 5688 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5689 | for (j = 0; j < digits; ++j) { |
| 5690 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5691 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5692 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5693 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5694 | errors, &errorHandler, |
| 5695 | "unicodeescape", message, |
| 5696 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5697 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5698 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5699 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5700 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5701 | } |
| 5702 | chr = (chr<<4) & ~0xF; |
| 5703 | if (c >= '0' && c <= '9') |
| 5704 | chr += c - '0'; |
| 5705 | else if (c >= 'a' && c <= 'f') |
| 5706 | chr += 10 + c - 'a'; |
| 5707 | else |
| 5708 | chr += 10 + c - 'A'; |
| 5709 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5710 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5711 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5712 | /* _decoding_error will have already written into the |
| 5713 | target buffer. */ |
| 5714 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5715 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5716 | /* when we get here, chr is a 32-bit unicode character */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5717 | if (chr <= 0x10ffff) { |
| 5718 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5719 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5720 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5721 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5722 | errors, &errorHandler, |
| 5723 | "unicodeescape", "illegal Unicode character", |
| 5724 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5725 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5726 | goto onError; |
| 5727 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5728 | break; |
| 5729 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5730 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5731 | case 'N': |
| 5732 | message = "malformed \\N character escape"; |
| 5733 | if (ucnhash_CAPI == NULL) { |
| 5734 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5735 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5736 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5737 | if (ucnhash_CAPI == NULL) |
| 5738 | goto ucnhashError; |
| 5739 | } |
| 5740 | if (*s == '{') { |
| 5741 | const char *start = s+1; |
| 5742 | /* look for the closing brace */ |
| 5743 | while (*s != '}' && s < end) |
| 5744 | s++; |
| 5745 | if (s > start && s < end && *s == '}') { |
| 5746 | /* found a name. look it up in the unicode database */ |
| 5747 | message = "unknown Unicode character name"; |
| 5748 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5749 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5750 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5751 | goto store; |
| 5752 | } |
| 5753 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5754 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5755 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5756 | errors, &errorHandler, |
| 5757 | "unicodeescape", message, |
| 5758 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5759 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5760 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5761 | break; |
| 5762 | |
| 5763 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5764 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5765 | message = "\\ at end of string"; |
| 5766 | s--; |
| 5767 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5769 | errors, &errorHandler, |
| 5770 | "unicodeescape", message, |
| 5771 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5772 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5773 | goto onError; |
| 5774 | } |
| 5775 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5776 | WRITECHAR('\\'); |
| 5777 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5778 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5779 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5781 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5783 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5784 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5785 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5786 | if (PyUnicode_Resize(&v, i) < 0) |
| 5787 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5788 | Py_XDECREF(errorHandler); |
| 5789 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5790 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5791 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5792 | Py_DECREF(v); |
| 5793 | return NULL; |
| 5794 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5795 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5796 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5797 | return v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5798 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5799 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5800 | PyErr_SetString( |
| 5801 | PyExc_UnicodeError, |
| 5802 | "\\N escapes not supported (can't load unicodedata module)" |
| 5803 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5804 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5805 | Py_XDECREF(errorHandler); |
| 5806 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5807 | return NULL; |
| 5808 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5811 | Py_XDECREF(errorHandler); |
| 5812 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | return NULL; |
| 5814 | } |
| 5815 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5816 | #undef WRITE_ASCII_OR_WSTR |
| 5817 | #undef WRITE_WSTR |
| 5818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5820 | |
| 5821 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5822 | appropriate. |
| 5823 | |
| 5824 | */ |
| 5825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5826 | PyObject * |
| 5827 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5828 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5829 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5830 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5833 | #ifdef Py_UNICODE_WIDE |
| 5834 | const Py_ssize_t expandsize = 10; |
| 5835 | #else |
| 5836 | const Py_ssize_t expandsize = 6; |
| 5837 | #endif |
| 5838 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5839 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5840 | better to choose a different scheme. Perhaps scan the |
| 5841 | first N-chars of the string and allocate based on that size. |
| 5842 | */ |
| 5843 | /* Initial allocation is based on the longest-possible unichr |
| 5844 | escape. |
| 5845 | |
| 5846 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5847 | unichr, so in this case it's the longest unichr escape. In |
| 5848 | narrow (UTF-16) builds this is five chars per source unichr |
| 5849 | since there are two unichrs in the surrogate pair, so in narrow |
| 5850 | (UTF-16) builds it's not the longest unichr escape. |
| 5851 | |
| 5852 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5853 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5854 | escape. |
| 5855 | */ |
| 5856 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5857 | if (size == 0) |
| 5858 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5859 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5860 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5862 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5863 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5864 | 2 |
| 5865 | + expandsize*size |
| 5866 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | if (repr == NULL) |
| 5868 | return NULL; |
| 5869 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5870 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | while (size-- > 0) { |
| 5873 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5874 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5875 | /* Escape backslashes */ |
| 5876 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 | *p++ = '\\'; |
| 5878 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5879 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5880 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5881 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5882 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5883 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5884 | else if (ch >= 0x10000) { |
| 5885 | *p++ = '\\'; |
| 5886 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5887 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5888 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5889 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5890 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5891 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5892 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5893 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5894 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5895 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5896 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5897 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5898 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5899 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5900 | Py_UNICODE ch2; |
| 5901 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | ch2 = *s++; |
| 5904 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5905 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5906 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5907 | *p++ = '\\'; |
| 5908 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5909 | *p++ = Py_hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5910 | *p++ = Py_hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5911 | *p++ = Py_hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5912 | *p++ = Py_hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5913 | *p++ = Py_hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5914 | *p++ = Py_hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5915 | *p++ = Py_hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5916 | *p++ = Py_hexdigits[ucs & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5917 | continue; |
| 5918 | } |
| 5919 | /* Fall through: isolated surrogates are copied as-is */ |
| 5920 | s--; |
| 5921 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5922 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5923 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5924 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5926 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | *p++ = '\\'; |
| 5928 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5929 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5930 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5931 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5932 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5934 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5935 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5936 | else if (ch == '\t') { |
| 5937 | *p++ = '\\'; |
| 5938 | *p++ = 't'; |
| 5939 | } |
| 5940 | else if (ch == '\n') { |
| 5941 | *p++ = '\\'; |
| 5942 | *p++ = 'n'; |
| 5943 | } |
| 5944 | else if (ch == '\r') { |
| 5945 | *p++ = '\\'; |
| 5946 | *p++ = 'r'; |
| 5947 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5948 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5949 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5950 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5951 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5952 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5953 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5954 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5955 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5956 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | /* Copy everything else as-is */ |
| 5958 | else |
| 5959 | *p++ = (char) ch; |
| 5960 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5962 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5963 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5964 | return NULL; |
| 5965 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5968 | PyObject * |
| 5969 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5971 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | if (!PyUnicode_Check(unicode)) { |
| 5973 | PyErr_BadArgument(); |
| 5974 | return NULL; |
| 5975 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5976 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5977 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5978 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | } |
| 5980 | |
| 5981 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5982 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5983 | PyObject * |
| 5984 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5985 | Py_ssize_t size, |
| 5986 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5988 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5989 | Py_ssize_t startinpos; |
| 5990 | Py_ssize_t endinpos; |
| 5991 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5992 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | const char *end; |
| 5994 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5995 | PyObject *errorHandler = NULL; |
| 5996 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5998 | /* Escaped strings will always be longer than the resulting |
| 5999 | 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] | 6000 | length after conversion to the true value. (But decoding error |
| 6001 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6002 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6006 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6007 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | end = s + size; |
| 6009 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6010 | unsigned char c; |
| 6011 | Py_UCS4 x; |
| 6012 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6013 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6015 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6016 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6017 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6018 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6020 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6021 | startinpos = s-starts; |
| 6022 | |
| 6023 | /* \u-escapes are only interpreted iff the number of leading |
| 6024 | backslashes if odd */ |
| 6025 | bs = s; |
| 6026 | for (;s < end;) { |
| 6027 | if (*s != '\\') |
| 6028 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6029 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6030 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6031 | } |
| 6032 | if (((s - bs) & 1) == 0 || |
| 6033 | s >= end || |
| 6034 | (*s != 'u' && *s != 'U')) { |
| 6035 | continue; |
| 6036 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6037 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6038 | count = *s=='u' ? 4 : 8; |
| 6039 | s++; |
| 6040 | |
| 6041 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6043 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6044 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6045 | endinpos = s-starts; |
| 6046 | if (unicode_decode_call_errorhandler( |
| 6047 | errors, &errorHandler, |
| 6048 | "rawunicodeescape", "truncated \\uXXXX", |
| 6049 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6050 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6051 | goto onError; |
| 6052 | goto nextByte; |
| 6053 | } |
| 6054 | x = (x<<4) & ~0xF; |
| 6055 | if (c >= '0' && c <= '9') |
| 6056 | x += c - '0'; |
| 6057 | else if (c >= 'a' && c <= 'f') |
| 6058 | x += 10 + c - 'a'; |
| 6059 | else |
| 6060 | x += 10 + c - 'A'; |
| 6061 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6062 | if (x <= 0x10ffff) { |
| 6063 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6064 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6065 | } else { |
| 6066 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6067 | if (unicode_decode_call_errorhandler( |
| 6068 | errors, &errorHandler, |
| 6069 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6070 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6071 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6072 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6073 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6074 | nextByte: |
| 6075 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6077 | if (PyUnicode_Resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6078 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6079 | Py_XDECREF(errorHandler); |
| 6080 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6081 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6082 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6083 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6084 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6086 | Py_XDECREF(errorHandler); |
| 6087 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | return NULL; |
| 6089 | } |
| 6090 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6091 | PyObject * |
| 6092 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6093 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6095 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | char *p; |
| 6097 | char *q; |
| 6098 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6099 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6100 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6101 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6102 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6103 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6104 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6105 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6107 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6108 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | if (repr == NULL) |
| 6110 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6111 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6112 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6114 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6115 | while (size-- > 0) { |
| 6116 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6117 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6119 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6120 | *p++ = '\\'; |
| 6121 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6122 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6123 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6124 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6125 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6126 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6127 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6128 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6129 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6130 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6131 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6132 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6133 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6134 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6135 | Py_UNICODE ch2; |
| 6136 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6137 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | ch2 = *s++; |
| 6139 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6140 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6141 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6142 | *p++ = '\\'; |
| 6143 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6144 | *p++ = Py_hexdigits[(ucs >> 28) & 0xf]; |
| 6145 | *p++ = Py_hexdigits[(ucs >> 24) & 0xf]; |
| 6146 | *p++ = Py_hexdigits[(ucs >> 20) & 0xf]; |
| 6147 | *p++ = Py_hexdigits[(ucs >> 16) & 0xf]; |
| 6148 | *p++ = Py_hexdigits[(ucs >> 12) & 0xf]; |
| 6149 | *p++ = Py_hexdigits[(ucs >> 8) & 0xf]; |
| 6150 | *p++ = Py_hexdigits[(ucs >> 4) & 0xf]; |
| 6151 | *p++ = Py_hexdigits[ucs & 0xf]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6152 | continue; |
| 6153 | } |
| 6154 | /* Fall through: isolated surrogates are copied as-is */ |
| 6155 | s--; |
| 6156 | size++; |
| 6157 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6158 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6159 | /* Map 16-bit characters to '\uxxxx' */ |
| 6160 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | *p++ = '\\'; |
| 6162 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6163 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6164 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6165 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6166 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6168 | /* Copy everything else as-is */ |
| 6169 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | *p++ = (char) ch; |
| 6171 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6172 | size = p - q; |
| 6173 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6174 | assert(size > 0); |
| 6175 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6176 | return NULL; |
| 6177 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6180 | PyObject * |
| 6181 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6183 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6185 | PyErr_BadArgument(); |
| 6186 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6188 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6189 | PyUnicode_GET_SIZE(unicode)); |
| 6190 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6191 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | } |
| 6193 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6194 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6195 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6196 | PyObject * |
| 6197 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6198 | Py_ssize_t size, |
| 6199 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6200 | { |
| 6201 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6202 | Py_ssize_t startinpos; |
| 6203 | Py_ssize_t endinpos; |
| 6204 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6205 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6206 | const char *end; |
| 6207 | const char *reason; |
| 6208 | PyObject *errorHandler = NULL; |
| 6209 | PyObject *exc = NULL; |
| 6210 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6211 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6212 | 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] | 6213 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6215 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6216 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6217 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6218 | end = s + size; |
| 6219 | |
| 6220 | while (s < end) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6221 | Py_UCS4 ch = *(Py_UNICODE*)s; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6222 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6223 | some malformed UCS-4 data. */ |
| 6224 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6225 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6226 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6228 | end-s < Py_UNICODE_SIZE |
| 6229 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6231 | startinpos = s - starts; |
| 6232 | if (end-s < Py_UNICODE_SIZE) { |
| 6233 | endinpos = end-starts; |
| 6234 | reason = "truncated input"; |
| 6235 | } |
| 6236 | else { |
| 6237 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6238 | reason = "illegal code point (> 0x10FFFF)"; |
| 6239 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6240 | if (unicode_decode_call_errorhandler( |
| 6241 | errors, &errorHandler, |
| 6242 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6243 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6244 | &v, &outpos)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6245 | goto onError; |
| 6246 | } |
| 6247 | } |
| 6248 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6249 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6250 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6251 | s += Py_UNICODE_SIZE; |
| 6252 | } |
| 6253 | } |
| 6254 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6255 | if (PyUnicode_Resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6256 | goto onError; |
| 6257 | Py_XDECREF(errorHandler); |
| 6258 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6259 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6260 | return v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6261 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6263 | Py_XDECREF(v); |
| 6264 | Py_XDECREF(errorHandler); |
| 6265 | Py_XDECREF(exc); |
| 6266 | return NULL; |
| 6267 | } |
| 6268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6270 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6271 | PyObject * |
| 6272 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6273 | Py_ssize_t size, |
| 6274 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6276 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6277 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6280 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6281 | static void |
| 6282 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6283 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6284 | PyObject *unicode, |
| 6285 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6286 | const char *reason) |
| 6287 | { |
| 6288 | if (*exceptionObject == NULL) { |
| 6289 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6290 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6291 | encoding, unicode, startpos, endpos, reason); |
| 6292 | } |
| 6293 | else { |
| 6294 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6295 | goto onError; |
| 6296 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6297 | goto onError; |
| 6298 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6299 | goto onError; |
| 6300 | return; |
| 6301 | onError: |
| 6302 | Py_DECREF(*exceptionObject); |
| 6303 | *exceptionObject = NULL; |
| 6304 | } |
| 6305 | } |
| 6306 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6307 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6308 | static void |
| 6309 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6310 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6311 | PyObject *unicode, |
| 6312 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6313 | const char *reason) |
| 6314 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6315 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6316 | encoding, unicode, startpos, endpos, reason); |
| 6317 | if (*exceptionObject != NULL) |
| 6318 | PyCodec_StrictErrors(*exceptionObject); |
| 6319 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6320 | |
| 6321 | /* error handling callback helper: |
| 6322 | build arguments, call the callback and check the arguments, |
| 6323 | put the result into newpos and return the replacement string, which |
| 6324 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6325 | static PyObject * |
| 6326 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6327 | PyObject **errorHandler, |
| 6328 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6329 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6330 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6331 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6332 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6333 | 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] | 6334 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6335 | PyObject *restuple; |
| 6336 | PyObject *resunicode; |
| 6337 | |
| 6338 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6339 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6340 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6341 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6344 | if (PyUnicode_READY(unicode) < 0) |
| 6345 | return NULL; |
| 6346 | len = PyUnicode_GET_LENGTH(unicode); |
| 6347 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6348 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6349 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6350 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6351 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6352 | |
| 6353 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6354 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6355 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6357 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6358 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | Py_DECREF(restuple); |
| 6360 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6361 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6362 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6363 | &resunicode, newpos)) { |
| 6364 | Py_DECREF(restuple); |
| 6365 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6366 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6367 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6368 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6369 | Py_DECREF(restuple); |
| 6370 | return NULL; |
| 6371 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6372 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6373 | *newpos = len + *newpos; |
| 6374 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6376 | Py_DECREF(restuple); |
| 6377 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6378 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6379 | Py_INCREF(resunicode); |
| 6380 | Py_DECREF(restuple); |
| 6381 | return resunicode; |
| 6382 | } |
| 6383 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6384 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6385 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6386 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6387 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6388 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6389 | /* input state */ |
| 6390 | Py_ssize_t pos=0, size; |
| 6391 | int kind; |
| 6392 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6393 | /* output object */ |
| 6394 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6395 | /* pointer into the output */ |
| 6396 | char *str; |
| 6397 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6398 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6399 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6400 | 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] | 6401 | PyObject *errorHandler = NULL; |
| 6402 | PyObject *exc = NULL; |
| 6403 | /* the following variable is used for caching string comparisons |
| 6404 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6405 | int known_errorHandler = -1; |
| 6406 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6407 | if (PyUnicode_READY(unicode) < 0) |
| 6408 | return NULL; |
| 6409 | size = PyUnicode_GET_LENGTH(unicode); |
| 6410 | kind = PyUnicode_KIND(unicode); |
| 6411 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | /* allocate enough for a simple encoding without |
| 6413 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6414 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6415 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6416 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6418 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6419 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6420 | ressize = size; |
| 6421 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6422 | while (pos < size) { |
| 6423 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6424 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6425 | /* can we encode this? */ |
| 6426 | if (c<limit) { |
| 6427 | /* no overflow check, because we know that the space is enough */ |
| 6428 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6429 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6430 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6432 | Py_ssize_t requiredsize; |
| 6433 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6434 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6435 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6436 | Py_ssize_t collstart = pos; |
| 6437 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6438 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6439 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6440 | ++collend; |
| 6441 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6442 | if (known_errorHandler==-1) { |
| 6443 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6444 | known_errorHandler = 1; |
| 6445 | else if (!strcmp(errors, "replace")) |
| 6446 | known_errorHandler = 2; |
| 6447 | else if (!strcmp(errors, "ignore")) |
| 6448 | known_errorHandler = 3; |
| 6449 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6450 | known_errorHandler = 4; |
| 6451 | else |
| 6452 | known_errorHandler = 0; |
| 6453 | } |
| 6454 | switch (known_errorHandler) { |
| 6455 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6456 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6457 | goto onError; |
| 6458 | case 2: /* replace */ |
| 6459 | while (collstart++<collend) |
| 6460 | *str++ = '?'; /* fall through */ |
| 6461 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6462 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6463 | break; |
| 6464 | case 4: /* xmlcharrefreplace */ |
| 6465 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6466 | /* determine replacement size */ |
| 6467 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6468 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6469 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6470 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6471 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6472 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6473 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6475 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6477 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6478 | else |
| 6479 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6480 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6481 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6482 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6483 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6484 | repsize += 2+6+1; |
| 6485 | else |
| 6486 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6487 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6489 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | if (requiredsize > ressize) { |
| 6491 | if (requiredsize<2*ressize) |
| 6492 | requiredsize = 2*ressize; |
| 6493 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6494 | goto onError; |
| 6495 | str = PyBytes_AS_STRING(res) + respos; |
| 6496 | ressize = requiredsize; |
| 6497 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6498 | /* generate replacement */ |
| 6499 | for (i = collstart; i < collend; ++i) { |
| 6500 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6501 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6502 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6503 | break; |
| 6504 | default: |
| 6505 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6506 | encoding, reason, unicode, &exc, |
| 6507 | collstart, collend, &newpos); |
| 6508 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6509 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6510 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6511 | if (PyBytes_Check(repunicode)) { |
| 6512 | /* Directly copy bytes result to output. */ |
| 6513 | repsize = PyBytes_Size(repunicode); |
| 6514 | if (repsize > 1) { |
| 6515 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6516 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6517 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6518 | Py_DECREF(repunicode); |
| 6519 | goto onError; |
| 6520 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6521 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6522 | ressize += repsize-1; |
| 6523 | } |
| 6524 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6525 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6526 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6527 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6528 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6529 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6530 | /* need more space? (at least enough for what we |
| 6531 | have+the replacement+the rest of the string, so |
| 6532 | we won't have to check space for encodable characters) */ |
| 6533 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6534 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6535 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6536 | if (requiredsize > ressize) { |
| 6537 | if (requiredsize<2*ressize) |
| 6538 | requiredsize = 2*ressize; |
| 6539 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6540 | Py_DECREF(repunicode); |
| 6541 | goto onError; |
| 6542 | } |
| 6543 | str = PyBytes_AS_STRING(res) + respos; |
| 6544 | ressize = requiredsize; |
| 6545 | } |
| 6546 | /* check if there is anything unencodable in the replacement |
| 6547 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6548 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6549 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6551 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6552 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6553 | Py_DECREF(repunicode); |
| 6554 | goto onError; |
| 6555 | } |
| 6556 | *str = (char)c; |
| 6557 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6558 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6559 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6560 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6561 | } |
| 6562 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6563 | /* Resize if we allocated to much */ |
| 6564 | size = str - PyBytes_AS_STRING(res); |
| 6565 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6566 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6567 | if (_PyBytes_Resize(&res, size) < 0) |
| 6568 | goto onError; |
| 6569 | } |
| 6570 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6571 | Py_XDECREF(errorHandler); |
| 6572 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6573 | return res; |
| 6574 | |
| 6575 | onError: |
| 6576 | Py_XDECREF(res); |
| 6577 | Py_XDECREF(errorHandler); |
| 6578 | Py_XDECREF(exc); |
| 6579 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6582 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6583 | PyObject * |
| 6584 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6585 | Py_ssize_t size, |
| 6586 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6587 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6588 | PyObject *result; |
| 6589 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6590 | if (unicode == NULL) |
| 6591 | return NULL; |
| 6592 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6593 | Py_DECREF(unicode); |
| 6594 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6597 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6598 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | { |
| 6600 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6601 | PyErr_BadArgument(); |
| 6602 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6604 | if (PyUnicode_READY(unicode) == -1) |
| 6605 | return NULL; |
| 6606 | /* Fast path: if it is a one-byte string, construct |
| 6607 | bytes object directly. */ |
| 6608 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6609 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6610 | PyUnicode_GET_LENGTH(unicode)); |
| 6611 | /* Non-Latin-1 characters present. Defer to above function to |
| 6612 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6613 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6614 | } |
| 6615 | |
| 6616 | PyObject* |
| 6617 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6618 | { |
| 6619 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6623 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6624 | PyObject * |
| 6625 | PyUnicode_DecodeASCII(const char *s, |
| 6626 | Py_ssize_t size, |
| 6627 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6628 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6629 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6630 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6631 | int kind; |
| 6632 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6633 | Py_ssize_t startinpos; |
| 6634 | Py_ssize_t endinpos; |
| 6635 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6636 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6637 | int has_error; |
| 6638 | const unsigned char *p = (const unsigned char *)s; |
| 6639 | const unsigned char *end = p + size; |
| 6640 | 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] | 6641 | PyObject *errorHandler = NULL; |
| 6642 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6645 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6646 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6647 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6648 | has_error = 0; |
| 6649 | while (p < end && !has_error) { |
| 6650 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6651 | an explanation. */ |
| 6652 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6653 | /* Help register allocation */ |
| 6654 | register const unsigned char *_p = p; |
| 6655 | while (_p < aligned_end) { |
| 6656 | unsigned long value = *(unsigned long *) _p; |
| 6657 | if (value & ASCII_CHAR_MASK) { |
| 6658 | has_error = 1; |
| 6659 | break; |
| 6660 | } |
| 6661 | _p += SIZEOF_LONG; |
| 6662 | } |
| 6663 | if (_p == end) |
| 6664 | break; |
| 6665 | if (has_error) |
| 6666 | break; |
| 6667 | p = _p; |
| 6668 | } |
| 6669 | if (*p & 0x80) { |
| 6670 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6671 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6672 | } |
| 6673 | else { |
| 6674 | ++p; |
| 6675 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6676 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6677 | if (!has_error) |
| 6678 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6679 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6680 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6682 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6684 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6685 | kind = PyUnicode_KIND(v); |
| 6686 | data = PyUnicode_DATA(v); |
| 6687 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6688 | e = s + size; |
| 6689 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6690 | register unsigned char c = (unsigned char)*s; |
| 6691 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6692 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6693 | ++s; |
| 6694 | } |
| 6695 | else { |
| 6696 | startinpos = s-starts; |
| 6697 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6698 | if (unicode_decode_call_errorhandler( |
| 6699 | errors, &errorHandler, |
| 6700 | "ascii", "ordinal not in range(128)", |
| 6701 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6702 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6703 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6704 | kind = PyUnicode_KIND(v); |
| 6705 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6706 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6708 | if (PyUnicode_Resize(&v, outpos) < 0) |
| 6709 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6710 | Py_XDECREF(errorHandler); |
| 6711 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6712 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6713 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6714 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6715 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6717 | Py_XDECREF(errorHandler); |
| 6718 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6719 | return NULL; |
| 6720 | } |
| 6721 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6722 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6723 | PyObject * |
| 6724 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6725 | Py_ssize_t size, |
| 6726 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6727 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6728 | PyObject *result; |
| 6729 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6730 | if (unicode == NULL) |
| 6731 | return NULL; |
| 6732 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6733 | Py_DECREF(unicode); |
| 6734 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6737 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6738 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | { |
| 6740 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | PyErr_BadArgument(); |
| 6742 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6744 | if (PyUnicode_READY(unicode) == -1) |
| 6745 | return NULL; |
| 6746 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6747 | directly. Else defer to above function to raise the exception. */ |
| 6748 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6749 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6750 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6751 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | PyObject * |
| 6755 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6756 | { |
| 6757 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | } |
| 6759 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6760 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6761 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6762 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6763 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6764 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6765 | #define NEED_RETRY |
| 6766 | #endif |
| 6767 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6768 | #ifndef WC_ERR_INVALID_CHARS |
| 6769 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6770 | #endif |
| 6771 | |
| 6772 | static char* |
| 6773 | code_page_name(UINT code_page, PyObject **obj) |
| 6774 | { |
| 6775 | *obj = NULL; |
| 6776 | if (code_page == CP_ACP) |
| 6777 | return "mbcs"; |
| 6778 | if (code_page == CP_UTF7) |
| 6779 | return "CP_UTF7"; |
| 6780 | if (code_page == CP_UTF8) |
| 6781 | return "CP_UTF8"; |
| 6782 | |
| 6783 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6784 | if (*obj == NULL) |
| 6785 | return NULL; |
| 6786 | return PyBytes_AS_STRING(*obj); |
| 6787 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6788 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6789 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6790 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6791 | { |
| 6792 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6793 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6794 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6795 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6796 | return 0; |
| 6797 | |
| 6798 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6799 | if (prev == curr) |
| 6800 | return 1; |
| 6801 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6802 | as it assumes an incomplete character consists of a single |
| 6803 | byte. */ |
| 6804 | if (curr - prev == 2) |
| 6805 | return 1; |
| 6806 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6807 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6808 | return 0; |
| 6809 | } |
| 6810 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6811 | static DWORD |
| 6812 | decode_code_page_flags(UINT code_page) |
| 6813 | { |
| 6814 | if (code_page == CP_UTF7) { |
| 6815 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6816 | return 0; |
| 6817 | } |
| 6818 | else |
| 6819 | return MB_ERR_INVALID_CHARS; |
| 6820 | } |
| 6821 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6822 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6823 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6824 | * mode. |
| 6825 | * |
| 6826 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6827 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6828 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6829 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6830 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6831 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6832 | const char *in, |
| 6833 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6834 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6835 | const DWORD flags = decode_code_page_flags(code_page); |
| 6836 | Py_UNICODE *out; |
| 6837 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6838 | |
| 6839 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6840 | assert(insize > 0); |
| 6841 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6842 | if (outsize <= 0) |
| 6843 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6844 | |
| 6845 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6846 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6847 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6848 | if (*v == NULL) |
| 6849 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6850 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6851 | } |
| 6852 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6853 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6854 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6855 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6856 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6857 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
| 6860 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6861 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6862 | if (outsize <= 0) |
| 6863 | goto error; |
| 6864 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6865 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6866 | error: |
| 6867 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6868 | return -2; |
| 6869 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6870 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6871 | } |
| 6872 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6873 | /* |
| 6874 | * Decode a byte string from a code page into unicode object with an error |
| 6875 | * handler. |
| 6876 | * |
| 6877 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6878 | * UnicodeDecodeError exception and returns -1 on error. |
| 6879 | */ |
| 6880 | static int |
| 6881 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6882 | PyObject **v, |
| 6883 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6884 | const char *errors) |
| 6885 | { |
| 6886 | const char *startin = in; |
| 6887 | const char *endin = in + size; |
| 6888 | const DWORD flags = decode_code_page_flags(code_page); |
| 6889 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6890 | 2000 English version of the message. */ |
| 6891 | const char *reason = "No mapping for the Unicode character exists " |
| 6892 | "in the target code page."; |
| 6893 | /* each step cannot decode more than 1 character, but a character can be |
| 6894 | represented as a surrogate pair */ |
| 6895 | wchar_t buffer[2], *startout, *out; |
| 6896 | int insize, outsize; |
| 6897 | PyObject *errorHandler = NULL; |
| 6898 | PyObject *exc = NULL; |
| 6899 | PyObject *encoding_obj = NULL; |
| 6900 | char *encoding; |
| 6901 | DWORD err; |
| 6902 | int ret = -1; |
| 6903 | |
| 6904 | assert(size > 0); |
| 6905 | |
| 6906 | encoding = code_page_name(code_page, &encoding_obj); |
| 6907 | if (encoding == NULL) |
| 6908 | return -1; |
| 6909 | |
| 6910 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6911 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6912 | UnicodeDecodeError. */ |
| 6913 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6914 | if (exc != NULL) { |
| 6915 | PyCodec_StrictErrors(exc); |
| 6916 | Py_CLEAR(exc); |
| 6917 | } |
| 6918 | goto error; |
| 6919 | } |
| 6920 | |
| 6921 | if (*v == NULL) { |
| 6922 | /* Create unicode object */ |
| 6923 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6924 | PyErr_NoMemory(); |
| 6925 | goto error; |
| 6926 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6927 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6928 | if (*v == NULL) |
| 6929 | goto error; |
| 6930 | startout = PyUnicode_AS_UNICODE(*v); |
| 6931 | } |
| 6932 | else { |
| 6933 | /* Extend unicode object */ |
| 6934 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6935 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6936 | PyErr_NoMemory(); |
| 6937 | goto error; |
| 6938 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6939 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6940 | goto error; |
| 6941 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6942 | } |
| 6943 | |
| 6944 | /* Decode the byte string character per character */ |
| 6945 | out = startout; |
| 6946 | while (in < endin) |
| 6947 | { |
| 6948 | /* Decode a character */ |
| 6949 | insize = 1; |
| 6950 | do |
| 6951 | { |
| 6952 | outsize = MultiByteToWideChar(code_page, flags, |
| 6953 | in, insize, |
| 6954 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6955 | if (outsize > 0) |
| 6956 | break; |
| 6957 | err = GetLastError(); |
| 6958 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6959 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6960 | { |
| 6961 | PyErr_SetFromWindowsErr(0); |
| 6962 | goto error; |
| 6963 | } |
| 6964 | insize++; |
| 6965 | } |
| 6966 | /* 4=maximum length of a UTF-8 sequence */ |
| 6967 | while (insize <= 4 && (in + insize) <= endin); |
| 6968 | |
| 6969 | if (outsize <= 0) { |
| 6970 | Py_ssize_t startinpos, endinpos, outpos; |
| 6971 | |
| 6972 | startinpos = in - startin; |
| 6973 | endinpos = startinpos + 1; |
| 6974 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 6975 | if (unicode_decode_call_errorhandler( |
| 6976 | errors, &errorHandler, |
| 6977 | encoding, reason, |
| 6978 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
| 6979 | v, &outpos, &out)) |
| 6980 | { |
| 6981 | goto error; |
| 6982 | } |
| 6983 | } |
| 6984 | else { |
| 6985 | in += insize; |
| 6986 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6987 | out += outsize; |
| 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | /* write a NUL character at the end */ |
| 6992 | *out = 0; |
| 6993 | |
| 6994 | /* Extend unicode object */ |
| 6995 | outsize = out - startout; |
| 6996 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6997 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6999 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 | |
| 7001 | error: |
| 7002 | Py_XDECREF(encoding_obj); |
| 7003 | Py_XDECREF(errorHandler); |
| 7004 | Py_XDECREF(exc); |
| 7005 | return ret; |
| 7006 | } |
| 7007 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7008 | static PyObject * |
| 7009 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7010 | const char *s, Py_ssize_t size, |
| 7011 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7012 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7013 | PyObject *v = NULL; |
| 7014 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7015 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7016 | if (code_page < 0) { |
| 7017 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7018 | return NULL; |
| 7019 | } |
| 7020 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7021 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7022 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7023 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7024 | do |
| 7025 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7026 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7027 | if (size > INT_MAX) { |
| 7028 | chunk_size = INT_MAX; |
| 7029 | final = 0; |
| 7030 | done = 0; |
| 7031 | } |
| 7032 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7033 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7034 | { |
| 7035 | chunk_size = (int)size; |
| 7036 | final = (consumed == NULL); |
| 7037 | done = 1; |
| 7038 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7039 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7040 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7041 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7042 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7043 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7044 | if (chunk_size == 0 && done) { |
| 7045 | if (v != NULL) |
| 7046 | break; |
| 7047 | Py_INCREF(unicode_empty); |
| 7048 | return unicode_empty; |
| 7049 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7050 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7051 | |
| 7052 | converted = decode_code_page_strict(code_page, &v, |
| 7053 | s, chunk_size); |
| 7054 | if (converted == -2) |
| 7055 | converted = decode_code_page_errors(code_page, &v, |
| 7056 | s, chunk_size, |
| 7057 | errors); |
| 7058 | assert(converted != 0); |
| 7059 | |
| 7060 | if (converted < 0) { |
| 7061 | Py_XDECREF(v); |
| 7062 | return NULL; |
| 7063 | } |
| 7064 | |
| 7065 | if (consumed) |
| 7066 | *consumed += converted; |
| 7067 | |
| 7068 | s += converted; |
| 7069 | size -= converted; |
| 7070 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7072 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7073 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7074 | Py_DECREF(v); |
| 7075 | return NULL; |
| 7076 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7077 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7078 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7079 | return v; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7080 | } |
| 7081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7082 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7083 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7084 | const char *s, |
| 7085 | Py_ssize_t size, |
| 7086 | const char *errors, |
| 7087 | Py_ssize_t *consumed) |
| 7088 | { |
| 7089 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7090 | } |
| 7091 | |
| 7092 | PyObject * |
| 7093 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7094 | Py_ssize_t size, |
| 7095 | const char *errors, |
| 7096 | Py_ssize_t *consumed) |
| 7097 | { |
| 7098 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7099 | } |
| 7100 | |
| 7101 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7102 | PyUnicode_DecodeMBCS(const char *s, |
| 7103 | Py_ssize_t size, |
| 7104 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7105 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7106 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7107 | } |
| 7108 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7109 | static DWORD |
| 7110 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7111 | { |
| 7112 | if (code_page == CP_UTF8) { |
| 7113 | if (winver.dwMajorVersion >= 6) |
| 7114 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7115 | and later */ |
| 7116 | return WC_ERR_INVALID_CHARS; |
| 7117 | else |
| 7118 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7119 | return 0; |
| 7120 | } |
| 7121 | else if (code_page == CP_UTF7) { |
| 7122 | /* CP_UTF7 only supports flags=0 */ |
| 7123 | return 0; |
| 7124 | } |
| 7125 | else { |
| 7126 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7127 | return 0; |
| 7128 | else |
| 7129 | return WC_NO_BEST_FIT_CHARS; |
| 7130 | } |
| 7131 | } |
| 7132 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7133 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7134 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7135 | * mode. |
| 7136 | * |
| 7137 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7138 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7139 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7140 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7141 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7142 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7143 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7144 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7145 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7146 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7147 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7148 | PyObject *exc = NULL; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7149 | Py_UNICODE *p; |
| 7150 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7151 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7152 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7153 | /* Create a substring so that we can get the UTF-16 representation |
| 7154 | of just the slice under consideration. */ |
| 7155 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7156 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7157 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7158 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7159 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7160 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7161 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7162 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7163 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7164 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7165 | if (substring == NULL) |
| 7166 | return -1; |
| 7167 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7168 | if (p == NULL) { |
| 7169 | Py_DECREF(substring); |
| 7170 | return -1; |
| 7171 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7172 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7173 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7174 | outsize = WideCharToMultiByte(code_page, flags, |
| 7175 | p, size, |
| 7176 | NULL, 0, |
| 7177 | NULL, pusedDefaultChar); |
| 7178 | if (outsize <= 0) |
| 7179 | goto error; |
| 7180 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7181 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7182 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7184 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7185 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7187 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7188 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7189 | if (*outbytes == NULL) { |
| 7190 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7191 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7192 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7193 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7194 | } |
| 7195 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7198 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7199 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7200 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7201 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7202 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7203 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7204 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7206 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7208 | } |
| 7209 | |
| 7210 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7211 | outsize = WideCharToMultiByte(code_page, flags, |
| 7212 | p, size, |
| 7213 | out, outsize, |
| 7214 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7215 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7216 | if (outsize <= 0) |
| 7217 | goto error; |
| 7218 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7219 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7220 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7221 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7223 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7224 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7225 | return -2; |
| 7226 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7227 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7228 | } |
| 7229 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7230 | /* |
| 7231 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7232 | * error handler. |
| 7233 | * |
| 7234 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7235 | * -1 on other error. |
| 7236 | */ |
| 7237 | static int |
| 7238 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7239 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7240 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7241 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7243 | Py_ssize_t pos = unicode_offset; |
| 7244 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7245 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7246 | 2000 English version of the message. */ |
| 7247 | const char *reason = "invalid character"; |
| 7248 | /* 4=maximum length of a UTF-8 sequence */ |
| 7249 | char buffer[4]; |
| 7250 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7251 | Py_ssize_t outsize; |
| 7252 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | PyObject *errorHandler = NULL; |
| 7254 | PyObject *exc = NULL; |
| 7255 | PyObject *encoding_obj = NULL; |
| 7256 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7257 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7258 | PyObject *rep; |
| 7259 | int ret = -1; |
| 7260 | |
| 7261 | assert(insize > 0); |
| 7262 | |
| 7263 | encoding = code_page_name(code_page, &encoding_obj); |
| 7264 | if (encoding == NULL) |
| 7265 | return -1; |
| 7266 | |
| 7267 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7268 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7269 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7270 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7271 | if (exc != NULL) { |
| 7272 | PyCodec_StrictErrors(exc); |
| 7273 | Py_DECREF(exc); |
| 7274 | } |
| 7275 | Py_XDECREF(encoding_obj); |
| 7276 | return -1; |
| 7277 | } |
| 7278 | |
| 7279 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7280 | pusedDefaultChar = &usedDefaultChar; |
| 7281 | else |
| 7282 | pusedDefaultChar = NULL; |
| 7283 | |
| 7284 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7285 | PyErr_NoMemory(); |
| 7286 | goto error; |
| 7287 | } |
| 7288 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7289 | |
| 7290 | if (*outbytes == NULL) { |
| 7291 | /* Create string object */ |
| 7292 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7293 | if (*outbytes == NULL) |
| 7294 | goto error; |
| 7295 | out = PyBytes_AS_STRING(*outbytes); |
| 7296 | } |
| 7297 | else { |
| 7298 | /* Extend string object */ |
| 7299 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7300 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7301 | PyErr_NoMemory(); |
| 7302 | goto error; |
| 7303 | } |
| 7304 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7305 | goto error; |
| 7306 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7307 | } |
| 7308 | |
| 7309 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7310 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7311 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7312 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7313 | wchar_t chars[2]; |
| 7314 | int charsize; |
| 7315 | if (ch < 0x10000) { |
| 7316 | chars[0] = (wchar_t)ch; |
| 7317 | charsize = 1; |
| 7318 | } |
| 7319 | else { |
| 7320 | ch -= 0x10000; |
| 7321 | chars[0] = 0xd800 + (ch >> 10); |
| 7322 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7323 | charsize = 2; |
| 7324 | } |
| 7325 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7326 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7327 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7329 | NULL, pusedDefaultChar); |
| 7330 | if (outsize > 0) { |
| 7331 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7332 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7333 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7334 | memcpy(out, buffer, outsize); |
| 7335 | out += outsize; |
| 7336 | continue; |
| 7337 | } |
| 7338 | } |
| 7339 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7340 | PyErr_SetFromWindowsErr(0); |
| 7341 | goto error; |
| 7342 | } |
| 7343 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7344 | rep = unicode_encode_call_errorhandler( |
| 7345 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7346 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7347 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7348 | if (rep == NULL) |
| 7349 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7350 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7351 | |
| 7352 | if (PyBytes_Check(rep)) { |
| 7353 | outsize = PyBytes_GET_SIZE(rep); |
| 7354 | if (outsize != 1) { |
| 7355 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7356 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7357 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7358 | Py_DECREF(rep); |
| 7359 | goto error; |
| 7360 | } |
| 7361 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7362 | } |
| 7363 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7364 | out += outsize; |
| 7365 | } |
| 7366 | else { |
| 7367 | Py_ssize_t i; |
| 7368 | enum PyUnicode_Kind kind; |
| 7369 | void *data; |
| 7370 | |
| 7371 | if (PyUnicode_READY(rep) < 0) { |
| 7372 | Py_DECREF(rep); |
| 7373 | goto error; |
| 7374 | } |
| 7375 | |
| 7376 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7377 | if (outsize != 1) { |
| 7378 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7379 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7380 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7381 | Py_DECREF(rep); |
| 7382 | goto error; |
| 7383 | } |
| 7384 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7385 | } |
| 7386 | kind = PyUnicode_KIND(rep); |
| 7387 | data = PyUnicode_DATA(rep); |
| 7388 | for (i=0; i < outsize; i++) { |
| 7389 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7390 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7391 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7392 | encoding, unicode, |
| 7393 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | "unable to encode error handler result to ASCII"); |
| 7395 | Py_DECREF(rep); |
| 7396 | goto error; |
| 7397 | } |
| 7398 | *out = (unsigned char)ch; |
| 7399 | out++; |
| 7400 | } |
| 7401 | } |
| 7402 | Py_DECREF(rep); |
| 7403 | } |
| 7404 | /* write a NUL byte */ |
| 7405 | *out = 0; |
| 7406 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7407 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7408 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7409 | goto error; |
| 7410 | ret = 0; |
| 7411 | |
| 7412 | error: |
| 7413 | Py_XDECREF(encoding_obj); |
| 7414 | Py_XDECREF(errorHandler); |
| 7415 | Py_XDECREF(exc); |
| 7416 | return ret; |
| 7417 | } |
| 7418 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7419 | static PyObject * |
| 7420 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7421 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7422 | const char *errors) |
| 7423 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7424 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7425 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7426 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7427 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7428 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7429 | if (PyUnicode_READY(unicode) < 0) |
| 7430 | return NULL; |
| 7431 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7432 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7433 | if (code_page < 0) { |
| 7434 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7435 | return NULL; |
| 7436 | } |
| 7437 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7438 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7439 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7440 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7441 | offset = 0; |
| 7442 | do |
| 7443 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7444 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7445 | /* 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] | 7446 | chunks. */ |
| 7447 | if (len > INT_MAX/2) { |
| 7448 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7449 | done = 0; |
| 7450 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7451 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7452 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7453 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7454 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7455 | done = 1; |
| 7456 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7457 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7458 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7459 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7460 | errors); |
| 7461 | if (ret == -2) |
| 7462 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7463 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7464 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7465 | if (ret < 0) { |
| 7466 | Py_XDECREF(outbytes); |
| 7467 | return NULL; |
| 7468 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7469 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7470 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7471 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7472 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7473 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7474 | return outbytes; |
| 7475 | } |
| 7476 | |
| 7477 | PyObject * |
| 7478 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7479 | Py_ssize_t size, |
| 7480 | const char *errors) |
| 7481 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7482 | PyObject *unicode, *res; |
| 7483 | unicode = PyUnicode_FromUnicode(p, size); |
| 7484 | if (unicode == NULL) |
| 7485 | return NULL; |
| 7486 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7487 | Py_DECREF(unicode); |
| 7488 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7489 | } |
| 7490 | |
| 7491 | PyObject * |
| 7492 | PyUnicode_EncodeCodePage(int code_page, |
| 7493 | PyObject *unicode, |
| 7494 | const char *errors) |
| 7495 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7496 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7497 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7499 | PyObject * |
| 7500 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7501 | { |
| 7502 | if (!PyUnicode_Check(unicode)) { |
| 7503 | PyErr_BadArgument(); |
| 7504 | return NULL; |
| 7505 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7506 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7507 | } |
| 7508 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7509 | #undef NEED_RETRY |
| 7510 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7511 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7512 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7513 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7514 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7515 | PyObject * |
| 7516 | PyUnicode_DecodeCharmap(const char *s, |
| 7517 | Py_ssize_t size, |
| 7518 | PyObject *mapping, |
| 7519 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7520 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7521 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7522 | Py_ssize_t startinpos; |
| 7523 | Py_ssize_t endinpos; |
| 7524 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7525 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7526 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7527 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7528 | PyObject *errorHandler = NULL; |
| 7529 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7530 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7531 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7532 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | /* Default to Latin-1 */ |
| 7534 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7535 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7537 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7539 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7540 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7541 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7542 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7543 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7544 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7545 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7546 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7547 | while (s < e) { |
| 7548 | unsigned char ch = *s; |
| 7549 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7551 | if (ch < maplen) |
| 7552 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7554 | if (x == 0xfffe) { |
| 7555 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7556 | startinpos = s-starts; |
| 7557 | endinpos = startinpos+1; |
| 7558 | if (unicode_decode_call_errorhandler( |
| 7559 | errors, &errorHandler, |
| 7560 | "charmap", "character maps to <undefined>", |
| 7561 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7562 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7563 | goto onError; |
| 7564 | } |
| 7565 | continue; |
| 7566 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7567 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7568 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7569 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7570 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7571 | } |
| 7572 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7573 | while (s < e) { |
| 7574 | unsigned char ch = *s; |
| 7575 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7576 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7577 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7578 | w = PyLong_FromLong((long)ch); |
| 7579 | if (w == NULL) |
| 7580 | goto onError; |
| 7581 | x = PyObject_GetItem(mapping, w); |
| 7582 | Py_DECREF(w); |
| 7583 | if (x == NULL) { |
| 7584 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7585 | /* No mapping found means: mapping is undefined. */ |
| 7586 | PyErr_Clear(); |
| 7587 | x = Py_None; |
| 7588 | Py_INCREF(x); |
| 7589 | } else |
| 7590 | goto onError; |
| 7591 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | /* Apply mapping */ |
| 7594 | if (PyLong_Check(x)) { |
| 7595 | long value = PyLong_AS_LONG(x); |
| 7596 | if (value < 0 || value > 65535) { |
| 7597 | PyErr_SetString(PyExc_TypeError, |
| 7598 | "character mapping must be in range(65536)"); |
| 7599 | Py_DECREF(x); |
| 7600 | goto onError; |
| 7601 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7602 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7603 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7604 | } |
| 7605 | else if (x == Py_None) { |
| 7606 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7607 | startinpos = s-starts; |
| 7608 | endinpos = startinpos+1; |
| 7609 | if (unicode_decode_call_errorhandler( |
| 7610 | errors, &errorHandler, |
| 7611 | "charmap", "character maps to <undefined>", |
| 7612 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7613 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7614 | Py_DECREF(x); |
| 7615 | goto onError; |
| 7616 | } |
| 7617 | Py_DECREF(x); |
| 7618 | continue; |
| 7619 | } |
| 7620 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7621 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7622 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7623 | if (PyUnicode_READY(x) < 0) |
| 7624 | goto onError; |
| 7625 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7626 | |
| 7627 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7628 | /* 1-1 mapping */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7629 | if (unicode_putchar(&v, &outpos, |
| 7630 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7631 | goto onError; |
| 7632 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | else if (targetsize > 1) { |
| 7634 | /* 1-n mapping */ |
| 7635 | if (targetsize > extrachars) { |
| 7636 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7637 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7638 | (targetsize << 2); |
| 7639 | extrachars += needed; |
| 7640 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7641 | if (PyUnicode_Resize(&v, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7642 | PyUnicode_GET_LENGTH(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7643 | Py_DECREF(x); |
| 7644 | goto onError; |
| 7645 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7646 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7647 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7648 | goto onError; |
| 7649 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7650 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7651 | extrachars -= targetsize; |
| 7652 | } |
| 7653 | /* 1-0 mapping: skip the character */ |
| 7654 | } |
| 7655 | else { |
| 7656 | /* wrong return value */ |
| 7657 | PyErr_SetString(PyExc_TypeError, |
| 7658 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7659 | Py_DECREF(x); |
| 7660 | goto onError; |
| 7661 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7662 | Py_DECREF(x); |
| 7663 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7664 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7666 | if (PyUnicode_Resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame^] | 7667 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7668 | Py_XDECREF(errorHandler); |
| 7669 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7670 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7671 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7672 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7674 | Py_XDECREF(errorHandler); |
| 7675 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7676 | Py_XDECREF(v); |
| 7677 | return NULL; |
| 7678 | } |
| 7679 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7680 | /* Charmap encoding: the lookup table */ |
| 7681 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7682 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7683 | PyObject_HEAD |
| 7684 | unsigned char level1[32]; |
| 7685 | int count2, count3; |
| 7686 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7687 | }; |
| 7688 | |
| 7689 | static PyObject* |
| 7690 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7691 | { |
| 7692 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7693 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7695 | } |
| 7696 | |
| 7697 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7698 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7699 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7700 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7701 | }; |
| 7702 | |
| 7703 | static void |
| 7704 | encoding_map_dealloc(PyObject* o) |
| 7705 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7706 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7707 | } |
| 7708 | |
| 7709 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7710 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7711 | "EncodingMap", /*tp_name*/ |
| 7712 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7713 | 0, /*tp_itemsize*/ |
| 7714 | /* methods */ |
| 7715 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7716 | 0, /*tp_print*/ |
| 7717 | 0, /*tp_getattr*/ |
| 7718 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7719 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7720 | 0, /*tp_repr*/ |
| 7721 | 0, /*tp_as_number*/ |
| 7722 | 0, /*tp_as_sequence*/ |
| 7723 | 0, /*tp_as_mapping*/ |
| 7724 | 0, /*tp_hash*/ |
| 7725 | 0, /*tp_call*/ |
| 7726 | 0, /*tp_str*/ |
| 7727 | 0, /*tp_getattro*/ |
| 7728 | 0, /*tp_setattro*/ |
| 7729 | 0, /*tp_as_buffer*/ |
| 7730 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7731 | 0, /*tp_doc*/ |
| 7732 | 0, /*tp_traverse*/ |
| 7733 | 0, /*tp_clear*/ |
| 7734 | 0, /*tp_richcompare*/ |
| 7735 | 0, /*tp_weaklistoffset*/ |
| 7736 | 0, /*tp_iter*/ |
| 7737 | 0, /*tp_iternext*/ |
| 7738 | encoding_map_methods, /*tp_methods*/ |
| 7739 | 0, /*tp_members*/ |
| 7740 | 0, /*tp_getset*/ |
| 7741 | 0, /*tp_base*/ |
| 7742 | 0, /*tp_dict*/ |
| 7743 | 0, /*tp_descr_get*/ |
| 7744 | 0, /*tp_descr_set*/ |
| 7745 | 0, /*tp_dictoffset*/ |
| 7746 | 0, /*tp_init*/ |
| 7747 | 0, /*tp_alloc*/ |
| 7748 | 0, /*tp_new*/ |
| 7749 | 0, /*tp_free*/ |
| 7750 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7751 | }; |
| 7752 | |
| 7753 | PyObject* |
| 7754 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7755 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7756 | PyObject *result; |
| 7757 | struct encoding_map *mresult; |
| 7758 | int i; |
| 7759 | int need_dict = 0; |
| 7760 | unsigned char level1[32]; |
| 7761 | unsigned char level2[512]; |
| 7762 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7763 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7764 | int kind; |
| 7765 | void *data; |
| 7766 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7768 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7769 | PyErr_BadArgument(); |
| 7770 | return NULL; |
| 7771 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7772 | kind = PyUnicode_KIND(string); |
| 7773 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7774 | memset(level1, 0xFF, sizeof level1); |
| 7775 | memset(level2, 0xFF, sizeof level2); |
| 7776 | |
| 7777 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7778 | or if there are non-BMP characters, we need to use |
| 7779 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7780 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7781 | need_dict = 1; |
| 7782 | for (i = 1; i < 256; i++) { |
| 7783 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7784 | ch = PyUnicode_READ(kind, data, i); |
| 7785 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7786 | need_dict = 1; |
| 7787 | break; |
| 7788 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7789 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7790 | /* unmapped character */ |
| 7791 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7792 | l1 = ch >> 11; |
| 7793 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7794 | if (level1[l1] == 0xFF) |
| 7795 | level1[l1] = count2++; |
| 7796 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7797 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7798 | } |
| 7799 | |
| 7800 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7801 | need_dict = 1; |
| 7802 | |
| 7803 | if (need_dict) { |
| 7804 | PyObject *result = PyDict_New(); |
| 7805 | PyObject *key, *value; |
| 7806 | if (!result) |
| 7807 | return NULL; |
| 7808 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7809 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7810 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7811 | if (!key || !value) |
| 7812 | goto failed1; |
| 7813 | if (PyDict_SetItem(result, key, value) == -1) |
| 7814 | goto failed1; |
| 7815 | Py_DECREF(key); |
| 7816 | Py_DECREF(value); |
| 7817 | } |
| 7818 | return result; |
| 7819 | failed1: |
| 7820 | Py_XDECREF(key); |
| 7821 | Py_XDECREF(value); |
| 7822 | Py_DECREF(result); |
| 7823 | return NULL; |
| 7824 | } |
| 7825 | |
| 7826 | /* Create a three-level trie */ |
| 7827 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7828 | 16*count2 + 128*count3 - 1); |
| 7829 | if (!result) |
| 7830 | return PyErr_NoMemory(); |
| 7831 | PyObject_Init(result, &EncodingMapType); |
| 7832 | mresult = (struct encoding_map*)result; |
| 7833 | mresult->count2 = count2; |
| 7834 | mresult->count3 = count3; |
| 7835 | mlevel1 = mresult->level1; |
| 7836 | mlevel2 = mresult->level23; |
| 7837 | mlevel3 = mresult->level23 + 16*count2; |
| 7838 | memcpy(mlevel1, level1, 32); |
| 7839 | memset(mlevel2, 0xFF, 16*count2); |
| 7840 | memset(mlevel3, 0, 128*count3); |
| 7841 | count3 = 0; |
| 7842 | for (i = 1; i < 256; i++) { |
| 7843 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7844 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7845 | /* unmapped character */ |
| 7846 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7847 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7848 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7849 | i2 = 16*mlevel1[o1] + o2; |
| 7850 | if (mlevel2[i2] == 0xFF) |
| 7851 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7852 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7853 | i3 = 128*mlevel2[i2] + o3; |
| 7854 | mlevel3[i3] = i; |
| 7855 | } |
| 7856 | return result; |
| 7857 | } |
| 7858 | |
| 7859 | static int |
| 7860 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7861 | { |
| 7862 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7863 | int l1 = c>>11; |
| 7864 | int l2 = (c>>7) & 0xF; |
| 7865 | int l3 = c & 0x7F; |
| 7866 | int i; |
| 7867 | |
| 7868 | #ifdef Py_UNICODE_WIDE |
| 7869 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7870 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7871 | } |
| 7872 | #endif |
| 7873 | if (c == 0) |
| 7874 | return 0; |
| 7875 | /* level 1*/ |
| 7876 | i = map->level1[l1]; |
| 7877 | if (i == 0xFF) { |
| 7878 | return -1; |
| 7879 | } |
| 7880 | /* level 2*/ |
| 7881 | i = map->level23[16*i+l2]; |
| 7882 | if (i == 0xFF) { |
| 7883 | return -1; |
| 7884 | } |
| 7885 | /* level 3 */ |
| 7886 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7887 | if (i == 0) { |
| 7888 | return -1; |
| 7889 | } |
| 7890 | return i; |
| 7891 | } |
| 7892 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7893 | /* Lookup the character ch in the mapping. If the character |
| 7894 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7895 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7896 | static PyObject * |
| 7897 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7898 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7899 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7900 | PyObject *x; |
| 7901 | |
| 7902 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7904 | x = PyObject_GetItem(mapping, w); |
| 7905 | Py_DECREF(w); |
| 7906 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7907 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7908 | /* No mapping found means: mapping is undefined. */ |
| 7909 | PyErr_Clear(); |
| 7910 | x = Py_None; |
| 7911 | Py_INCREF(x); |
| 7912 | return x; |
| 7913 | } else |
| 7914 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7915 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7916 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7917 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7918 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7919 | long value = PyLong_AS_LONG(x); |
| 7920 | if (value < 0 || value > 255) { |
| 7921 | PyErr_SetString(PyExc_TypeError, |
| 7922 | "character mapping must be in range(256)"); |
| 7923 | Py_DECREF(x); |
| 7924 | return NULL; |
| 7925 | } |
| 7926 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7927 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7928 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7929 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7930 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7931 | /* wrong return value */ |
| 7932 | PyErr_Format(PyExc_TypeError, |
| 7933 | "character mapping must return integer, bytes or None, not %.400s", |
| 7934 | x->ob_type->tp_name); |
| 7935 | Py_DECREF(x); |
| 7936 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7937 | } |
| 7938 | } |
| 7939 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7940 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7941 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7942 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7943 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7944 | /* exponentially overallocate to minimize reallocations */ |
| 7945 | if (requiredsize < 2*outsize) |
| 7946 | requiredsize = 2*outsize; |
| 7947 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7948 | return -1; |
| 7949 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7950 | } |
| 7951 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7952 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7953 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7954 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7955 | /* 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] | 7956 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7957 | space is available. Return a new reference to the object that |
| 7958 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7959 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7960 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7961 | static charmapencode_result |
| 7962 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7963 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7964 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7965 | PyObject *rep; |
| 7966 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7967 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7968 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7969 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7970 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7971 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7972 | if (res == -1) |
| 7973 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7974 | if (outsize<requiredsize) |
| 7975 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7976 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7977 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7978 | outstart[(*outpos)++] = (char)res; |
| 7979 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7983 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7985 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | Py_DECREF(rep); |
| 7987 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7988 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | if (PyLong_Check(rep)) { |
| 7990 | Py_ssize_t requiredsize = *outpos+1; |
| 7991 | if (outsize<requiredsize) |
| 7992 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7993 | Py_DECREF(rep); |
| 7994 | return enc_EXCEPTION; |
| 7995 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7996 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7997 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7998 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | else { |
| 8000 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8001 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8002 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8003 | if (outsize<requiredsize) |
| 8004 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8005 | Py_DECREF(rep); |
| 8006 | return enc_EXCEPTION; |
| 8007 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8008 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8009 | memcpy(outstart + *outpos, repchars, repsize); |
| 8010 | *outpos += repsize; |
| 8011 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8012 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8013 | Py_DECREF(rep); |
| 8014 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8015 | } |
| 8016 | |
| 8017 | /* handle an error in PyUnicode_EncodeCharmap |
| 8018 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8019 | static int |
| 8020 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8021 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8022 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8023 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8024 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | { |
| 8026 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8027 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8028 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8029 | Py_UNICODE *uni2; |
| 8030 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8031 | Py_ssize_t collstartpos = *inpos; |
| 8032 | Py_ssize_t collendpos = *inpos+1; |
| 8033 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8034 | char *encoding = "charmap"; |
| 8035 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8036 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8037 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8038 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8039 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8040 | if (PyUnicode_READY(unicode) < 0) |
| 8041 | return -1; |
| 8042 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8043 | /* find all unencodable characters */ |
| 8044 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8045 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8046 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8047 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8048 | val = encoding_map_lookup(ch, mapping); |
| 8049 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | break; |
| 8051 | ++collendpos; |
| 8052 | continue; |
| 8053 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8054 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8055 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8056 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8057 | if (rep==NULL) |
| 8058 | return -1; |
| 8059 | else if (rep!=Py_None) { |
| 8060 | Py_DECREF(rep); |
| 8061 | break; |
| 8062 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8063 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8064 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8065 | } |
| 8066 | /* cache callback name lookup |
| 8067 | * (if not done yet, i.e. it's the first error) */ |
| 8068 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8070 | *known_errorHandler = 1; |
| 8071 | else if (!strcmp(errors, "replace")) |
| 8072 | *known_errorHandler = 2; |
| 8073 | else if (!strcmp(errors, "ignore")) |
| 8074 | *known_errorHandler = 3; |
| 8075 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8076 | *known_errorHandler = 4; |
| 8077 | else |
| 8078 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8079 | } |
| 8080 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8081 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8082 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8083 | return -1; |
| 8084 | case 2: /* replace */ |
| 8085 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8086 | x = charmapencode_output('?', mapping, res, respos); |
| 8087 | if (x==enc_EXCEPTION) { |
| 8088 | return -1; |
| 8089 | } |
| 8090 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8091 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | return -1; |
| 8093 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8094 | } |
| 8095 | /* fall through */ |
| 8096 | case 3: /* ignore */ |
| 8097 | *inpos = collendpos; |
| 8098 | break; |
| 8099 | case 4: /* xmlcharrefreplace */ |
| 8100 | /* generate replacement (temporarily (mis)uses p) */ |
| 8101 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8102 | char buffer[2+29+1+1]; |
| 8103 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8104 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8105 | for (cp = buffer; *cp; ++cp) { |
| 8106 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8107 | if (x==enc_EXCEPTION) |
| 8108 | return -1; |
| 8109 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8110 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8111 | return -1; |
| 8112 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8113 | } |
| 8114 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8115 | *inpos = collendpos; |
| 8116 | break; |
| 8117 | default: |
| 8118 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8119 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8121 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8123 | if (PyBytes_Check(repunicode)) { |
| 8124 | /* Directly copy bytes result to output. */ |
| 8125 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8126 | Py_ssize_t requiredsize; |
| 8127 | repsize = PyBytes_Size(repunicode); |
| 8128 | requiredsize = *respos + repsize; |
| 8129 | if (requiredsize > outsize) |
| 8130 | /* Make room for all additional bytes. */ |
| 8131 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8132 | Py_DECREF(repunicode); |
| 8133 | return -1; |
| 8134 | } |
| 8135 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8136 | PyBytes_AsString(repunicode), repsize); |
| 8137 | *respos += repsize; |
| 8138 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8139 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8140 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8141 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8142 | /* generate replacement */ |
| 8143 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8144 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8145 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 8146 | if (x==enc_EXCEPTION) { |
| 8147 | return -1; |
| 8148 | } |
| 8149 | else if (x==enc_FAILED) { |
| 8150 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8151 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8152 | return -1; |
| 8153 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8154 | } |
| 8155 | *inpos = newpos; |
| 8156 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8157 | } |
| 8158 | return 0; |
| 8159 | } |
| 8160 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8161 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8162 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8163 | PyObject *mapping, |
| 8164 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8165 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8166 | /* output object */ |
| 8167 | PyObject *res = NULL; |
| 8168 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8169 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8170 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8171 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8172 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8173 | PyObject *errorHandler = NULL; |
| 8174 | PyObject *exc = NULL; |
| 8175 | /* the following variable is used for caching string comparisons |
| 8176 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8177 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8178 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8179 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8180 | if (PyUnicode_READY(unicode) < 0) |
| 8181 | return NULL; |
| 8182 | size = PyUnicode_GET_LENGTH(unicode); |
| 8183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | /* Default to Latin-1 */ |
| 8185 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8186 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | /* allocate enough for a simple encoding without |
| 8189 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8190 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8191 | if (res == NULL) |
| 8192 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8193 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8194 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8196 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8197 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8198 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8199 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8200 | if (x==enc_EXCEPTION) /* error */ |
| 8201 | goto onError; |
| 8202 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8203 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8204 | &exc, |
| 8205 | &known_errorHandler, &errorHandler, errors, |
| 8206 | &res, &respos)) { |
| 8207 | goto onError; |
| 8208 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8209 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | else |
| 8211 | /* done with this character => adjust input position */ |
| 8212 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8214 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8215 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8216 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8217 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8218 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8219 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8220 | Py_XDECREF(exc); |
| 8221 | Py_XDECREF(errorHandler); |
| 8222 | return res; |
| 8223 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8224 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8225 | Py_XDECREF(res); |
| 8226 | Py_XDECREF(exc); |
| 8227 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 | return NULL; |
| 8229 | } |
| 8230 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8231 | /* Deprecated */ |
| 8232 | PyObject * |
| 8233 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8234 | Py_ssize_t size, |
| 8235 | PyObject *mapping, |
| 8236 | const char *errors) |
| 8237 | { |
| 8238 | PyObject *result; |
| 8239 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8240 | if (unicode == NULL) |
| 8241 | return NULL; |
| 8242 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8243 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8244 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8245 | } |
| 8246 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8247 | PyObject * |
| 8248 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8249 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8250 | { |
| 8251 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | PyErr_BadArgument(); |
| 8253 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8254 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8255 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8256 | } |
| 8257 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8258 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8259 | static void |
| 8260 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8261 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8262 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8263 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8265 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8266 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8267 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8268 | } |
| 8269 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8270 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8271 | goto onError; |
| 8272 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8273 | goto onError; |
| 8274 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8275 | goto onError; |
| 8276 | return; |
| 8277 | onError: |
| 8278 | Py_DECREF(*exceptionObject); |
| 8279 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | } |
| 8281 | } |
| 8282 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8283 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8284 | static void |
| 8285 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8286 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8287 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8288 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8289 | { |
| 8290 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8291 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8292 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8293 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
| 8296 | /* error handling callback helper: |
| 8297 | build arguments, call the callback and check the arguments, |
| 8298 | put the result into newpos and return the replacement string, which |
| 8299 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8300 | static PyObject * |
| 8301 | unicode_translate_call_errorhandler(const char *errors, |
| 8302 | PyObject **errorHandler, |
| 8303 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8304 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8305 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8306 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8307 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8308 | 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] | 8309 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8310 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8311 | PyObject *restuple; |
| 8312 | PyObject *resunicode; |
| 8313 | |
| 8314 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8315 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8316 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8317 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
| 8320 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8321 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8322 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8324 | |
| 8325 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8327 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8328 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8330 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | Py_DECREF(restuple); |
| 8332 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8333 | } |
| 8334 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | &resunicode, &i_newpos)) { |
| 8336 | Py_DECREF(restuple); |
| 8337 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8338 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8339 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8340 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8341 | else |
| 8342 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8343 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8345 | Py_DECREF(restuple); |
| 8346 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8347 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8348 | Py_INCREF(resunicode); |
| 8349 | Py_DECREF(restuple); |
| 8350 | return resunicode; |
| 8351 | } |
| 8352 | |
| 8353 | /* Lookup the character ch in the mapping and put the result in result, |
| 8354 | which must be decrefed by the caller. |
| 8355 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8356 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8357 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8358 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8359 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8360 | PyObject *x; |
| 8361 | |
| 8362 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8364 | x = PyObject_GetItem(mapping, w); |
| 8365 | Py_DECREF(w); |
| 8366 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8367 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8368 | /* No mapping found means: use 1:1 mapping. */ |
| 8369 | PyErr_Clear(); |
| 8370 | *result = NULL; |
| 8371 | return 0; |
| 8372 | } else |
| 8373 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8374 | } |
| 8375 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8376 | *result = x; |
| 8377 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8378 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8379 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | long value = PyLong_AS_LONG(x); |
| 8381 | long max = PyUnicode_GetMax(); |
| 8382 | if (value < 0 || value > max) { |
| 8383 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8384 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8385 | Py_DECREF(x); |
| 8386 | return -1; |
| 8387 | } |
| 8388 | *result = x; |
| 8389 | return 0; |
| 8390 | } |
| 8391 | else if (PyUnicode_Check(x)) { |
| 8392 | *result = x; |
| 8393 | return 0; |
| 8394 | } |
| 8395 | else { |
| 8396 | /* wrong return value */ |
| 8397 | PyErr_SetString(PyExc_TypeError, |
| 8398 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8399 | Py_DECREF(x); |
| 8400 | return -1; |
| 8401 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8402 | } |
| 8403 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8404 | if not reallocate and adjust various state variables. |
| 8405 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8406 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8407 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8408 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8409 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8410 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8411 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8412 | /* exponentially overallocate to minimize reallocations */ |
| 8413 | if (requiredsize < 2 * oldsize) |
| 8414 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8415 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8416 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8417 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8418 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8419 | } |
| 8420 | return 0; |
| 8421 | } |
| 8422 | /* lookup the character, put the result in the output string and adjust |
| 8423 | various state variables. Return a new reference to the object that |
| 8424 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8425 | undefined (in which case no character was written). |
| 8426 | The called must decref result. |
| 8427 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8428 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8429 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8430 | PyObject *mapping, Py_UCS4 **output, |
| 8431 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8432 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8434 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8435 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8436 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8437 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8438 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8439 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8440 | } |
| 8441 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8443 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | /* 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] | 8445 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8446 | } |
| 8447 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8448 | Py_ssize_t repsize; |
| 8449 | if (PyUnicode_READY(*res) == -1) |
| 8450 | return -1; |
| 8451 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | if (repsize==1) { |
| 8453 | /* 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] | 8454 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8455 | } |
| 8456 | else if (repsize!=0) { |
| 8457 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8458 | Py_ssize_t requiredsize = *opos + |
| 8459 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8461 | Py_ssize_t i; |
| 8462 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | for(i = 0; i < repsize; i++) |
| 8465 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8467 | } |
| 8468 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8470 | return 0; |
| 8471 | } |
| 8472 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8473 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8474 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8475 | PyObject *mapping, |
| 8476 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8477 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8478 | /* input object */ |
| 8479 | char *idata; |
| 8480 | Py_ssize_t size, i; |
| 8481 | int kind; |
| 8482 | /* output buffer */ |
| 8483 | Py_UCS4 *output = NULL; |
| 8484 | Py_ssize_t osize; |
| 8485 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8486 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8487 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8488 | char *reason = "character maps to <undefined>"; |
| 8489 | PyObject *errorHandler = NULL; |
| 8490 | PyObject *exc = NULL; |
| 8491 | /* the following variable is used for caching string comparisons |
| 8492 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8493 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8494 | int known_errorHandler = -1; |
| 8495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8496 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | PyErr_BadArgument(); |
| 8498 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8499 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8500 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8501 | if (PyUnicode_READY(input) == -1) |
| 8502 | return NULL; |
| 8503 | idata = (char*)PyUnicode_DATA(input); |
| 8504 | kind = PyUnicode_KIND(input); |
| 8505 | size = PyUnicode_GET_LENGTH(input); |
| 8506 | i = 0; |
| 8507 | |
| 8508 | if (size == 0) { |
| 8509 | Py_INCREF(input); |
| 8510 | return input; |
| 8511 | } |
| 8512 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8513 | /* allocate enough for a simple 1:1 translation without |
| 8514 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 | osize = size; |
| 8516 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8517 | opos = 0; |
| 8518 | if (output == NULL) { |
| 8519 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8521 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8522 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8523 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8524 | /* try to encode it */ |
| 8525 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | if (charmaptranslate_output(input, i, mapping, |
| 8527 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8528 | Py_XDECREF(x); |
| 8529 | goto onError; |
| 8530 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8531 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8533 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8534 | else { /* untranslatable character */ |
| 8535 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8536 | Py_ssize_t repsize; |
| 8537 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8538 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8539 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8540 | Py_ssize_t collstart = i; |
| 8541 | Py_ssize_t collend = i+1; |
| 8542 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8544 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8545 | while (collend < size) { |
| 8546 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8547 | goto onError; |
| 8548 | Py_XDECREF(x); |
| 8549 | if (x!=Py_None) |
| 8550 | break; |
| 8551 | ++collend; |
| 8552 | } |
| 8553 | /* cache callback name lookup |
| 8554 | * (if not done yet, i.e. it's the first error) */ |
| 8555 | if (known_errorHandler==-1) { |
| 8556 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8557 | known_errorHandler = 1; |
| 8558 | else if (!strcmp(errors, "replace")) |
| 8559 | known_errorHandler = 2; |
| 8560 | else if (!strcmp(errors, "ignore")) |
| 8561 | known_errorHandler = 3; |
| 8562 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8563 | known_errorHandler = 4; |
| 8564 | else |
| 8565 | known_errorHandler = 0; |
| 8566 | } |
| 8567 | switch (known_errorHandler) { |
| 8568 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8569 | raise_translate_exception(&exc, input, collstart, |
| 8570 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8571 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8572 | case 2: /* replace */ |
| 8573 | /* 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] | 8574 | for (coll = collstart; coll<collend; coll++) |
| 8575 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8576 | /* fall through */ |
| 8577 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | break; |
| 8580 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8581 | /* generate replacement (temporarily (mis)uses i) */ |
| 8582 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8583 | char buffer[2+29+1+1]; |
| 8584 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8585 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8586 | if (charmaptranslate_makespace(&output, &osize, |
| 8587 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8588 | goto onError; |
| 8589 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8593 | break; |
| 8594 | default: |
| 8595 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8596 | reason, input, &exc, |
| 8597 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8598 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8599 | goto onError; |
| 8600 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8602 | if (charmaptranslate_makespace(&output, &osize, |
| 8603 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8604 | Py_DECREF(repunicode); |
| 8605 | goto onError; |
| 8606 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8607 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8608 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8609 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8611 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8612 | } |
| 8613 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8614 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8615 | if (!res) |
| 8616 | goto onError; |
| 8617 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8618 | Py_XDECREF(exc); |
| 8619 | Py_XDECREF(errorHandler); |
| 8620 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8621 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8622 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8623 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8624 | Py_XDECREF(exc); |
| 8625 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | return NULL; |
| 8627 | } |
| 8628 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8629 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8630 | PyObject * |
| 8631 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8632 | Py_ssize_t size, |
| 8633 | PyObject *mapping, |
| 8634 | const char *errors) |
| 8635 | { |
| 8636 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8637 | if (!unicode) |
| 8638 | return NULL; |
| 8639 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8640 | } |
| 8641 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8642 | PyObject * |
| 8643 | PyUnicode_Translate(PyObject *str, |
| 8644 | PyObject *mapping, |
| 8645 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8646 | { |
| 8647 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8649 | str = PyUnicode_FromObject(str); |
| 8650 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8653 | Py_DECREF(str); |
| 8654 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8655 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8656 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8657 | Py_XDECREF(str); |
| 8658 | return NULL; |
| 8659 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8661 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8662 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | { |
| 8664 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8665 | called as a callback from fixup() which does it already. */ |
| 8666 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8667 | const int kind = PyUnicode_KIND(self); |
| 8668 | void *data = PyUnicode_DATA(self); |
| 8669 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8670 | Py_ssize_t i; |
| 8671 | |
| 8672 | for (i = 0; i < len; ++i) { |
| 8673 | ch = PyUnicode_READ(kind, data, i); |
| 8674 | fixed = 0; |
| 8675 | if (ch > 127) { |
| 8676 | if (Py_UNICODE_ISSPACE(ch)) |
| 8677 | fixed = ' '; |
| 8678 | else { |
| 8679 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8680 | if (decimal >= 0) |
| 8681 | fixed = '0' + decimal; |
| 8682 | } |
| 8683 | if (fixed != 0) { |
| 8684 | if (fixed > maxchar) |
| 8685 | maxchar = fixed; |
| 8686 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8687 | } |
| 8688 | else if (ch > maxchar) |
| 8689 | maxchar = ch; |
| 8690 | } |
| 8691 | else if (ch > maxchar) |
| 8692 | maxchar = ch; |
| 8693 | } |
| 8694 | |
| 8695 | return maxchar; |
| 8696 | } |
| 8697 | |
| 8698 | PyObject * |
| 8699 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8700 | { |
| 8701 | if (!PyUnicode_Check(unicode)) { |
| 8702 | PyErr_BadInternalCall(); |
| 8703 | return NULL; |
| 8704 | } |
| 8705 | if (PyUnicode_READY(unicode) == -1) |
| 8706 | return NULL; |
| 8707 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8708 | /* If the string is already ASCII, just return the same string */ |
| 8709 | Py_INCREF(unicode); |
| 8710 | return unicode; |
| 8711 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8712 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | } |
| 8714 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8715 | PyObject * |
| 8716 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8717 | Py_ssize_t length) |
| 8718 | { |
| 8719 | PyObject *result; |
| 8720 | Py_UNICODE *p; /* write pointer into result */ |
| 8721 | Py_ssize_t i; |
| 8722 | /* Copy to a new string */ |
| 8723 | result = (PyObject *)_PyUnicode_New(length); |
| 8724 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8725 | if (result == NULL) |
| 8726 | return result; |
| 8727 | p = PyUnicode_AS_UNICODE(result); |
| 8728 | /* Iterate over code points */ |
| 8729 | for (i = 0; i < length; i++) { |
| 8730 | Py_UNICODE ch =s[i]; |
| 8731 | if (ch > 127) { |
| 8732 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8733 | if (decimal >= 0) |
| 8734 | p[i] = '0' + decimal; |
| 8735 | } |
| 8736 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8737 | #ifndef DONT_MAKE_RESULT_READY |
| 8738 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | Py_DECREF(result); |
| 8740 | return NULL; |
| 8741 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8742 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8743 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8744 | return result; |
| 8745 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8746 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8747 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8748 | int |
| 8749 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8750 | Py_ssize_t length, |
| 8751 | char *output, |
| 8752 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8753 | { |
| 8754 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8755 | PyObject *errorHandler = NULL; |
| 8756 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8757 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8758 | const char *encoding = "decimal"; |
| 8759 | const char *reason = "invalid decimal Unicode string"; |
| 8760 | /* the following variable is used for caching string comparisons |
| 8761 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8762 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8763 | |
| 8764 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | PyErr_BadArgument(); |
| 8766 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8767 | } |
| 8768 | |
| 8769 | p = s; |
| 8770 | end = s + length; |
| 8771 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8772 | register Py_UNICODE ch = *p; |
| 8773 | int decimal; |
| 8774 | PyObject *repunicode; |
| 8775 | Py_ssize_t repsize; |
| 8776 | Py_ssize_t newpos; |
| 8777 | Py_UNICODE *uni2; |
| 8778 | Py_UNICODE *collstart; |
| 8779 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8780 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8781 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8782 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8783 | ++p; |
| 8784 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8785 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8786 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8787 | if (decimal >= 0) { |
| 8788 | *output++ = '0' + decimal; |
| 8789 | ++p; |
| 8790 | continue; |
| 8791 | } |
| 8792 | if (0 < ch && ch < 256) { |
| 8793 | *output++ = (char)ch; |
| 8794 | ++p; |
| 8795 | continue; |
| 8796 | } |
| 8797 | /* All other characters are considered unencodable */ |
| 8798 | collstart = p; |
| 8799 | collend = p+1; |
| 8800 | while (collend < end) { |
| 8801 | if ((0 < *collend && *collend < 256) || |
| 8802 | !Py_UNICODE_ISSPACE(*collend) || |
| 8803 | Py_UNICODE_TODECIMAL(*collend)) |
| 8804 | break; |
| 8805 | } |
| 8806 | /* cache callback name lookup |
| 8807 | * (if not done yet, i.e. it's the first error) */ |
| 8808 | if (known_errorHandler==-1) { |
| 8809 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8810 | known_errorHandler = 1; |
| 8811 | else if (!strcmp(errors, "replace")) |
| 8812 | known_errorHandler = 2; |
| 8813 | else if (!strcmp(errors, "ignore")) |
| 8814 | known_errorHandler = 3; |
| 8815 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8816 | known_errorHandler = 4; |
| 8817 | else |
| 8818 | known_errorHandler = 0; |
| 8819 | } |
| 8820 | switch (known_errorHandler) { |
| 8821 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8822 | unicode = PyUnicode_FromUnicode(s, length); |
| 8823 | if (unicode == NULL) |
| 8824 | goto onError; |
| 8825 | raise_encode_exception(&exc, encoding, unicode, collstart-s, collend-s, reason); |
| 8826 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8827 | goto onError; |
| 8828 | case 2: /* replace */ |
| 8829 | for (p = collstart; p < collend; ++p) |
| 8830 | *output++ = '?'; |
| 8831 | /* fall through */ |
| 8832 | case 3: /* ignore */ |
| 8833 | p = collend; |
| 8834 | break; |
| 8835 | case 4: /* xmlcharrefreplace */ |
| 8836 | /* generate replacement (temporarily (mis)uses p) */ |
| 8837 | for (p = collstart; p < collend; ++p) |
| 8838 | output += sprintf(output, "&#%d;", (int)*p); |
| 8839 | p = collend; |
| 8840 | break; |
| 8841 | default: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8842 | unicode = PyUnicode_FromUnicode(s, length); |
| 8843 | if (unicode == NULL) |
| 8844 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8845 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8846 | encoding, reason, unicode, &exc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8847 | collstart-s, collend-s, &newpos); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8848 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8849 | if (repunicode == NULL) |
| 8850 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8851 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8852 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8853 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8854 | Py_DECREF(repunicode); |
| 8855 | goto onError; |
| 8856 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8857 | /* generate replacement */ |
| 8858 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8859 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8860 | Py_UNICODE ch = *uni2; |
| 8861 | if (Py_UNICODE_ISSPACE(ch)) |
| 8862 | *output++ = ' '; |
| 8863 | else { |
| 8864 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8865 | if (decimal >= 0) |
| 8866 | *output++ = '0' + decimal; |
| 8867 | else if (0 < ch && ch < 256) |
| 8868 | *output++ = (char)ch; |
| 8869 | else { |
| 8870 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8871 | unicode = PyUnicode_FromUnicode(s, length); |
| 8872 | if (unicode == NULL) |
| 8873 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8874 | raise_encode_exception(&exc, encoding, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8875 | unicode, collstart-s, collend-s, reason); |
| 8876 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8877 | goto onError; |
| 8878 | } |
| 8879 | } |
| 8880 | } |
| 8881 | p = s + newpos; |
| 8882 | Py_DECREF(repunicode); |
| 8883 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8884 | } |
| 8885 | /* 0-terminate the output string */ |
| 8886 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8887 | Py_XDECREF(exc); |
| 8888 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8889 | return 0; |
| 8890 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8891 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8892 | Py_XDECREF(exc); |
| 8893 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8894 | return -1; |
| 8895 | } |
| 8896 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8897 | /* --- Helpers ------------------------------------------------------------ */ |
| 8898 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8899 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8900 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | Py_ssize_t start, |
| 8902 | Py_ssize_t end) |
| 8903 | { |
| 8904 | int kind1, kind2, kind; |
| 8905 | void *buf1, *buf2; |
| 8906 | Py_ssize_t len1, len2, result; |
| 8907 | |
| 8908 | kind1 = PyUnicode_KIND(s1); |
| 8909 | kind2 = PyUnicode_KIND(s2); |
| 8910 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8911 | buf1 = PyUnicode_DATA(s1); |
| 8912 | buf2 = PyUnicode_DATA(s2); |
| 8913 | if (kind1 != kind) |
| 8914 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8915 | if (!buf1) |
| 8916 | return -2; |
| 8917 | if (kind2 != kind) |
| 8918 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8919 | if (!buf2) { |
| 8920 | if (kind1 != kind) PyMem_Free(buf1); |
| 8921 | return -2; |
| 8922 | } |
| 8923 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8924 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8925 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8926 | if (direction > 0) { |
| 8927 | switch(kind) { |
| 8928 | case PyUnicode_1BYTE_KIND: |
| 8929 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8930 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8931 | else |
| 8932 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8933 | break; |
| 8934 | case PyUnicode_2BYTE_KIND: |
| 8935 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8936 | break; |
| 8937 | case PyUnicode_4BYTE_KIND: |
| 8938 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8939 | break; |
| 8940 | default: |
| 8941 | assert(0); result = -2; |
| 8942 | } |
| 8943 | } |
| 8944 | else { |
| 8945 | switch(kind) { |
| 8946 | case PyUnicode_1BYTE_KIND: |
| 8947 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8948 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8949 | else |
| 8950 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8951 | break; |
| 8952 | case PyUnicode_2BYTE_KIND: |
| 8953 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8954 | break; |
| 8955 | case PyUnicode_4BYTE_KIND: |
| 8956 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8957 | break; |
| 8958 | default: |
| 8959 | assert(0); result = -2; |
| 8960 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8961 | } |
| 8962 | |
| 8963 | if (kind1 != kind) |
| 8964 | PyMem_Free(buf1); |
| 8965 | if (kind2 != kind) |
| 8966 | PyMem_Free(buf2); |
| 8967 | |
| 8968 | return result; |
| 8969 | } |
| 8970 | |
| 8971 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8972 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | Py_ssize_t n_buffer, |
| 8974 | void *digits, Py_ssize_t n_digits, |
| 8975 | Py_ssize_t min_width, |
| 8976 | const char *grouping, |
| 8977 | const char *thousands_sep) |
| 8978 | { |
| 8979 | switch(kind) { |
| 8980 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8981 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8982 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8983 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8984 | min_width, grouping, thousands_sep); |
| 8985 | else |
| 8986 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8987 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8988 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8989 | case PyUnicode_2BYTE_KIND: |
| 8990 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8991 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8992 | min_width, grouping, thousands_sep); |
| 8993 | case PyUnicode_4BYTE_KIND: |
| 8994 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8995 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8996 | min_width, grouping, thousands_sep); |
| 8997 | } |
| 8998 | assert(0); |
| 8999 | return -1; |
| 9000 | } |
| 9001 | |
| 9002 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9003 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9004 | #define ADJUST_INDICES(start, end, len) \ |
| 9005 | if (end > len) \ |
| 9006 | end = len; \ |
| 9007 | else if (end < 0) { \ |
| 9008 | end += len; \ |
| 9009 | if (end < 0) \ |
| 9010 | end = 0; \ |
| 9011 | } \ |
| 9012 | if (start < 0) { \ |
| 9013 | start += len; \ |
| 9014 | if (start < 0) \ |
| 9015 | start = 0; \ |
| 9016 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9017 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9018 | Py_ssize_t |
| 9019 | PyUnicode_Count(PyObject *str, |
| 9020 | PyObject *substr, |
| 9021 | Py_ssize_t start, |
| 9022 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9024 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9025 | PyObject* str_obj; |
| 9026 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9027 | int kind1, kind2, kind; |
| 9028 | void *buf1 = NULL, *buf2 = NULL; |
| 9029 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9030 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9031 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9032 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9033 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9034 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9035 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9036 | Py_DECREF(str_obj); |
| 9037 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9038 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9039 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9040 | kind1 = PyUnicode_KIND(str_obj); |
| 9041 | kind2 = PyUnicode_KIND(sub_obj); |
| 9042 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9043 | buf1 = PyUnicode_DATA(str_obj); |
| 9044 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9045 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9046 | if (!buf1) |
| 9047 | goto onError; |
| 9048 | buf2 = PyUnicode_DATA(sub_obj); |
| 9049 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9050 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | if (!buf2) |
| 9052 | goto onError; |
| 9053 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9054 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9055 | |
| 9056 | ADJUST_INDICES(start, end, len1); |
| 9057 | switch(kind) { |
| 9058 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9059 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9060 | result = asciilib_count( |
| 9061 | ((Py_UCS1*)buf1) + start, end - start, |
| 9062 | buf2, len2, PY_SSIZE_T_MAX |
| 9063 | ); |
| 9064 | else |
| 9065 | result = ucs1lib_count( |
| 9066 | ((Py_UCS1*)buf1) + start, end - start, |
| 9067 | buf2, len2, PY_SSIZE_T_MAX |
| 9068 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9069 | break; |
| 9070 | case PyUnicode_2BYTE_KIND: |
| 9071 | result = ucs2lib_count( |
| 9072 | ((Py_UCS2*)buf1) + start, end - start, |
| 9073 | buf2, len2, PY_SSIZE_T_MAX |
| 9074 | ); |
| 9075 | break; |
| 9076 | case PyUnicode_4BYTE_KIND: |
| 9077 | result = ucs4lib_count( |
| 9078 | ((Py_UCS4*)buf1) + start, end - start, |
| 9079 | buf2, len2, PY_SSIZE_T_MAX |
| 9080 | ); |
| 9081 | break; |
| 9082 | default: |
| 9083 | assert(0); result = 0; |
| 9084 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9085 | |
| 9086 | Py_DECREF(sub_obj); |
| 9087 | Py_DECREF(str_obj); |
| 9088 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9089 | if (kind1 != kind) |
| 9090 | PyMem_Free(buf1); |
| 9091 | if (kind2 != kind) |
| 9092 | PyMem_Free(buf2); |
| 9093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9094 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9095 | onError: |
| 9096 | Py_DECREF(sub_obj); |
| 9097 | Py_DECREF(str_obj); |
| 9098 | if (kind1 != kind && buf1) |
| 9099 | PyMem_Free(buf1); |
| 9100 | if (kind2 != kind && buf2) |
| 9101 | PyMem_Free(buf2); |
| 9102 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | } |
| 9104 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9105 | Py_ssize_t |
| 9106 | PyUnicode_Find(PyObject *str, |
| 9107 | PyObject *sub, |
| 9108 | Py_ssize_t start, |
| 9109 | Py_ssize_t end, |
| 9110 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9112 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9114 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9115 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9116 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9117 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9119 | Py_DECREF(str); |
| 9120 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9122 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9123 | result = any_find_slice(direction, |
| 9124 | str, sub, start, end |
| 9125 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9128 | Py_DECREF(sub); |
| 9129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | return result; |
| 9131 | } |
| 9132 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9133 | Py_ssize_t |
| 9134 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9135 | Py_ssize_t start, Py_ssize_t end, |
| 9136 | int direction) |
| 9137 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9138 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9139 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9140 | if (PyUnicode_READY(str) == -1) |
| 9141 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9142 | if (start < 0 || end < 0) { |
| 9143 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9144 | return -2; |
| 9145 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9146 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9147 | end = PyUnicode_GET_LENGTH(str); |
| 9148 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9149 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9150 | kind, end-start, ch, direction); |
| 9151 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9152 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9153 | else |
| 9154 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9155 | } |
| 9156 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9157 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9158 | tailmatch(PyObject *self, |
| 9159 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9160 | Py_ssize_t start, |
| 9161 | Py_ssize_t end, |
| 9162 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9163 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9164 | int kind_self; |
| 9165 | int kind_sub; |
| 9166 | void *data_self; |
| 9167 | void *data_sub; |
| 9168 | Py_ssize_t offset; |
| 9169 | Py_ssize_t i; |
| 9170 | Py_ssize_t end_sub; |
| 9171 | |
| 9172 | if (PyUnicode_READY(self) == -1 || |
| 9173 | PyUnicode_READY(substring) == -1) |
| 9174 | return 0; |
| 9175 | |
| 9176 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9177 | return 1; |
| 9178 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9179 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9180 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9181 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9182 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9183 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9184 | kind_self = PyUnicode_KIND(self); |
| 9185 | data_self = PyUnicode_DATA(self); |
| 9186 | kind_sub = PyUnicode_KIND(substring); |
| 9187 | data_sub = PyUnicode_DATA(substring); |
| 9188 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9189 | |
| 9190 | if (direction > 0) |
| 9191 | offset = end; |
| 9192 | else |
| 9193 | offset = start; |
| 9194 | |
| 9195 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9196 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9197 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9198 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9199 | /* If both are of the same kind, memcmp is sufficient */ |
| 9200 | if (kind_self == kind_sub) { |
| 9201 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9202 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9203 | data_sub, |
| 9204 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9205 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9206 | } |
| 9207 | /* otherwise we have to compare each character by first accesing it */ |
| 9208 | else { |
| 9209 | /* We do not need to compare 0 and len(substring)-1 because |
| 9210 | the if statement above ensured already that they are equal |
| 9211 | when we end up here. */ |
| 9212 | // TODO: honor direction and do a forward or backwards search |
| 9213 | for (i = 1; i < end_sub; ++i) { |
| 9214 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9215 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9216 | return 0; |
| 9217 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9218 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9219 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9220 | } |
| 9221 | |
| 9222 | return 0; |
| 9223 | } |
| 9224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9225 | Py_ssize_t |
| 9226 | PyUnicode_Tailmatch(PyObject *str, |
| 9227 | PyObject *substr, |
| 9228 | Py_ssize_t start, |
| 9229 | Py_ssize_t end, |
| 9230 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9231 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9232 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9233 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9234 | str = PyUnicode_FromObject(str); |
| 9235 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9236 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9237 | substr = PyUnicode_FromObject(substr); |
| 9238 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9239 | Py_DECREF(str); |
| 9240 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9241 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9242 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9243 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9244 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9245 | Py_DECREF(str); |
| 9246 | Py_DECREF(substr); |
| 9247 | return result; |
| 9248 | } |
| 9249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | /* Apply fixfct filter to the Unicode object self and return a |
| 9251 | reference to the modified object */ |
| 9252 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9253 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9254 | fixup(PyObject *self, |
| 9255 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9256 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9257 | PyObject *u; |
| 9258 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9259 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9260 | if (PyUnicode_READY(self) == -1) |
| 9261 | return NULL; |
| 9262 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 9263 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 9264 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9265 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9266 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9267 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9268 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9269 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9271 | /* fix functions return the new maximum character in a string, |
| 9272 | if the kind of the resulting unicode object does not change, |
| 9273 | everything is fine. Otherwise we need to change the string kind |
| 9274 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9275 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9276 | if (maxchar_new == 0) |
| 9277 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9278 | else if (maxchar_new <= 127) |
| 9279 | maxchar_new = 127; |
| 9280 | else if (maxchar_new <= 255) |
| 9281 | maxchar_new = 255; |
| 9282 | else if (maxchar_new <= 65535) |
| 9283 | maxchar_new = 65535; |
| 9284 | else |
| 9285 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9286 | |
| 9287 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9288 | /* fixfct should return TRUE if it modified the buffer. If |
| 9289 | FALSE, return a reference to the original buffer instead |
| 9290 | (to save space, not time) */ |
| 9291 | Py_INCREF(self); |
| 9292 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9293 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9294 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | else if (maxchar_new == maxchar_old) { |
| 9296 | return u; |
| 9297 | } |
| 9298 | else { |
| 9299 | /* In case the maximum character changed, we need to |
| 9300 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9301 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9302 | if (v == NULL) { |
| 9303 | Py_DECREF(u); |
| 9304 | return NULL; |
| 9305 | } |
| 9306 | if (maxchar_new > maxchar_old) { |
| 9307 | /* If the maxchar increased so that the kind changed, not all |
| 9308 | characters are representable anymore and we need to fix the |
| 9309 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9310 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9311 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9313 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9314 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9315 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9316 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9317 | |
| 9318 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9319 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9320 | return v; |
| 9321 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9322 | } |
| 9323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9324 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9325 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9326 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9327 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9328 | called as a callback from fixup() which does it already. */ |
| 9329 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9330 | const int kind = PyUnicode_KIND(self); |
| 9331 | void *data = PyUnicode_DATA(self); |
| 9332 | int touched = 0; |
| 9333 | Py_UCS4 maxchar = 0; |
| 9334 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | for (i = 0; i < len; ++i) { |
| 9337 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9338 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9339 | if (up != ch) { |
| 9340 | if (up > maxchar) |
| 9341 | maxchar = up; |
| 9342 | PyUnicode_WRITE(kind, data, i, up); |
| 9343 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9344 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9345 | else if (ch > maxchar) |
| 9346 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9347 | } |
| 9348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9349 | if (touched) |
| 9350 | return maxchar; |
| 9351 | else |
| 9352 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9353 | } |
| 9354 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9355 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9356 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9357 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9358 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9359 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9360 | const int kind = PyUnicode_KIND(self); |
| 9361 | void *data = PyUnicode_DATA(self); |
| 9362 | int touched = 0; |
| 9363 | Py_UCS4 maxchar = 0; |
| 9364 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9365 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9366 | for(i = 0; i < len; ++i) { |
| 9367 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9368 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9369 | if (lo != ch) { |
| 9370 | if (lo > maxchar) |
| 9371 | maxchar = lo; |
| 9372 | PyUnicode_WRITE(kind, data, i, lo); |
| 9373 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9374 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9375 | else if (ch > maxchar) |
| 9376 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 | } |
| 9378 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9379 | if (touched) |
| 9380 | return maxchar; |
| 9381 | else |
| 9382 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9383 | } |
| 9384 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9385 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9386 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9387 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9388 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9389 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9390 | const int kind = PyUnicode_KIND(self); |
| 9391 | void *data = PyUnicode_DATA(self); |
| 9392 | int touched = 0; |
| 9393 | Py_UCS4 maxchar = 0; |
| 9394 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9396 | for(i = 0; i < len; ++i) { |
| 9397 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9398 | Py_UCS4 nu = 0; |
| 9399 | |
| 9400 | if (Py_UNICODE_ISUPPER(ch)) |
| 9401 | nu = Py_UNICODE_TOLOWER(ch); |
| 9402 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9403 | nu = Py_UNICODE_TOUPPER(ch); |
| 9404 | |
| 9405 | if (nu != 0) { |
| 9406 | if (nu > maxchar) |
| 9407 | maxchar = nu; |
| 9408 | PyUnicode_WRITE(kind, data, i, nu); |
| 9409 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9410 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9411 | else if (ch > maxchar) |
| 9412 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | } |
| 9414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9415 | if (touched) |
| 9416 | return maxchar; |
| 9417 | else |
| 9418 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9419 | } |
| 9420 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9421 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9422 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9423 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9424 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9425 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9426 | const int kind = PyUnicode_KIND(self); |
| 9427 | void *data = PyUnicode_DATA(self); |
| 9428 | int touched = 0; |
| 9429 | Py_UCS4 maxchar = 0; |
| 9430 | Py_ssize_t i = 0; |
| 9431 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9432 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9433 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9434 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9435 | |
| 9436 | ch = PyUnicode_READ(kind, data, i); |
| 9437 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9438 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9439 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9440 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9441 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9442 | ++i; |
| 9443 | for(; i < len; ++i) { |
| 9444 | ch = PyUnicode_READ(kind, data, i); |
| 9445 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9446 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9447 | if (lo > maxchar) |
| 9448 | maxchar = lo; |
| 9449 | PyUnicode_WRITE(kind, data, i, lo); |
| 9450 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9451 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9452 | else if (ch > maxchar) |
| 9453 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9454 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | |
| 9456 | if (touched) |
| 9457 | return maxchar; |
| 9458 | else |
| 9459 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9460 | } |
| 9461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9462 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9463 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9464 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9466 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9467 | const int kind = PyUnicode_KIND(self); |
| 9468 | void *data = PyUnicode_DATA(self); |
| 9469 | Py_UCS4 maxchar = 0; |
| 9470 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9471 | int previous_is_cased; |
| 9472 | |
| 9473 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9474 | if (len == 1) { |
| 9475 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9476 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9477 | if (ti != ch) { |
| 9478 | PyUnicode_WRITE(kind, data, i, ti); |
| 9479 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9480 | } |
| 9481 | else |
| 9482 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9485 | for(; i < len; ++i) { |
| 9486 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9487 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9488 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9489 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9490 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9491 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9492 | nu = Py_UNICODE_TOTITLE(ch); |
| 9493 | |
| 9494 | if (nu > maxchar) |
| 9495 | maxchar = nu; |
| 9496 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9497 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9498 | if (Py_UNICODE_ISLOWER(ch) || |
| 9499 | Py_UNICODE_ISUPPER(ch) || |
| 9500 | Py_UNICODE_ISTITLE(ch)) |
| 9501 | previous_is_cased = 1; |
| 9502 | else |
| 9503 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9505 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | } |
| 9507 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9508 | PyObject * |
| 9509 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9510 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9511 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9512 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9513 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9514 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9515 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9516 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9517 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9518 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9519 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9520 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9521 | int use_memcpy; |
| 9522 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9523 | PyObject *last_obj; |
| 9524 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9525 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9526 | fseq = PySequence_Fast(seq, ""); |
| 9527 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9528 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9529 | } |
| 9530 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9531 | /* NOTE: the following code can't call back into Python code, |
| 9532 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9533 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9534 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9535 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9536 | /* If empty sequence, return u"". */ |
| 9537 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9538 | Py_DECREF(fseq); |
| 9539 | Py_INCREF(unicode_empty); |
| 9540 | res = unicode_empty; |
| 9541 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9542 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9543 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9544 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9545 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9546 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9547 | if (seqlen == 1) { |
| 9548 | if (PyUnicode_CheckExact(items[0])) { |
| 9549 | res = items[0]; |
| 9550 | Py_INCREF(res); |
| 9551 | Py_DECREF(fseq); |
| 9552 | return res; |
| 9553 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9554 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9555 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9556 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9557 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9558 | /* Set up sep and seplen */ |
| 9559 | if (separator == NULL) { |
| 9560 | /* fall back to a blank space separator */ |
| 9561 | sep = PyUnicode_FromOrdinal(' '); |
| 9562 | if (!sep) |
| 9563 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9564 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9565 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9566 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9567 | else { |
| 9568 | if (!PyUnicode_Check(separator)) { |
| 9569 | PyErr_Format(PyExc_TypeError, |
| 9570 | "separator: expected str instance," |
| 9571 | " %.80s found", |
| 9572 | Py_TYPE(separator)->tp_name); |
| 9573 | goto onError; |
| 9574 | } |
| 9575 | if (PyUnicode_READY(separator)) |
| 9576 | goto onError; |
| 9577 | sep = separator; |
| 9578 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9579 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9580 | /* inc refcount to keep this code path symmetric with the |
| 9581 | above case of a blank separator */ |
| 9582 | Py_INCREF(sep); |
| 9583 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9584 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9585 | } |
| 9586 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9587 | /* There are at least two things to join, or else we have a subclass |
| 9588 | * of str in the sequence. |
| 9589 | * Do a pre-pass to figure out the total amount of space we'll |
| 9590 | * need (sz), and see whether all argument are strings. |
| 9591 | */ |
| 9592 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9593 | #ifdef Py_DEBUG |
| 9594 | use_memcpy = 0; |
| 9595 | #else |
| 9596 | use_memcpy = 1; |
| 9597 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9598 | for (i = 0; i < seqlen; i++) { |
| 9599 | const Py_ssize_t old_sz = sz; |
| 9600 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9601 | if (!PyUnicode_Check(item)) { |
| 9602 | PyErr_Format(PyExc_TypeError, |
| 9603 | "sequence item %zd: expected str instance," |
| 9604 | " %.80s found", |
| 9605 | i, Py_TYPE(item)->tp_name); |
| 9606 | goto onError; |
| 9607 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9608 | if (PyUnicode_READY(item) == -1) |
| 9609 | goto onError; |
| 9610 | sz += PyUnicode_GET_LENGTH(item); |
| 9611 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9612 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9613 | if (i != 0) |
| 9614 | sz += seplen; |
| 9615 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9616 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9617 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9618 | goto onError; |
| 9619 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9620 | if (use_memcpy && last_obj != NULL) { |
| 9621 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9622 | use_memcpy = 0; |
| 9623 | } |
| 9624 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9625 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9626 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9627 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9628 | if (res == NULL) |
| 9629 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9630 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9631 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9632 | #ifdef Py_DEBUG |
| 9633 | use_memcpy = 0; |
| 9634 | #else |
| 9635 | if (use_memcpy) { |
| 9636 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9637 | kind = PyUnicode_KIND(res); |
| 9638 | if (seplen != 0) |
| 9639 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9640 | } |
| 9641 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9642 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9643 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9644 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9645 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9646 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9647 | if (use_memcpy) { |
| 9648 | Py_MEMCPY(res_data, |
| 9649 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9650 | kind * seplen); |
| 9651 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9652 | } |
| 9653 | else { |
| 9654 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9655 | res_offset += seplen; |
| 9656 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9657 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9658 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9659 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9660 | if (use_memcpy) { |
| 9661 | Py_MEMCPY(res_data, |
| 9662 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9663 | kind * itemlen); |
| 9664 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9665 | } |
| 9666 | else { |
| 9667 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9668 | res_offset += itemlen; |
| 9669 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9670 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9671 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9672 | if (use_memcpy) |
| 9673 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9674 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9675 | else |
| 9676 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9677 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9678 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9679 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9680 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9681 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9683 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9684 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9686 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9687 | return NULL; |
| 9688 | } |
| 9689 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9690 | #define FILL(kind, data, value, start, length) \ |
| 9691 | do { \ |
| 9692 | Py_ssize_t i_ = 0; \ |
| 9693 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9694 | switch ((kind)) { \ |
| 9695 | case PyUnicode_1BYTE_KIND: { \ |
| 9696 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9697 | memset(to_, (unsigned char)value, length); \ |
| 9698 | break; \ |
| 9699 | } \ |
| 9700 | case PyUnicode_2BYTE_KIND: { \ |
| 9701 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9702 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9703 | break; \ |
| 9704 | } \ |
| 9705 | default: { \ |
| 9706 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9707 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9708 | break; \ |
| 9709 | } \ |
| 9710 | } \ |
| 9711 | } while (0) |
| 9712 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9713 | static PyObject * |
| 9714 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9715 | Py_ssize_t left, |
| 9716 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9717 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9718 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9719 | PyObject *u; |
| 9720 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9721 | int kind; |
| 9722 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9723 | |
| 9724 | if (left < 0) |
| 9725 | left = 0; |
| 9726 | if (right < 0) |
| 9727 | right = 0; |
| 9728 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9729 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9730 | Py_INCREF(self); |
| 9731 | return self; |
| 9732 | } |
| 9733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9734 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9735 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9736 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9737 | return NULL; |
| 9738 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9739 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9740 | if (fill > maxchar) |
| 9741 | maxchar = fill; |
| 9742 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9743 | if (!u) |
| 9744 | return NULL; |
| 9745 | |
| 9746 | kind = PyUnicode_KIND(u); |
| 9747 | data = PyUnicode_DATA(u); |
| 9748 | if (left) |
| 9749 | FILL(kind, data, fill, 0, left); |
| 9750 | if (right) |
| 9751 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9752 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9753 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9754 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9755 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9757 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9758 | PyObject * |
| 9759 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9760 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9761 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | |
| 9763 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9764 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9765 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9767 | switch(PyUnicode_KIND(string)) { |
| 9768 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9769 | if (PyUnicode_IS_ASCII(string)) |
| 9770 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9771 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9772 | PyUnicode_GET_LENGTH(string), keepends); |
| 9773 | else |
| 9774 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9775 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9776 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9777 | break; |
| 9778 | case PyUnicode_2BYTE_KIND: |
| 9779 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9780 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9781 | PyUnicode_GET_LENGTH(string), keepends); |
| 9782 | break; |
| 9783 | case PyUnicode_4BYTE_KIND: |
| 9784 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9785 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9786 | PyUnicode_GET_LENGTH(string), keepends); |
| 9787 | break; |
| 9788 | default: |
| 9789 | assert(0); |
| 9790 | list = 0; |
| 9791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9792 | Py_DECREF(string); |
| 9793 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9794 | } |
| 9795 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9796 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9797 | split(PyObject *self, |
| 9798 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9799 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9801 | int kind1, kind2, kind; |
| 9802 | void *buf1, *buf2; |
| 9803 | Py_ssize_t len1, len2; |
| 9804 | PyObject* out; |
| 9805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9806 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9807 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9808 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9809 | if (PyUnicode_READY(self) == -1) |
| 9810 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9811 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | if (substring == NULL) |
| 9813 | switch(PyUnicode_KIND(self)) { |
| 9814 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9815 | if (PyUnicode_IS_ASCII(self)) |
| 9816 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9817 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9818 | PyUnicode_GET_LENGTH(self), maxcount |
| 9819 | ); |
| 9820 | else |
| 9821 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9822 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9823 | PyUnicode_GET_LENGTH(self), maxcount |
| 9824 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9825 | case PyUnicode_2BYTE_KIND: |
| 9826 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9827 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9828 | PyUnicode_GET_LENGTH(self), maxcount |
| 9829 | ); |
| 9830 | case PyUnicode_4BYTE_KIND: |
| 9831 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9832 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | PyUnicode_GET_LENGTH(self), maxcount |
| 9834 | ); |
| 9835 | default: |
| 9836 | assert(0); |
| 9837 | return NULL; |
| 9838 | } |
| 9839 | |
| 9840 | if (PyUnicode_READY(substring) == -1) |
| 9841 | return NULL; |
| 9842 | |
| 9843 | kind1 = PyUnicode_KIND(self); |
| 9844 | kind2 = PyUnicode_KIND(substring); |
| 9845 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9846 | buf1 = PyUnicode_DATA(self); |
| 9847 | buf2 = PyUnicode_DATA(substring); |
| 9848 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9849 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9850 | if (!buf1) |
| 9851 | return NULL; |
| 9852 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9853 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9854 | if (!buf2) { |
| 9855 | if (kind1 != kind) PyMem_Free(buf1); |
| 9856 | return NULL; |
| 9857 | } |
| 9858 | len1 = PyUnicode_GET_LENGTH(self); |
| 9859 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9860 | |
| 9861 | switch(kind) { |
| 9862 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9863 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9864 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9865 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9866 | else |
| 9867 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9868 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | break; |
| 9870 | case PyUnicode_2BYTE_KIND: |
| 9871 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9872 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | break; |
| 9874 | case PyUnicode_4BYTE_KIND: |
| 9875 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9876 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9877 | break; |
| 9878 | default: |
| 9879 | out = NULL; |
| 9880 | } |
| 9881 | if (kind1 != kind) |
| 9882 | PyMem_Free(buf1); |
| 9883 | if (kind2 != kind) |
| 9884 | PyMem_Free(buf2); |
| 9885 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9888 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9889 | rsplit(PyObject *self, |
| 9890 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9891 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9892 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9893 | int kind1, kind2, kind; |
| 9894 | void *buf1, *buf2; |
| 9895 | Py_ssize_t len1, len2; |
| 9896 | PyObject* out; |
| 9897 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9898 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9899 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9900 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9901 | if (PyUnicode_READY(self) == -1) |
| 9902 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9903 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | if (substring == NULL) |
| 9905 | switch(PyUnicode_KIND(self)) { |
| 9906 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9907 | if (PyUnicode_IS_ASCII(self)) |
| 9908 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9909 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9910 | PyUnicode_GET_LENGTH(self), maxcount |
| 9911 | ); |
| 9912 | else |
| 9913 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9914 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9915 | PyUnicode_GET_LENGTH(self), maxcount |
| 9916 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | case PyUnicode_2BYTE_KIND: |
| 9918 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9919 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | PyUnicode_GET_LENGTH(self), maxcount |
| 9921 | ); |
| 9922 | case PyUnicode_4BYTE_KIND: |
| 9923 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9924 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | PyUnicode_GET_LENGTH(self), maxcount |
| 9926 | ); |
| 9927 | default: |
| 9928 | assert(0); |
| 9929 | return NULL; |
| 9930 | } |
| 9931 | |
| 9932 | if (PyUnicode_READY(substring) == -1) |
| 9933 | return NULL; |
| 9934 | |
| 9935 | kind1 = PyUnicode_KIND(self); |
| 9936 | kind2 = PyUnicode_KIND(substring); |
| 9937 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9938 | buf1 = PyUnicode_DATA(self); |
| 9939 | buf2 = PyUnicode_DATA(substring); |
| 9940 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9941 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | if (!buf1) |
| 9943 | return NULL; |
| 9944 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9945 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9946 | if (!buf2) { |
| 9947 | if (kind1 != kind) PyMem_Free(buf1); |
| 9948 | return NULL; |
| 9949 | } |
| 9950 | len1 = PyUnicode_GET_LENGTH(self); |
| 9951 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9952 | |
| 9953 | switch(kind) { |
| 9954 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9955 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9956 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9957 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9958 | else |
| 9959 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9960 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9961 | break; |
| 9962 | case PyUnicode_2BYTE_KIND: |
| 9963 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9964 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9965 | break; |
| 9966 | case PyUnicode_4BYTE_KIND: |
| 9967 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9968 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9969 | break; |
| 9970 | default: |
| 9971 | out = NULL; |
| 9972 | } |
| 9973 | if (kind1 != kind) |
| 9974 | PyMem_Free(buf1); |
| 9975 | if (kind2 != kind) |
| 9976 | PyMem_Free(buf2); |
| 9977 | return out; |
| 9978 | } |
| 9979 | |
| 9980 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9981 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9982 | 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] | 9983 | { |
| 9984 | switch(kind) { |
| 9985 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9986 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9987 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9988 | else |
| 9989 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9990 | case PyUnicode_2BYTE_KIND: |
| 9991 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9992 | case PyUnicode_4BYTE_KIND: |
| 9993 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9994 | } |
| 9995 | assert(0); |
| 9996 | return -1; |
| 9997 | } |
| 9998 | |
| 9999 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10000 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10001 | 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] | 10002 | { |
| 10003 | switch(kind) { |
| 10004 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10005 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10006 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10007 | else |
| 10008 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | case PyUnicode_2BYTE_KIND: |
| 10010 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10011 | case PyUnicode_4BYTE_KIND: |
| 10012 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10013 | } |
| 10014 | assert(0); |
| 10015 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10018 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | replace(PyObject *self, PyObject *str1, |
| 10020 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10021 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10022 | PyObject *u; |
| 10023 | char *sbuf = PyUnicode_DATA(self); |
| 10024 | char *buf1 = PyUnicode_DATA(str1); |
| 10025 | char *buf2 = PyUnicode_DATA(str2); |
| 10026 | int srelease = 0, release1 = 0, release2 = 0; |
| 10027 | int skind = PyUnicode_KIND(self); |
| 10028 | int kind1 = PyUnicode_KIND(str1); |
| 10029 | int kind2 = PyUnicode_KIND(str2); |
| 10030 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10031 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10032 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10033 | int mayshrink; |
| 10034 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10035 | |
| 10036 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10037 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10039 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10040 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10041 | if (str1 == str2) |
| 10042 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 | if (skind < kind1) |
| 10044 | /* substring too wide to be present */ |
| 10045 | goto nothing; |
| 10046 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10047 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10048 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10049 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10050 | result string. */ |
| 10051 | mayshrink = (maxchar_str2 < maxchar); |
| 10052 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10053 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10054 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10055 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10056 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10058 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10060 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10061 | Py_UCS4 u1, u2; |
| 10062 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10064 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10065 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10066 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10068 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10069 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10071 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10072 | rkind = PyUnicode_KIND(u); |
| 10073 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10074 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10075 | if (--maxcount < 0) |
| 10076 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10077 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10078 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10079 | } |
| 10080 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10081 | int rkind = skind; |
| 10082 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10083 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10084 | if (kind1 < rkind) { |
| 10085 | /* widen substring */ |
| 10086 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10087 | if (!buf1) goto error; |
| 10088 | release1 = 1; |
| 10089 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10090 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10091 | if (i < 0) |
| 10092 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | if (rkind > kind2) { |
| 10094 | /* widen replacement */ |
| 10095 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10096 | if (!buf2) goto error; |
| 10097 | release2 = 1; |
| 10098 | } |
| 10099 | else if (rkind < kind2) { |
| 10100 | /* widen self and buf1 */ |
| 10101 | rkind = kind2; |
| 10102 | if (release1) PyMem_Free(buf1); |
| 10103 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10104 | if (!sbuf) goto error; |
| 10105 | srelease = 1; |
| 10106 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10107 | if (!buf1) goto error; |
| 10108 | release1 = 1; |
| 10109 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10110 | u = PyUnicode_New(slen, maxchar); |
| 10111 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10112 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10113 | assert(PyUnicode_KIND(u) == rkind); |
| 10114 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10115 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10116 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10117 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10118 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10120 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10122 | |
| 10123 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10124 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10125 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10126 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10127 | if (i == -1) |
| 10128 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10129 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10131 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10133 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10134 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10135 | } |
| 10136 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | Py_ssize_t n, i, j, ires; |
| 10138 | Py_ssize_t product, new_size; |
| 10139 | int rkind = skind; |
| 10140 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10142 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10143 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10144 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10145 | if (!buf1) goto error; |
| 10146 | release1 = 1; |
| 10147 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10148 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10149 | if (n == 0) |
| 10150 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10152 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10154 | if (!buf2) goto error; |
| 10155 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10156 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10158 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | rkind = kind2; |
| 10160 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10161 | if (!sbuf) goto error; |
| 10162 | srelease = 1; |
| 10163 | if (release1) PyMem_Free(buf1); |
| 10164 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10165 | if (!buf1) goto error; |
| 10166 | release1 = 1; |
| 10167 | } |
| 10168 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10169 | PyUnicode_GET_LENGTH(str1))); */ |
| 10170 | product = n * (len2-len1); |
| 10171 | if ((product / (len2-len1)) != n) { |
| 10172 | PyErr_SetString(PyExc_OverflowError, |
| 10173 | "replace string is too long"); |
| 10174 | goto error; |
| 10175 | } |
| 10176 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10177 | if (new_size == 0) { |
| 10178 | Py_INCREF(unicode_empty); |
| 10179 | u = unicode_empty; |
| 10180 | goto done; |
| 10181 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10182 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10183 | PyErr_SetString(PyExc_OverflowError, |
| 10184 | "replace string is too long"); |
| 10185 | goto error; |
| 10186 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10187 | u = PyUnicode_New(new_size, maxchar); |
| 10188 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10189 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10190 | assert(PyUnicode_KIND(u) == rkind); |
| 10191 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10192 | ires = i = 0; |
| 10193 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10194 | while (n-- > 0) { |
| 10195 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10196 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10197 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10198 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10199 | if (j == -1) |
| 10200 | break; |
| 10201 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10202 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10203 | memcpy(res + rkind * ires, |
| 10204 | sbuf + rkind * i, |
| 10205 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10206 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10207 | } |
| 10208 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10209 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10210 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10212 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10214 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10216 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10217 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10218 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10219 | memcpy(res + rkind * ires, |
| 10220 | sbuf + rkind * i, |
| 10221 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10222 | } |
| 10223 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10224 | /* interleave */ |
| 10225 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10226 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10228 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10229 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10230 | if (--n <= 0) |
| 10231 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10232 | memcpy(res + rkind * ires, |
| 10233 | sbuf + rkind * i, |
| 10234 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | ires++; |
| 10236 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10237 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10238 | memcpy(res + rkind * ires, |
| 10239 | sbuf + rkind * i, |
| 10240 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10241 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10242 | } |
| 10243 | |
| 10244 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10245 | unicode_adjust_maxchar(&u); |
| 10246 | if (u == NULL) |
| 10247 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10248 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10249 | |
| 10250 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10251 | if (srelease) |
| 10252 | PyMem_FREE(sbuf); |
| 10253 | if (release1) |
| 10254 | PyMem_FREE(buf1); |
| 10255 | if (release2) |
| 10256 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10257 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10259 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10260 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10261 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10262 | if (srelease) |
| 10263 | PyMem_FREE(sbuf); |
| 10264 | if (release1) |
| 10265 | PyMem_FREE(buf1); |
| 10266 | if (release2) |
| 10267 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10268 | if (PyUnicode_CheckExact(self)) { |
| 10269 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10270 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10271 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10272 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10273 | error: |
| 10274 | if (srelease && sbuf) |
| 10275 | PyMem_FREE(sbuf); |
| 10276 | if (release1 && buf1) |
| 10277 | PyMem_FREE(buf1); |
| 10278 | if (release2 && buf2) |
| 10279 | PyMem_FREE(buf2); |
| 10280 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10281 | } |
| 10282 | |
| 10283 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10285 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10286 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10287 | \n\ |
| 10288 | 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] | 10289 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10290 | |
| 10291 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10292 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10293 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10294 | return fixup(self, fixtitle); |
| 10295 | } |
| 10296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10297 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10298 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 | \n\ |
| 10300 | 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] | 10301 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10302 | |
| 10303 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10304 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10305 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10306 | return fixup(self, fixcapitalize); |
| 10307 | } |
| 10308 | |
| 10309 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10310 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10311 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10312 | \n\ |
| 10313 | Apply .capitalize() to all words in S and return the result with\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10314 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10315 | |
| 10316 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10317 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10318 | { |
| 10319 | PyObject *list; |
| 10320 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10321 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10322 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10323 | /* Split into words */ |
| 10324 | list = split(self, NULL, -1); |
| 10325 | if (!list) |
| 10326 | return NULL; |
| 10327 | |
| 10328 | /* Capitalize each word */ |
| 10329 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10330 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10331 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10332 | if (item == NULL) |
| 10333 | goto onError; |
| 10334 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10335 | PyList_SET_ITEM(list, i, item); |
| 10336 | } |
| 10337 | |
| 10338 | /* Join the words to form a new string */ |
| 10339 | item = PyUnicode_Join(NULL, list); |
| 10340 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10341 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10342 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10343 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10344 | } |
| 10345 | #endif |
| 10346 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10347 | /* Argument converter. Coerces to a single unicode character */ |
| 10348 | |
| 10349 | static int |
| 10350 | convert_uc(PyObject *obj, void *addr) |
| 10351 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10353 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10354 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10355 | uniobj = PyUnicode_FromObject(obj); |
| 10356 | if (uniobj == NULL) { |
| 10357 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10358 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10359 | return 0; |
| 10360 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10361 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10362 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10363 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10364 | Py_DECREF(uniobj); |
| 10365 | return 0; |
| 10366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10367 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10368 | Py_DECREF(uniobj); |
| 10369 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10370 | } |
| 10371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10372 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10373 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10375 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10376 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | |
| 10378 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10379 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10380 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10381 | Py_ssize_t marg, left; |
| 10382 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | Py_UCS4 fillchar = ' '; |
| 10384 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10385 | 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] | 10386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10388 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | return NULL; |
| 10390 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10392 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10393 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | } |
| 10395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10396 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10397 | left = marg / 2 + (marg & width & 1); |
| 10398 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10399 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | } |
| 10401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10402 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10403 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10404 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10405 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10406 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | int kind1, kind2; |
| 10408 | void *data1, *data2; |
| 10409 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10410 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10411 | kind1 = PyUnicode_KIND(str1); |
| 10412 | kind2 = PyUnicode_KIND(str2); |
| 10413 | data1 = PyUnicode_DATA(str1); |
| 10414 | data2 = PyUnicode_DATA(str2); |
| 10415 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10416 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10419 | Py_UCS4 c1, c2; |
| 10420 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10421 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10422 | |
| 10423 | if (c1 != c2) |
| 10424 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10425 | } |
| 10426 | |
| 10427 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10428 | } |
| 10429 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10430 | int |
| 10431 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10432 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10433 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10434 | if (PyUnicode_READY(left) == -1 || |
| 10435 | PyUnicode_READY(right) == -1) |
| 10436 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10437 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10438 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10439 | PyErr_Format(PyExc_TypeError, |
| 10440 | "Can't compare %.100s and %.100s", |
| 10441 | left->ob_type->tp_name, |
| 10442 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10443 | return -1; |
| 10444 | } |
| 10445 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10446 | int |
| 10447 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10448 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | Py_ssize_t i; |
| 10450 | int kind; |
| 10451 | void *data; |
| 10452 | Py_UCS4 chr; |
| 10453 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10454 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10455 | if (PyUnicode_READY(uni) == -1) |
| 10456 | return -1; |
| 10457 | kind = PyUnicode_KIND(uni); |
| 10458 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10459 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10460 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10461 | if (chr != str[i]) |
| 10462 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10463 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10464 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10466 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10467 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10468 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10469 | return 0; |
| 10470 | } |
| 10471 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10472 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10473 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10474 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10475 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10476 | PyObject * |
| 10477 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10478 | { |
| 10479 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10480 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10481 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10482 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10483 | if (PyUnicode_READY(left) == -1 || |
| 10484 | PyUnicode_READY(right) == -1) |
| 10485 | return NULL; |
| 10486 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10487 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10488 | if (op == Py_EQ) { |
| 10489 | Py_INCREF(Py_False); |
| 10490 | return Py_False; |
| 10491 | } |
| 10492 | if (op == Py_NE) { |
| 10493 | Py_INCREF(Py_True); |
| 10494 | return Py_True; |
| 10495 | } |
| 10496 | } |
| 10497 | if (left == right) |
| 10498 | result = 0; |
| 10499 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10500 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10501 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10502 | /* Convert the return value to a Boolean */ |
| 10503 | switch (op) { |
| 10504 | case Py_EQ: |
| 10505 | v = TEST_COND(result == 0); |
| 10506 | break; |
| 10507 | case Py_NE: |
| 10508 | v = TEST_COND(result != 0); |
| 10509 | break; |
| 10510 | case Py_LE: |
| 10511 | v = TEST_COND(result <= 0); |
| 10512 | break; |
| 10513 | case Py_GE: |
| 10514 | v = TEST_COND(result >= 0); |
| 10515 | break; |
| 10516 | case Py_LT: |
| 10517 | v = TEST_COND(result == -1); |
| 10518 | break; |
| 10519 | case Py_GT: |
| 10520 | v = TEST_COND(result == 1); |
| 10521 | break; |
| 10522 | default: |
| 10523 | PyErr_BadArgument(); |
| 10524 | return NULL; |
| 10525 | } |
| 10526 | Py_INCREF(v); |
| 10527 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10528 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10529 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10530 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10531 | } |
| 10532 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10533 | int |
| 10534 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10535 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10536 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10537 | int kind1, kind2, kind; |
| 10538 | void *buf1, *buf2; |
| 10539 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10540 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10541 | |
| 10542 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10543 | sub = PyUnicode_FromObject(element); |
| 10544 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10545 | PyErr_Format(PyExc_TypeError, |
| 10546 | "'in <string>' requires string as left operand, not %s", |
| 10547 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10548 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10549 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10550 | if (PyUnicode_READY(sub) == -1) |
| 10551 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10552 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10553 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10554 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10555 | Py_DECREF(sub); |
| 10556 | return -1; |
| 10557 | } |
| 10558 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10559 | kind1 = PyUnicode_KIND(str); |
| 10560 | kind2 = PyUnicode_KIND(sub); |
| 10561 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10562 | buf1 = PyUnicode_DATA(str); |
| 10563 | buf2 = PyUnicode_DATA(sub); |
| 10564 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10565 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | if (!buf1) { |
| 10567 | Py_DECREF(sub); |
| 10568 | return -1; |
| 10569 | } |
| 10570 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10571 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | if (!buf2) { |
| 10573 | Py_DECREF(sub); |
| 10574 | if (kind1 != kind) PyMem_Free(buf1); |
| 10575 | return -1; |
| 10576 | } |
| 10577 | len1 = PyUnicode_GET_LENGTH(str); |
| 10578 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10579 | |
| 10580 | switch(kind) { |
| 10581 | case PyUnicode_1BYTE_KIND: |
| 10582 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10583 | break; |
| 10584 | case PyUnicode_2BYTE_KIND: |
| 10585 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10586 | break; |
| 10587 | case PyUnicode_4BYTE_KIND: |
| 10588 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10589 | break; |
| 10590 | default: |
| 10591 | result = -1; |
| 10592 | assert(0); |
| 10593 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10594 | |
| 10595 | Py_DECREF(str); |
| 10596 | Py_DECREF(sub); |
| 10597 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10598 | if (kind1 != kind) |
| 10599 | PyMem_Free(buf1); |
| 10600 | if (kind2 != kind) |
| 10601 | PyMem_Free(buf2); |
| 10602 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10603 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10604 | } |
| 10605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10606 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10607 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10608 | PyObject * |
| 10609 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10611 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10612 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10613 | |
| 10614 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10616 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10617 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10619 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10620 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10621 | |
| 10622 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10623 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10624 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10625 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10626 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10627 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10628 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10630 | } |
| 10631 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10632 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10633 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10634 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10637 | w = PyUnicode_New( |
| 10638 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10639 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10641 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10642 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10643 | copy_characters(w, PyUnicode_GET_LENGTH(u), v, 0, PyUnicode_GET_LENGTH(v)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | Py_DECREF(u); |
| 10645 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10646 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10647 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10649 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10650 | Py_XDECREF(u); |
| 10651 | Py_XDECREF(v); |
| 10652 | return NULL; |
| 10653 | } |
| 10654 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10655 | static void |
| 10656 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10657 | { |
| 10658 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10659 | |
| 10660 | assert(PyUnicode_IS_READY(*p_left)); |
| 10661 | assert(PyUnicode_IS_READY(right)); |
| 10662 | |
| 10663 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10664 | right_len = PyUnicode_GET_LENGTH(right); |
| 10665 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10666 | PyErr_SetString(PyExc_OverflowError, |
| 10667 | "strings are too large to concat"); |
| 10668 | goto error; |
| 10669 | } |
| 10670 | new_len = left_len + right_len; |
| 10671 | |
| 10672 | /* Now we own the last reference to 'left', so we can resize it |
| 10673 | * in-place. |
| 10674 | */ |
| 10675 | if (unicode_resize(p_left, new_len) != 0) { |
| 10676 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10677 | * deallocated so it cannot be put back into |
| 10678 | * 'variable'. The MemoryError is raised when there |
| 10679 | * is no value in 'variable', which might (very |
| 10680 | * remotely) be a cause of incompatibilities. |
| 10681 | */ |
| 10682 | goto error; |
| 10683 | } |
| 10684 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10685 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10686 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10687 | return; |
| 10688 | |
| 10689 | error: |
| 10690 | Py_DECREF(*p_left); |
| 10691 | *p_left = NULL; |
| 10692 | } |
| 10693 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10694 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10695 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10696 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10697 | PyObject *left, *res; |
| 10698 | |
| 10699 | if (p_left == NULL) { |
| 10700 | if (!PyErr_Occurred()) |
| 10701 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10702 | return; |
| 10703 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10704 | left = *p_left; |
| 10705 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10706 | if (!PyErr_Occurred()) |
| 10707 | PyErr_BadInternalCall(); |
| 10708 | goto error; |
| 10709 | } |
| 10710 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10711 | if (PyUnicode_READY(left)) |
| 10712 | goto error; |
| 10713 | if (PyUnicode_READY(right)) |
| 10714 | goto error; |
| 10715 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10716 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10717 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10718 | && unicode_resizable(left) |
| 10719 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10720 | || _PyUnicode_WSTR(left) != NULL)) |
| 10721 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10722 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10723 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10724 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10725 | not so different than duplicating the string. */ |
| 10726 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10727 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10728 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10729 | if (p_left != NULL) |
| 10730 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10731 | return; |
| 10732 | } |
| 10733 | } |
| 10734 | |
| 10735 | res = PyUnicode_Concat(left, right); |
| 10736 | if (res == NULL) |
| 10737 | goto error; |
| 10738 | Py_DECREF(left); |
| 10739 | *p_left = res; |
| 10740 | return; |
| 10741 | |
| 10742 | error: |
| 10743 | Py_DECREF(*p_left); |
| 10744 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10745 | } |
| 10746 | |
| 10747 | void |
| 10748 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10749 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10750 | PyUnicode_Append(pleft, right); |
| 10751 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10752 | } |
| 10753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10754 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10755 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10756 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10757 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10758 | 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] | 10759 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 | |
| 10761 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10762 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10764 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10765 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10766 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10768 | int kind1, kind2, kind; |
| 10769 | void *buf1, *buf2; |
| 10770 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10772 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10773 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10774 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10775 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10776 | kind1 = PyUnicode_KIND(self); |
| 10777 | kind2 = PyUnicode_KIND(substring); |
| 10778 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10779 | buf1 = PyUnicode_DATA(self); |
| 10780 | buf2 = PyUnicode_DATA(substring); |
| 10781 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10782 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10783 | if (!buf1) { |
| 10784 | Py_DECREF(substring); |
| 10785 | return NULL; |
| 10786 | } |
| 10787 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10788 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10789 | if (!buf2) { |
| 10790 | Py_DECREF(substring); |
| 10791 | if (kind1 != kind) PyMem_Free(buf1); |
| 10792 | return NULL; |
| 10793 | } |
| 10794 | len1 = PyUnicode_GET_LENGTH(self); |
| 10795 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10796 | |
| 10797 | ADJUST_INDICES(start, end, len1); |
| 10798 | switch(kind) { |
| 10799 | case PyUnicode_1BYTE_KIND: |
| 10800 | iresult = ucs1lib_count( |
| 10801 | ((Py_UCS1*)buf1) + start, end - start, |
| 10802 | buf2, len2, PY_SSIZE_T_MAX |
| 10803 | ); |
| 10804 | break; |
| 10805 | case PyUnicode_2BYTE_KIND: |
| 10806 | iresult = ucs2lib_count( |
| 10807 | ((Py_UCS2*)buf1) + start, end - start, |
| 10808 | buf2, len2, PY_SSIZE_T_MAX |
| 10809 | ); |
| 10810 | break; |
| 10811 | case PyUnicode_4BYTE_KIND: |
| 10812 | iresult = ucs4lib_count( |
| 10813 | ((Py_UCS4*)buf1) + start, end - start, |
| 10814 | buf2, len2, PY_SSIZE_T_MAX |
| 10815 | ); |
| 10816 | break; |
| 10817 | default: |
| 10818 | assert(0); iresult = 0; |
| 10819 | } |
| 10820 | |
| 10821 | result = PyLong_FromSsize_t(iresult); |
| 10822 | |
| 10823 | if (kind1 != kind) |
| 10824 | PyMem_Free(buf1); |
| 10825 | if (kind2 != kind) |
| 10826 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10827 | |
| 10828 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10829 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10830 | return result; |
| 10831 | } |
| 10832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10833 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10834 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10836 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10837 | 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] | 10838 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10839 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10840 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10841 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10842 | |
| 10843 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10844 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10845 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10846 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | char *encoding = NULL; |
| 10848 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10849 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10850 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10851 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10853 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10854 | } |
| 10855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10856 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10857 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10858 | \n\ |
| 10859 | 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] | 10860 | 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] | 10861 | |
| 10862 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10863 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10865 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10866 | Py_UCS4 ch; |
| 10867 | PyObject *u; |
| 10868 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10870 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10871 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10872 | |
| 10873 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10874 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10875 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10876 | if (PyUnicode_READY(self) == -1) |
| 10877 | return NULL; |
| 10878 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10879 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10880 | src_len = PyUnicode_GET_LENGTH(self); |
| 10881 | i = j = line_pos = 0; |
| 10882 | kind = PyUnicode_KIND(self); |
| 10883 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10884 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10885 | for (; i < src_len; i++) { |
| 10886 | ch = PyUnicode_READ(kind, src_data, i); |
| 10887 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10888 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10889 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10890 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10891 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10892 | goto overflow; |
| 10893 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10894 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10895 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10896 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10897 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10898 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10899 | goto overflow; |
| 10900 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10901 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10902 | if (ch == '\n' || ch == '\r') |
| 10903 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10904 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10905 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10906 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10907 | Py_INCREF(self); |
| 10908 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10909 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10911 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10912 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10913 | if (!u) |
| 10914 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10915 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10916 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10917 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10918 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10919 | for (; i < src_len; i++) { |
| 10920 | ch = PyUnicode_READ(kind, src_data, i); |
| 10921 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10922 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10923 | incr = tabsize - (line_pos % tabsize); |
| 10924 | line_pos += incr; |
| 10925 | while (incr--) { |
| 10926 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10927 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10928 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10929 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10930 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10931 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10932 | line_pos++; |
| 10933 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10934 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10935 | if (ch == '\n' || ch == '\r') |
| 10936 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10937 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10938 | } |
| 10939 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10940 | #ifndef DONT_MAKE_RESULT_READY |
| 10941 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10942 | Py_DECREF(u); |
| 10943 | return NULL; |
| 10944 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10945 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10946 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10947 | return u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10948 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10949 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10950 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10951 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | } |
| 10953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10954 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10955 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | \n\ |
| 10957 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10958 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10959 | arguments start and end are interpreted as in slice notation.\n\ |
| 10960 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10961 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | |
| 10963 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10964 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10965 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10966 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10967 | Py_ssize_t start; |
| 10968 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10969 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10970 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10971 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10972 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10974 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10975 | if (PyUnicode_READY(self) == -1) |
| 10976 | return NULL; |
| 10977 | if (PyUnicode_READY(substring) == -1) |
| 10978 | return NULL; |
| 10979 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10980 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10981 | |
| 10982 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10984 | if (result == -2) |
| 10985 | return NULL; |
| 10986 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10987 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10988 | } |
| 10989 | |
| 10990 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10991 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10993 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10994 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10995 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10996 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10997 | } |
| 10998 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10999 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11000 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11001 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11002 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11004 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11005 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11006 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11007 | if (_PyUnicode_HASH(self) != -1) |
| 11008 | return _PyUnicode_HASH(self); |
| 11009 | if (PyUnicode_READY(self) == -1) |
| 11010 | return -1; |
| 11011 | len = PyUnicode_GET_LENGTH(self); |
| 11012 | |
| 11013 | /* The hash function as a macro, gets expanded three times below. */ |
| 11014 | #define HASH(P) \ |
| 11015 | x = (Py_uhash_t)*P << 7; \ |
| 11016 | while (--len >= 0) \ |
| 11017 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11018 | |
| 11019 | switch (PyUnicode_KIND(self)) { |
| 11020 | case PyUnicode_1BYTE_KIND: { |
| 11021 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11022 | HASH(c); |
| 11023 | break; |
| 11024 | } |
| 11025 | case PyUnicode_2BYTE_KIND: { |
| 11026 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11027 | HASH(s); |
| 11028 | break; |
| 11029 | } |
| 11030 | default: { |
| 11031 | Py_UCS4 *l; |
| 11032 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11033 | "Impossible switch case in unicode_hash"); |
| 11034 | l = PyUnicode_4BYTE_DATA(self); |
| 11035 | HASH(l); |
| 11036 | break; |
| 11037 | } |
| 11038 | } |
| 11039 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11040 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11041 | if (x == -1) |
| 11042 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11043 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11044 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11046 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11048 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11049 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11051 | 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] | 11052 | |
| 11053 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11056 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11057 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11058 | Py_ssize_t start; |
| 11059 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11061 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11062 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11065 | if (PyUnicode_READY(self) == -1) |
| 11066 | return NULL; |
| 11067 | if (PyUnicode_READY(substring) == -1) |
| 11068 | return NULL; |
| 11069 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11070 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | |
| 11072 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11073 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11074 | if (result == -2) |
| 11075 | return NULL; |
| 11076 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | if (result < 0) { |
| 11078 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11079 | return NULL; |
| 11080 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11081 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11082 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11083 | } |
| 11084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11085 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11086 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11087 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11088 | 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] | 11089 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11090 | |
| 11091 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11092 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11094 | Py_ssize_t i, length; |
| 11095 | int kind; |
| 11096 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11097 | int cased; |
| 11098 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11099 | if (PyUnicode_READY(self) == -1) |
| 11100 | return NULL; |
| 11101 | length = PyUnicode_GET_LENGTH(self); |
| 11102 | kind = PyUnicode_KIND(self); |
| 11103 | data = PyUnicode_DATA(self); |
| 11104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11105 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11106 | if (length == 1) |
| 11107 | return PyBool_FromLong( |
| 11108 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11110 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11111 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11112 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11114 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11115 | for (i = 0; i < length; i++) { |
| 11116 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11118 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11119 | return PyBool_FromLong(0); |
| 11120 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11121 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11123 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11124 | } |
| 11125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11126 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11127 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11128 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11129 | 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] | 11130 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11131 | |
| 11132 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11133 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11134 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11135 | Py_ssize_t i, length; |
| 11136 | int kind; |
| 11137 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | int cased; |
| 11139 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11140 | if (PyUnicode_READY(self) == -1) |
| 11141 | return NULL; |
| 11142 | length = PyUnicode_GET_LENGTH(self); |
| 11143 | kind = PyUnicode_KIND(self); |
| 11144 | data = PyUnicode_DATA(self); |
| 11145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11147 | if (length == 1) |
| 11148 | return PyBool_FromLong( |
| 11149 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11150 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11151 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11152 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11153 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11154 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11155 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11156 | for (i = 0; i < length; i++) { |
| 11157 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11158 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11159 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11160 | return PyBool_FromLong(0); |
| 11161 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11162 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11164 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | } |
| 11166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11167 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11168 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11170 | Return True if S is a titlecased string and there is at least one\n\ |
| 11171 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11172 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11173 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11174 | |
| 11175 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11176 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11178 | Py_ssize_t i, length; |
| 11179 | int kind; |
| 11180 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | int cased, previous_is_cased; |
| 11182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11183 | if (PyUnicode_READY(self) == -1) |
| 11184 | return NULL; |
| 11185 | length = PyUnicode_GET_LENGTH(self); |
| 11186 | kind = PyUnicode_KIND(self); |
| 11187 | data = PyUnicode_DATA(self); |
| 11188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11190 | if (length == 1) { |
| 11191 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11192 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11193 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11194 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11196 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11197 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11198 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11200 | cased = 0; |
| 11201 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11202 | for (i = 0; i < length; i++) { |
| 11203 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11204 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11205 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11206 | if (previous_is_cased) |
| 11207 | return PyBool_FromLong(0); |
| 11208 | previous_is_cased = 1; |
| 11209 | cased = 1; |
| 11210 | } |
| 11211 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11212 | if (!previous_is_cased) |
| 11213 | return PyBool_FromLong(0); |
| 11214 | previous_is_cased = 1; |
| 11215 | cased = 1; |
| 11216 | } |
| 11217 | else |
| 11218 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11220 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | } |
| 11222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11223 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11224 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11226 | Return True if all characters in S are whitespace\n\ |
| 11227 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11228 | |
| 11229 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11230 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11232 | Py_ssize_t i, length; |
| 11233 | int kind; |
| 11234 | void *data; |
| 11235 | |
| 11236 | if (PyUnicode_READY(self) == -1) |
| 11237 | return NULL; |
| 11238 | length = PyUnicode_GET_LENGTH(self); |
| 11239 | kind = PyUnicode_KIND(self); |
| 11240 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11242 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11243 | if (length == 1) |
| 11244 | return PyBool_FromLong( |
| 11245 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11246 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11247 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11248 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11249 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11250 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11251 | for (i = 0; i < length; i++) { |
| 11252 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11253 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11254 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11256 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | } |
| 11258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11259 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11260 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11261 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11262 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11263 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11264 | |
| 11265 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11266 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11267 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | Py_ssize_t i, length; |
| 11269 | int kind; |
| 11270 | void *data; |
| 11271 | |
| 11272 | if (PyUnicode_READY(self) == -1) |
| 11273 | return NULL; |
| 11274 | length = PyUnicode_GET_LENGTH(self); |
| 11275 | kind = PyUnicode_KIND(self); |
| 11276 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11277 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11278 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11279 | if (length == 1) |
| 11280 | return PyBool_FromLong( |
| 11281 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11282 | |
| 11283 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11284 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11285 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11286 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11287 | for (i = 0; i < length; i++) { |
| 11288 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11289 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11290 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11291 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11292 | } |
| 11293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11294 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11296 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11297 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11299 | |
| 11300 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11301 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11302 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11303 | int kind; |
| 11304 | void *data; |
| 11305 | Py_ssize_t len, i; |
| 11306 | |
| 11307 | if (PyUnicode_READY(self) == -1) |
| 11308 | return NULL; |
| 11309 | |
| 11310 | kind = PyUnicode_KIND(self); |
| 11311 | data = PyUnicode_DATA(self); |
| 11312 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11313 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11314 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11315 | if (len == 1) { |
| 11316 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11317 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11318 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11319 | |
| 11320 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11321 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11322 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11324 | for (i = 0; i < len; i++) { |
| 11325 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11326 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11327 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11328 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11329 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11330 | } |
| 11331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11332 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11333 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11334 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11335 | 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] | 11336 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11337 | |
| 11338 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11339 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11340 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | Py_ssize_t i, length; |
| 11342 | int kind; |
| 11343 | void *data; |
| 11344 | |
| 11345 | if (PyUnicode_READY(self) == -1) |
| 11346 | return NULL; |
| 11347 | length = PyUnicode_GET_LENGTH(self); |
| 11348 | kind = PyUnicode_KIND(self); |
| 11349 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | if (length == 1) |
| 11353 | return PyBool_FromLong( |
| 11354 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11355 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11356 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11357 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11358 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11360 | for (i = 0; i < length; i++) { |
| 11361 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11362 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11363 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11364 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | } |
| 11366 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11367 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11368 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11370 | Return True if all characters in S are digits\n\ |
| 11371 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | |
| 11373 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11374 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11376 | Py_ssize_t i, length; |
| 11377 | int kind; |
| 11378 | void *data; |
| 11379 | |
| 11380 | if (PyUnicode_READY(self) == -1) |
| 11381 | return NULL; |
| 11382 | length = PyUnicode_GET_LENGTH(self); |
| 11383 | kind = PyUnicode_KIND(self); |
| 11384 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11387 | if (length == 1) { |
| 11388 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11389 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11390 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11391 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11392 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11393 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11394 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | for (i = 0; i < length; i++) { |
| 11397 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11398 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11400 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | } |
| 11402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11403 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11404 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11406 | 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] | 11407 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11408 | |
| 11409 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11410 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11411 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11412 | Py_ssize_t i, length; |
| 11413 | int kind; |
| 11414 | void *data; |
| 11415 | |
| 11416 | if (PyUnicode_READY(self) == -1) |
| 11417 | return NULL; |
| 11418 | length = PyUnicode_GET_LENGTH(self); |
| 11419 | kind = PyUnicode_KIND(self); |
| 11420 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11422 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11423 | if (length == 1) |
| 11424 | return PyBool_FromLong( |
| 11425 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11427 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11428 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11429 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11430 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11431 | for (i = 0; i < length; i++) { |
| 11432 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11433 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11435 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11436 | } |
| 11437 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11438 | int |
| 11439 | PyUnicode_IsIdentifier(PyObject *self) |
| 11440 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | int kind; |
| 11442 | void *data; |
| 11443 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11444 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | if (PyUnicode_READY(self) == -1) { |
| 11447 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | } |
| 11450 | |
| 11451 | /* Special case for empty strings */ |
| 11452 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11453 | return 0; |
| 11454 | kind = PyUnicode_KIND(self); |
| 11455 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11456 | |
| 11457 | /* PEP 3131 says that the first character must be in |
| 11458 | XID_Start and subsequent characters in XID_Continue, |
| 11459 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11460 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11461 | letters, digits, underscore). However, given the current |
| 11462 | definition of XID_Start and XID_Continue, it is sufficient |
| 11463 | to check just for these, except that _ must be allowed |
| 11464 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11466 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11467 | return 0; |
| 11468 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11469 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11471 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11472 | return 1; |
| 11473 | } |
| 11474 | |
| 11475 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11476 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11477 | \n\ |
| 11478 | Return True if S is a valid identifier according\n\ |
| 11479 | to the language definition."); |
| 11480 | |
| 11481 | static PyObject* |
| 11482 | unicode_isidentifier(PyObject *self) |
| 11483 | { |
| 11484 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11485 | } |
| 11486 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11487 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11488 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11489 | \n\ |
| 11490 | Return True if all characters in S are considered\n\ |
| 11491 | printable in repr() or S is empty, False otherwise."); |
| 11492 | |
| 11493 | static PyObject* |
| 11494 | unicode_isprintable(PyObject *self) |
| 11495 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11496 | Py_ssize_t i, length; |
| 11497 | int kind; |
| 11498 | void *data; |
| 11499 | |
| 11500 | if (PyUnicode_READY(self) == -1) |
| 11501 | return NULL; |
| 11502 | length = PyUnicode_GET_LENGTH(self); |
| 11503 | kind = PyUnicode_KIND(self); |
| 11504 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11505 | |
| 11506 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | if (length == 1) |
| 11508 | return PyBool_FromLong( |
| 11509 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11510 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11511 | for (i = 0; i < length; i++) { |
| 11512 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11513 | Py_RETURN_FALSE; |
| 11514 | } |
| 11515 | } |
| 11516 | Py_RETURN_TRUE; |
| 11517 | } |
| 11518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11519 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11520 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | \n\ |
| 11522 | 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] | 11523 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | |
| 11525 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11526 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11528 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | } |
| 11530 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11531 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11532 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11534 | if (PyUnicode_READY(self) == -1) |
| 11535 | return -1; |
| 11536 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 | } |
| 11538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11539 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11540 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11542 | 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] | 11543 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11544 | |
| 11545 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11546 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11548 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11549 | Py_UCS4 fillchar = ' '; |
| 11550 | |
| 11551 | if (PyUnicode_READY(self) == -1) |
| 11552 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11553 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11554 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | return NULL; |
| 11556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11558 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11559 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11560 | } |
| 11561 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11562 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | } |
| 11564 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11565 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11566 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11568 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | |
| 11570 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11571 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11573 | return fixup(self, fixlower); |
| 11574 | } |
| 11575 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11576 | #define LEFTSTRIP 0 |
| 11577 | #define RIGHTSTRIP 1 |
| 11578 | #define BOTHSTRIP 2 |
| 11579 | |
| 11580 | /* Arrays indexed by above */ |
| 11581 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11582 | |
| 11583 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11584 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11585 | /* externally visible for str.strip(unicode) */ |
| 11586 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11587 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11588 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | void *data; |
| 11590 | int kind; |
| 11591 | Py_ssize_t i, j, len; |
| 11592 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11593 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11594 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11595 | return NULL; |
| 11596 | |
| 11597 | kind = PyUnicode_KIND(self); |
| 11598 | data = PyUnicode_DATA(self); |
| 11599 | len = PyUnicode_GET_LENGTH(self); |
| 11600 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11601 | PyUnicode_DATA(sepobj), |
| 11602 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11603 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11604 | i = 0; |
| 11605 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11606 | while (i < len && |
| 11607 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11608 | i++; |
| 11609 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11610 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11611 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11612 | j = len; |
| 11613 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11614 | do { |
| 11615 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11616 | } while (j >= i && |
| 11617 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11618 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11619 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11620 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11621 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | } |
| 11623 | |
| 11624 | PyObject* |
| 11625 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11626 | { |
| 11627 | unsigned char *data; |
| 11628 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11629 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11630 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11631 | if (PyUnicode_READY(self) == -1) |
| 11632 | return NULL; |
| 11633 | |
| 11634 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11635 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11636 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11637 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11638 | if (PyUnicode_CheckExact(self)) { |
| 11639 | Py_INCREF(self); |
| 11640 | return self; |
| 11641 | } |
| 11642 | else |
| 11643 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11644 | } |
| 11645 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11646 | length = end - start; |
| 11647 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11648 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11650 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11651 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11652 | return NULL; |
| 11653 | } |
| 11654 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11655 | if (PyUnicode_IS_ASCII(self)) { |
| 11656 | kind = PyUnicode_KIND(self); |
| 11657 | data = PyUnicode_1BYTE_DATA(self); |
| 11658 | return unicode_fromascii(data + start, length); |
| 11659 | } |
| 11660 | else { |
| 11661 | kind = PyUnicode_KIND(self); |
| 11662 | data = PyUnicode_1BYTE_DATA(self); |
| 11663 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11664 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11665 | length); |
| 11666 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11667 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11668 | |
| 11669 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11670 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11672 | int kind; |
| 11673 | void *data; |
| 11674 | Py_ssize_t len, i, j; |
| 11675 | |
| 11676 | if (PyUnicode_READY(self) == -1) |
| 11677 | return NULL; |
| 11678 | |
| 11679 | kind = PyUnicode_KIND(self); |
| 11680 | data = PyUnicode_DATA(self); |
| 11681 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11682 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11683 | i = 0; |
| 11684 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11685 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11686 | i++; |
| 11687 | } |
| 11688 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11689 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11690 | j = len; |
| 11691 | if (striptype != LEFTSTRIP) { |
| 11692 | do { |
| 11693 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11694 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11695 | j++; |
| 11696 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11697 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11698 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11699 | } |
| 11700 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11701 | |
| 11702 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11703 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11704 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11705 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11706 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11707 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11708 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11709 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11710 | if (sep != NULL && sep != Py_None) { |
| 11711 | if (PyUnicode_Check(sep)) |
| 11712 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11713 | else { |
| 11714 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11715 | "%s arg must be None or str", |
| 11716 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11717 | return NULL; |
| 11718 | } |
| 11719 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11720 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11721 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11722 | } |
| 11723 | |
| 11724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11725 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11726 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11727 | \n\ |
| 11728 | Return a copy of the string S with leading and trailing\n\ |
| 11729 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11730 | 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] | 11731 | |
| 11732 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11733 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11734 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11735 | if (PyTuple_GET_SIZE(args) == 0) |
| 11736 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11737 | else |
| 11738 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11739 | } |
| 11740 | |
| 11741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11742 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11743 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11744 | \n\ |
| 11745 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11746 | 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] | 11747 | |
| 11748 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11749 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11750 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11751 | if (PyTuple_GET_SIZE(args) == 0) |
| 11752 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11753 | else |
| 11754 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11755 | } |
| 11756 | |
| 11757 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11758 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11759 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11760 | \n\ |
| 11761 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11762 | 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] | 11763 | |
| 11764 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11765 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11766 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11767 | if (PyTuple_GET_SIZE(args) == 0) |
| 11768 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11769 | else |
| 11770 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11771 | } |
| 11772 | |
| 11773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11775 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11776 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11777 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11779 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11780 | if (len < 1) { |
| 11781 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11782 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11783 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11785 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11786 | /* no repeat, return original string */ |
| 11787 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11788 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11789 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11790 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11791 | if (PyUnicode_READY(str) == -1) |
| 11792 | return NULL; |
| 11793 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11794 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11795 | PyErr_SetString(PyExc_OverflowError, |
| 11796 | "repeated string is too long"); |
| 11797 | return NULL; |
| 11798 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11799 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11800 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11801 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11802 | if (!u) |
| 11803 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11804 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11806 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11807 | const int kind = PyUnicode_KIND(str); |
| 11808 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11809 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11810 | if (kind == PyUnicode_1BYTE_KIND) |
| 11811 | memset(to, (unsigned char)fill_char, len); |
| 11812 | else { |
| 11813 | for (n = 0; n < len; ++n) |
| 11814 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11815 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11816 | } |
| 11817 | else { |
| 11818 | /* number of characters copied this far */ |
| 11819 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11820 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | char *to = (char *) PyUnicode_DATA(u); |
| 11822 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11823 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11824 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11825 | n = (done <= nchars-done) ? done : nchars-done; |
| 11826 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11827 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11828 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | } |
| 11830 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11831 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11832 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11833 | } |
| 11834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11835 | PyObject * |
| 11836 | PyUnicode_Replace(PyObject *obj, |
| 11837 | PyObject *subobj, |
| 11838 | PyObject *replobj, |
| 11839 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11840 | { |
| 11841 | PyObject *self; |
| 11842 | PyObject *str1; |
| 11843 | PyObject *str2; |
| 11844 | PyObject *result; |
| 11845 | |
| 11846 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11847 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11848 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11849 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11850 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11851 | Py_DECREF(self); |
| 11852 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11853 | } |
| 11854 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11855 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11856 | Py_DECREF(self); |
| 11857 | Py_DECREF(str1); |
| 11858 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11859 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11860 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11861 | Py_DECREF(self); |
| 11862 | Py_DECREF(str1); |
| 11863 | Py_DECREF(str2); |
| 11864 | return result; |
| 11865 | } |
| 11866 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11867 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11868 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11869 | \n\ |
| 11870 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11871 | old replaced by new. If the optional argument count is\n\ |
| 11872 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11873 | |
| 11874 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11875 | unicode_replace(PyObject *self, PyObject *args) |
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 | PyObject *str1; |
| 11878 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11879 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11880 | PyObject *result; |
| 11881 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11882 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11884 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11885 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | str1 = PyUnicode_FromObject(str1); |
| 11887 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11888 | return NULL; |
| 11889 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11890 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11891 | Py_DECREF(str1); |
| 11892 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11893 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11894 | |
| 11895 | result = replace(self, str1, str2, maxcount); |
| 11896 | |
| 11897 | Py_DECREF(str1); |
| 11898 | Py_DECREF(str2); |
| 11899 | return result; |
| 11900 | } |
| 11901 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11902 | static PyObject * |
| 11903 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11905 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11906 | Py_ssize_t isize; |
| 11907 | Py_ssize_t osize, squote, dquote, i, o; |
| 11908 | Py_UCS4 max, quote; |
| 11909 | int ikind, okind; |
| 11910 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11913 | return NULL; |
| 11914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11915 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11916 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11917 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | /* Compute length of output, quote characters, and |
| 11919 | maximum character */ |
| 11920 | osize = 2; /* quotes */ |
| 11921 | max = 127; |
| 11922 | squote = dquote = 0; |
| 11923 | ikind = PyUnicode_KIND(unicode); |
| 11924 | for (i = 0; i < isize; i++) { |
| 11925 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11926 | switch (ch) { |
| 11927 | case '\'': squote++; osize++; break; |
| 11928 | case '"': dquote++; osize++; break; |
| 11929 | case '\\': case '\t': case '\r': case '\n': |
| 11930 | osize += 2; break; |
| 11931 | default: |
| 11932 | /* Fast-path ASCII */ |
| 11933 | if (ch < ' ' || ch == 0x7f) |
| 11934 | osize += 4; /* \xHH */ |
| 11935 | else if (ch < 0x7f) |
| 11936 | osize++; |
| 11937 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11938 | osize++; |
| 11939 | max = ch > max ? ch : max; |
| 11940 | } |
| 11941 | else if (ch < 0x100) |
| 11942 | osize += 4; /* \xHH */ |
| 11943 | else if (ch < 0x10000) |
| 11944 | osize += 6; /* \uHHHH */ |
| 11945 | else |
| 11946 | osize += 10; /* \uHHHHHHHH */ |
| 11947 | } |
| 11948 | } |
| 11949 | |
| 11950 | quote = '\''; |
| 11951 | if (squote) { |
| 11952 | if (dquote) |
| 11953 | /* Both squote and dquote present. Use squote, |
| 11954 | and escape them */ |
| 11955 | osize += squote; |
| 11956 | else |
| 11957 | quote = '"'; |
| 11958 | } |
| 11959 | |
| 11960 | repr = PyUnicode_New(osize, max); |
| 11961 | if (repr == NULL) |
| 11962 | return NULL; |
| 11963 | okind = PyUnicode_KIND(repr); |
| 11964 | odata = PyUnicode_DATA(repr); |
| 11965 | |
| 11966 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11967 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11968 | |
| 11969 | for (i = 0, o = 1; i < isize; i++) { |
| 11970 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11971 | |
| 11972 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11973 | if ((ch == quote) || (ch == '\\')) { |
| 11974 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11975 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11976 | continue; |
| 11977 | } |
| 11978 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11979 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11980 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11981 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11982 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11983 | } |
| 11984 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11985 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11986 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11987 | } |
| 11988 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11989 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11990 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11991 | } |
| 11992 | |
| 11993 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11994 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11995 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11996 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11997 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11998 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11999 | } |
| 12000 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12001 | /* Copy ASCII characters as-is */ |
| 12002 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12003 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12004 | } |
| 12005 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12006 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12007 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12008 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12009 | (categories Z* and C* except ASCII space) |
| 12010 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12012 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12013 | if (ch <= 0xff) { |
| 12014 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12015 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12016 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12017 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12018 | } |
| 12019 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12020 | else if (ch >= 0x10000) { |
| 12021 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12022 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12023 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12024 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12025 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12026 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12027 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12028 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12029 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12030 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12031 | } |
| 12032 | /* Map 16-bit characters to '\uxxxx' */ |
| 12033 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12035 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12036 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12037 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12038 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12039 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12040 | } |
| 12041 | } |
| 12042 | /* Copy characters as-is */ |
| 12043 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12044 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12045 | } |
| 12046 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12048 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12049 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12050 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12051 | } |
| 12052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12053 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12054 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12055 | \n\ |
| 12056 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12057 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12058 | arguments start and end are interpreted as in slice notation.\n\ |
| 12059 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12060 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12061 | |
| 12062 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12064 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12065 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12066 | Py_ssize_t start; |
| 12067 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12068 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12069 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12070 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12071 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12072 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12073 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12074 | if (PyUnicode_READY(self) == -1) |
| 12075 | return NULL; |
| 12076 | if (PyUnicode_READY(substring) == -1) |
| 12077 | return NULL; |
| 12078 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12079 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12080 | |
| 12081 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12083 | if (result == -2) |
| 12084 | return NULL; |
| 12085 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12086 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | } |
| 12088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12089 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12090 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12091 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12092 | 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] | 12093 | |
| 12094 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12095 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12096 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12097 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12098 | Py_ssize_t start; |
| 12099 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12100 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12102 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12103 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12104 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12105 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12106 | if (PyUnicode_READY(self) == -1) |
| 12107 | return NULL; |
| 12108 | if (PyUnicode_READY(substring) == -1) |
| 12109 | return NULL; |
| 12110 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12111 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12112 | |
| 12113 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12115 | if (result == -2) |
| 12116 | return NULL; |
| 12117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12118 | if (result < 0) { |
| 12119 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12120 | return NULL; |
| 12121 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12122 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12123 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12124 | } |
| 12125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12126 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12127 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12128 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12129 | 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] | 12130 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12131 | |
| 12132 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12133 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12134 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12135 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12136 | Py_UCS4 fillchar = ' '; |
| 12137 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12138 | 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] | 12139 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12140 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12141 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | return NULL; |
| 12143 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12144 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12145 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12146 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12147 | } |
| 12148 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12149 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12150 | } |
| 12151 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12152 | PyObject * |
| 12153 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | { |
| 12155 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12157 | s = PyUnicode_FromObject(s); |
| 12158 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12159 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12160 | if (sep != NULL) { |
| 12161 | sep = PyUnicode_FromObject(sep); |
| 12162 | if (sep == NULL) { |
| 12163 | Py_DECREF(s); |
| 12164 | return NULL; |
| 12165 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | } |
| 12167 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12168 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12169 | |
| 12170 | Py_DECREF(s); |
| 12171 | Py_XDECREF(sep); |
| 12172 | return result; |
| 12173 | } |
| 12174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12175 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12176 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | \n\ |
| 12178 | Return a list of the words in S, using sep as the\n\ |
| 12179 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12180 | 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] | 12181 | whitespace string is a separator and empty strings are\n\ |
| 12182 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12183 | |
| 12184 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12185 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12186 | { |
| 12187 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12188 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12189 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12190 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | return NULL; |
| 12192 | |
| 12193 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12194 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12196 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12197 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12198 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | } |
| 12200 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12201 | PyObject * |
| 12202 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12203 | { |
| 12204 | PyObject* str_obj; |
| 12205 | PyObject* sep_obj; |
| 12206 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12207 | int kind1, kind2, kind; |
| 12208 | void *buf1 = NULL, *buf2 = NULL; |
| 12209 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12210 | |
| 12211 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12212 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12213 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12214 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12215 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12216 | Py_DECREF(str_obj); |
| 12217 | return NULL; |
| 12218 | } |
| 12219 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12220 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12222 | kind = Py_MAX(kind1, kind2); |
| 12223 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12224 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12225 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12226 | if (!buf1) |
| 12227 | goto onError; |
| 12228 | buf2 = PyUnicode_DATA(sep_obj); |
| 12229 | if (kind2 != kind) |
| 12230 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12231 | if (!buf2) |
| 12232 | goto onError; |
| 12233 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12234 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12235 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12236 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12237 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12238 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12239 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12240 | else |
| 12241 | 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] | 12242 | break; |
| 12243 | case PyUnicode_2BYTE_KIND: |
| 12244 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12245 | break; |
| 12246 | case PyUnicode_4BYTE_KIND: |
| 12247 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12248 | break; |
| 12249 | default: |
| 12250 | assert(0); |
| 12251 | out = 0; |
| 12252 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12253 | |
| 12254 | Py_DECREF(sep_obj); |
| 12255 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | if (kind1 != kind) |
| 12257 | PyMem_Free(buf1); |
| 12258 | if (kind2 != kind) |
| 12259 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12260 | |
| 12261 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12262 | onError: |
| 12263 | Py_DECREF(sep_obj); |
| 12264 | Py_DECREF(str_obj); |
| 12265 | if (kind1 != kind && buf1) |
| 12266 | PyMem_Free(buf1); |
| 12267 | if (kind2 != kind && buf2) |
| 12268 | PyMem_Free(buf2); |
| 12269 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12270 | } |
| 12271 | |
| 12272 | |
| 12273 | PyObject * |
| 12274 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12275 | { |
| 12276 | PyObject* str_obj; |
| 12277 | PyObject* sep_obj; |
| 12278 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12279 | int kind1, kind2, kind; |
| 12280 | void *buf1 = NULL, *buf2 = NULL; |
| 12281 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12282 | |
| 12283 | str_obj = PyUnicode_FromObject(str_in); |
| 12284 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12285 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12286 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12287 | if (!sep_obj) { |
| 12288 | Py_DECREF(str_obj); |
| 12289 | return NULL; |
| 12290 | } |
| 12291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12292 | kind1 = PyUnicode_KIND(str_in); |
| 12293 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12294 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12295 | buf1 = PyUnicode_DATA(str_in); |
| 12296 | if (kind1 != kind) |
| 12297 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12298 | if (!buf1) |
| 12299 | goto onError; |
| 12300 | buf2 = PyUnicode_DATA(sep_obj); |
| 12301 | if (kind2 != kind) |
| 12302 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12303 | if (!buf2) |
| 12304 | goto onError; |
| 12305 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12306 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12307 | |
| 12308 | switch(PyUnicode_KIND(str_in)) { |
| 12309 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12310 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12311 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12312 | else |
| 12313 | 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] | 12314 | break; |
| 12315 | case PyUnicode_2BYTE_KIND: |
| 12316 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12317 | break; |
| 12318 | case PyUnicode_4BYTE_KIND: |
| 12319 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12320 | break; |
| 12321 | default: |
| 12322 | assert(0); |
| 12323 | out = 0; |
| 12324 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12325 | |
| 12326 | Py_DECREF(sep_obj); |
| 12327 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | if (kind1 != kind) |
| 12329 | PyMem_Free(buf1); |
| 12330 | if (kind2 != kind) |
| 12331 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12332 | |
| 12333 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | onError: |
| 12335 | Py_DECREF(sep_obj); |
| 12336 | Py_DECREF(str_obj); |
| 12337 | if (kind1 != kind && buf1) |
| 12338 | PyMem_Free(buf1); |
| 12339 | if (kind2 != kind && buf2) |
| 12340 | PyMem_Free(buf2); |
| 12341 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12342 | } |
| 12343 | |
| 12344 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12345 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12346 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12347 | 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] | 12348 | 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] | 12349 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12350 | |
| 12351 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12352 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12353 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12354 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12355 | } |
| 12356 | |
| 12357 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12358 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12359 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12360 | 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] | 12361 | 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] | 12362 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12363 | |
| 12364 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12365 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12366 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12367 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12368 | } |
| 12369 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12370 | PyObject * |
| 12371 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12372 | { |
| 12373 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12374 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12375 | s = PyUnicode_FromObject(s); |
| 12376 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12377 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12378 | if (sep != NULL) { |
| 12379 | sep = PyUnicode_FromObject(sep); |
| 12380 | if (sep == NULL) { |
| 12381 | Py_DECREF(s); |
| 12382 | return NULL; |
| 12383 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12384 | } |
| 12385 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12386 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12387 | |
| 12388 | Py_DECREF(s); |
| 12389 | Py_XDECREF(sep); |
| 12390 | return result; |
| 12391 | } |
| 12392 | |
| 12393 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12394 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12395 | \n\ |
| 12396 | Return a list of the words in S, using sep as the\n\ |
| 12397 | delimiter string, starting at the end of the string and\n\ |
| 12398 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12399 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12400 | is a separator."); |
| 12401 | |
| 12402 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12403 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12404 | { |
| 12405 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12406 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12407 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12408 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12409 | return NULL; |
| 12410 | |
| 12411 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12412 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12413 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12414 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12415 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12416 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12417 | } |
| 12418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12419 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12420 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12421 | \n\ |
| 12422 | 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] | 12423 | 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] | 12424 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12425 | |
| 12426 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12427 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12428 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12429 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12430 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12431 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12432 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12433 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12434 | return NULL; |
| 12435 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12436 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12437 | } |
| 12438 | |
| 12439 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12440 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12441 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12442 | if (PyUnicode_CheckExact(self)) { |
| 12443 | Py_INCREF(self); |
| 12444 | return self; |
| 12445 | } else |
| 12446 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12447 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12448 | } |
| 12449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12450 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12451 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | \n\ |
| 12453 | 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] | 12454 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12455 | |
| 12456 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12457 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12458 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12459 | return fixup(self, fixswapcase); |
| 12460 | } |
| 12461 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12462 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12463 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12464 | \n\ |
| 12465 | Return a translation table usable for str.translate().\n\ |
| 12466 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12467 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12468 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12469 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12470 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12471 | character at the same position in y. If there is a third argument, it\n\ |
| 12472 | must be a string, whose characters will be mapped to None in the result."); |
| 12473 | |
| 12474 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12475 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12476 | { |
| 12477 | PyObject *x, *y = NULL, *z = NULL; |
| 12478 | PyObject *new = NULL, *key, *value; |
| 12479 | Py_ssize_t i = 0; |
| 12480 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12481 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12482 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12483 | return NULL; |
| 12484 | new = PyDict_New(); |
| 12485 | if (!new) |
| 12486 | return NULL; |
| 12487 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12488 | int x_kind, y_kind, z_kind; |
| 12489 | void *x_data, *y_data, *z_data; |
| 12490 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12491 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12492 | if (!PyUnicode_Check(x)) { |
| 12493 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12494 | "be a string if there is a second argument"); |
| 12495 | goto err; |
| 12496 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12497 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12498 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12499 | "arguments must have equal length"); |
| 12500 | goto err; |
| 12501 | } |
| 12502 | /* 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] | 12503 | x_kind = PyUnicode_KIND(x); |
| 12504 | y_kind = PyUnicode_KIND(y); |
| 12505 | x_data = PyUnicode_DATA(x); |
| 12506 | y_data = PyUnicode_DATA(y); |
| 12507 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12508 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12509 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12510 | if (!key || !value) |
| 12511 | goto err; |
| 12512 | res = PyDict_SetItem(new, key, value); |
| 12513 | Py_DECREF(key); |
| 12514 | Py_DECREF(value); |
| 12515 | if (res < 0) |
| 12516 | goto err; |
| 12517 | } |
| 12518 | /* create entries for deleting chars in z */ |
| 12519 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12520 | z_kind = PyUnicode_KIND(z); |
| 12521 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12522 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12523 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12524 | if (!key) |
| 12525 | goto err; |
| 12526 | res = PyDict_SetItem(new, key, Py_None); |
| 12527 | Py_DECREF(key); |
| 12528 | if (res < 0) |
| 12529 | goto err; |
| 12530 | } |
| 12531 | } |
| 12532 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12533 | int kind; |
| 12534 | void *data; |
| 12535 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12536 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12537 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12538 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12539 | "to maketrans it must be a dict"); |
| 12540 | goto err; |
| 12541 | } |
| 12542 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12543 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12544 | if (PyUnicode_Check(key)) { |
| 12545 | /* convert string keys to integer keys */ |
| 12546 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12547 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12548 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12549 | "table must be of length 1"); |
| 12550 | goto err; |
| 12551 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12552 | kind = PyUnicode_KIND(key); |
| 12553 | data = PyUnicode_DATA(key); |
| 12554 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12555 | if (!newkey) |
| 12556 | goto err; |
| 12557 | res = PyDict_SetItem(new, newkey, value); |
| 12558 | Py_DECREF(newkey); |
| 12559 | if (res < 0) |
| 12560 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12561 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12562 | /* just keep integer keys */ |
| 12563 | if (PyDict_SetItem(new, key, value) < 0) |
| 12564 | goto err; |
| 12565 | } else { |
| 12566 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12567 | "be strings or integers"); |
| 12568 | goto err; |
| 12569 | } |
| 12570 | } |
| 12571 | } |
| 12572 | return new; |
| 12573 | err: |
| 12574 | Py_DECREF(new); |
| 12575 | return NULL; |
| 12576 | } |
| 12577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12578 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12579 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12580 | \n\ |
| 12581 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12582 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12583 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12584 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12585 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12586 | |
| 12587 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12588 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12589 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12590 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | } |
| 12592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12593 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12594 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12595 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12596 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12597 | |
| 12598 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12599 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12601 | return fixup(self, fixupper); |
| 12602 | } |
| 12603 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12604 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12605 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12606 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12607 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12608 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12609 | |
| 12610 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12611 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12612 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12613 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12614 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12615 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12616 | int kind; |
| 12617 | void *data; |
| 12618 | Py_UCS4 chr; |
| 12619 | |
| 12620 | if (PyUnicode_READY(self) == -1) |
| 12621 | return NULL; |
| 12622 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12623 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12624 | return NULL; |
| 12625 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12626 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12627 | if (PyUnicode_CheckExact(self)) { |
| 12628 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12629 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12630 | } |
| 12631 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12632 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | } |
| 12634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12635 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | |
| 12637 | u = pad(self, fill, 0, '0'); |
| 12638 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12639 | if (u == NULL) |
| 12640 | return NULL; |
| 12641 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12642 | kind = PyUnicode_KIND(u); |
| 12643 | data = PyUnicode_DATA(u); |
| 12644 | chr = PyUnicode_READ(kind, data, fill); |
| 12645 | |
| 12646 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12647 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12648 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12649 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | } |
| 12651 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12652 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12653 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12655 | |
| 12656 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12657 | static PyObject * |
| 12658 | unicode__decimal2ascii(PyObject *self) |
| 12659 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12660 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12661 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12662 | #endif |
| 12663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12664 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12665 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12666 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12667 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12668 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12669 | With optional end, stop comparing S at that position.\n\ |
| 12670 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12671 | |
| 12672 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12673 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12674 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12675 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12676 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12677 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12678 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12679 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12680 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12681 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12682 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12683 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12684 | if (PyTuple_Check(subobj)) { |
| 12685 | Py_ssize_t i; |
| 12686 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12687 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12688 | if (substring == NULL) |
| 12689 | return NULL; |
| 12690 | result = tailmatch(self, substring, start, end, -1); |
| 12691 | Py_DECREF(substring); |
| 12692 | if (result) { |
| 12693 | Py_RETURN_TRUE; |
| 12694 | } |
| 12695 | } |
| 12696 | /* nothing matched */ |
| 12697 | Py_RETURN_FALSE; |
| 12698 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12699 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12700 | if (substring == NULL) { |
| 12701 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12702 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12703 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12704 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12705 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12706 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12707 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12708 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12709 | } |
| 12710 | |
| 12711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12712 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12713 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12714 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12715 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12716 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12717 | With optional end, stop comparing S at that position.\n\ |
| 12718 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12719 | |
| 12720 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12721 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12722 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12723 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12724 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12725 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12726 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12727 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12728 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12729 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12730 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12731 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12732 | if (PyTuple_Check(subobj)) { |
| 12733 | Py_ssize_t i; |
| 12734 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12735 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12736 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12737 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12738 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12739 | result = tailmatch(self, substring, start, end, +1); |
| 12740 | Py_DECREF(substring); |
| 12741 | if (result) { |
| 12742 | Py_RETURN_TRUE; |
| 12743 | } |
| 12744 | } |
| 12745 | Py_RETURN_FALSE; |
| 12746 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12747 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12748 | if (substring == NULL) { |
| 12749 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12750 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12751 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12752 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12753 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12754 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12755 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12756 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12757 | } |
| 12758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12759 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12760 | |
| 12761 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12762 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12763 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12764 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12765 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12766 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12767 | PyDoc_STRVAR(format_map__doc__, |
| 12768 | "S.format_map(mapping) -> str\n\ |
| 12769 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12770 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12771 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12772 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12773 | static PyObject * |
| 12774 | unicode__format__(PyObject* self, PyObject* args) |
| 12775 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12776 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12777 | |
| 12778 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12779 | return NULL; |
| 12780 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12781 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12782 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12783 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12784 | } |
| 12785 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12786 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12787 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12788 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12789 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12790 | |
| 12791 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12792 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12793 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12794 | Py_ssize_t size; |
| 12795 | |
| 12796 | /* If it's a compact object, account for base structure + |
| 12797 | character data. */ |
| 12798 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12799 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12800 | else if (PyUnicode_IS_COMPACT(v)) |
| 12801 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12802 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12803 | else { |
| 12804 | /* If it is a two-block object, account for base object, and |
| 12805 | for character block if present. */ |
| 12806 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12807 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12808 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12809 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12810 | } |
| 12811 | /* 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] | 12812 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12813 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12814 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12815 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12816 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12817 | |
| 12818 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12819 | } |
| 12820 | |
| 12821 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12822 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12823 | |
| 12824 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12825 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12826 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12827 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12828 | if (!copy) |
| 12829 | return NULL; |
| 12830 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12831 | } |
| 12832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12833 | static PyMethodDef unicode_methods[] = { |
| 12834 | |
| 12835 | /* Order is according to common usage: often used methods should |
| 12836 | appear first, since lookup is done sequentially. */ |
| 12837 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12838 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12839 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12840 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12841 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12842 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12843 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12844 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12845 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12846 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12847 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12848 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12849 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12850 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12851 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12852 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12853 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12854 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12855 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12856 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12857 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12858 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12859 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12860 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12861 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12862 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12863 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12864 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12865 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12866 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12867 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12868 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12869 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12870 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12871 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12872 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12873 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12874 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12875 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12876 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12877 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12878 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12879 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12880 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12881 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12882 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12883 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12884 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12885 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 | #endif |
| 12887 | |
| 12888 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12889 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12890 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12891 | #endif |
| 12892 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12893 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12894 | {NULL, NULL} |
| 12895 | }; |
| 12896 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12897 | static PyObject * |
| 12898 | unicode_mod(PyObject *v, PyObject *w) |
| 12899 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12900 | if (!PyUnicode_Check(v)) |
| 12901 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12902 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12903 | } |
| 12904 | |
| 12905 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12906 | 0, /*nb_add*/ |
| 12907 | 0, /*nb_subtract*/ |
| 12908 | 0, /*nb_multiply*/ |
| 12909 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12910 | }; |
| 12911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12912 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12913 | (lenfunc) unicode_length, /* sq_length */ |
| 12914 | PyUnicode_Concat, /* sq_concat */ |
| 12915 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12916 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12917 | 0, /* sq_slice */ |
| 12918 | 0, /* sq_ass_item */ |
| 12919 | 0, /* sq_ass_slice */ |
| 12920 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12921 | }; |
| 12922 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12923 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12924 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12925 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12926 | if (PyUnicode_READY(self) == -1) |
| 12927 | return NULL; |
| 12928 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12929 | if (PyIndex_Check(item)) { |
| 12930 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12931 | if (i == -1 && PyErr_Occurred()) |
| 12932 | return NULL; |
| 12933 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12934 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12935 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12936 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12937 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12938 | PyObject *result; |
| 12939 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12940 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12941 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12943 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12944 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12945 | return NULL; |
| 12946 | } |
| 12947 | |
| 12948 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12949 | return PyUnicode_New(0, 0); |
| 12950 | } else if (start == 0 && step == 1 && |
| 12951 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12952 | PyUnicode_CheckExact(self)) { |
| 12953 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12954 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12955 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12956 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12957 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12958 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12959 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12960 | src_kind = PyUnicode_KIND(self); |
| 12961 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 12962 | if (!PyUnicode_IS_ASCII(self)) { |
| 12963 | kind_limit = kind_maxchar_limit(src_kind); |
| 12964 | max_char = 0; |
| 12965 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12966 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12967 | if (ch > max_char) { |
| 12968 | max_char = ch; |
| 12969 | if (max_char >= kind_limit) |
| 12970 | break; |
| 12971 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12972 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12973 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 12974 | else |
| 12975 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12976 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12977 | if (result == NULL) |
| 12978 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12979 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12980 | dest_data = PyUnicode_DATA(result); |
| 12981 | |
| 12982 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12983 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12984 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12985 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12986 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12987 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12988 | } else { |
| 12989 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12990 | return NULL; |
| 12991 | } |
| 12992 | } |
| 12993 | |
| 12994 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12995 | (lenfunc)unicode_length, /* mp_length */ |
| 12996 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12997 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12998 | }; |
| 12999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13001 | /* Helpers for PyUnicode_Format() */ |
| 13002 | |
| 13003 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13004 | 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] | 13005 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13006 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13007 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13008 | (*p_argidx)++; |
| 13009 | if (arglen < 0) |
| 13010 | return args; |
| 13011 | else |
| 13012 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13013 | } |
| 13014 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13015 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13016 | return NULL; |
| 13017 | } |
| 13018 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13019 | /* 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] | 13020 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13021 | static PyObject * |
| 13022 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13023 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13024 | char *p; |
| 13025 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13026 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13028 | x = PyFloat_AsDouble(v); |
| 13029 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13030 | return NULL; |
| 13031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13032 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13033 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13034 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13035 | p = PyOS_double_to_string(x, type, prec, |
| 13036 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13037 | if (p == NULL) |
| 13038 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13039 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13040 | PyMem_Free(p); |
| 13041 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13042 | } |
| 13043 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13044 | static PyObject* |
| 13045 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13046 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13047 | char *buf; |
| 13048 | int len; |
| 13049 | PyObject *str; /* temporary string object. */ |
| 13050 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13051 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13052 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13053 | if (!str) |
| 13054 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13055 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13056 | Py_DECREF(str); |
| 13057 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13058 | } |
| 13059 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13060 | static Py_UCS4 |
| 13061 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13062 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13063 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13064 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13065 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13066 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13067 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13068 | goto onError; |
| 13069 | } |
| 13070 | else { |
| 13071 | /* Integer input truncated to a character */ |
| 13072 | long x; |
| 13073 | x = PyLong_AsLong(v); |
| 13074 | if (x == -1 && PyErr_Occurred()) |
| 13075 | goto onError; |
| 13076 | |
| 13077 | if (x < 0 || x > 0x10ffff) { |
| 13078 | PyErr_SetString(PyExc_OverflowError, |
| 13079 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13080 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13081 | } |
| 13082 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13083 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13084 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13085 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13086 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13087 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13088 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13089 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13090 | } |
| 13091 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13092 | static int |
| 13093 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13094 | { |
| 13095 | int r; |
| 13096 | assert(count > 0); |
| 13097 | assert(PyUnicode_Check(obj)); |
| 13098 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13099 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13100 | if (repeated == NULL) |
| 13101 | return -1; |
| 13102 | r = _PyAccu_Accumulate(acc, repeated); |
| 13103 | Py_DECREF(repeated); |
| 13104 | return r; |
| 13105 | } |
| 13106 | else { |
| 13107 | do { |
| 13108 | if (_PyAccu_Accumulate(acc, obj)) |
| 13109 | return -1; |
| 13110 | } while (--count); |
| 13111 | return 0; |
| 13112 | } |
| 13113 | } |
| 13114 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13115 | PyObject * |
| 13116 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13117 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13118 | void *fmt; |
| 13119 | int fmtkind; |
| 13120 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13121 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13122 | int r; |
| 13123 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13124 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13125 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13126 | PyObject *temp = NULL; |
| 13127 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13128 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13129 | _PyAccu acc; |
| 13130 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13131 | |
| 13132 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13133 | return NULL; |
| 13134 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13135 | return NULL; |
| 13136 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13137 | return NULL; |
| 13138 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13139 | return NULL; |
| 13140 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13141 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13143 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13144 | PyErr_BadInternalCall(); |
| 13145 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13146 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13147 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13148 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13149 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13150 | if (_PyAccu_Init(&acc)) |
| 13151 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13152 | fmt = PyUnicode_DATA(uformat); |
| 13153 | fmtkind = PyUnicode_KIND(uformat); |
| 13154 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13155 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13157 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13158 | arglen = PyTuple_Size(args); |
| 13159 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13160 | } |
| 13161 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13162 | arglen = -1; |
| 13163 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13164 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13165 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13166 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13167 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | |
| 13169 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13170 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13171 | PyObject *nonfmt; |
| 13172 | Py_ssize_t nonfmtpos; |
| 13173 | nonfmtpos = fmtpos++; |
| 13174 | while (fmtcnt >= 0 && |
| 13175 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13176 | fmtpos++; |
| 13177 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13178 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13179 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13180 | if (nonfmt == NULL) |
| 13181 | goto onError; |
| 13182 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13183 | Py_DECREF(nonfmt); |
| 13184 | if (r) |
| 13185 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13186 | } |
| 13187 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13188 | /* Got a format specifier */ |
| 13189 | int flags = 0; |
| 13190 | Py_ssize_t width = -1; |
| 13191 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13192 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13193 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13194 | int isnumok; |
| 13195 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13196 | void *pbuf = NULL; |
| 13197 | Py_ssize_t pindex, len; |
| 13198 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13199 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13200 | fmtpos++; |
| 13201 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13202 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13203 | Py_ssize_t keylen; |
| 13204 | PyObject *key; |
| 13205 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13206 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13207 | if (dict == NULL) { |
| 13208 | PyErr_SetString(PyExc_TypeError, |
| 13209 | "format requires a mapping"); |
| 13210 | goto onError; |
| 13211 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13212 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13213 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13214 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13215 | /* Skip over balanced parentheses */ |
| 13216 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13217 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13218 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13219 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13220 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13221 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13222 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13223 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13224 | if (fmtcnt < 0 || pcount > 0) { |
| 13225 | PyErr_SetString(PyExc_ValueError, |
| 13226 | "incomplete format key"); |
| 13227 | goto onError; |
| 13228 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13229 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13230 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13231 | if (key == NULL) |
| 13232 | goto onError; |
| 13233 | if (args_owned) { |
| 13234 | Py_DECREF(args); |
| 13235 | args_owned = 0; |
| 13236 | } |
| 13237 | args = PyObject_GetItem(dict, key); |
| 13238 | Py_DECREF(key); |
| 13239 | if (args == NULL) { |
| 13240 | goto onError; |
| 13241 | } |
| 13242 | args_owned = 1; |
| 13243 | arglen = -1; |
| 13244 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13245 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13246 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13247 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13248 | case '-': flags |= F_LJUST; continue; |
| 13249 | case '+': flags |= F_SIGN; continue; |
| 13250 | case ' ': flags |= F_BLANK; continue; |
| 13251 | case '#': flags |= F_ALT; continue; |
| 13252 | case '0': flags |= F_ZERO; continue; |
| 13253 | } |
| 13254 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13255 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13256 | if (c == '*') { |
| 13257 | v = getnextarg(args, arglen, &argidx); |
| 13258 | if (v == NULL) |
| 13259 | goto onError; |
| 13260 | if (!PyLong_Check(v)) { |
| 13261 | PyErr_SetString(PyExc_TypeError, |
| 13262 | "* wants int"); |
| 13263 | goto onError; |
| 13264 | } |
| 13265 | width = PyLong_AsLong(v); |
| 13266 | if (width == -1 && PyErr_Occurred()) |
| 13267 | goto onError; |
| 13268 | if (width < 0) { |
| 13269 | flags |= F_LJUST; |
| 13270 | width = -width; |
| 13271 | } |
| 13272 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13273 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13274 | } |
| 13275 | else if (c >= '0' && c <= '9') { |
| 13276 | width = c - '0'; |
| 13277 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13278 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13279 | if (c < '0' || c > '9') |
| 13280 | break; |
| 13281 | if ((width*10) / 10 != width) { |
| 13282 | PyErr_SetString(PyExc_ValueError, |
| 13283 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13284 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13285 | } |
| 13286 | width = width*10 + (c - '0'); |
| 13287 | } |
| 13288 | } |
| 13289 | if (c == '.') { |
| 13290 | prec = 0; |
| 13291 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13292 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13293 | if (c == '*') { |
| 13294 | v = getnextarg(args, arglen, &argidx); |
| 13295 | if (v == NULL) |
| 13296 | goto onError; |
| 13297 | if (!PyLong_Check(v)) { |
| 13298 | PyErr_SetString(PyExc_TypeError, |
| 13299 | "* wants int"); |
| 13300 | goto onError; |
| 13301 | } |
| 13302 | prec = PyLong_AsLong(v); |
| 13303 | if (prec == -1 && PyErr_Occurred()) |
| 13304 | goto onError; |
| 13305 | if (prec < 0) |
| 13306 | prec = 0; |
| 13307 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13308 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13309 | } |
| 13310 | else if (c >= '0' && c <= '9') { |
| 13311 | prec = c - '0'; |
| 13312 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13313 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13314 | if (c < '0' || c > '9') |
| 13315 | break; |
| 13316 | if ((prec*10) / 10 != prec) { |
| 13317 | PyErr_SetString(PyExc_ValueError, |
| 13318 | "prec too big"); |
| 13319 | goto onError; |
| 13320 | } |
| 13321 | prec = prec*10 + (c - '0'); |
| 13322 | } |
| 13323 | } |
| 13324 | } /* prec */ |
| 13325 | if (fmtcnt >= 0) { |
| 13326 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13327 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13328 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13329 | } |
| 13330 | } |
| 13331 | if (fmtcnt < 0) { |
| 13332 | PyErr_SetString(PyExc_ValueError, |
| 13333 | "incomplete format"); |
| 13334 | goto onError; |
| 13335 | } |
| 13336 | if (c != '%') { |
| 13337 | v = getnextarg(args, arglen, &argidx); |
| 13338 | if (v == NULL) |
| 13339 | goto onError; |
| 13340 | } |
| 13341 | sign = 0; |
| 13342 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13343 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13344 | switch (c) { |
| 13345 | |
| 13346 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13347 | _PyAccu_Accumulate(&acc, percent); |
| 13348 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13349 | |
| 13350 | case 's': |
| 13351 | case 'r': |
| 13352 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13353 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13354 | temp = v; |
| 13355 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13356 | } |
| 13357 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13358 | if (c == 's') |
| 13359 | temp = PyObject_Str(v); |
| 13360 | else if (c == 'r') |
| 13361 | temp = PyObject_Repr(v); |
| 13362 | else |
| 13363 | temp = PyObject_ASCII(v); |
| 13364 | if (temp == NULL) |
| 13365 | goto onError; |
| 13366 | if (PyUnicode_Check(temp)) |
| 13367 | /* nothing to do */; |
| 13368 | else { |
| 13369 | Py_DECREF(temp); |
| 13370 | PyErr_SetString(PyExc_TypeError, |
| 13371 | "%s argument has non-string str()"); |
| 13372 | goto onError; |
| 13373 | } |
| 13374 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13375 | if (PyUnicode_READY(temp) == -1) { |
| 13376 | Py_CLEAR(temp); |
| 13377 | goto onError; |
| 13378 | } |
| 13379 | pbuf = PyUnicode_DATA(temp); |
| 13380 | kind = PyUnicode_KIND(temp); |
| 13381 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13382 | if (prec >= 0 && len > prec) |
| 13383 | len = prec; |
| 13384 | break; |
| 13385 | |
| 13386 | case 'i': |
| 13387 | case 'd': |
| 13388 | case 'u': |
| 13389 | case 'o': |
| 13390 | case 'x': |
| 13391 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13392 | isnumok = 0; |
| 13393 | if (PyNumber_Check(v)) { |
| 13394 | PyObject *iobj=NULL; |
| 13395 | |
| 13396 | if (PyLong_Check(v)) { |
| 13397 | iobj = v; |
| 13398 | Py_INCREF(iobj); |
| 13399 | } |
| 13400 | else { |
| 13401 | iobj = PyNumber_Long(v); |
| 13402 | } |
| 13403 | if (iobj!=NULL) { |
| 13404 | if (PyLong_Check(iobj)) { |
| 13405 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13406 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13407 | Py_DECREF(iobj); |
| 13408 | if (!temp) |
| 13409 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13410 | if (PyUnicode_READY(temp) == -1) { |
| 13411 | Py_CLEAR(temp); |
| 13412 | goto onError; |
| 13413 | } |
| 13414 | pbuf = PyUnicode_DATA(temp); |
| 13415 | kind = PyUnicode_KIND(temp); |
| 13416 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13417 | sign = 1; |
| 13418 | } |
| 13419 | else { |
| 13420 | Py_DECREF(iobj); |
| 13421 | } |
| 13422 | } |
| 13423 | } |
| 13424 | if (!isnumok) { |
| 13425 | PyErr_Format(PyExc_TypeError, |
| 13426 | "%%%c format: a number is required, " |
| 13427 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13428 | goto onError; |
| 13429 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13430 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13431 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13432 | fillobj = zero; |
| 13433 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13434 | break; |
| 13435 | |
| 13436 | case 'e': |
| 13437 | case 'E': |
| 13438 | case 'f': |
| 13439 | case 'F': |
| 13440 | case 'g': |
| 13441 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13442 | temp = formatfloat(v, flags, prec, c); |
| 13443 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13444 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13445 | if (PyUnicode_READY(temp) == -1) { |
| 13446 | Py_CLEAR(temp); |
| 13447 | goto onError; |
| 13448 | } |
| 13449 | pbuf = PyUnicode_DATA(temp); |
| 13450 | kind = PyUnicode_KIND(temp); |
| 13451 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13452 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13453 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13454 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13455 | fillobj = zero; |
| 13456 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13457 | break; |
| 13458 | |
| 13459 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13460 | { |
| 13461 | Py_UCS4 ch = formatchar(v); |
| 13462 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13463 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13464 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13465 | if (temp == NULL) |
| 13466 | goto onError; |
| 13467 | pbuf = PyUnicode_DATA(temp); |
| 13468 | kind = PyUnicode_KIND(temp); |
| 13469 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13471 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13472 | |
| 13473 | default: |
| 13474 | PyErr_Format(PyExc_ValueError, |
| 13475 | "unsupported format character '%c' (0x%x) " |
| 13476 | "at index %zd", |
| 13477 | (31<=c && c<=126) ? (char)c : '?', |
| 13478 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13479 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13480 | goto onError; |
| 13481 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13482 | /* pbuf is initialized here. */ |
| 13483 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13484 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13485 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13486 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13487 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13488 | pindex++; |
| 13489 | } |
| 13490 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13491 | signobj = plus; |
| 13492 | len--; |
| 13493 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13494 | } |
| 13495 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13496 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13498 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13499 | else |
| 13500 | sign = 0; |
| 13501 | } |
| 13502 | if (width < len) |
| 13503 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13504 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13505 | if (fill != ' ') { |
| 13506 | assert(signobj != NULL); |
| 13507 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13508 | goto onError; |
| 13509 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13510 | if (width > len) |
| 13511 | width--; |
| 13512 | } |
| 13513 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13514 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13515 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13516 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13517 | second = get_latin1_char( |
| 13518 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13519 | pindex += 2; |
| 13520 | if (second == NULL || |
| 13521 | _PyAccu_Accumulate(&acc, zero) || |
| 13522 | _PyAccu_Accumulate(&acc, second)) |
| 13523 | goto onError; |
| 13524 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13525 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13526 | width -= 2; |
| 13527 | if (width < 0) |
| 13528 | width = 0; |
| 13529 | len -= 2; |
| 13530 | } |
| 13531 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13532 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13533 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13534 | goto onError; |
| 13535 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13536 | } |
| 13537 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13538 | if (sign) { |
| 13539 | assert(signobj != NULL); |
| 13540 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13541 | goto onError; |
| 13542 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13543 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13544 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13545 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13546 | second = get_latin1_char( |
| 13547 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13548 | pindex += 2; |
| 13549 | if (second == NULL || |
| 13550 | _PyAccu_Accumulate(&acc, zero) || |
| 13551 | _PyAccu_Accumulate(&acc, second)) |
| 13552 | goto onError; |
| 13553 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13554 | } |
| 13555 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13556 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13557 | if (temp != NULL) { |
| 13558 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13559 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13560 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13561 | else { |
| 13562 | const char *p = (const char *) pbuf; |
| 13563 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13564 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13565 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13566 | } |
| 13567 | if (v == NULL) |
| 13568 | goto onError; |
| 13569 | r = _PyAccu_Accumulate(&acc, v); |
| 13570 | Py_DECREF(v); |
| 13571 | if (r) |
| 13572 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13573 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13574 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | if (dict && (argidx < arglen) && c != '%') { |
| 13576 | PyErr_SetString(PyExc_TypeError, |
| 13577 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13578 | goto onError; |
| 13579 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13580 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13581 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13582 | } /* until end */ |
| 13583 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13584 | PyErr_SetString(PyExc_TypeError, |
| 13585 | "not all arguments converted during string formatting"); |
| 13586 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13587 | } |
| 13588 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13589 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13590 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13591 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13592 | } |
| 13593 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13594 | Py_XDECREF(temp); |
| 13595 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13596 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13598 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13599 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13600 | Py_XDECREF(temp); |
| 13601 | Py_XDECREF(second); |
| 13602 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13603 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13604 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13605 | } |
| 13606 | return NULL; |
| 13607 | } |
| 13608 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13609 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13610 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13611 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13612 | static PyObject * |
| 13613 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13614 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13615 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13616 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13617 | char *encoding = NULL; |
| 13618 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13619 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13620 | if (type != &PyUnicode_Type) |
| 13621 | return unicode_subtype_new(type, args, kwds); |
| 13622 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13623 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13624 | return NULL; |
| 13625 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13626 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13627 | if (encoding == NULL && errors == NULL) |
| 13628 | return PyObject_Str(x); |
| 13629 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13630 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13631 | } |
| 13632 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13633 | static PyObject * |
| 13634 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13635 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13636 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13637 | Py_ssize_t length, char_size; |
| 13638 | int share_wstr, share_utf8; |
| 13639 | unsigned int kind; |
| 13640 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13641 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13642 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13643 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13644 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13645 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13646 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13647 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13648 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13649 | return NULL; |
| 13650 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13651 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13652 | if (self == NULL) { |
| 13653 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13654 | return NULL; |
| 13655 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13656 | kind = PyUnicode_KIND(unicode); |
| 13657 | length = PyUnicode_GET_LENGTH(unicode); |
| 13658 | |
| 13659 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13660 | #ifdef Py_DEBUG |
| 13661 | _PyUnicode_HASH(self) = -1; |
| 13662 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13663 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13664 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13665 | _PyUnicode_STATE(self).interned = 0; |
| 13666 | _PyUnicode_STATE(self).kind = kind; |
| 13667 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13668 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13669 | _PyUnicode_STATE(self).ready = 1; |
| 13670 | _PyUnicode_WSTR(self) = NULL; |
| 13671 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13672 | _PyUnicode_UTF8(self) = NULL; |
| 13673 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13674 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13675 | |
| 13676 | share_utf8 = 0; |
| 13677 | share_wstr = 0; |
| 13678 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13679 | char_size = 1; |
| 13680 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13681 | share_utf8 = 1; |
| 13682 | } |
| 13683 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13684 | char_size = 2; |
| 13685 | if (sizeof(wchar_t) == 2) |
| 13686 | share_wstr = 1; |
| 13687 | } |
| 13688 | else { |
| 13689 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13690 | char_size = 4; |
| 13691 | if (sizeof(wchar_t) == 4) |
| 13692 | share_wstr = 1; |
| 13693 | } |
| 13694 | |
| 13695 | /* Ensure we won't overflow the length. */ |
| 13696 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13697 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13698 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13699 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13700 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13701 | if (data == NULL) { |
| 13702 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13703 | goto onError; |
| 13704 | } |
| 13705 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13706 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13707 | if (share_utf8) { |
| 13708 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13709 | _PyUnicode_UTF8(self) = data; |
| 13710 | } |
| 13711 | if (share_wstr) { |
| 13712 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13713 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13714 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13715 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13716 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13717 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13718 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13719 | #ifdef Py_DEBUG |
| 13720 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13721 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13722 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13723 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13724 | |
| 13725 | onError: |
| 13726 | Py_DECREF(unicode); |
| 13727 | Py_DECREF(self); |
| 13728 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13729 | } |
| 13730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13731 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13732 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13733 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13734 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13735 | encoding defaults to the current default string encoding.\n\ |
| 13736 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13737 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13738 | static PyObject *unicode_iter(PyObject *seq); |
| 13739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13740 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13741 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13742 | "str", /* tp_name */ |
| 13743 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13744 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13745 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13746 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13747 | 0, /* tp_print */ |
| 13748 | 0, /* tp_getattr */ |
| 13749 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13750 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13751 | unicode_repr, /* tp_repr */ |
| 13752 | &unicode_as_number, /* tp_as_number */ |
| 13753 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13754 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13755 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13756 | 0, /* tp_call*/ |
| 13757 | (reprfunc) unicode_str, /* tp_str */ |
| 13758 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13759 | 0, /* tp_setattro */ |
| 13760 | 0, /* tp_as_buffer */ |
| 13761 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13762 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13763 | unicode_doc, /* tp_doc */ |
| 13764 | 0, /* tp_traverse */ |
| 13765 | 0, /* tp_clear */ |
| 13766 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13767 | 0, /* tp_weaklistoffset */ |
| 13768 | unicode_iter, /* tp_iter */ |
| 13769 | 0, /* tp_iternext */ |
| 13770 | unicode_methods, /* tp_methods */ |
| 13771 | 0, /* tp_members */ |
| 13772 | 0, /* tp_getset */ |
| 13773 | &PyBaseObject_Type, /* tp_base */ |
| 13774 | 0, /* tp_dict */ |
| 13775 | 0, /* tp_descr_get */ |
| 13776 | 0, /* tp_descr_set */ |
| 13777 | 0, /* tp_dictoffset */ |
| 13778 | 0, /* tp_init */ |
| 13779 | 0, /* tp_alloc */ |
| 13780 | unicode_new, /* tp_new */ |
| 13781 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13782 | }; |
| 13783 | |
| 13784 | /* Initialize the Unicode implementation */ |
| 13785 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13786 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13787 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13788 | int i; |
| 13789 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13790 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13791 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13792 | 0x000A, /* LINE FEED */ |
| 13793 | 0x000D, /* CARRIAGE RETURN */ |
| 13794 | 0x001C, /* FILE SEPARATOR */ |
| 13795 | 0x001D, /* GROUP SEPARATOR */ |
| 13796 | 0x001E, /* RECORD SEPARATOR */ |
| 13797 | 0x0085, /* NEXT LINE */ |
| 13798 | 0x2028, /* LINE SEPARATOR */ |
| 13799 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13800 | }; |
| 13801 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13802 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13803 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13804 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13805 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13806 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13807 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13808 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13809 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13810 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13811 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13812 | |
| 13813 | /* initialize the linebreak bloom filter */ |
| 13814 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13815 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13816 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13817 | |
| 13818 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13819 | |
| 13820 | #ifdef HAVE_MBCS |
| 13821 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13822 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13823 | PyErr_SetFromWindowsErr(0); |
| 13824 | return -1; |
| 13825 | } |
| 13826 | #endif |
| 13827 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13828 | } |
| 13829 | |
| 13830 | /* Finalize the Unicode implementation */ |
| 13831 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13832 | int |
| 13833 | PyUnicode_ClearFreeList(void) |
| 13834 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13835 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13836 | } |
| 13837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13838 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13839 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13840 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13841 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13842 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13843 | Py_XDECREF(unicode_empty); |
| 13844 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13845 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13846 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13847 | if (unicode_latin1[i]) { |
| 13848 | Py_DECREF(unicode_latin1[i]); |
| 13849 | unicode_latin1[i] = NULL; |
| 13850 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13851 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13852 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13853 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13854 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13855 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13856 | void |
| 13857 | PyUnicode_InternInPlace(PyObject **p) |
| 13858 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13859 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13860 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13861 | #ifdef Py_DEBUG |
| 13862 | assert(s != NULL); |
| 13863 | assert(_PyUnicode_CHECK(s)); |
| 13864 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13865 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13866 | return; |
| 13867 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13868 | /* If it's a subclass, we don't really know what putting |
| 13869 | it in the interned dict might do. */ |
| 13870 | if (!PyUnicode_CheckExact(s)) |
| 13871 | return; |
| 13872 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13873 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13874 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13875 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13876 | return; |
| 13877 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13878 | s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13879 | if (interned == NULL) { |
| 13880 | interned = PyDict_New(); |
| 13881 | if (interned == NULL) { |
| 13882 | PyErr_Clear(); /* Don't leave an exception */ |
| 13883 | return; |
| 13884 | } |
| 13885 | } |
| 13886 | /* It might be that the GetItem call fails even |
| 13887 | though the key is present in the dictionary, |
| 13888 | namely when this happens during a stack overflow. */ |
| 13889 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13890 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13891 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13893 | if (t) { |
| 13894 | Py_INCREF(t); |
| 13895 | Py_DECREF(*p); |
| 13896 | *p = t; |
| 13897 | return; |
| 13898 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13899 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13900 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13901 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13902 | PyErr_Clear(); |
| 13903 | PyThreadState_GET()->recursion_critical = 0; |
| 13904 | return; |
| 13905 | } |
| 13906 | PyThreadState_GET()->recursion_critical = 0; |
| 13907 | /* The two references in interned are not counted by refcnt. |
| 13908 | The deallocator will take care of this */ |
| 13909 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13910 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13911 | } |
| 13912 | |
| 13913 | void |
| 13914 | PyUnicode_InternImmortal(PyObject **p) |
| 13915 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13916 | PyUnicode_InternInPlace(p); |
| 13917 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 13918 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13919 | Py_INCREF(*p); |
| 13920 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13921 | } |
| 13922 | |
| 13923 | PyObject * |
| 13924 | PyUnicode_InternFromString(const char *cp) |
| 13925 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13926 | PyObject *s = PyUnicode_FromString(cp); |
| 13927 | if (s == NULL) |
| 13928 | return NULL; |
| 13929 | PyUnicode_InternInPlace(&s); |
| 13930 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13931 | } |
| 13932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13933 | void |
| 13934 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13935 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13936 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13937 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13938 | Py_ssize_t i, n; |
| 13939 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13940 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13941 | if (interned == NULL || !PyDict_Check(interned)) |
| 13942 | return; |
| 13943 | keys = PyDict_Keys(interned); |
| 13944 | if (keys == NULL || !PyList_Check(keys)) { |
| 13945 | PyErr_Clear(); |
| 13946 | return; |
| 13947 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13948 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13949 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13950 | detector, interned unicode strings are not forcibly deallocated; |
| 13951 | rather, we give them their stolen references back, and then clear |
| 13952 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13953 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13954 | n = PyList_GET_SIZE(keys); |
| 13955 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13956 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13957 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13958 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13959 | if (PyUnicode_READY(s) == -1) { |
| 13960 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13961 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13963 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13964 | case SSTATE_NOT_INTERNED: |
| 13965 | /* XXX Shouldn't happen */ |
| 13966 | break; |
| 13967 | case SSTATE_INTERNED_IMMORTAL: |
| 13968 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13969 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13970 | break; |
| 13971 | case SSTATE_INTERNED_MORTAL: |
| 13972 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13973 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13974 | break; |
| 13975 | default: |
| 13976 | Py_FatalError("Inconsistent interned string state."); |
| 13977 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13978 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13979 | } |
| 13980 | fprintf(stderr, "total size of all interned strings: " |
| 13981 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13982 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13983 | Py_DECREF(keys); |
| 13984 | PyDict_Clear(interned); |
| 13985 | Py_DECREF(interned); |
| 13986 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13987 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13988 | |
| 13989 | |
| 13990 | /********************* Unicode Iterator **************************/ |
| 13991 | |
| 13992 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13993 | PyObject_HEAD |
| 13994 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13995 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13996 | } unicodeiterobject; |
| 13997 | |
| 13998 | static void |
| 13999 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14000 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14001 | _PyObject_GC_UNTRACK(it); |
| 14002 | Py_XDECREF(it->it_seq); |
| 14003 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14004 | } |
| 14005 | |
| 14006 | static int |
| 14007 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14008 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14009 | Py_VISIT(it->it_seq); |
| 14010 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14011 | } |
| 14012 | |
| 14013 | static PyObject * |
| 14014 | unicodeiter_next(unicodeiterobject *it) |
| 14015 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14016 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14017 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14018 | assert(it != NULL); |
| 14019 | seq = it->it_seq; |
| 14020 | if (seq == NULL) |
| 14021 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14022 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14024 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14025 | int kind = PyUnicode_KIND(seq); |
| 14026 | void *data = PyUnicode_DATA(seq); |
| 14027 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14028 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14029 | if (item != NULL) |
| 14030 | ++it->it_index; |
| 14031 | return item; |
| 14032 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14033 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14034 | Py_DECREF(seq); |
| 14035 | it->it_seq = NULL; |
| 14036 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14037 | } |
| 14038 | |
| 14039 | static PyObject * |
| 14040 | unicodeiter_len(unicodeiterobject *it) |
| 14041 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14042 | Py_ssize_t len = 0; |
| 14043 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14044 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14045 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14046 | } |
| 14047 | |
| 14048 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14049 | |
| 14050 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14051 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14052 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14053 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14054 | }; |
| 14055 | |
| 14056 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14057 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14058 | "str_iterator", /* tp_name */ |
| 14059 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14060 | 0, /* tp_itemsize */ |
| 14061 | /* methods */ |
| 14062 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14063 | 0, /* tp_print */ |
| 14064 | 0, /* tp_getattr */ |
| 14065 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14066 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14067 | 0, /* tp_repr */ |
| 14068 | 0, /* tp_as_number */ |
| 14069 | 0, /* tp_as_sequence */ |
| 14070 | 0, /* tp_as_mapping */ |
| 14071 | 0, /* tp_hash */ |
| 14072 | 0, /* tp_call */ |
| 14073 | 0, /* tp_str */ |
| 14074 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14075 | 0, /* tp_setattro */ |
| 14076 | 0, /* tp_as_buffer */ |
| 14077 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14078 | 0, /* tp_doc */ |
| 14079 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14080 | 0, /* tp_clear */ |
| 14081 | 0, /* tp_richcompare */ |
| 14082 | 0, /* tp_weaklistoffset */ |
| 14083 | PyObject_SelfIter, /* tp_iter */ |
| 14084 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14085 | unicodeiter_methods, /* tp_methods */ |
| 14086 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14087 | }; |
| 14088 | |
| 14089 | static PyObject * |
| 14090 | unicode_iter(PyObject *seq) |
| 14091 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14092 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14093 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14094 | if (!PyUnicode_Check(seq)) { |
| 14095 | PyErr_BadInternalCall(); |
| 14096 | return NULL; |
| 14097 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14098 | if (PyUnicode_READY(seq) == -1) |
| 14099 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14100 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14101 | if (it == NULL) |
| 14102 | return NULL; |
| 14103 | it->it_index = 0; |
| 14104 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14105 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14106 | _PyObject_GC_TRACK(it); |
| 14107 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14108 | } |
| 14109 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14110 | |
| 14111 | size_t |
| 14112 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14113 | { |
| 14114 | int res = 0; |
| 14115 | while(*u++) |
| 14116 | res++; |
| 14117 | return res; |
| 14118 | } |
| 14119 | |
| 14120 | Py_UNICODE* |
| 14121 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14122 | { |
| 14123 | Py_UNICODE *u = s1; |
| 14124 | while ((*u++ = *s2++)); |
| 14125 | return s1; |
| 14126 | } |
| 14127 | |
| 14128 | Py_UNICODE* |
| 14129 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14130 | { |
| 14131 | Py_UNICODE *u = s1; |
| 14132 | while ((*u++ = *s2++)) |
| 14133 | if (n-- == 0) |
| 14134 | break; |
| 14135 | return s1; |
| 14136 | } |
| 14137 | |
| 14138 | Py_UNICODE* |
| 14139 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14140 | { |
| 14141 | Py_UNICODE *u1 = s1; |
| 14142 | u1 += Py_UNICODE_strlen(u1); |
| 14143 | Py_UNICODE_strcpy(u1, s2); |
| 14144 | return s1; |
| 14145 | } |
| 14146 | |
| 14147 | int |
| 14148 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14149 | { |
| 14150 | while (*s1 && *s2 && *s1 == *s2) |
| 14151 | s1++, s2++; |
| 14152 | if (*s1 && *s2) |
| 14153 | return (*s1 < *s2) ? -1 : +1; |
| 14154 | if (*s1) |
| 14155 | return 1; |
| 14156 | if (*s2) |
| 14157 | return -1; |
| 14158 | return 0; |
| 14159 | } |
| 14160 | |
| 14161 | int |
| 14162 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14163 | { |
| 14164 | register Py_UNICODE u1, u2; |
| 14165 | for (; n != 0; n--) { |
| 14166 | u1 = *s1; |
| 14167 | u2 = *s2; |
| 14168 | if (u1 != u2) |
| 14169 | return (u1 < u2) ? -1 : +1; |
| 14170 | if (u1 == '\0') |
| 14171 | return 0; |
| 14172 | s1++; |
| 14173 | s2++; |
| 14174 | } |
| 14175 | return 0; |
| 14176 | } |
| 14177 | |
| 14178 | Py_UNICODE* |
| 14179 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14180 | { |
| 14181 | const Py_UNICODE *p; |
| 14182 | for (p = s; *p; p++) |
| 14183 | if (*p == c) |
| 14184 | return (Py_UNICODE*)p; |
| 14185 | return NULL; |
| 14186 | } |
| 14187 | |
| 14188 | Py_UNICODE* |
| 14189 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14190 | { |
| 14191 | const Py_UNICODE *p; |
| 14192 | p = s + Py_UNICODE_strlen(s); |
| 14193 | while (p != s) { |
| 14194 | p--; |
| 14195 | if (*p == c) |
| 14196 | return (Py_UNICODE*)p; |
| 14197 | } |
| 14198 | return NULL; |
| 14199 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14200 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14201 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14202 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14203 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14204 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14205 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14207 | if (!PyUnicode_Check(unicode)) { |
| 14208 | PyErr_BadArgument(); |
| 14209 | return NULL; |
| 14210 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14211 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14212 | if (u == NULL) |
| 14213 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14214 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14215 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14216 | PyErr_NoMemory(); |
| 14217 | return NULL; |
| 14218 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14219 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14220 | size *= sizeof(Py_UNICODE); |
| 14221 | copy = PyMem_Malloc(size); |
| 14222 | if (copy == NULL) { |
| 14223 | PyErr_NoMemory(); |
| 14224 | return NULL; |
| 14225 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14226 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14227 | return copy; |
| 14228 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14229 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14230 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14231 | to the string.Formatter class implemented in Python. */ |
| 14232 | |
| 14233 | static PyMethodDef _string_methods[] = { |
| 14234 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14235 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14236 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14237 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14238 | {NULL, NULL} |
| 14239 | }; |
| 14240 | |
| 14241 | static struct PyModuleDef _string_module = { |
| 14242 | PyModuleDef_HEAD_INIT, |
| 14243 | "_string", |
| 14244 | PyDoc_STR("string helper module"), |
| 14245 | 0, |
| 14246 | _string_methods, |
| 14247 | NULL, |
| 14248 | NULL, |
| 14249 | NULL, |
| 14250 | NULL |
| 14251 | }; |
| 14252 | |
| 14253 | PyMODINIT_FUNC |
| 14254 | PyInit__string(void) |
| 14255 | { |
| 14256 | return PyModule_Create(&_string_module); |
| 14257 | } |
| 14258 | |
| 14259 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14260 | #ifdef __cplusplus |
| 14261 | } |
| 14262 | #endif |