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 |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1559 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 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 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2058 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 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 | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3054 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3055 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3056 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3057 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3058 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3059 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3060 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3061 | the Python codec requires to encode at least its own filename. Use the C |
| 3062 | version of the locale codec until the codec registry is initialized and |
| 3063 | the Python codec is loaded. |
| 3064 | |
| 3065 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3066 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3067 | subinterpreters. */ |
| 3068 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3069 | return PyUnicode_AsEncodedString(unicode, |
| 3070 | Py_FileSystemDefaultEncoding, |
| 3071 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3072 | } |
| 3073 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3074 | /* locale encoding with surrogateescape */ |
| 3075 | wchar_t *wchar; |
| 3076 | char *bytes; |
| 3077 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3078 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3079 | |
| 3080 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3081 | if (wchar == NULL) |
| 3082 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3083 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3084 | if (bytes == NULL) { |
| 3085 | if (error_pos != (size_t)-1) { |
| 3086 | char *errmsg = strerror(errno); |
| 3087 | PyObject *exc = NULL; |
| 3088 | if (errmsg == NULL) |
| 3089 | errmsg = "Py_wchar2char() failed"; |
| 3090 | raise_encode_exception(&exc, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 3091 | "filesystemencoding", unicode, |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3092 | error_pos, error_pos+1, |
| 3093 | errmsg); |
| 3094 | Py_XDECREF(exc); |
| 3095 | } |
| 3096 | else |
| 3097 | PyErr_NoMemory(); |
| 3098 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3099 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3100 | } |
| 3101 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3102 | |
| 3103 | bytes_obj = PyBytes_FromString(bytes); |
| 3104 | PyMem_Free(bytes); |
| 3105 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3106 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3107 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3110 | PyObject * |
| 3111 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3112 | const char *encoding, |
| 3113 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3114 | { |
| 3115 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3116 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3118 | if (!PyUnicode_Check(unicode)) { |
| 3119 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3120 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3121 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3122 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3123 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3124 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3125 | if ((strcmp(lower, "utf-8") == 0) || |
| 3126 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3127 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3128 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3129 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3130 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3131 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3132 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3133 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3134 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3135 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3136 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3137 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3138 | else if (strcmp(lower, "mbcs") == 0) |
| 3139 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3140 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3141 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3142 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3143 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3144 | |
| 3145 | /* Encode via the codec registry */ |
| 3146 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3147 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3148 | return NULL; |
| 3149 | |
| 3150 | /* The normal path */ |
| 3151 | if (PyBytes_Check(v)) |
| 3152 | return v; |
| 3153 | |
| 3154 | /* 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] | 3155 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3156 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3157 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3158 | |
| 3159 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3160 | "encoder %s returned bytearray instead of bytes", |
| 3161 | encoding); |
| 3162 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3163 | Py_DECREF(v); |
| 3164 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3165 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3166 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3167 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3168 | Py_DECREF(v); |
| 3169 | return b; |
| 3170 | } |
| 3171 | |
| 3172 | PyErr_Format(PyExc_TypeError, |
| 3173 | "encoder did not return a bytes object (type=%.400s)", |
| 3174 | Py_TYPE(v)->tp_name); |
| 3175 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3176 | return NULL; |
| 3177 | } |
| 3178 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3179 | PyObject * |
| 3180 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3181 | const char *encoding, |
| 3182 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3183 | { |
| 3184 | PyObject *v; |
| 3185 | |
| 3186 | if (!PyUnicode_Check(unicode)) { |
| 3187 | PyErr_BadArgument(); |
| 3188 | goto onError; |
| 3189 | } |
| 3190 | |
| 3191 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3192 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3193 | |
| 3194 | /* Encode via the codec registry */ |
| 3195 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3196 | if (v == NULL) |
| 3197 | goto onError; |
| 3198 | if (!PyUnicode_Check(v)) { |
| 3199 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3200 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3201 | Py_TYPE(v)->tp_name); |
| 3202 | Py_DECREF(v); |
| 3203 | goto onError; |
| 3204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3205 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3206 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3207 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | return NULL; |
| 3209 | } |
| 3210 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3211 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3212 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3213 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3214 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3215 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3216 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3217 | PyObject* |
| 3218 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3219 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3220 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3221 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3222 | #elif defined(__APPLE__) |
| 3223 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3224 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3225 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3226 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3227 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3228 | the Python codec requires to encode at least its own filename. Use the C |
| 3229 | version of the locale codec until the codec registry is initialized and |
| 3230 | the Python codec is loaded. |
| 3231 | |
| 3232 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3233 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3234 | subinterpreters. */ |
| 3235 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3236 | return PyUnicode_Decode(s, size, |
| 3237 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3238 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3239 | } |
| 3240 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3241 | /* locale encoding with surrogateescape */ |
| 3242 | wchar_t *wchar; |
| 3243 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3244 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3245 | |
| 3246 | if (s[size] != '\0' || size != strlen(s)) { |
| 3247 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3248 | return NULL; |
| 3249 | } |
| 3250 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3251 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3252 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3253 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3254 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3255 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3256 | PyMem_Free(wchar); |
| 3257 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3258 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3259 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3262 | |
| 3263 | int |
| 3264 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3265 | { |
| 3266 | PyObject *output = NULL; |
| 3267 | Py_ssize_t size; |
| 3268 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3269 | if (arg == NULL) { |
| 3270 | Py_DECREF(*(PyObject**)addr); |
| 3271 | return 1; |
| 3272 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3273 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3274 | output = arg; |
| 3275 | Py_INCREF(output); |
| 3276 | } |
| 3277 | else { |
| 3278 | arg = PyUnicode_FromObject(arg); |
| 3279 | if (!arg) |
| 3280 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3281 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3282 | Py_DECREF(arg); |
| 3283 | if (!output) |
| 3284 | return 0; |
| 3285 | if (!PyBytes_Check(output)) { |
| 3286 | Py_DECREF(output); |
| 3287 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3288 | return 0; |
| 3289 | } |
| 3290 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3291 | size = PyBytes_GET_SIZE(output); |
| 3292 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3293 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3294 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3295 | Py_DECREF(output); |
| 3296 | return 0; |
| 3297 | } |
| 3298 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3299 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3303 | int |
| 3304 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3305 | { |
| 3306 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3307 | if (arg == NULL) { |
| 3308 | Py_DECREF(*(PyObject**)addr); |
| 3309 | return 1; |
| 3310 | } |
| 3311 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3312 | if (PyUnicode_READY(arg)) |
| 3313 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3314 | output = arg; |
| 3315 | Py_INCREF(output); |
| 3316 | } |
| 3317 | else { |
| 3318 | arg = PyBytes_FromObject(arg); |
| 3319 | if (!arg) |
| 3320 | return 0; |
| 3321 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3322 | PyBytes_GET_SIZE(arg)); |
| 3323 | Py_DECREF(arg); |
| 3324 | if (!output) |
| 3325 | return 0; |
| 3326 | if (!PyUnicode_Check(output)) { |
| 3327 | Py_DECREF(output); |
| 3328 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3329 | return 0; |
| 3330 | } |
| 3331 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3332 | if (PyUnicode_READY(output) < 0) { |
| 3333 | Py_DECREF(output); |
| 3334 | return 0; |
| 3335 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3336 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3337 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3338 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3339 | Py_DECREF(output); |
| 3340 | return 0; |
| 3341 | } |
| 3342 | *(PyObject**)addr = output; |
| 3343 | return Py_CLEANUP_SUPPORTED; |
| 3344 | } |
| 3345 | |
| 3346 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3347 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3348 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3349 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3350 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3351 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3352 | if (!PyUnicode_Check(unicode)) { |
| 3353 | PyErr_BadArgument(); |
| 3354 | return NULL; |
| 3355 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3356 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3357 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3358 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3359 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3360 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3361 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3362 | if (bytes == NULL) |
| 3363 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3364 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3365 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3366 | Py_DECREF(bytes); |
| 3367 | return NULL; |
| 3368 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3369 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3370 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3371 | PyBytes_AS_STRING(bytes), |
| 3372 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3373 | Py_DECREF(bytes); |
| 3374 | } |
| 3375 | |
| 3376 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3377 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3378 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3382 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3383 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3384 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3385 | } |
| 3386 | |
| 3387 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3388 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3389 | #endif |
| 3390 | |
| 3391 | |
| 3392 | Py_UNICODE * |
| 3393 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3394 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3395 | const unsigned char *one_byte; |
| 3396 | #if SIZEOF_WCHAR_T == 4 |
| 3397 | const Py_UCS2 *two_bytes; |
| 3398 | #else |
| 3399 | const Py_UCS4 *four_bytes; |
| 3400 | const Py_UCS4 *ucs4_end; |
| 3401 | Py_ssize_t num_surrogates; |
| 3402 | #endif |
| 3403 | wchar_t *w; |
| 3404 | wchar_t *wchar_end; |
| 3405 | |
| 3406 | if (!PyUnicode_Check(unicode)) { |
| 3407 | PyErr_BadArgument(); |
| 3408 | return NULL; |
| 3409 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3410 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3411 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3412 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3413 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3414 | |
| 3415 | #ifdef Py_DEBUG |
| 3416 | ++unicode_as_unicode_calls; |
| 3417 | #endif |
| 3418 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3419 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3420 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3421 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3422 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3423 | num_surrogates = 0; |
| 3424 | |
| 3425 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3426 | if (*four_bytes > 0xFFFF) |
| 3427 | ++num_surrogates; |
| 3428 | } |
| 3429 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3430 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3431 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3432 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3433 | PyErr_NoMemory(); |
| 3434 | return NULL; |
| 3435 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3436 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3437 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3438 | w = _PyUnicode_WSTR(unicode); |
| 3439 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3440 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3441 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3442 | if (*four_bytes > 0xFFFF) { |
| 3443 | /* encode surrogate pair in this case */ |
| 3444 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3445 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3446 | } |
| 3447 | else |
| 3448 | *w = *four_bytes; |
| 3449 | |
| 3450 | if (w > wchar_end) { |
| 3451 | assert(0 && "Miscalculated string end"); |
| 3452 | } |
| 3453 | } |
| 3454 | *w = 0; |
| 3455 | #else |
| 3456 | /* sizeof(wchar_t) == 4 */ |
| 3457 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3458 | "should share memory already."); |
| 3459 | return NULL; |
| 3460 | #endif |
| 3461 | } |
| 3462 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3463 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3464 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3465 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3466 | PyErr_NoMemory(); |
| 3467 | return NULL; |
| 3468 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3469 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3470 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3471 | w = _PyUnicode_WSTR(unicode); |
| 3472 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3473 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3474 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3475 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3476 | for (; w < wchar_end; ++one_byte, ++w) |
| 3477 | *w = *one_byte; |
| 3478 | /* null-terminate the wstr */ |
| 3479 | *w = 0; |
| 3480 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3481 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3482 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3483 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3484 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3485 | *w = *two_bytes; |
| 3486 | /* null-terminate the wstr */ |
| 3487 | *w = 0; |
| 3488 | #else |
| 3489 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3490 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3491 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3492 | Py_FatalError("Impossible unicode object state, wstr " |
| 3493 | "and str should share memory already."); |
| 3494 | return NULL; |
| 3495 | #endif |
| 3496 | } |
| 3497 | else { |
| 3498 | assert(0 && "This should never happen."); |
| 3499 | } |
| 3500 | } |
| 3501 | } |
| 3502 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3503 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3504 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3507 | Py_UNICODE * |
| 3508 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3509 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3510 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3513 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3514 | Py_ssize_t |
| 3515 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3516 | { |
| 3517 | if (!PyUnicode_Check(unicode)) { |
| 3518 | PyErr_BadArgument(); |
| 3519 | goto onError; |
| 3520 | } |
| 3521 | return PyUnicode_GET_SIZE(unicode); |
| 3522 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3523 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3524 | return -1; |
| 3525 | } |
| 3526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3527 | Py_ssize_t |
| 3528 | PyUnicode_GetLength(PyObject *unicode) |
| 3529 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3530 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3531 | PyErr_BadArgument(); |
| 3532 | return -1; |
| 3533 | } |
| 3534 | |
| 3535 | return PyUnicode_GET_LENGTH(unicode); |
| 3536 | } |
| 3537 | |
| 3538 | Py_UCS4 |
| 3539 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3540 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3541 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3542 | PyErr_BadArgument(); |
| 3543 | return (Py_UCS4)-1; |
| 3544 | } |
| 3545 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3546 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3547 | return (Py_UCS4)-1; |
| 3548 | } |
| 3549 | return PyUnicode_READ_CHAR(unicode, index); |
| 3550 | } |
| 3551 | |
| 3552 | int |
| 3553 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3554 | { |
| 3555 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3556 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3557 | return -1; |
| 3558 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3559 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3560 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3561 | return -1; |
| 3562 | } |
| 3563 | if (_PyUnicode_Dirty(unicode)) |
| 3564 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3565 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3566 | index, ch); |
| 3567 | return 0; |
| 3568 | } |
| 3569 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3570 | const char * |
| 3571 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3572 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3573 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3576 | /* create or adjust a UnicodeDecodeError */ |
| 3577 | static void |
| 3578 | make_decode_exception(PyObject **exceptionObject, |
| 3579 | const char *encoding, |
| 3580 | const char *input, Py_ssize_t length, |
| 3581 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3582 | const char *reason) |
| 3583 | { |
| 3584 | if (*exceptionObject == NULL) { |
| 3585 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3586 | encoding, input, length, startpos, endpos, reason); |
| 3587 | } |
| 3588 | else { |
| 3589 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3590 | goto onError; |
| 3591 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3592 | goto onError; |
| 3593 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3594 | goto onError; |
| 3595 | } |
| 3596 | return; |
| 3597 | |
| 3598 | onError: |
| 3599 | Py_DECREF(*exceptionObject); |
| 3600 | *exceptionObject = NULL; |
| 3601 | } |
| 3602 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3603 | /* error handling callback helper: |
| 3604 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3605 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 | and adjust various state variables. |
| 3607 | return 0 on success, -1 on error |
| 3608 | */ |
| 3609 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3610 | static int |
| 3611 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3612 | const char *encoding, const char *reason, |
| 3613 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3614 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3615 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3616 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3617 | 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] | 3618 | |
| 3619 | PyObject *restuple = NULL; |
| 3620 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3621 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3622 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3623 | Py_ssize_t requiredsize; |
| 3624 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3625 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3626 | int res = -1; |
| 3627 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3628 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3629 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3630 | else |
| 3631 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3632 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3681 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3682 | /* need more space? (at least enough for what we |
| 3683 | have+the replacement+the rest of the string (starting |
| 3684 | at the new input position), so we won't have to check space |
| 3685 | when there are no errors in the rest of the string) */ |
| 3686 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3687 | requiredsize = *outpos + replen + insize-newpos; |
| 3688 | if (requiredsize > outsize) { |
| 3689 | if (requiredsize<2*outsize) |
| 3690 | requiredsize = 2*outsize; |
| 3691 | if (unicode_resize(output, requiredsize) < 0) |
| 3692 | goto onError; |
| 3693 | } |
| 3694 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3695 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3696 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 3697 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3698 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3699 | else { |
| 3700 | wchar_t *repwstr; |
| 3701 | Py_ssize_t repwlen; |
| 3702 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3703 | if (repwstr == NULL) |
| 3704 | goto onError; |
| 3705 | /* need more space? (at least enough for what we |
| 3706 | have+the replacement+the rest of the string (starting |
| 3707 | at the new input position), so we won't have to check space |
| 3708 | when there are no errors in the rest of the string) */ |
| 3709 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3710 | if (requiredsize > outsize) { |
| 3711 | if (requiredsize < 2*outsize) |
| 3712 | requiredsize = 2*outsize; |
| 3713 | if (unicode_resize(output, requiredsize) < 0) |
| 3714 | goto onError; |
| 3715 | } |
| 3716 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3717 | *outpos += repwlen; |
| 3718 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3720 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3721 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3722 | /* we made it! */ |
| 3723 | res = 0; |
| 3724 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3725 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3726 | Py_XDECREF(restuple); |
| 3727 | return res; |
| 3728 | } |
| 3729 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3730 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3731 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3732 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3733 | |
| 3734 | /* Three simple macros defining base-64. */ |
| 3735 | |
| 3736 | /* Is c a base-64 character? */ |
| 3737 | |
| 3738 | #define IS_BASE64(c) \ |
| 3739 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3740 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3741 | ((c) >= '0' && (c) <= '9') || \ |
| 3742 | (c) == '+' || (c) == '/') |
| 3743 | |
| 3744 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3745 | |
| 3746 | #define FROM_BASE64(c) \ |
| 3747 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3748 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3749 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3750 | (c) == '+' ? 62 : 63) |
| 3751 | |
| 3752 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3753 | |
| 3754 | #define TO_BASE64(n) \ |
| 3755 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3756 | |
| 3757 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3758 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3759 | * byte not decoding to itself is the + which begins a base64 |
| 3760 | * string. */ |
| 3761 | |
| 3762 | #define DECODE_DIRECT(c) \ |
| 3763 | ((c) <= 127 && (c) != '+') |
| 3764 | |
| 3765 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3766 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3767 | * the above). See RFC2152. This array identifies these different |
| 3768 | * sets: |
| 3769 | * 0 : "Set D" |
| 3770 | * alphanumeric and '(),-./:? |
| 3771 | * 1 : "Set O" |
| 3772 | * !"#$%&*;<=>@[]^_`{|} |
| 3773 | * 2 : "whitespace" |
| 3774 | * ht nl cr sp |
| 3775 | * 3 : special (must be base64 encoded) |
| 3776 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3777 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3778 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3779 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3780 | char utf7_category[128] = { |
| 3781 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3782 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3783 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3784 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3785 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3786 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3787 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3788 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3789 | /* @ A B C D E F G H I J K L M N O */ |
| 3790 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3791 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3792 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3793 | /* ` a b c d e f g h i j k l m n o */ |
| 3794 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3795 | /* p q r s t u v w x y z { | } ~ del */ |
| 3796 | 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] | 3797 | }; |
| 3798 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3799 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3800 | * answer depends on whether we are encoding set O as itself, and also |
| 3801 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3802 | * clear that the answers to these questions vary between |
| 3803 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3804 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3805 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3806 | ((c) < 128 && (c) > 0 && \ |
| 3807 | ((utf7_category[(c)] == 0) || \ |
| 3808 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3809 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3810 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3811 | PyObject * |
| 3812 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3813 | Py_ssize_t size, |
| 3814 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3815 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3816 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3817 | } |
| 3818 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3819 | /* The decoder. The only state we preserve is our read position, |
| 3820 | * i.e. how many characters we have consumed. So if we end in the |
| 3821 | * middle of a shift sequence we have to back off the read position |
| 3822 | * and the output to the beginning of the sequence, otherwise we lose |
| 3823 | * all the shift state (seen bits, number of bits seen, high |
| 3824 | * surrogate). */ |
| 3825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3826 | PyObject * |
| 3827 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3828 | Py_ssize_t size, |
| 3829 | const char *errors, |
| 3830 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3831 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3832 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3833 | Py_ssize_t startinpos; |
| 3834 | Py_ssize_t endinpos; |
| 3835 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3836 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3837 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3838 | const char *errmsg = ""; |
| 3839 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3840 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3841 | unsigned int base64bits = 0; |
| 3842 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3843 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3844 | PyObject *errorHandler = NULL; |
| 3845 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3846 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3847 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 3848 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3849 | if (!unicode) |
| 3850 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3851 | if (size == 0) { |
| 3852 | if (consumed) |
| 3853 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3854 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3855 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3856 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3857 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3858 | e = s + size; |
| 3859 | |
| 3860 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3861 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3862 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3863 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3865 | if (inShift) { /* in a base-64 section */ |
| 3866 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3867 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3868 | base64bits += 6; |
| 3869 | s++; |
| 3870 | if (base64bits >= 16) { |
| 3871 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3872 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3873 | base64bits -= 16; |
| 3874 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3875 | if (surrogate) { |
| 3876 | /* expecting a second surrogate */ |
| 3877 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3878 | Py_UCS4 ch2 = (((surrogate & 0x3FF)<<10) |
| 3879 | | (outCh & 0x3FF)) + 0x10000; |
| 3880 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 3881 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3882 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3883 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3884 | } |
| 3885 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3886 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3887 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3888 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3891 | if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3892 | /* first surrogate */ |
| 3893 | surrogate = outCh; |
| 3894 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3895 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3896 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 3897 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3898 | } |
| 3899 | } |
| 3900 | } |
| 3901 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3902 | inShift = 0; |
| 3903 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3904 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3905 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3906 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3907 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3908 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3909 | if (base64bits > 0) { /* left-over bits */ |
| 3910 | if (base64bits >= 6) { |
| 3911 | /* We've seen at least one base-64 character */ |
| 3912 | errmsg = "partial character in shift sequence"; |
| 3913 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3914 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3915 | else { |
| 3916 | /* Some bits remain; they should be zero */ |
| 3917 | if (base64buffer != 0) { |
| 3918 | errmsg = "non-zero padding bits in shift sequence"; |
| 3919 | goto utf7Error; |
| 3920 | } |
| 3921 | } |
| 3922 | } |
| 3923 | if (ch != '-') { |
| 3924 | /* '-' is absorbed; other terminating |
| 3925 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3926 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3927 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3928 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3933 | s++; /* consume '+' */ |
| 3934 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3935 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3936 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 3937 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3938 | } |
| 3939 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3940 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3941 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3942 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3943 | } |
| 3944 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3945 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3946 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3947 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3948 | s++; |
| 3949 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3950 | else { |
| 3951 | startinpos = s-starts; |
| 3952 | s++; |
| 3953 | errmsg = "unexpected special character"; |
| 3954 | goto utf7Error; |
| 3955 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3956 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3957 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3958 | endinpos = s-starts; |
| 3959 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3960 | errors, &errorHandler, |
| 3961 | "utf7", errmsg, |
| 3962 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3963 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3964 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3967 | /* end of string */ |
| 3968 | |
| 3969 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3970 | /* if we're in an inconsistent state, that's an error */ |
| 3971 | if (surrogate || |
| 3972 | (base64bits >= 6) || |
| 3973 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3974 | endinpos = size; |
| 3975 | if (unicode_decode_call_errorhandler( |
| 3976 | errors, &errorHandler, |
| 3977 | "utf7", "unterminated shift sequence", |
| 3978 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3979 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3980 | goto onError; |
| 3981 | if (s < e) |
| 3982 | goto restart; |
| 3983 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3984 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3985 | |
| 3986 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3987 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3988 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3989 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3990 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3991 | } |
| 3992 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3993 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3994 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3995 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3996 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3997 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3998 | goto onError; |
| 3999 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4000 | Py_XDECREF(errorHandler); |
| 4001 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4002 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4003 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4004 | Py_DECREF(unicode); |
| 4005 | return NULL; |
| 4006 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4007 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4008 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4009 | return unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4011 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4012 | Py_XDECREF(errorHandler); |
| 4013 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4014 | Py_DECREF(unicode); |
| 4015 | return NULL; |
| 4016 | } |
| 4017 | |
| 4018 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4019 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4020 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4021 | int base64SetO, |
| 4022 | int base64WhiteSpace, |
| 4023 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4024 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4025 | int kind; |
| 4026 | void *data; |
| 4027 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4028 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4029 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4030 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4031 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4032 | unsigned int base64bits = 0; |
| 4033 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4034 | char * out; |
| 4035 | char * start; |
| 4036 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4037 | if (PyUnicode_READY(str) < 0) |
| 4038 | return NULL; |
| 4039 | kind = PyUnicode_KIND(str); |
| 4040 | data = PyUnicode_DATA(str); |
| 4041 | len = PyUnicode_GET_LENGTH(str); |
| 4042 | |
| 4043 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4045 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4046 | /* It might be possible to tighten this worst case */ |
| 4047 | allocated = 8 * len; |
| 4048 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4049 | return PyErr_NoMemory(); |
| 4050 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4051 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4052 | if (v == NULL) |
| 4053 | return NULL; |
| 4054 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4055 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4056 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4057 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4058 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4059 | if (inShift) { |
| 4060 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4061 | /* shifting out */ |
| 4062 | if (base64bits) { /* output remaining bits */ |
| 4063 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4064 | base64buffer = 0; |
| 4065 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4066 | } |
| 4067 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4068 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4069 | so no '-' is required, except if the character is itself a '-' */ |
| 4070 | if (IS_BASE64(ch) || ch == '-') { |
| 4071 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4072 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4073 | *out++ = (char) ch; |
| 4074 | } |
| 4075 | else { |
| 4076 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4077 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4078 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4079 | else { /* not in a shift sequence */ |
| 4080 | if (ch == '+') { |
| 4081 | *out++ = '+'; |
| 4082 | *out++ = '-'; |
| 4083 | } |
| 4084 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4085 | *out++ = (char) ch; |
| 4086 | } |
| 4087 | else { |
| 4088 | *out++ = '+'; |
| 4089 | inShift = 1; |
| 4090 | goto encode_char; |
| 4091 | } |
| 4092 | } |
| 4093 | continue; |
| 4094 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4095 | if (ch >= 0x10000) { |
| 4096 | /* code first surrogate */ |
| 4097 | base64bits += 16; |
| 4098 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4099 | while (base64bits >= 6) { |
| 4100 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4101 | base64bits -= 6; |
| 4102 | } |
| 4103 | /* prepare second surrogate */ |
| 4104 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4105 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4106 | base64bits += 16; |
| 4107 | base64buffer = (base64buffer << 16) | ch; |
| 4108 | while (base64bits >= 6) { |
| 4109 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4110 | base64bits -= 6; |
| 4111 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4112 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4113 | if (base64bits) |
| 4114 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4115 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4116 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4117 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4118 | return NULL; |
| 4119 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4120 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4121 | PyObject * |
| 4122 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4123 | Py_ssize_t size, |
| 4124 | int base64SetO, |
| 4125 | int base64WhiteSpace, |
| 4126 | const char *errors) |
| 4127 | { |
| 4128 | PyObject *result; |
| 4129 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4130 | if (tmp == NULL) |
| 4131 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4132 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4133 | base64WhiteSpace, errors); |
| 4134 | Py_DECREF(tmp); |
| 4135 | return result; |
| 4136 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4137 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4138 | #undef IS_BASE64 |
| 4139 | #undef FROM_BASE64 |
| 4140 | #undef TO_BASE64 |
| 4141 | #undef DECODE_DIRECT |
| 4142 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4144 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4145 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4146 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4147 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4148 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4149 | illegal prefix. See RFC 3629 for details */ |
| 4150 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4151 | 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] | 4152 | 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] | 4153 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4154 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4155 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4156 | 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] | 4157 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4158 | 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] | 4159 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4160 | 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] | 4161 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4162 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4163 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4164 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4165 | 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] | 4166 | }; |
| 4167 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4168 | PyObject * |
| 4169 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4170 | Py_ssize_t size, |
| 4171 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4172 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4173 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4174 | } |
| 4175 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4176 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4177 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4178 | |
| 4179 | /* Mask to quickly check whether a C 'long' contains a |
| 4180 | non-ASCII, UTF8-encoded char. */ |
| 4181 | #if (SIZEOF_LONG == 8) |
| 4182 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4183 | #elif (SIZEOF_LONG == 4) |
| 4184 | # define ASCII_CHAR_MASK 0x80808080L |
| 4185 | #else |
| 4186 | # error C 'long' size should be either 4 or 8! |
| 4187 | #endif |
| 4188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4189 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4190 | the size of the decoded unicode string and if any major errors were |
| 4191 | encountered. |
| 4192 | |
| 4193 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4194 | if the string contains surrogates, and if all continuation bytes are |
| 4195 | within the correct ranges, these checks are performed in |
| 4196 | PyUnicode_DecodeUTF8Stateful. |
| 4197 | |
| 4198 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4199 | will be bogus and you should not rely on useful information in them. |
| 4200 | */ |
| 4201 | static Py_UCS4 |
| 4202 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4203 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4204 | int *has_errors) |
| 4205 | { |
| 4206 | Py_ssize_t n; |
| 4207 | Py_ssize_t char_count = 0; |
| 4208 | Py_UCS4 max_char = 127, new_max; |
| 4209 | Py_UCS4 upper_bound; |
| 4210 | const unsigned char *p = (const unsigned char *)s; |
| 4211 | const unsigned char *end = p + string_size; |
| 4212 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4213 | int err = 0; |
| 4214 | |
| 4215 | for (; p < end && !err; ++p, ++char_count) { |
| 4216 | /* Only check value if it's not a ASCII char... */ |
| 4217 | if (*p < 0x80) { |
| 4218 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4219 | an explanation. */ |
| 4220 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4221 | /* Help register allocation */ |
| 4222 | register const unsigned char *_p = p; |
| 4223 | while (_p < aligned_end) { |
| 4224 | unsigned long value = *(unsigned long *) _p; |
| 4225 | if (value & ASCII_CHAR_MASK) |
| 4226 | break; |
| 4227 | _p += SIZEOF_LONG; |
| 4228 | char_count += SIZEOF_LONG; |
| 4229 | } |
| 4230 | p = _p; |
| 4231 | if (p == end) |
| 4232 | break; |
| 4233 | } |
| 4234 | } |
| 4235 | if (*p >= 0x80) { |
| 4236 | n = utf8_code_length[*p]; |
| 4237 | new_max = max_char; |
| 4238 | switch (n) { |
| 4239 | /* invalid start byte */ |
| 4240 | case 0: |
| 4241 | err = 1; |
| 4242 | break; |
| 4243 | case 2: |
| 4244 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4245 | Approximate the upper bound of the code point, |
| 4246 | if this flips over 255 we can be sure it will be more |
| 4247 | than 255 and the string will need 2 bytes per code coint, |
| 4248 | if it stays under or equal to 255, we can be sure 1 byte |
| 4249 | is enough. |
| 4250 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4251 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4252 | if (max_char < upper_bound) |
| 4253 | new_max = upper_bound; |
| 4254 | /* Ensure we track at least that we left ASCII space. */ |
| 4255 | if (new_max < 128) |
| 4256 | new_max = 128; |
| 4257 | break; |
| 4258 | case 3: |
| 4259 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4260 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4261 | if (max_char < 65535) |
| 4262 | new_max = 65535; |
| 4263 | break; |
| 4264 | case 4: |
| 4265 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4266 | new_max = 65537; |
| 4267 | break; |
| 4268 | /* Internal error, this should be caught by the first if */ |
| 4269 | case 1: |
| 4270 | default: |
| 4271 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4272 | err = 1; |
| 4273 | } |
| 4274 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4275 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4276 | --n; |
| 4277 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4278 | if (n >= 1) { |
| 4279 | const unsigned char *cont; |
| 4280 | if ((p + n) >= end) { |
| 4281 | if (consumed == 0) |
| 4282 | /* incomplete data, non-incremental decoding */ |
| 4283 | err = 1; |
| 4284 | break; |
| 4285 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4286 | for (cont = p + 1; cont <= (p + n); ++cont) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4287 | if ((*cont & 0xc0) != 0x80) { |
| 4288 | err = 1; |
| 4289 | break; |
| 4290 | } |
| 4291 | } |
| 4292 | p += n; |
| 4293 | } |
| 4294 | else |
| 4295 | err = 1; |
| 4296 | max_char = new_max; |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | if (unicode_size) |
| 4301 | *unicode_size = char_count; |
| 4302 | if (has_errors) |
| 4303 | *has_errors = err; |
| 4304 | return max_char; |
| 4305 | } |
| 4306 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4307 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
| 4308 | in case of errors. Implicit parameters: unicode, kind, data, has_errors, |
| 4309 | onError. Potential resizing overallocates, so the result needs to shrink |
| 4310 | at the end. |
| 4311 | */ |
| 4312 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4313 | do { \ |
| 4314 | if (has_errors) { \ |
| 4315 | Py_ssize_t pos = index; \ |
| 4316 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4317 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4318 | goto onError; \ |
| 4319 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4320 | goto onError; \ |
| 4321 | } \ |
| 4322 | else \ |
| 4323 | PyUnicode_WRITE(kind, data, index, value); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4324 | } while (0) |
| 4325 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4326 | PyObject * |
| 4327 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4328 | Py_ssize_t size, |
| 4329 | const char *errors, |
| 4330 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4331 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4332 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4333 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4334 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4335 | Py_ssize_t startinpos; |
| 4336 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4337 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4338 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4339 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4340 | PyObject *errorHandler = NULL; |
| 4341 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4342 | Py_UCS4 maxchar = 0; |
| 4343 | Py_ssize_t unicode_size; |
| 4344 | Py_ssize_t i; |
| 4345 | int kind; |
| 4346 | void *data; |
| 4347 | int has_errors; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4348 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4349 | if (size == 0) { |
| 4350 | if (consumed) |
| 4351 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4352 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4353 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4354 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4355 | consumed, &has_errors); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4356 | if (has_errors) |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 4357 | /* maxchar and size computation might be incorrect; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4358 | code below widens and resizes as necessary. */ |
| 4359 | unicode = PyUnicode_New(size, 127); |
| 4360 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4361 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4362 | if (!unicode) |
| 4363 | return NULL; |
| 4364 | /* When the string is ASCII only, just use memcpy and return. |
| 4365 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4366 | sequence at the end of the ASCII block. */ |
| 4367 | if (!has_errors && maxchar < 128 && size == unicode_size) { |
| 4368 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4369 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4370 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4371 | kind = PyUnicode_KIND(unicode); |
| 4372 | data = PyUnicode_DATA(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4373 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4374 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4375 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4376 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4377 | |
| 4378 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4379 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4380 | |
| 4381 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4382 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4383 | input will consist of an overwhelming majority of ASCII |
| 4384 | characters, we try to optimize for this case by checking |
| 4385 | as many characters as a C 'long' can contain. |
| 4386 | First, check if we can do an aligned read, as most CPUs have |
| 4387 | a penalty for unaligned reads. |
| 4388 | */ |
| 4389 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4390 | /* Help register allocation */ |
| 4391 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4392 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4393 | while (_s < aligned_end) { |
| 4394 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4395 | and do a fast unrolled copy if it only contains ASCII |
| 4396 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4397 | unsigned long value = *(unsigned long *) _s; |
| 4398 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4399 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4400 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4401 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4402 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4403 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4404 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4405 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4406 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4407 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4408 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4409 | #endif |
| 4410 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4411 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4412 | } |
| 4413 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4414 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4415 | if (s == e) |
| 4416 | break; |
| 4417 | ch = (unsigned char)*s; |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4422 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4423 | s++; |
| 4424 | continue; |
| 4425 | } |
| 4426 | |
| 4427 | n = utf8_code_length[ch]; |
| 4428 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4429 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4430 | if (consumed) |
| 4431 | break; |
| 4432 | else { |
| 4433 | errmsg = "unexpected end of data"; |
| 4434 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4435 | endinpos = startinpos+1; |
| 4436 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4437 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4438 | goto utf8Error; |
| 4439 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4440 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4441 | |
| 4442 | switch (n) { |
| 4443 | |
| 4444 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4445 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4446 | startinpos = s-starts; |
| 4447 | endinpos = startinpos+1; |
| 4448 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4449 | |
| 4450 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4451 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4452 | startinpos = s-starts; |
| 4453 | endinpos = startinpos+1; |
| 4454 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4455 | |
| 4456 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4457 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4458 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4459 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4460 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4461 | goto utf8Error; |
| 4462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4463 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4464 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4465 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4466 | break; |
| 4467 | |
| 4468 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4469 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4470 | will result in surrogates in range d800-dfff. Surrogates are |
| 4471 | not valid UTF-8 so they are rejected. |
| 4472 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4473 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4474 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4475 | (s[2] & 0xc0) != 0x80 || |
| 4476 | ((unsigned char)s[0] == 0xE0 && |
| 4477 | (unsigned char)s[1] < 0xA0) || |
| 4478 | ((unsigned char)s[0] == 0xED && |
| 4479 | (unsigned char)s[1] > 0x9F)) { |
| 4480 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4481 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4482 | endinpos = startinpos + 1; |
| 4483 | |
| 4484 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4485 | continuation byte is s[2], so increment endinpos by 1, |
| 4486 | if not, s[1] is invalid and endinpos doesn't need to |
| 4487 | be incremented. */ |
| 4488 | if ((s[1] & 0xC0) == 0x80) |
| 4489 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4490 | goto utf8Error; |
| 4491 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4492 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4493 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4494 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4495 | break; |
| 4496 | |
| 4497 | case 4: |
| 4498 | if ((s[1] & 0xc0) != 0x80 || |
| 4499 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4500 | (s[3] & 0xc0) != 0x80 || |
| 4501 | ((unsigned char)s[0] == 0xF0 && |
| 4502 | (unsigned char)s[1] < 0x90) || |
| 4503 | ((unsigned char)s[0] == 0xF4 && |
| 4504 | (unsigned char)s[1] > 0x8F)) { |
| 4505 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4506 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4507 | endinpos = startinpos + 1; |
| 4508 | if ((s[1] & 0xC0) == 0x80) { |
| 4509 | endinpos++; |
| 4510 | if ((s[2] & 0xC0) == 0x80) |
| 4511 | endinpos++; |
| 4512 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4513 | goto utf8Error; |
| 4514 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4515 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4516 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4517 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4518 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4519 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4521 | } |
| 4522 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4523 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4524 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4525 | utf8Error: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4526 | if (!has_errors) { |
| 4527 | PyObject *tmp; |
| 4528 | Py_ssize_t k; |
| 4529 | /* We encountered some error that wasn't detected in the original scan, |
| 4530 | e.g. an encoded surrogate character. The original maxchar computation may |
| 4531 | have been incorrect, so redo it now. */ |
| 4532 | for (k = 0, maxchar = 0; k < i; k++) |
| 4533 | maxchar = Py_MAX(maxchar, PyUnicode_READ(kind, data, k)); |
| 4534 | tmp = PyUnicode_New(PyUnicode_GET_LENGTH(unicode), maxchar); |
| 4535 | if (tmp == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4536 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4537 | PyUnicode_CopyCharacters(tmp, 0, unicode, 0, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4538 | Py_DECREF(unicode); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4539 | unicode = tmp; |
| 4540 | has_errors = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4541 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4542 | if (unicode_decode_call_errorhandler( |
| 4543 | errors, &errorHandler, |
| 4544 | "utf8", errmsg, |
| 4545 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4546 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4547 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4548 | /* Update data because unicode_decode_call_errorhandler might have |
| 4549 | re-created or resized the unicode object. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4550 | data = PyUnicode_DATA(unicode); |
| 4551 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4552 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4553 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4554 | /* Ensure the unicode_size calculation above was correct: */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4555 | assert(has_errors || i == unicode_size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4556 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4557 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4558 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4559 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4560 | /* Adjust length and ready string when it contained errors and |
| 4561 | is of the old resizable kind. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4562 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4563 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4564 | goto onError; |
| 4565 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4566 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4567 | Py_XDECREF(errorHandler); |
| 4568 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4569 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4570 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4572 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4573 | Py_XDECREF(errorHandler); |
| 4574 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4575 | Py_DECREF(unicode); |
| 4576 | return NULL; |
| 4577 | } |
| 4578 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4579 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4580 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4581 | #ifdef __APPLE__ |
| 4582 | |
| 4583 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4584 | used to decode the command line arguments on Mac OS X. */ |
| 4585 | |
| 4586 | wchar_t* |
| 4587 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4588 | { |
| 4589 | int n; |
| 4590 | const char *e; |
| 4591 | wchar_t *unicode, *p; |
| 4592 | |
| 4593 | /* Note: size will always be longer than the resulting Unicode |
| 4594 | character count */ |
| 4595 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4596 | PyErr_NoMemory(); |
| 4597 | return NULL; |
| 4598 | } |
| 4599 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4600 | if (!unicode) |
| 4601 | return NULL; |
| 4602 | |
| 4603 | /* Unpack UTF-8 encoded data */ |
| 4604 | p = unicode; |
| 4605 | e = s + size; |
| 4606 | while (s < e) { |
| 4607 | Py_UCS4 ch = (unsigned char)*s; |
| 4608 | |
| 4609 | if (ch < 0x80) { |
| 4610 | *p++ = (wchar_t)ch; |
| 4611 | s++; |
| 4612 | continue; |
| 4613 | } |
| 4614 | |
| 4615 | n = utf8_code_length[ch]; |
| 4616 | if (s + n > e) { |
| 4617 | goto surrogateescape; |
| 4618 | } |
| 4619 | |
| 4620 | switch (n) { |
| 4621 | case 0: |
| 4622 | case 1: |
| 4623 | goto surrogateescape; |
| 4624 | |
| 4625 | case 2: |
| 4626 | if ((s[1] & 0xc0) != 0x80) |
| 4627 | goto surrogateescape; |
| 4628 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4629 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4630 | *p++ = (wchar_t)ch; |
| 4631 | break; |
| 4632 | |
| 4633 | case 3: |
| 4634 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4635 | will result in surrogates in range d800-dfff. Surrogates are |
| 4636 | not valid UTF-8 so they are rejected. |
| 4637 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4638 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4639 | if ((s[1] & 0xc0) != 0x80 || |
| 4640 | (s[2] & 0xc0) != 0x80 || |
| 4641 | ((unsigned char)s[0] == 0xE0 && |
| 4642 | (unsigned char)s[1] < 0xA0) || |
| 4643 | ((unsigned char)s[0] == 0xED && |
| 4644 | (unsigned char)s[1] > 0x9F)) { |
| 4645 | |
| 4646 | goto surrogateescape; |
| 4647 | } |
| 4648 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4649 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4650 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4651 | break; |
| 4652 | |
| 4653 | case 4: |
| 4654 | if ((s[1] & 0xc0) != 0x80 || |
| 4655 | (s[2] & 0xc0) != 0x80 || |
| 4656 | (s[3] & 0xc0) != 0x80 || |
| 4657 | ((unsigned char)s[0] == 0xF0 && |
| 4658 | (unsigned char)s[1] < 0x90) || |
| 4659 | ((unsigned char)s[0] == 0xF4 && |
| 4660 | (unsigned char)s[1] > 0x8F)) { |
| 4661 | goto surrogateescape; |
| 4662 | } |
| 4663 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4664 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4665 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4666 | |
| 4667 | #if SIZEOF_WCHAR_T == 4 |
| 4668 | *p++ = (wchar_t)ch; |
| 4669 | #else |
| 4670 | /* compute and append the two surrogates: */ |
| 4671 | |
| 4672 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4673 | ch -= 0x10000; |
| 4674 | |
| 4675 | /* high surrogate = top 10 bits added to D800 */ |
| 4676 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4677 | |
| 4678 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4679 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4680 | #endif |
| 4681 | break; |
| 4682 | } |
| 4683 | s += n; |
| 4684 | continue; |
| 4685 | |
| 4686 | surrogateescape: |
| 4687 | *p++ = 0xDC00 + ch; |
| 4688 | s++; |
| 4689 | } |
| 4690 | *p = L'\0'; |
| 4691 | return unicode; |
| 4692 | } |
| 4693 | |
| 4694 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4696 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4697 | |
| 4698 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4699 | and allocate exactly as much space needed at the end. Else allocate the |
| 4700 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4701 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4702 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4703 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4704 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4705 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4706 | #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] | 4707 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4708 | Py_ssize_t i; /* index into s of next input byte */ |
| 4709 | PyObject *result; /* result string object */ |
| 4710 | char *p; /* next free byte in output buffer */ |
| 4711 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4712 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4713 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4714 | PyObject *errorHandler = NULL; |
| 4715 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4716 | int kind; |
| 4717 | void *data; |
| 4718 | Py_ssize_t size; |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4719 | PyObject *rep = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4721 | if (!PyUnicode_Check(unicode)) { |
| 4722 | PyErr_BadArgument(); |
| 4723 | return NULL; |
| 4724 | } |
| 4725 | |
| 4726 | if (PyUnicode_READY(unicode) == -1) |
| 4727 | return NULL; |
| 4728 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4729 | if (PyUnicode_UTF8(unicode)) |
| 4730 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4731 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4732 | |
| 4733 | kind = PyUnicode_KIND(unicode); |
| 4734 | data = PyUnicode_DATA(unicode); |
| 4735 | size = PyUnicode_GET_LENGTH(unicode); |
| 4736 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4737 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4738 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4739 | if (size <= MAX_SHORT_UNICHARS) { |
| 4740 | /* Write into the stack buffer; nallocated can't overflow. |
| 4741 | * At the end, we'll allocate exactly as much heap space as it |
| 4742 | * turns out we need. |
| 4743 | */ |
| 4744 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4745 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4746 | p = stackbuf; |
| 4747 | } |
| 4748 | else { |
| 4749 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4750 | nallocated = size * 4; |
| 4751 | if (nallocated / 4 != size) /* overflow! */ |
| 4752 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4753 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4754 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4755 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4756 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4757 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4758 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4759 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4760 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4761 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4762 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4763 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4764 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4766 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4767 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4768 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4769 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4770 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4771 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4772 | Py_ssize_t repsize, k, startpos; |
| 4773 | startpos = i-1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4774 | rep = unicode_encode_call_errorhandler( |
| 4775 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4776 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4777 | if (!rep) |
| 4778 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4780 | if (PyBytes_Check(rep)) |
| 4781 | repsize = PyBytes_GET_SIZE(rep); |
| 4782 | else |
| 4783 | repsize = PyUnicode_GET_SIZE(rep); |
| 4784 | |
| 4785 | if (repsize > 4) { |
| 4786 | Py_ssize_t offset; |
| 4787 | |
| 4788 | if (result == NULL) |
| 4789 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4790 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4791 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4793 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4794 | /* integer overflow */ |
| 4795 | PyErr_NoMemory(); |
| 4796 | goto error; |
| 4797 | } |
| 4798 | nallocated += repsize - 4; |
| 4799 | if (result != NULL) { |
| 4800 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4801 | goto error; |
| 4802 | } else { |
| 4803 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4804 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4805 | goto error; |
| 4806 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4807 | } |
| 4808 | p = PyBytes_AS_STRING(result) + offset; |
| 4809 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4811 | if (PyBytes_Check(rep)) { |
| 4812 | char *prep = PyBytes_AS_STRING(rep); |
| 4813 | for(k = repsize; k > 0; k--) |
| 4814 | *p++ = *prep++; |
| 4815 | } else /* rep is unicode */ { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4816 | enum PyUnicode_Kind repkind; |
| 4817 | void *repdata; |
| 4818 | |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4819 | if (PyUnicode_READY(rep) < 0) |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4820 | goto error; |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4821 | repkind = PyUnicode_KIND(rep); |
| 4822 | repdata = PyUnicode_DATA(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4823 | |
| 4824 | for(k=0; k<repsize; k++) { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4825 | Py_UCS4 c = PyUnicode_READ(repkind, repdata, k); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4826 | if (0x80 <= c) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 4827 | raise_encode_exception(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4828 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4829 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4830 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4831 | goto error; |
| 4832 | } |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4833 | *p++ = (char)c; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4834 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4835 | } |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4836 | Py_CLEAR(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4837 | } else if (ch < 0x10000) { |
| 4838 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4839 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4840 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4841 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4842 | /* Encode UCS4 Unicode ordinals */ |
| 4843 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4844 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4845 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4846 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4847 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4848 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4849 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4850 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4851 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4852 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4853 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4854 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4855 | } |
| 4856 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4857 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4858 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4859 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4860 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4861 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4862 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4863 | Py_XDECREF(errorHandler); |
| 4864 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4865 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4866 | error: |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4867 | Py_XDECREF(rep); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4868 | Py_XDECREF(errorHandler); |
| 4869 | Py_XDECREF(exc); |
| 4870 | Py_XDECREF(result); |
| 4871 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4872 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4873 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4876 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4877 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4878 | Py_ssize_t size, |
| 4879 | const char *errors) |
| 4880 | { |
| 4881 | PyObject *v, *unicode; |
| 4882 | |
| 4883 | unicode = PyUnicode_FromUnicode(s, size); |
| 4884 | if (unicode == NULL) |
| 4885 | return NULL; |
| 4886 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4887 | Py_DECREF(unicode); |
| 4888 | return v; |
| 4889 | } |
| 4890 | |
| 4891 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4892 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4893 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4894 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4897 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4898 | |
| 4899 | PyObject * |
| 4900 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4901 | Py_ssize_t size, |
| 4902 | const char *errors, |
| 4903 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4904 | { |
| 4905 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4906 | } |
| 4907 | |
| 4908 | PyObject * |
| 4909 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4910 | Py_ssize_t size, |
| 4911 | const char *errors, |
| 4912 | int *byteorder, |
| 4913 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4914 | { |
| 4915 | const char *starts = s; |
| 4916 | Py_ssize_t startinpos; |
| 4917 | Py_ssize_t endinpos; |
| 4918 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4919 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4920 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4921 | int bo = 0; /* assume native ordering by default */ |
| 4922 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4923 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4924 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4925 | int iorder[] = {0, 1, 2, 3}; |
| 4926 | #else |
| 4927 | int iorder[] = {3, 2, 1, 0}; |
| 4928 | #endif |
| 4929 | PyObject *errorHandler = NULL; |
| 4930 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4931 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4932 | q = (unsigned char *)s; |
| 4933 | e = q + size; |
| 4934 | |
| 4935 | if (byteorder) |
| 4936 | bo = *byteorder; |
| 4937 | |
| 4938 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4939 | byte order setting accordingly. In native mode, the leading BOM |
| 4940 | mark is skipped, in all other modes, it is copied to the output |
| 4941 | stream as-is (giving a ZWNBSP character). */ |
| 4942 | if (bo == 0) { |
| 4943 | if (size >= 4) { |
| 4944 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4945 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4946 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4947 | if (bom == 0x0000FEFF) { |
| 4948 | q += 4; |
| 4949 | bo = -1; |
| 4950 | } |
| 4951 | else if (bom == 0xFFFE0000) { |
| 4952 | q += 4; |
| 4953 | bo = 1; |
| 4954 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4955 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4956 | if (bom == 0x0000FEFF) { |
| 4957 | q += 4; |
| 4958 | bo = 1; |
| 4959 | } |
| 4960 | else if (bom == 0xFFFE0000) { |
| 4961 | q += 4; |
| 4962 | bo = -1; |
| 4963 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4964 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4965 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | if (bo == -1) { |
| 4969 | /* force LE */ |
| 4970 | iorder[0] = 0; |
| 4971 | iorder[1] = 1; |
| 4972 | iorder[2] = 2; |
| 4973 | iorder[3] = 3; |
| 4974 | } |
| 4975 | else if (bo == 1) { |
| 4976 | /* force BE */ |
| 4977 | iorder[0] = 3; |
| 4978 | iorder[1] = 2; |
| 4979 | iorder[2] = 1; |
| 4980 | iorder[3] = 0; |
| 4981 | } |
| 4982 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4983 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4984 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4985 | if (!unicode) |
| 4986 | return NULL; |
| 4987 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4988 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4989 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4990 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4991 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4992 | Py_UCS4 ch; |
| 4993 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4994 | if (e-q<4) { |
| 4995 | if (consumed) |
| 4996 | break; |
| 4997 | errmsg = "truncated data"; |
| 4998 | startinpos = ((const char *)q)-starts; |
| 4999 | endinpos = ((const char *)e)-starts; |
| 5000 | goto utf32Error; |
| 5001 | /* The remaining input chars are ignored if the callback |
| 5002 | chooses to skip the input */ |
| 5003 | } |
| 5004 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5005 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5007 | if (ch >= 0x110000) |
| 5008 | { |
| 5009 | errmsg = "codepoint not in range(0x110000)"; |
| 5010 | startinpos = ((const char *)q)-starts; |
| 5011 | endinpos = startinpos+4; |
| 5012 | goto utf32Error; |
| 5013 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5014 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5015 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5016 | q += 4; |
| 5017 | continue; |
| 5018 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5019 | if (unicode_decode_call_errorhandler( |
| 5020 | errors, &errorHandler, |
| 5021 | "utf32", errmsg, |
| 5022 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5023 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5024 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | if (byteorder) |
| 5028 | *byteorder = bo; |
| 5029 | |
| 5030 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5032 | |
| 5033 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5034 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5035 | goto onError; |
| 5036 | |
| 5037 | Py_XDECREF(errorHandler); |
| 5038 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5039 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5040 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5041 | Py_DECREF(unicode); |
| 5042 | return NULL; |
| 5043 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5044 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5045 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5046 | return unicode; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5047 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5049 | Py_DECREF(unicode); |
| 5050 | Py_XDECREF(errorHandler); |
| 5051 | Py_XDECREF(exc); |
| 5052 | return NULL; |
| 5053 | } |
| 5054 | |
| 5055 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5056 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5057 | const char *errors, |
| 5058 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5059 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5060 | int kind; |
| 5061 | void *data; |
| 5062 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5063 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5064 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5065 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5066 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5067 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5068 | int iorder[] = {0, 1, 2, 3}; |
| 5069 | #else |
| 5070 | int iorder[] = {3, 2, 1, 0}; |
| 5071 | #endif |
| 5072 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5073 | #define STORECHAR(CH) \ |
| 5074 | do { \ |
| 5075 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5076 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5077 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5078 | p[iorder[0]] = (CH) & 0xff; \ |
| 5079 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5080 | } while(0) |
| 5081 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5082 | if (!PyUnicode_Check(str)) { |
| 5083 | PyErr_BadArgument(); |
| 5084 | return NULL; |
| 5085 | } |
| 5086 | if (PyUnicode_READY(str) < 0) |
| 5087 | return NULL; |
| 5088 | kind = PyUnicode_KIND(str); |
| 5089 | data = PyUnicode_DATA(str); |
| 5090 | len = PyUnicode_GET_LENGTH(str); |
| 5091 | |
| 5092 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5093 | bytesize = nsize * 4; |
| 5094 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5096 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | if (v == NULL) |
| 5098 | return NULL; |
| 5099 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5100 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5101 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5102 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5103 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5104 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5105 | |
| 5106 | if (byteorder == -1) { |
| 5107 | /* force LE */ |
| 5108 | iorder[0] = 0; |
| 5109 | iorder[1] = 1; |
| 5110 | iorder[2] = 2; |
| 5111 | iorder[3] = 3; |
| 5112 | } |
| 5113 | else if (byteorder == 1) { |
| 5114 | /* force BE */ |
| 5115 | iorder[0] = 3; |
| 5116 | iorder[1] = 2; |
| 5117 | iorder[2] = 1; |
| 5118 | iorder[3] = 0; |
| 5119 | } |
| 5120 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5121 | for (i = 0; i < len; i++) |
| 5122 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5123 | |
| 5124 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5125 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5126 | #undef STORECHAR |
| 5127 | } |
| 5128 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5129 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5130 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5131 | Py_ssize_t size, |
| 5132 | const char *errors, |
| 5133 | int byteorder) |
| 5134 | { |
| 5135 | PyObject *result; |
| 5136 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5137 | if (tmp == NULL) |
| 5138 | return NULL; |
| 5139 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5140 | Py_DECREF(tmp); |
| 5141 | return result; |
| 5142 | } |
| 5143 | |
| 5144 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5145 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5146 | { |
Victor Stinner | 1f79517 | 2011-11-17 00:45:54 +0100 | [diff] [blame] | 5147 | const Py_UNICODE *wstr; |
| 5148 | Py_ssize_t wlen; |
| 5149 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen); |
| 5150 | if (wstr == NULL) |
| 5151 | return NULL; |
| 5152 | return PyUnicode_EncodeUTF32(wstr, wlen, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5155 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5156 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5157 | PyObject * |
| 5158 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5159 | Py_ssize_t size, |
| 5160 | const char *errors, |
| 5161 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5163 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5164 | } |
| 5165 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5166 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5167 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5168 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5169 | rare in most input. |
| 5170 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5171 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5172 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5173 | #if (SIZEOF_LONG == 8) |
| 5174 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5175 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5176 | #elif (SIZEOF_LONG == 4) |
| 5177 | # define FAST_CHAR_MASK 0x80008000L |
| 5178 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5179 | #else |
| 5180 | # error C 'long' size should be either 4 or 8! |
| 5181 | #endif |
| 5182 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5183 | PyObject * |
| 5184 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5185 | Py_ssize_t size, |
| 5186 | const char *errors, |
| 5187 | int *byteorder, |
| 5188 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5189 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5190 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5191 | Py_ssize_t startinpos; |
| 5192 | Py_ssize_t endinpos; |
| 5193 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5194 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5195 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5196 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5197 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5198 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5199 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5200 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5201 | int ihi = 1, ilo = 0; |
| 5202 | #else |
| 5203 | int ihi = 0, ilo = 1; |
| 5204 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5205 | PyObject *errorHandler = NULL; |
| 5206 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5207 | |
| 5208 | /* Note: size will always be longer than the resulting Unicode |
| 5209 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5210 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | if (!unicode) |
| 5212 | return NULL; |
| 5213 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5214 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5215 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5217 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5218 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | |
| 5220 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5221 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5222 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5223 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5224 | byte order setting accordingly. In native mode, the leading BOM |
| 5225 | mark is skipped, in all other modes, it is copied to the output |
| 5226 | stream as-is (giving a ZWNBSP character). */ |
| 5227 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5228 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5229 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5230 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5231 | if (bom == 0xFEFF) { |
| 5232 | q += 2; |
| 5233 | bo = -1; |
| 5234 | } |
| 5235 | else if (bom == 0xFFFE) { |
| 5236 | q += 2; |
| 5237 | bo = 1; |
| 5238 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5239 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | if (bom == 0xFEFF) { |
| 5241 | q += 2; |
| 5242 | bo = 1; |
| 5243 | } |
| 5244 | else if (bom == 0xFFFE) { |
| 5245 | q += 2; |
| 5246 | bo = -1; |
| 5247 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5248 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5249 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5250 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5252 | if (bo == -1) { |
| 5253 | /* force LE */ |
| 5254 | ihi = 1; |
| 5255 | ilo = 0; |
| 5256 | } |
| 5257 | else if (bo == 1) { |
| 5258 | /* force BE */ |
| 5259 | ihi = 0; |
| 5260 | ilo = 1; |
| 5261 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5262 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5263 | native_ordering = ilo < ihi; |
| 5264 | #else |
| 5265 | native_ordering = ilo > ihi; |
| 5266 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5267 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5268 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5269 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5270 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5271 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5272 | reads are more expensive, better to defer to another iteration. */ |
| 5273 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5274 | /* Fast path for runs of non-surrogate chars. */ |
| 5275 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5276 | int kind = PyUnicode_KIND(unicode); |
| 5277 | void *data = PyUnicode_DATA(unicode); |
| 5278 | while (_q < aligned_end) { |
| 5279 | unsigned long block = * (unsigned long *) _q; |
| 5280 | unsigned short *pblock = (unsigned short*)█ |
| 5281 | Py_UCS4 maxch; |
| 5282 | if (native_ordering) { |
| 5283 | /* Can use buffer directly */ |
| 5284 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5285 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5286 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5287 | else { |
| 5288 | /* Need to byte-swap */ |
| 5289 | unsigned char *_p = (unsigned char*)pblock; |
| 5290 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5291 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5292 | _p[0] = _q[1]; |
| 5293 | _p[1] = _q[0]; |
| 5294 | _p[2] = _q[3]; |
| 5295 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5296 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5297 | _p[4] = _q[5]; |
| 5298 | _p[5] = _q[4]; |
| 5299 | _p[6] = _q[7]; |
| 5300 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5301 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5302 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5303 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5304 | #if SIZEOF_LONG == 8 |
| 5305 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5306 | #endif |
| 5307 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5308 | if (unicode_widen(&unicode, maxch) < 0) |
| 5309 | goto onError; |
| 5310 | kind = PyUnicode_KIND(unicode); |
| 5311 | data = PyUnicode_DATA(unicode); |
| 5312 | } |
| 5313 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5314 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5315 | #if SIZEOF_LONG == 8 |
| 5316 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5317 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5318 | #endif |
| 5319 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5320 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5321 | q = _q; |
| 5322 | if (q >= e) |
| 5323 | break; |
| 5324 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5325 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5326 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5327 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5328 | |
| 5329 | if (ch < 0xD800 || ch > 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5330 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5331 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5332 | continue; |
| 5333 | } |
| 5334 | |
| 5335 | /* UTF-16 code pair: */ |
| 5336 | if (q > e) { |
| 5337 | errmsg = "unexpected end of data"; |
| 5338 | startinpos = (((const char *)q) - 2) - starts; |
| 5339 | endinpos = ((const char *)e) + 1 - starts; |
| 5340 | goto utf16Error; |
| 5341 | } |
| 5342 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5343 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5344 | q += 2; |
| 5345 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5346 | if (unicode_putchar(&unicode, &outpos, |
| 5347 | (((ch & 0x3FF)<<10) | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5348 | (ch2 & 0x3FF)) + 0x10000) < 0) |
| 5349 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5350 | continue; |
| 5351 | } |
| 5352 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5353 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5354 | startinpos = (((const char *)q)-4)-starts; |
| 5355 | endinpos = startinpos+2; |
| 5356 | goto utf16Error; |
| 5357 | } |
| 5358 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5359 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5360 | errmsg = "illegal encoding"; |
| 5361 | startinpos = (((const char *)q)-2)-starts; |
| 5362 | endinpos = startinpos+2; |
| 5363 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5364 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5365 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5367 | errors, |
| 5368 | &errorHandler, |
| 5369 | "utf16", errmsg, |
| 5370 | &starts, |
| 5371 | (const char **)&e, |
| 5372 | &startinpos, |
| 5373 | &endinpos, |
| 5374 | &exc, |
| 5375 | (const char **)&q, |
| 5376 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5377 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5378 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5379 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5380 | /* remaining byte at the end? (size should be even) */ |
| 5381 | if (e == q) { |
| 5382 | if (!consumed) { |
| 5383 | errmsg = "truncated data"; |
| 5384 | startinpos = ((const char *)q) - starts; |
| 5385 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5386 | if (unicode_decode_call_errorhandler( |
| 5387 | errors, |
| 5388 | &errorHandler, |
| 5389 | "utf16", errmsg, |
| 5390 | &starts, |
| 5391 | (const char **)&e, |
| 5392 | &startinpos, |
| 5393 | &endinpos, |
| 5394 | &exc, |
| 5395 | (const char **)&q, |
| 5396 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5397 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5398 | goto onError; |
| 5399 | /* The remaining input chars are ignored if the callback |
| 5400 | chooses to skip the input */ |
| 5401 | } |
| 5402 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5403 | |
| 5404 | if (byteorder) |
| 5405 | *byteorder = bo; |
| 5406 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5407 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5411 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | goto onError; |
| 5413 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5414 | Py_XDECREF(errorHandler); |
| 5415 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5416 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5417 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5419 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5421 | Py_XDECREF(errorHandler); |
| 5422 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | return NULL; |
| 5424 | } |
| 5425 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5426 | #undef FAST_CHAR_MASK |
| 5427 | #undef SWAPPED_FAST_CHAR_MASK |
| 5428 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5429 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5430 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5431 | const char *errors, |
| 5432 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5434 | int kind; |
| 5435 | void *data; |
| 5436 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5437 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5438 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5439 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5440 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5441 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5442 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5443 | int ihi = 1, ilo = 0; |
| 5444 | #else |
| 5445 | int ihi = 0, ilo = 1; |
| 5446 | #endif |
| 5447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | #define STORECHAR(CH) \ |
| 5449 | do { \ |
| 5450 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5451 | p[ilo] = (CH) & 0xff; \ |
| 5452 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5453 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5455 | if (!PyUnicode_Check(str)) { |
| 5456 | PyErr_BadArgument(); |
| 5457 | return NULL; |
| 5458 | } |
| 5459 | if (PyUnicode_READY(str) < 0) |
| 5460 | return NULL; |
| 5461 | kind = PyUnicode_KIND(str); |
| 5462 | data = PyUnicode_DATA(str); |
| 5463 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5464 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5465 | pairs = 0; |
| 5466 | if (kind == PyUnicode_4BYTE_KIND) |
| 5467 | for (i = 0; i < len; i++) |
| 5468 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5469 | pairs++; |
| 5470 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5471 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5472 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5473 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5474 | bytesize = nsize * 2; |
| 5475 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5477 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | if (v == NULL) |
| 5479 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5481 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5483 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5484 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5485 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5486 | |
| 5487 | if (byteorder == -1) { |
| 5488 | /* force LE */ |
| 5489 | ihi = 1; |
| 5490 | ilo = 0; |
| 5491 | } |
| 5492 | else if (byteorder == 1) { |
| 5493 | /* force BE */ |
| 5494 | ihi = 0; |
| 5495 | ilo = 1; |
| 5496 | } |
| 5497 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5498 | for (i = 0; i < len; i++) { |
| 5499 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5500 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | if (ch >= 0x10000) { |
| 5502 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5503 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5504 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5505 | STORECHAR(ch); |
| 5506 | if (ch2) |
| 5507 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5508 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5509 | |
| 5510 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5511 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5512 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | } |
| 5514 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5515 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5516 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5517 | Py_ssize_t size, |
| 5518 | const char *errors, |
| 5519 | int byteorder) |
| 5520 | { |
| 5521 | PyObject *result; |
| 5522 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5523 | if (tmp == NULL) |
| 5524 | return NULL; |
| 5525 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5526 | Py_DECREF(tmp); |
| 5527 | return result; |
| 5528 | } |
| 5529 | |
| 5530 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5531 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5532 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5533 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5537 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5538 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5539 | if all the escapes in the string make it still a valid ASCII string. |
| 5540 | Returns -1 if any escapes were found which cause the string to |
| 5541 | pop out of ASCII range. Otherwise returns the length of the |
| 5542 | required buffer to hold the string. |
| 5543 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5544 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5545 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5546 | { |
| 5547 | const unsigned char *p = (const unsigned char *)s; |
| 5548 | const unsigned char *end = p + size; |
| 5549 | Py_ssize_t length = 0; |
| 5550 | |
| 5551 | if (size < 0) |
| 5552 | return -1; |
| 5553 | |
| 5554 | for (; p < end; ++p) { |
| 5555 | if (*p > 127) { |
| 5556 | /* Non-ASCII */ |
| 5557 | return -1; |
| 5558 | } |
| 5559 | else if (*p != '\\') { |
| 5560 | /* Normal character */ |
| 5561 | ++length; |
| 5562 | } |
| 5563 | else { |
| 5564 | /* Backslash-escape, check next char */ |
| 5565 | ++p; |
| 5566 | /* Escape sequence reaches till end of string or |
| 5567 | non-ASCII follow-up. */ |
| 5568 | if (p >= end || *p > 127) |
| 5569 | return -1; |
| 5570 | switch (*p) { |
| 5571 | case '\n': |
| 5572 | /* backslash + \n result in zero characters */ |
| 5573 | break; |
| 5574 | case '\\': case '\'': case '\"': |
| 5575 | case 'b': case 'f': case 't': |
| 5576 | case 'n': case 'r': case 'v': case 'a': |
| 5577 | ++length; |
| 5578 | break; |
| 5579 | case '0': case '1': case '2': case '3': |
| 5580 | case '4': case '5': case '6': case '7': |
| 5581 | case 'x': case 'u': case 'U': case 'N': |
| 5582 | /* these do not guarantee ASCII characters */ |
| 5583 | return -1; |
| 5584 | default: |
| 5585 | /* count the backslash + the other character */ |
| 5586 | length += 2; |
| 5587 | } |
| 5588 | } |
| 5589 | } |
| 5590 | return length; |
| 5591 | } |
| 5592 | |
| 5593 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5594 | or treat string as ASCII. */ |
| 5595 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5596 | do { \ |
| 5597 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5598 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5599 | else \ |
| 5600 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5601 | } while (0) |
| 5602 | |
| 5603 | #define WRITE_WSTR(buf, index, value) \ |
| 5604 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5605 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5606 | |
| 5607 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5608 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5609 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5610 | PyObject * |
| 5611 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5612 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5613 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5615 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5616 | Py_ssize_t startinpos; |
| 5617 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5618 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5619 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5621 | char* message; |
| 5622 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5623 | PyObject *errorHandler = NULL; |
| 5624 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5625 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5626 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5627 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5628 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5629 | |
| 5630 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5631 | either the string is pure ASCII with named escapes like \n, etc. |
| 5632 | and we determined it's exact size (common case) |
| 5633 | or it contains \x, \u, ... escape sequences. then we create a |
| 5634 | 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] | 5635 | if (len >= 0) { |
| 5636 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5637 | if (!v) |
| 5638 | goto onError; |
| 5639 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5640 | } |
| 5641 | else { |
| 5642 | /* Escaped strings will always be longer than the resulting |
| 5643 | Unicode string, so we start with size here and then reduce the |
| 5644 | length after conversion to the true value. |
| 5645 | (but if the error callback returns a long replacement string |
| 5646 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5647 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5648 | if (!v) |
| 5649 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5650 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5651 | } |
| 5652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5654 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5655 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5657 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | while (s < end) { |
| 5659 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5660 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5661 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5663 | /* The only case in which i == ascii_length is a backslash |
| 5664 | followed by a newline. */ |
| 5665 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5668 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5669 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5670 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | continue; |
| 5672 | } |
| 5673 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5674 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | /* \ - Escapes */ |
| 5676 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5677 | c = *s++; |
| 5678 | if (s > end) |
| 5679 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5680 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5681 | /* The only case in which i == ascii_length is a backslash |
| 5682 | followed by a newline. */ |
| 5683 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5684 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5685 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5688 | #define WRITECHAR(ch) \ |
| 5689 | do { \ |
| 5690 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5691 | goto onError; \ |
| 5692 | }while(0) |
| 5693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5695 | case '\\': WRITECHAR('\\'); break; |
| 5696 | case '\'': WRITECHAR('\''); break; |
| 5697 | case '\"': WRITECHAR('\"'); break; |
| 5698 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5699 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5700 | case 'f': WRITECHAR('\014'); break; |
| 5701 | case 't': WRITECHAR('\t'); break; |
| 5702 | case 'n': WRITECHAR('\n'); break; |
| 5703 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5705 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5706 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5707 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5708 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5709 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | case '0': case '1': case '2': case '3': |
| 5711 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5712 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5713 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5714 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5715 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5716 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5718 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | break; |
| 5720 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5721 | /* hex escapes */ |
| 5722 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5724 | digits = 2; |
| 5725 | message = "truncated \\xXX escape"; |
| 5726 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5728 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5730 | digits = 4; |
| 5731 | message = "truncated \\uXXXX escape"; |
| 5732 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5735 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5736 | digits = 8; |
| 5737 | message = "truncated \\UXXXXXXXX escape"; |
| 5738 | hexescape: |
| 5739 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5740 | if (s+digits>end) { |
| 5741 | endinpos = size; |
| 5742 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5743 | errors, &errorHandler, |
| 5744 | "unicodeescape", "end of string in escape sequence", |
| 5745 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5746 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5747 | goto onError; |
| 5748 | goto nextByte; |
| 5749 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5750 | for (j = 0; j < digits; ++j) { |
| 5751 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5752 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5753 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5754 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | errors, &errorHandler, |
| 5756 | "unicodeescape", message, |
| 5757 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5758 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5759 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5760 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5761 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5762 | } |
| 5763 | chr = (chr<<4) & ~0xF; |
| 5764 | if (c >= '0' && c <= '9') |
| 5765 | chr += c - '0'; |
| 5766 | else if (c >= 'a' && c <= 'f') |
| 5767 | chr += 10 + c - 'a'; |
| 5768 | else |
| 5769 | chr += 10 + c - 'A'; |
| 5770 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5771 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5772 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5773 | /* _decoding_error will have already written into the |
| 5774 | target buffer. */ |
| 5775 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5776 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5777 | /* 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] | 5778 | if (chr <= 0x10ffff) { |
| 5779 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5780 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5781 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5783 | errors, &errorHandler, |
| 5784 | "unicodeescape", "illegal Unicode character", |
| 5785 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5786 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5787 | goto onError; |
| 5788 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5789 | break; |
| 5790 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5791 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5792 | case 'N': |
| 5793 | message = "malformed \\N character escape"; |
| 5794 | if (ucnhash_CAPI == NULL) { |
| 5795 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5796 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5797 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5798 | if (ucnhash_CAPI == NULL) |
| 5799 | goto ucnhashError; |
| 5800 | } |
| 5801 | if (*s == '{') { |
| 5802 | const char *start = s+1; |
| 5803 | /* look for the closing brace */ |
| 5804 | while (*s != '}' && s < end) |
| 5805 | s++; |
| 5806 | if (s > start && s < end && *s == '}') { |
| 5807 | /* found a name. look it up in the unicode database */ |
| 5808 | message = "unknown Unicode character name"; |
| 5809 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5810 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5811 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5812 | goto store; |
| 5813 | } |
| 5814 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5815 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5816 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5817 | errors, &errorHandler, |
| 5818 | "unicodeescape", message, |
| 5819 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5820 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5821 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5822 | break; |
| 5823 | |
| 5824 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5825 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5826 | message = "\\ at end of string"; |
| 5827 | s--; |
| 5828 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5829 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | errors, &errorHandler, |
| 5831 | "unicodeescape", message, |
| 5832 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5833 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5834 | goto onError; |
| 5835 | } |
| 5836 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5837 | WRITECHAR('\\'); |
| 5838 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5839 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5840 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5842 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5843 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5845 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5846 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5847 | if (PyUnicode_Resize(&v, i) < 0) |
| 5848 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5849 | Py_XDECREF(errorHandler); |
| 5850 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5851 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5852 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5853 | Py_DECREF(v); |
| 5854 | return NULL; |
| 5855 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5856 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5857 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5858 | return v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5859 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5860 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5861 | PyErr_SetString( |
| 5862 | PyExc_UnicodeError, |
| 5863 | "\\N escapes not supported (can't load unicodedata module)" |
| 5864 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5865 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5866 | Py_XDECREF(errorHandler); |
| 5867 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5868 | return NULL; |
| 5869 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5872 | Py_XDECREF(errorHandler); |
| 5873 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | return NULL; |
| 5875 | } |
| 5876 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5877 | #undef WRITE_ASCII_OR_WSTR |
| 5878 | #undef WRITE_WSTR |
| 5879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5881 | |
| 5882 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5883 | appropriate. |
| 5884 | |
| 5885 | */ |
| 5886 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5887 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5888 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5890 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5891 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5892 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5893 | int kind; |
| 5894 | void *data; |
| 5895 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5897 | /* Initial allocation is based on the longest-possible unichr |
| 5898 | escape. |
| 5899 | |
| 5900 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5901 | unichr, so in this case it's the longest unichr escape. In |
| 5902 | narrow (UTF-16) builds this is five chars per source unichr |
| 5903 | since there are two unichrs in the surrogate pair, so in narrow |
| 5904 | (UTF-16) builds it's not the longest unichr escape. |
| 5905 | |
| 5906 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5907 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5908 | escape. |
| 5909 | */ |
| 5910 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5911 | if (!PyUnicode_Check(unicode)) { |
| 5912 | PyErr_BadArgument(); |
| 5913 | return NULL; |
| 5914 | } |
| 5915 | if (PyUnicode_READY(unicode) < 0) |
| 5916 | return NULL; |
| 5917 | len = PyUnicode_GET_LENGTH(unicode); |
| 5918 | kind = PyUnicode_KIND(unicode); |
| 5919 | data = PyUnicode_DATA(unicode); |
| 5920 | switch(kind) { |
| 5921 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5922 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5923 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5924 | } |
| 5925 | |
| 5926 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5927 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5928 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5929 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5930 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5931 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5932 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5934 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | if (repr == NULL) |
| 5937 | return NULL; |
| 5938 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5939 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5941 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5942 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5943 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5944 | /* Escape backslashes */ |
| 5945 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | *p++ = '\\'; |
| 5947 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5948 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5949 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5950 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5951 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5952 | else if (ch >= 0x10000) { |
| 5953 | *p++ = '\\'; |
| 5954 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5955 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5956 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5957 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5958 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5959 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5960 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5961 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5962 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5964 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5965 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5967 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | *p++ = '\\'; |
| 5969 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5970 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5971 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5972 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5973 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5975 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5976 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5977 | else if (ch == '\t') { |
| 5978 | *p++ = '\\'; |
| 5979 | *p++ = 't'; |
| 5980 | } |
| 5981 | else if (ch == '\n') { |
| 5982 | *p++ = '\\'; |
| 5983 | *p++ = 'n'; |
| 5984 | } |
| 5985 | else if (ch == '\r') { |
| 5986 | *p++ = '\\'; |
| 5987 | *p++ = 'r'; |
| 5988 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5989 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5990 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5991 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5993 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5994 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5995 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5996 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5998 | /* Copy everything else as-is */ |
| 5999 | else |
| 6000 | *p++ = (char) ch; |
| 6001 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6002 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6003 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6004 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6005 | return NULL; |
| 6006 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6009 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6010 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6011 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6013 | PyObject *result; |
| 6014 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6015 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6017 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6018 | Py_DECREF(tmp); |
| 6019 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6023 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6024 | PyObject * |
| 6025 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6026 | Py_ssize_t size, |
| 6027 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6029 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6030 | Py_ssize_t startinpos; |
| 6031 | Py_ssize_t endinpos; |
| 6032 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6033 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | const char *end; |
| 6035 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6036 | PyObject *errorHandler = NULL; |
| 6037 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | /* Escaped strings will always be longer than the resulting |
| 6040 | 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] | 6041 | length after conversion to the true value. (But decoding error |
| 6042 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6043 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6045 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6047 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6048 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | end = s + size; |
| 6050 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6051 | unsigned char c; |
| 6052 | Py_UCS4 x; |
| 6053 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6054 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6056 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6057 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6058 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6059 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6061 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6062 | startinpos = s-starts; |
| 6063 | |
| 6064 | /* \u-escapes are only interpreted iff the number of leading |
| 6065 | backslashes if odd */ |
| 6066 | bs = s; |
| 6067 | for (;s < end;) { |
| 6068 | if (*s != '\\') |
| 6069 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6070 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6071 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6072 | } |
| 6073 | if (((s - bs) & 1) == 0 || |
| 6074 | s >= end || |
| 6075 | (*s != 'u' && *s != 'U')) { |
| 6076 | continue; |
| 6077 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6078 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6079 | count = *s=='u' ? 4 : 8; |
| 6080 | s++; |
| 6081 | |
| 6082 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6084 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6085 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | endinpos = s-starts; |
| 6087 | if (unicode_decode_call_errorhandler( |
| 6088 | errors, &errorHandler, |
| 6089 | "rawunicodeescape", "truncated \\uXXXX", |
| 6090 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6091 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6092 | goto onError; |
| 6093 | goto nextByte; |
| 6094 | } |
| 6095 | x = (x<<4) & ~0xF; |
| 6096 | if (c >= '0' && c <= '9') |
| 6097 | x += c - '0'; |
| 6098 | else if (c >= 'a' && c <= 'f') |
| 6099 | x += 10 + c - 'a'; |
| 6100 | else |
| 6101 | x += 10 + c - 'A'; |
| 6102 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6103 | if (x <= 0x10ffff) { |
| 6104 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6105 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6106 | } else { |
| 6107 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6108 | if (unicode_decode_call_errorhandler( |
| 6109 | errors, &errorHandler, |
| 6110 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6111 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6112 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6113 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | nextByte: |
| 6116 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6118 | if (PyUnicode_Resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6120 | Py_XDECREF(errorHandler); |
| 6121 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6122 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6123 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6124 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6127 | Py_XDECREF(errorHandler); |
| 6128 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | return NULL; |
| 6130 | } |
| 6131 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6132 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6133 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6134 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6136 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | char *p; |
| 6138 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6139 | Py_ssize_t expandsize, pos; |
| 6140 | int kind; |
| 6141 | void *data; |
| 6142 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6144 | if (!PyUnicode_Check(unicode)) { |
| 6145 | PyErr_BadArgument(); |
| 6146 | return NULL; |
| 6147 | } |
| 6148 | if (PyUnicode_READY(unicode) < 0) |
| 6149 | return NULL; |
| 6150 | kind = PyUnicode_KIND(unicode); |
| 6151 | data = PyUnicode_DATA(unicode); |
| 6152 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6153 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6154 | switch(kind) { |
| 6155 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6156 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6157 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6158 | } |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6159 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6160 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6161 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6162 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6163 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | if (repr == NULL) |
| 6165 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6166 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6167 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6169 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6170 | for (pos = 0; pos < len; pos++) { |
| 6171 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6173 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6174 | *p++ = '\\'; |
| 6175 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6176 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6177 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6178 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6179 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6180 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6181 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6182 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6183 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6184 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6185 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6186 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | *p++ = '\\'; |
| 6188 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6189 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6190 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6191 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6192 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | /* Copy everything else as-is */ |
| 6195 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | *p++ = (char) ch; |
| 6197 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6198 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6199 | assert(p > q); |
| 6200 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6201 | return NULL; |
| 6202 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6205 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6206 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6207 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6209 | PyObject *result; |
| 6210 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6211 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6212 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6213 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6214 | Py_DECREF(tmp); |
| 6215 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6218 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6219 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6220 | PyObject * |
| 6221 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6222 | Py_ssize_t size, |
| 6223 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6224 | { |
| 6225 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6226 | Py_ssize_t startinpos; |
| 6227 | Py_ssize_t endinpos; |
| 6228 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6229 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6230 | const char *end; |
| 6231 | const char *reason; |
| 6232 | PyObject *errorHandler = NULL; |
| 6233 | PyObject *exc = NULL; |
| 6234 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6235 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6236 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6237 | 1)) |
| 6238 | return NULL; |
| 6239 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6240 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6241 | 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] | 6242 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6243 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6244 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6245 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6246 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6247 | end = s + size; |
| 6248 | |
| 6249 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6250 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6251 | Py_UCS4 ch; |
| 6252 | /* We copy the raw representation one byte at a time because the |
| 6253 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6254 | ((char *) &uch)[0] = s[0]; |
| 6255 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6256 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6257 | ((char *) &uch)[2] = s[2]; |
| 6258 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6259 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6260 | ch = uch; |
| 6261 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6262 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6263 | some malformed UCS-4 data. */ |
| 6264 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6266 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6267 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6268 | end-s < Py_UNICODE_SIZE |
| 6269 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6271 | startinpos = s - starts; |
| 6272 | if (end-s < Py_UNICODE_SIZE) { |
| 6273 | endinpos = end-starts; |
| 6274 | reason = "truncated input"; |
| 6275 | } |
| 6276 | else { |
| 6277 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6278 | reason = "illegal code point (> 0x10FFFF)"; |
| 6279 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6280 | if (unicode_decode_call_errorhandler( |
| 6281 | errors, &errorHandler, |
| 6282 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6283 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6284 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6285 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6286 | continue; |
| 6287 | } |
| 6288 | |
| 6289 | s += Py_UNICODE_SIZE; |
| 6290 | #ifndef Py_UNICODE_WIDE |
| 6291 | if (ch >= 0xD800 && ch <= 0xDBFF && s < end) |
| 6292 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6293 | Py_UNICODE uch2; |
| 6294 | ((char *) &uch2)[0] = s[0]; |
| 6295 | ((char *) &uch2)[1] = s[1]; |
| 6296 | if (uch2 >= 0xDC00 && uch2 <= 0xDFFF) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6297 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6298 | ch = (((uch & 0x3FF)<<10) | (uch2 & 0x3FF)) + 0x10000; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6299 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6300 | } |
| 6301 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6302 | #endif |
| 6303 | |
| 6304 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6305 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6308 | if (PyUnicode_Resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6309 | goto onError; |
| 6310 | Py_XDECREF(errorHandler); |
| 6311 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6312 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6313 | return v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6314 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6315 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6316 | Py_XDECREF(v); |
| 6317 | Py_XDECREF(errorHandler); |
| 6318 | Py_XDECREF(exc); |
| 6319 | return NULL; |
| 6320 | } |
| 6321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6322 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6323 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6324 | PyObject * |
| 6325 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6326 | Py_ssize_t size, |
| 6327 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6328 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6329 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6330 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6333 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6334 | static void |
| 6335 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6336 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6337 | PyObject *unicode, |
| 6338 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6339 | const char *reason) |
| 6340 | { |
| 6341 | if (*exceptionObject == NULL) { |
| 6342 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6343 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6344 | encoding, unicode, startpos, endpos, reason); |
| 6345 | } |
| 6346 | else { |
| 6347 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6348 | goto onError; |
| 6349 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6350 | goto onError; |
| 6351 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6352 | goto onError; |
| 6353 | return; |
| 6354 | onError: |
| 6355 | Py_DECREF(*exceptionObject); |
| 6356 | *exceptionObject = NULL; |
| 6357 | } |
| 6358 | } |
| 6359 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6360 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6361 | static void |
| 6362 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6363 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6364 | PyObject *unicode, |
| 6365 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6366 | const char *reason) |
| 6367 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6368 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6369 | encoding, unicode, startpos, endpos, reason); |
| 6370 | if (*exceptionObject != NULL) |
| 6371 | PyCodec_StrictErrors(*exceptionObject); |
| 6372 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6373 | |
| 6374 | /* error handling callback helper: |
| 6375 | build arguments, call the callback and check the arguments, |
| 6376 | put the result into newpos and return the replacement string, which |
| 6377 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6378 | static PyObject * |
| 6379 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6380 | PyObject **errorHandler, |
| 6381 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6382 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6383 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6384 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6385 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6386 | 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] | 6387 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6388 | PyObject *restuple; |
| 6389 | PyObject *resunicode; |
| 6390 | |
| 6391 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6393 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6394 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6395 | } |
| 6396 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6397 | if (PyUnicode_READY(unicode) < 0) |
| 6398 | return NULL; |
| 6399 | len = PyUnicode_GET_LENGTH(unicode); |
| 6400 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6401 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6402 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6403 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6404 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6405 | |
| 6406 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6408 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6410 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6411 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | Py_DECREF(restuple); |
| 6413 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6415 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | &resunicode, newpos)) { |
| 6417 | Py_DECREF(restuple); |
| 6418 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6420 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6421 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6422 | Py_DECREF(restuple); |
| 6423 | return NULL; |
| 6424 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6426 | *newpos = len + *newpos; |
| 6427 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6428 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6429 | Py_DECREF(restuple); |
| 6430 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6431 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6432 | Py_INCREF(resunicode); |
| 6433 | Py_DECREF(restuple); |
| 6434 | return resunicode; |
| 6435 | } |
| 6436 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6437 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6438 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6439 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6440 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6441 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6442 | /* input state */ |
| 6443 | Py_ssize_t pos=0, size; |
| 6444 | int kind; |
| 6445 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6446 | /* output object */ |
| 6447 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6448 | /* pointer into the output */ |
| 6449 | char *str; |
| 6450 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6451 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6452 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6453 | 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] | 6454 | PyObject *errorHandler = NULL; |
| 6455 | PyObject *exc = NULL; |
| 6456 | /* the following variable is used for caching string comparisons |
| 6457 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6458 | int known_errorHandler = -1; |
| 6459 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6460 | if (PyUnicode_READY(unicode) < 0) |
| 6461 | return NULL; |
| 6462 | size = PyUnicode_GET_LENGTH(unicode); |
| 6463 | kind = PyUnicode_KIND(unicode); |
| 6464 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6465 | /* allocate enough for a simple encoding without |
| 6466 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6467 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6468 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6469 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6470 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6471 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6472 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6473 | ressize = size; |
| 6474 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6475 | while (pos < size) { |
| 6476 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6477 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6478 | /* can we encode this? */ |
| 6479 | if (c<limit) { |
| 6480 | /* no overflow check, because we know that the space is enough */ |
| 6481 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6482 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6483 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6484 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6485 | Py_ssize_t requiredsize; |
| 6486 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6487 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6489 | Py_ssize_t collstart = pos; |
| 6490 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6491 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6492 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6493 | ++collend; |
| 6494 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6495 | if (known_errorHandler==-1) { |
| 6496 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6497 | known_errorHandler = 1; |
| 6498 | else if (!strcmp(errors, "replace")) |
| 6499 | known_errorHandler = 2; |
| 6500 | else if (!strcmp(errors, "ignore")) |
| 6501 | known_errorHandler = 3; |
| 6502 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6503 | known_errorHandler = 4; |
| 6504 | else |
| 6505 | known_errorHandler = 0; |
| 6506 | } |
| 6507 | switch (known_errorHandler) { |
| 6508 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6509 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6510 | goto onError; |
| 6511 | case 2: /* replace */ |
| 6512 | while (collstart++<collend) |
| 6513 | *str++ = '?'; /* fall through */ |
| 6514 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6515 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6516 | break; |
| 6517 | case 4: /* xmlcharrefreplace */ |
| 6518 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6519 | /* determine replacement size */ |
| 6520 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6521 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6522 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6524 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6525 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6526 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6528 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6530 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6531 | else |
| 6532 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6533 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6534 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6536 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6537 | repsize += 2+6+1; |
| 6538 | else |
| 6539 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6540 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6542 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | if (requiredsize > ressize) { |
| 6544 | if (requiredsize<2*ressize) |
| 6545 | requiredsize = 2*ressize; |
| 6546 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6547 | goto onError; |
| 6548 | str = PyBytes_AS_STRING(res) + respos; |
| 6549 | ressize = requiredsize; |
| 6550 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6551 | /* generate replacement */ |
| 6552 | for (i = collstart; i < collend; ++i) { |
| 6553 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6554 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6555 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | break; |
| 6557 | default: |
| 6558 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6559 | encoding, reason, unicode, &exc, |
| 6560 | collstart, collend, &newpos); |
| 6561 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6562 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6563 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6564 | if (PyBytes_Check(repunicode)) { |
| 6565 | /* Directly copy bytes result to output. */ |
| 6566 | repsize = PyBytes_Size(repunicode); |
| 6567 | if (repsize > 1) { |
| 6568 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6569 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6570 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6571 | Py_DECREF(repunicode); |
| 6572 | goto onError; |
| 6573 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6574 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6575 | ressize += repsize-1; |
| 6576 | } |
| 6577 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6578 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6579 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6580 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6581 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6582 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6583 | /* need more space? (at least enough for what we |
| 6584 | have+the replacement+the rest of the string, so |
| 6585 | we won't have to check space for encodable characters) */ |
| 6586 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6587 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6588 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6589 | if (requiredsize > ressize) { |
| 6590 | if (requiredsize<2*ressize) |
| 6591 | requiredsize = 2*ressize; |
| 6592 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6593 | Py_DECREF(repunicode); |
| 6594 | goto onError; |
| 6595 | } |
| 6596 | str = PyBytes_AS_STRING(res) + respos; |
| 6597 | ressize = requiredsize; |
| 6598 | } |
| 6599 | /* check if there is anything unencodable in the replacement |
| 6600 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6601 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6602 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6603 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6604 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6605 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6606 | Py_DECREF(repunicode); |
| 6607 | goto onError; |
| 6608 | } |
| 6609 | *str = (char)c; |
| 6610 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6611 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6612 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6613 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6614 | } |
| 6615 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6616 | /* Resize if we allocated to much */ |
| 6617 | size = str - PyBytes_AS_STRING(res); |
| 6618 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6619 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6620 | if (_PyBytes_Resize(&res, size) < 0) |
| 6621 | goto onError; |
| 6622 | } |
| 6623 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6624 | Py_XDECREF(errorHandler); |
| 6625 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6626 | return res; |
| 6627 | |
| 6628 | onError: |
| 6629 | Py_XDECREF(res); |
| 6630 | Py_XDECREF(errorHandler); |
| 6631 | Py_XDECREF(exc); |
| 6632 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6635 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6636 | PyObject * |
| 6637 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6638 | Py_ssize_t size, |
| 6639 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6641 | PyObject *result; |
| 6642 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6643 | if (unicode == NULL) |
| 6644 | return NULL; |
| 6645 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6646 | Py_DECREF(unicode); |
| 6647 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6650 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6651 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 | { |
| 6653 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6654 | PyErr_BadArgument(); |
| 6655 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6657 | if (PyUnicode_READY(unicode) == -1) |
| 6658 | return NULL; |
| 6659 | /* Fast path: if it is a one-byte string, construct |
| 6660 | bytes object directly. */ |
| 6661 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6662 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6663 | PyUnicode_GET_LENGTH(unicode)); |
| 6664 | /* Non-Latin-1 characters present. Defer to above function to |
| 6665 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6666 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | PyObject* |
| 6670 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6671 | { |
| 6672 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
| 6675 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6676 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6677 | PyObject * |
| 6678 | PyUnicode_DecodeASCII(const char *s, |
| 6679 | Py_ssize_t size, |
| 6680 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6682 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6683 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6684 | int kind; |
| 6685 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6686 | Py_ssize_t startinpos; |
| 6687 | Py_ssize_t endinpos; |
| 6688 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6689 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6690 | int has_error; |
| 6691 | const unsigned char *p = (const unsigned char *)s; |
| 6692 | const unsigned char *end = p + size; |
| 6693 | 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] | 6694 | PyObject *errorHandler = NULL; |
| 6695 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6697 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6698 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6699 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6700 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6701 | has_error = 0; |
| 6702 | while (p < end && !has_error) { |
| 6703 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6704 | an explanation. */ |
| 6705 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6706 | /* Help register allocation */ |
| 6707 | register const unsigned char *_p = p; |
| 6708 | while (_p < aligned_end) { |
| 6709 | unsigned long value = *(unsigned long *) _p; |
| 6710 | if (value & ASCII_CHAR_MASK) { |
| 6711 | has_error = 1; |
| 6712 | break; |
| 6713 | } |
| 6714 | _p += SIZEOF_LONG; |
| 6715 | } |
| 6716 | if (_p == end) |
| 6717 | break; |
| 6718 | if (has_error) |
| 6719 | break; |
| 6720 | p = _p; |
| 6721 | } |
| 6722 | if (*p & 0x80) { |
| 6723 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6724 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6725 | } |
| 6726 | else { |
| 6727 | ++p; |
| 6728 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6729 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6730 | if (!has_error) |
| 6731 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6732 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6733 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6735 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6736 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6737 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6738 | kind = PyUnicode_KIND(v); |
| 6739 | data = PyUnicode_DATA(v); |
| 6740 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6741 | e = s + size; |
| 6742 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | register unsigned char c = (unsigned char)*s; |
| 6744 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6745 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6746 | ++s; |
| 6747 | } |
| 6748 | else { |
| 6749 | startinpos = s-starts; |
| 6750 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | if (unicode_decode_call_errorhandler( |
| 6752 | errors, &errorHandler, |
| 6753 | "ascii", "ordinal not in range(128)", |
| 6754 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6755 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6756 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6757 | kind = PyUnicode_KIND(v); |
| 6758 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6759 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6760 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6761 | if (PyUnicode_Resize(&v, outpos) < 0) |
| 6762 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6763 | Py_XDECREF(errorHandler); |
| 6764 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6765 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6766 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6767 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6768 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6770 | Py_XDECREF(errorHandler); |
| 6771 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | return NULL; |
| 6773 | } |
| 6774 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6775 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6776 | PyObject * |
| 6777 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6778 | Py_ssize_t size, |
| 6779 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6781 | PyObject *result; |
| 6782 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6783 | if (unicode == NULL) |
| 6784 | return NULL; |
| 6785 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6786 | Py_DECREF(unicode); |
| 6787 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6790 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6791 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | { |
| 6793 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6794 | PyErr_BadArgument(); |
| 6795 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6797 | if (PyUnicode_READY(unicode) == -1) |
| 6798 | return NULL; |
| 6799 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6800 | directly. Else defer to above function to raise the exception. */ |
| 6801 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6802 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6803 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6804 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | PyObject * |
| 6808 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6809 | { |
| 6810 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6813 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6814 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6815 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6816 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6817 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6818 | #define NEED_RETRY |
| 6819 | #endif |
| 6820 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6821 | #ifndef WC_ERR_INVALID_CHARS |
| 6822 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6823 | #endif |
| 6824 | |
| 6825 | static char* |
| 6826 | code_page_name(UINT code_page, PyObject **obj) |
| 6827 | { |
| 6828 | *obj = NULL; |
| 6829 | if (code_page == CP_ACP) |
| 6830 | return "mbcs"; |
| 6831 | if (code_page == CP_UTF7) |
| 6832 | return "CP_UTF7"; |
| 6833 | if (code_page == CP_UTF8) |
| 6834 | return "CP_UTF8"; |
| 6835 | |
| 6836 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6837 | if (*obj == NULL) |
| 6838 | return NULL; |
| 6839 | return PyBytes_AS_STRING(*obj); |
| 6840 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6841 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6842 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6843 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6844 | { |
| 6845 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6846 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6847 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6848 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6849 | return 0; |
| 6850 | |
| 6851 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6852 | if (prev == curr) |
| 6853 | return 1; |
| 6854 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6855 | as it assumes an incomplete character consists of a single |
| 6856 | byte. */ |
| 6857 | if (curr - prev == 2) |
| 6858 | return 1; |
| 6859 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6860 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6861 | return 0; |
| 6862 | } |
| 6863 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6864 | static DWORD |
| 6865 | decode_code_page_flags(UINT code_page) |
| 6866 | { |
| 6867 | if (code_page == CP_UTF7) { |
| 6868 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6869 | return 0; |
| 6870 | } |
| 6871 | else |
| 6872 | return MB_ERR_INVALID_CHARS; |
| 6873 | } |
| 6874 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6875 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6876 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6877 | * mode. |
| 6878 | * |
| 6879 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6880 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6881 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6882 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6883 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6884 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6885 | const char *in, |
| 6886 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6887 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6888 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6889 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6890 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6891 | |
| 6892 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6893 | assert(insize > 0); |
| 6894 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6895 | if (outsize <= 0) |
| 6896 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6897 | |
| 6898 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6899 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6900 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | if (*v == NULL) |
| 6902 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6903 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6904 | } |
| 6905 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6906 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6907 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6908 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6909 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6910 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6914 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6915 | if (outsize <= 0) |
| 6916 | goto error; |
| 6917 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6918 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6919 | error: |
| 6920 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6921 | return -2; |
| 6922 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6923 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6926 | /* |
| 6927 | * Decode a byte string from a code page into unicode object with an error |
| 6928 | * handler. |
| 6929 | * |
| 6930 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6931 | * UnicodeDecodeError exception and returns -1 on error. |
| 6932 | */ |
| 6933 | static int |
| 6934 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6935 | PyObject **v, |
| 6936 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6937 | const char *errors) |
| 6938 | { |
| 6939 | const char *startin = in; |
| 6940 | const char *endin = in + size; |
| 6941 | const DWORD flags = decode_code_page_flags(code_page); |
| 6942 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6943 | 2000 English version of the message. */ |
| 6944 | const char *reason = "No mapping for the Unicode character exists " |
| 6945 | "in the target code page."; |
| 6946 | /* each step cannot decode more than 1 character, but a character can be |
| 6947 | represented as a surrogate pair */ |
| 6948 | wchar_t buffer[2], *startout, *out; |
| 6949 | int insize, outsize; |
| 6950 | PyObject *errorHandler = NULL; |
| 6951 | PyObject *exc = NULL; |
| 6952 | PyObject *encoding_obj = NULL; |
| 6953 | char *encoding; |
| 6954 | DWORD err; |
| 6955 | int ret = -1; |
| 6956 | |
| 6957 | assert(size > 0); |
| 6958 | |
| 6959 | encoding = code_page_name(code_page, &encoding_obj); |
| 6960 | if (encoding == NULL) |
| 6961 | return -1; |
| 6962 | |
| 6963 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6964 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6965 | UnicodeDecodeError. */ |
| 6966 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6967 | if (exc != NULL) { |
| 6968 | PyCodec_StrictErrors(exc); |
| 6969 | Py_CLEAR(exc); |
| 6970 | } |
| 6971 | goto error; |
| 6972 | } |
| 6973 | |
| 6974 | if (*v == NULL) { |
| 6975 | /* Create unicode object */ |
| 6976 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6977 | PyErr_NoMemory(); |
| 6978 | goto error; |
| 6979 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6980 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6981 | if (*v == NULL) |
| 6982 | goto error; |
| 6983 | startout = PyUnicode_AS_UNICODE(*v); |
| 6984 | } |
| 6985 | else { |
| 6986 | /* Extend unicode object */ |
| 6987 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6988 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6989 | PyErr_NoMemory(); |
| 6990 | goto error; |
| 6991 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6992 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6993 | goto error; |
| 6994 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6995 | } |
| 6996 | |
| 6997 | /* Decode the byte string character per character */ |
| 6998 | out = startout; |
| 6999 | while (in < endin) |
| 7000 | { |
| 7001 | /* Decode a character */ |
| 7002 | insize = 1; |
| 7003 | do |
| 7004 | { |
| 7005 | outsize = MultiByteToWideChar(code_page, flags, |
| 7006 | in, insize, |
| 7007 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7008 | if (outsize > 0) |
| 7009 | break; |
| 7010 | err = GetLastError(); |
| 7011 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7012 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7013 | { |
| 7014 | PyErr_SetFromWindowsErr(0); |
| 7015 | goto error; |
| 7016 | } |
| 7017 | insize++; |
| 7018 | } |
| 7019 | /* 4=maximum length of a UTF-8 sequence */ |
| 7020 | while (insize <= 4 && (in + insize) <= endin); |
| 7021 | |
| 7022 | if (outsize <= 0) { |
| 7023 | Py_ssize_t startinpos, endinpos, outpos; |
| 7024 | |
| 7025 | startinpos = in - startin; |
| 7026 | endinpos = startinpos + 1; |
| 7027 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7028 | if (unicode_decode_call_errorhandler( |
| 7029 | errors, &errorHandler, |
| 7030 | encoding, reason, |
| 7031 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7032 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7033 | { |
| 7034 | goto error; |
| 7035 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7036 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | } |
| 7038 | else { |
| 7039 | in += insize; |
| 7040 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7041 | out += outsize; |
| 7042 | } |
| 7043 | } |
| 7044 | |
| 7045 | /* write a NUL character at the end */ |
| 7046 | *out = 0; |
| 7047 | |
| 7048 | /* Extend unicode object */ |
| 7049 | outsize = out - startout; |
| 7050 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7051 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7052 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7053 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7054 | |
| 7055 | error: |
| 7056 | Py_XDECREF(encoding_obj); |
| 7057 | Py_XDECREF(errorHandler); |
| 7058 | Py_XDECREF(exc); |
| 7059 | return ret; |
| 7060 | } |
| 7061 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7062 | static PyObject * |
| 7063 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7064 | const char *s, Py_ssize_t size, |
| 7065 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7066 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7067 | PyObject *v = NULL; |
| 7068 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7069 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7070 | if (code_page < 0) { |
| 7071 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7072 | return NULL; |
| 7073 | } |
| 7074 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7075 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7076 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7077 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7078 | do |
| 7079 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7080 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7081 | if (size > INT_MAX) { |
| 7082 | chunk_size = INT_MAX; |
| 7083 | final = 0; |
| 7084 | done = 0; |
| 7085 | } |
| 7086 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7087 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7088 | { |
| 7089 | chunk_size = (int)size; |
| 7090 | final = (consumed == NULL); |
| 7091 | done = 1; |
| 7092 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7093 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7094 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7095 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7096 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7097 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7098 | if (chunk_size == 0 && done) { |
| 7099 | if (v != NULL) |
| 7100 | break; |
| 7101 | Py_INCREF(unicode_empty); |
| 7102 | return unicode_empty; |
| 7103 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7104 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7105 | |
| 7106 | converted = decode_code_page_strict(code_page, &v, |
| 7107 | s, chunk_size); |
| 7108 | if (converted == -2) |
| 7109 | converted = decode_code_page_errors(code_page, &v, |
| 7110 | s, chunk_size, |
| 7111 | errors); |
| 7112 | assert(converted != 0); |
| 7113 | |
| 7114 | if (converted < 0) { |
| 7115 | Py_XDECREF(v); |
| 7116 | return NULL; |
| 7117 | } |
| 7118 | |
| 7119 | if (consumed) |
| 7120 | *consumed += converted; |
| 7121 | |
| 7122 | s += converted; |
| 7123 | size -= converted; |
| 7124 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7125 | |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7126 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7127 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7128 | Py_DECREF(v); |
| 7129 | return NULL; |
| 7130 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7131 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7132 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7133 | return v; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7136 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7137 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7138 | const char *s, |
| 7139 | Py_ssize_t size, |
| 7140 | const char *errors, |
| 7141 | Py_ssize_t *consumed) |
| 7142 | { |
| 7143 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7144 | } |
| 7145 | |
| 7146 | PyObject * |
| 7147 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7148 | Py_ssize_t size, |
| 7149 | const char *errors, |
| 7150 | Py_ssize_t *consumed) |
| 7151 | { |
| 7152 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7153 | } |
| 7154 | |
| 7155 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7156 | PyUnicode_DecodeMBCS(const char *s, |
| 7157 | Py_ssize_t size, |
| 7158 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7159 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7160 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7161 | } |
| 7162 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7163 | static DWORD |
| 7164 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7165 | { |
| 7166 | if (code_page == CP_UTF8) { |
| 7167 | if (winver.dwMajorVersion >= 6) |
| 7168 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7169 | and later */ |
| 7170 | return WC_ERR_INVALID_CHARS; |
| 7171 | else |
| 7172 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7173 | return 0; |
| 7174 | } |
| 7175 | else if (code_page == CP_UTF7) { |
| 7176 | /* CP_UTF7 only supports flags=0 */ |
| 7177 | return 0; |
| 7178 | } |
| 7179 | else { |
| 7180 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7181 | return 0; |
| 7182 | else |
| 7183 | return WC_NO_BEST_FIT_CHARS; |
| 7184 | } |
| 7185 | } |
| 7186 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7187 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7188 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7189 | * mode. |
| 7190 | * |
| 7191 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7192 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7193 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7194 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7195 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7196 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7198 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7199 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7200 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7201 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7202 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7203 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7204 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7206 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7207 | /* Create a substring so that we can get the UTF-16 representation |
| 7208 | of just the slice under consideration. */ |
| 7209 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7210 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7211 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7212 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7213 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7214 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7215 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7216 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7217 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7218 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7219 | if (substring == NULL) |
| 7220 | return -1; |
| 7221 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7222 | if (p == NULL) { |
| 7223 | Py_DECREF(substring); |
| 7224 | return -1; |
| 7225 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7226 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7227 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | outsize = WideCharToMultiByte(code_page, flags, |
| 7229 | p, size, |
| 7230 | NULL, 0, |
| 7231 | NULL, pusedDefaultChar); |
| 7232 | if (outsize <= 0) |
| 7233 | goto error; |
| 7234 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7235 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7236 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7237 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7238 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7239 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7240 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7241 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7243 | if (*outbytes == NULL) { |
| 7244 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7245 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7246 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7248 | } |
| 7249 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7250 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7251 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7252 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7253 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7254 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7255 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7256 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7257 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7258 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7259 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7260 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7261 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
| 7264 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7265 | outsize = WideCharToMultiByte(code_page, flags, |
| 7266 | p, size, |
| 7267 | out, outsize, |
| 7268 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7269 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7270 | if (outsize <= 0) |
| 7271 | goto error; |
| 7272 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7273 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7274 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7275 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7277 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7278 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7279 | return -2; |
| 7280 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7281 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7282 | } |
| 7283 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7284 | /* |
| 7285 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7286 | * error handler. |
| 7287 | * |
| 7288 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7289 | * -1 on other error. |
| 7290 | */ |
| 7291 | static int |
| 7292 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7293 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7294 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7295 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7296 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7297 | Py_ssize_t pos = unicode_offset; |
| 7298 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7299 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7300 | 2000 English version of the message. */ |
| 7301 | const char *reason = "invalid character"; |
| 7302 | /* 4=maximum length of a UTF-8 sequence */ |
| 7303 | char buffer[4]; |
| 7304 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7305 | Py_ssize_t outsize; |
| 7306 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7307 | PyObject *errorHandler = NULL; |
| 7308 | PyObject *exc = NULL; |
| 7309 | PyObject *encoding_obj = NULL; |
| 7310 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7311 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7312 | PyObject *rep; |
| 7313 | int ret = -1; |
| 7314 | |
| 7315 | assert(insize > 0); |
| 7316 | |
| 7317 | encoding = code_page_name(code_page, &encoding_obj); |
| 7318 | if (encoding == NULL) |
| 7319 | return -1; |
| 7320 | |
| 7321 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7322 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7323 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7324 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7325 | if (exc != NULL) { |
| 7326 | PyCodec_StrictErrors(exc); |
| 7327 | Py_DECREF(exc); |
| 7328 | } |
| 7329 | Py_XDECREF(encoding_obj); |
| 7330 | return -1; |
| 7331 | } |
| 7332 | |
| 7333 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7334 | pusedDefaultChar = &usedDefaultChar; |
| 7335 | else |
| 7336 | pusedDefaultChar = NULL; |
| 7337 | |
| 7338 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7339 | PyErr_NoMemory(); |
| 7340 | goto error; |
| 7341 | } |
| 7342 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7343 | |
| 7344 | if (*outbytes == NULL) { |
| 7345 | /* Create string object */ |
| 7346 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7347 | if (*outbytes == NULL) |
| 7348 | goto error; |
| 7349 | out = PyBytes_AS_STRING(*outbytes); |
| 7350 | } |
| 7351 | else { |
| 7352 | /* Extend string object */ |
| 7353 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7354 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7355 | PyErr_NoMemory(); |
| 7356 | goto error; |
| 7357 | } |
| 7358 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7359 | goto error; |
| 7360 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7361 | } |
| 7362 | |
| 7363 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7364 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7365 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7366 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7367 | wchar_t chars[2]; |
| 7368 | int charsize; |
| 7369 | if (ch < 0x10000) { |
| 7370 | chars[0] = (wchar_t)ch; |
| 7371 | charsize = 1; |
| 7372 | } |
| 7373 | else { |
| 7374 | ch -= 0x10000; |
| 7375 | chars[0] = 0xd800 + (ch >> 10); |
| 7376 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7377 | charsize = 2; |
| 7378 | } |
| 7379 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7380 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7381 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7382 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7383 | NULL, pusedDefaultChar); |
| 7384 | if (outsize > 0) { |
| 7385 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7386 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7387 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7388 | memcpy(out, buffer, outsize); |
| 7389 | out += outsize; |
| 7390 | continue; |
| 7391 | } |
| 7392 | } |
| 7393 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7394 | PyErr_SetFromWindowsErr(0); |
| 7395 | goto error; |
| 7396 | } |
| 7397 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7398 | rep = unicode_encode_call_errorhandler( |
| 7399 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7400 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7401 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7402 | if (rep == NULL) |
| 7403 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7404 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7405 | |
| 7406 | if (PyBytes_Check(rep)) { |
| 7407 | outsize = PyBytes_GET_SIZE(rep); |
| 7408 | if (outsize != 1) { |
| 7409 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7410 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7411 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7412 | Py_DECREF(rep); |
| 7413 | goto error; |
| 7414 | } |
| 7415 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7416 | } |
| 7417 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7418 | out += outsize; |
| 7419 | } |
| 7420 | else { |
| 7421 | Py_ssize_t i; |
| 7422 | enum PyUnicode_Kind kind; |
| 7423 | void *data; |
| 7424 | |
| 7425 | if (PyUnicode_READY(rep) < 0) { |
| 7426 | Py_DECREF(rep); |
| 7427 | goto error; |
| 7428 | } |
| 7429 | |
| 7430 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7431 | if (outsize != 1) { |
| 7432 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7433 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7434 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7435 | Py_DECREF(rep); |
| 7436 | goto error; |
| 7437 | } |
| 7438 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7439 | } |
| 7440 | kind = PyUnicode_KIND(rep); |
| 7441 | data = PyUnicode_DATA(rep); |
| 7442 | for (i=0; i < outsize; i++) { |
| 7443 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7444 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7445 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7446 | encoding, unicode, |
| 7447 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7448 | "unable to encode error handler result to ASCII"); |
| 7449 | Py_DECREF(rep); |
| 7450 | goto error; |
| 7451 | } |
| 7452 | *out = (unsigned char)ch; |
| 7453 | out++; |
| 7454 | } |
| 7455 | } |
| 7456 | Py_DECREF(rep); |
| 7457 | } |
| 7458 | /* write a NUL byte */ |
| 7459 | *out = 0; |
| 7460 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7461 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7462 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7463 | goto error; |
| 7464 | ret = 0; |
| 7465 | |
| 7466 | error: |
| 7467 | Py_XDECREF(encoding_obj); |
| 7468 | Py_XDECREF(errorHandler); |
| 7469 | Py_XDECREF(exc); |
| 7470 | return ret; |
| 7471 | } |
| 7472 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7473 | static PyObject * |
| 7474 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7475 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7476 | const char *errors) |
| 7477 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7478 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7479 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7480 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7481 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7482 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7483 | if (PyUnicode_READY(unicode) < 0) |
| 7484 | return NULL; |
| 7485 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7486 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7487 | if (code_page < 0) { |
| 7488 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7489 | return NULL; |
| 7490 | } |
| 7491 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7492 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7493 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7494 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7495 | offset = 0; |
| 7496 | do |
| 7497 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7498 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7499 | /* 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] | 7500 | chunks. */ |
| 7501 | if (len > INT_MAX/2) { |
| 7502 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7503 | done = 0; |
| 7504 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7505 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7506 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7507 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7508 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7509 | done = 1; |
| 7510 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7511 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7512 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7513 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7514 | errors); |
| 7515 | if (ret == -2) |
| 7516 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7517 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7518 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7519 | if (ret < 0) { |
| 7520 | Py_XDECREF(outbytes); |
| 7521 | return NULL; |
| 7522 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7523 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7524 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7525 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7526 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7527 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7528 | return outbytes; |
| 7529 | } |
| 7530 | |
| 7531 | PyObject * |
| 7532 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7533 | Py_ssize_t size, |
| 7534 | const char *errors) |
| 7535 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7536 | PyObject *unicode, *res; |
| 7537 | unicode = PyUnicode_FromUnicode(p, size); |
| 7538 | if (unicode == NULL) |
| 7539 | return NULL; |
| 7540 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7541 | Py_DECREF(unicode); |
| 7542 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7543 | } |
| 7544 | |
| 7545 | PyObject * |
| 7546 | PyUnicode_EncodeCodePage(int code_page, |
| 7547 | PyObject *unicode, |
| 7548 | const char *errors) |
| 7549 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7550 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7551 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7552 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7553 | PyObject * |
| 7554 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7555 | { |
| 7556 | if (!PyUnicode_Check(unicode)) { |
| 7557 | PyErr_BadArgument(); |
| 7558 | return NULL; |
| 7559 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7560 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7561 | } |
| 7562 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7563 | #undef NEED_RETRY |
| 7564 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7565 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7568 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7569 | PyObject * |
| 7570 | PyUnicode_DecodeCharmap(const char *s, |
| 7571 | Py_ssize_t size, |
| 7572 | PyObject *mapping, |
| 7573 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7575 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7576 | Py_ssize_t startinpos; |
| 7577 | Py_ssize_t endinpos; |
| 7578 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7579 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7580 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7581 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7582 | PyObject *errorHandler = NULL; |
| 7583 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7584 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7585 | /* Default to Latin-1 */ |
| 7586 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7587 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7588 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7589 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7591 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7593 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7594 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7595 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7596 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7597 | Py_ssize_t maplen; |
| 7598 | enum PyUnicode_Kind kind; |
| 7599 | void *data; |
| 7600 | Py_UCS4 x; |
| 7601 | |
| 7602 | if (PyUnicode_READY(mapping) < 0) |
| 7603 | return NULL; |
| 7604 | |
| 7605 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7606 | data = PyUnicode_DATA(mapping); |
| 7607 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | while (s < e) { |
| 7609 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7610 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7611 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7612 | x = PyUnicode_READ(kind, data, ch); |
| 7613 | else |
| 7614 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7616 | if (x == 0xfffe) |
| 7617 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7619 | startinpos = s-starts; |
| 7620 | endinpos = startinpos+1; |
| 7621 | if (unicode_decode_call_errorhandler( |
| 7622 | errors, &errorHandler, |
| 7623 | "charmap", "character maps to <undefined>", |
| 7624 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7625 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7626 | goto onError; |
| 7627 | } |
| 7628 | continue; |
| 7629 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7630 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7631 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7632 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7634 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7635 | } |
| 7636 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7637 | while (s < e) { |
| 7638 | unsigned char ch = *s; |
| 7639 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7640 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7641 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7642 | w = PyLong_FromLong((long)ch); |
| 7643 | if (w == NULL) |
| 7644 | goto onError; |
| 7645 | x = PyObject_GetItem(mapping, w); |
| 7646 | Py_DECREF(w); |
| 7647 | if (x == NULL) { |
| 7648 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7649 | /* No mapping found means: mapping is undefined. */ |
| 7650 | PyErr_Clear(); |
| 7651 | x = Py_None; |
| 7652 | Py_INCREF(x); |
| 7653 | } else |
| 7654 | goto onError; |
| 7655 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7657 | /* Apply mapping */ |
| 7658 | if (PyLong_Check(x)) { |
| 7659 | long value = PyLong_AS_LONG(x); |
| 7660 | if (value < 0 || value > 65535) { |
| 7661 | PyErr_SetString(PyExc_TypeError, |
| 7662 | "character mapping must be in range(65536)"); |
| 7663 | Py_DECREF(x); |
| 7664 | goto onError; |
| 7665 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7666 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7667 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | } |
| 7669 | else if (x == Py_None) { |
| 7670 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7671 | startinpos = s-starts; |
| 7672 | endinpos = startinpos+1; |
| 7673 | if (unicode_decode_call_errorhandler( |
| 7674 | errors, &errorHandler, |
| 7675 | "charmap", "character maps to <undefined>", |
| 7676 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7677 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7678 | Py_DECREF(x); |
| 7679 | goto onError; |
| 7680 | } |
| 7681 | Py_DECREF(x); |
| 7682 | continue; |
| 7683 | } |
| 7684 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7685 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7686 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7687 | if (PyUnicode_READY(x) < 0) |
| 7688 | goto onError; |
| 7689 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7690 | |
| 7691 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7692 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7693 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7694 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7695 | goto onError; |
| 7696 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7697 | else if (targetsize > 1) { |
| 7698 | /* 1-n mapping */ |
| 7699 | if (targetsize > extrachars) { |
| 7700 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7701 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7702 | (targetsize << 2); |
| 7703 | extrachars += needed; |
| 7704 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7705 | if (PyUnicode_Resize(&v, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7706 | PyUnicode_GET_LENGTH(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7707 | Py_DECREF(x); |
| 7708 | goto onError; |
| 7709 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7710 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7711 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7712 | goto onError; |
| 7713 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7714 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7715 | extrachars -= targetsize; |
| 7716 | } |
| 7717 | /* 1-0 mapping: skip the character */ |
| 7718 | } |
| 7719 | else { |
| 7720 | /* wrong return value */ |
| 7721 | PyErr_SetString(PyExc_TypeError, |
| 7722 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7723 | Py_DECREF(x); |
| 7724 | goto onError; |
| 7725 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | Py_DECREF(x); |
| 7727 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7728 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7729 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7730 | if (PyUnicode_Resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7731 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7732 | Py_XDECREF(errorHandler); |
| 7733 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7734 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7735 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7736 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7737 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7738 | Py_XDECREF(errorHandler); |
| 7739 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | Py_XDECREF(v); |
| 7741 | return NULL; |
| 7742 | } |
| 7743 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7744 | /* Charmap encoding: the lookup table */ |
| 7745 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7746 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7747 | PyObject_HEAD |
| 7748 | unsigned char level1[32]; |
| 7749 | int count2, count3; |
| 7750 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7751 | }; |
| 7752 | |
| 7753 | static PyObject* |
| 7754 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7755 | { |
| 7756 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7757 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7758 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7759 | } |
| 7760 | |
| 7761 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7762 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7763 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7764 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7765 | }; |
| 7766 | |
| 7767 | static void |
| 7768 | encoding_map_dealloc(PyObject* o) |
| 7769 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7770 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7771 | } |
| 7772 | |
| 7773 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7774 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7775 | "EncodingMap", /*tp_name*/ |
| 7776 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7777 | 0, /*tp_itemsize*/ |
| 7778 | /* methods */ |
| 7779 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7780 | 0, /*tp_print*/ |
| 7781 | 0, /*tp_getattr*/ |
| 7782 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7783 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7784 | 0, /*tp_repr*/ |
| 7785 | 0, /*tp_as_number*/ |
| 7786 | 0, /*tp_as_sequence*/ |
| 7787 | 0, /*tp_as_mapping*/ |
| 7788 | 0, /*tp_hash*/ |
| 7789 | 0, /*tp_call*/ |
| 7790 | 0, /*tp_str*/ |
| 7791 | 0, /*tp_getattro*/ |
| 7792 | 0, /*tp_setattro*/ |
| 7793 | 0, /*tp_as_buffer*/ |
| 7794 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7795 | 0, /*tp_doc*/ |
| 7796 | 0, /*tp_traverse*/ |
| 7797 | 0, /*tp_clear*/ |
| 7798 | 0, /*tp_richcompare*/ |
| 7799 | 0, /*tp_weaklistoffset*/ |
| 7800 | 0, /*tp_iter*/ |
| 7801 | 0, /*tp_iternext*/ |
| 7802 | encoding_map_methods, /*tp_methods*/ |
| 7803 | 0, /*tp_members*/ |
| 7804 | 0, /*tp_getset*/ |
| 7805 | 0, /*tp_base*/ |
| 7806 | 0, /*tp_dict*/ |
| 7807 | 0, /*tp_descr_get*/ |
| 7808 | 0, /*tp_descr_set*/ |
| 7809 | 0, /*tp_dictoffset*/ |
| 7810 | 0, /*tp_init*/ |
| 7811 | 0, /*tp_alloc*/ |
| 7812 | 0, /*tp_new*/ |
| 7813 | 0, /*tp_free*/ |
| 7814 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7815 | }; |
| 7816 | |
| 7817 | PyObject* |
| 7818 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7819 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7820 | PyObject *result; |
| 7821 | struct encoding_map *mresult; |
| 7822 | int i; |
| 7823 | int need_dict = 0; |
| 7824 | unsigned char level1[32]; |
| 7825 | unsigned char level2[512]; |
| 7826 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7827 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7828 | int kind; |
| 7829 | void *data; |
| 7830 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7831 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7832 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7833 | PyErr_BadArgument(); |
| 7834 | return NULL; |
| 7835 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7836 | kind = PyUnicode_KIND(string); |
| 7837 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7838 | memset(level1, 0xFF, sizeof level1); |
| 7839 | memset(level2, 0xFF, sizeof level2); |
| 7840 | |
| 7841 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7842 | or if there are non-BMP characters, we need to use |
| 7843 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7844 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7845 | need_dict = 1; |
| 7846 | for (i = 1; i < 256; i++) { |
| 7847 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | ch = PyUnicode_READ(kind, data, i); |
| 7849 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7850 | need_dict = 1; |
| 7851 | break; |
| 7852 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7853 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | /* unmapped character */ |
| 7855 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7856 | l1 = ch >> 11; |
| 7857 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7858 | if (level1[l1] == 0xFF) |
| 7859 | level1[l1] = count2++; |
| 7860 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7861 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7865 | need_dict = 1; |
| 7866 | |
| 7867 | if (need_dict) { |
| 7868 | PyObject *result = PyDict_New(); |
| 7869 | PyObject *key, *value; |
| 7870 | if (!result) |
| 7871 | return NULL; |
| 7872 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7873 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7874 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7875 | if (!key || !value) |
| 7876 | goto failed1; |
| 7877 | if (PyDict_SetItem(result, key, value) == -1) |
| 7878 | goto failed1; |
| 7879 | Py_DECREF(key); |
| 7880 | Py_DECREF(value); |
| 7881 | } |
| 7882 | return result; |
| 7883 | failed1: |
| 7884 | Py_XDECREF(key); |
| 7885 | Py_XDECREF(value); |
| 7886 | Py_DECREF(result); |
| 7887 | return NULL; |
| 7888 | } |
| 7889 | |
| 7890 | /* Create a three-level trie */ |
| 7891 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7892 | 16*count2 + 128*count3 - 1); |
| 7893 | if (!result) |
| 7894 | return PyErr_NoMemory(); |
| 7895 | PyObject_Init(result, &EncodingMapType); |
| 7896 | mresult = (struct encoding_map*)result; |
| 7897 | mresult->count2 = count2; |
| 7898 | mresult->count3 = count3; |
| 7899 | mlevel1 = mresult->level1; |
| 7900 | mlevel2 = mresult->level23; |
| 7901 | mlevel3 = mresult->level23 + 16*count2; |
| 7902 | memcpy(mlevel1, level1, 32); |
| 7903 | memset(mlevel2, 0xFF, 16*count2); |
| 7904 | memset(mlevel3, 0, 128*count3); |
| 7905 | count3 = 0; |
| 7906 | for (i = 1; i < 256; i++) { |
| 7907 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7908 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7909 | /* unmapped character */ |
| 7910 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7911 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7912 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7913 | i2 = 16*mlevel1[o1] + o2; |
| 7914 | if (mlevel2[i2] == 0xFF) |
| 7915 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7916 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7917 | i3 = 128*mlevel2[i2] + o3; |
| 7918 | mlevel3[i3] = i; |
| 7919 | } |
| 7920 | return result; |
| 7921 | } |
| 7922 | |
| 7923 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7924 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7925 | { |
| 7926 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7927 | int l1 = c>>11; |
| 7928 | int l2 = (c>>7) & 0xF; |
| 7929 | int l3 = c & 0x7F; |
| 7930 | int i; |
| 7931 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7932 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7933 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7934 | if (c == 0) |
| 7935 | return 0; |
| 7936 | /* level 1*/ |
| 7937 | i = map->level1[l1]; |
| 7938 | if (i == 0xFF) { |
| 7939 | return -1; |
| 7940 | } |
| 7941 | /* level 2*/ |
| 7942 | i = map->level23[16*i+l2]; |
| 7943 | if (i == 0xFF) { |
| 7944 | return -1; |
| 7945 | } |
| 7946 | /* level 3 */ |
| 7947 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7948 | if (i == 0) { |
| 7949 | return -1; |
| 7950 | } |
| 7951 | return i; |
| 7952 | } |
| 7953 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7954 | /* Lookup the character ch in the mapping. If the character |
| 7955 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7956 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7957 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7958 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7959 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7960 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7961 | PyObject *x; |
| 7962 | |
| 7963 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7965 | x = PyObject_GetItem(mapping, w); |
| 7966 | Py_DECREF(w); |
| 7967 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7969 | /* No mapping found means: mapping is undefined. */ |
| 7970 | PyErr_Clear(); |
| 7971 | x = Py_None; |
| 7972 | Py_INCREF(x); |
| 7973 | return x; |
| 7974 | } else |
| 7975 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7976 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7977 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7978 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7979 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 | long value = PyLong_AS_LONG(x); |
| 7981 | if (value < 0 || value > 255) { |
| 7982 | PyErr_SetString(PyExc_TypeError, |
| 7983 | "character mapping must be in range(256)"); |
| 7984 | Py_DECREF(x); |
| 7985 | return NULL; |
| 7986 | } |
| 7987 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7988 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7989 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7990 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7991 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7992 | /* wrong return value */ |
| 7993 | PyErr_Format(PyExc_TypeError, |
| 7994 | "character mapping must return integer, bytes or None, not %.400s", |
| 7995 | x->ob_type->tp_name); |
| 7996 | Py_DECREF(x); |
| 7997 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7998 | } |
| 7999 | } |
| 8000 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8001 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8002 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8003 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8004 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8005 | /* exponentially overallocate to minimize reallocations */ |
| 8006 | if (requiredsize < 2*outsize) |
| 8007 | requiredsize = 2*outsize; |
| 8008 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8009 | return -1; |
| 8010 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8011 | } |
| 8012 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8013 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8014 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8015 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8016 | /* 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] | 8017 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8018 | space is available. Return a new reference to the object that |
| 8019 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8020 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8021 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8022 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8023 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8024 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8026 | PyObject *rep; |
| 8027 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8028 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8029 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8030 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8031 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8032 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8033 | if (res == -1) |
| 8034 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | if (outsize<requiredsize) |
| 8036 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8037 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8038 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8039 | outstart[(*outpos)++] = (char)res; |
| 8040 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8041 | } |
| 8042 | |
| 8043 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8044 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8045 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8046 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | Py_DECREF(rep); |
| 8048 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8049 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | if (PyLong_Check(rep)) { |
| 8051 | Py_ssize_t requiredsize = *outpos+1; |
| 8052 | if (outsize<requiredsize) |
| 8053 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8054 | Py_DECREF(rep); |
| 8055 | return enc_EXCEPTION; |
| 8056 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8057 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8058 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8059 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8060 | else { |
| 8061 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8062 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8063 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8064 | if (outsize<requiredsize) |
| 8065 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8066 | Py_DECREF(rep); |
| 8067 | return enc_EXCEPTION; |
| 8068 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8069 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8070 | memcpy(outstart + *outpos, repchars, repsize); |
| 8071 | *outpos += repsize; |
| 8072 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8073 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8074 | Py_DECREF(rep); |
| 8075 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
| 8078 | /* handle an error in PyUnicode_EncodeCharmap |
| 8079 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8080 | static int |
| 8081 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8082 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8083 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8084 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8085 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8086 | { |
| 8087 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8088 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8089 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame^] | 8090 | enum PyUnicode_Kind kind; |
| 8091 | void *data; |
| 8092 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8093 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8094 | Py_ssize_t collstartpos = *inpos; |
| 8095 | Py_ssize_t collendpos = *inpos+1; |
| 8096 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8097 | char *encoding = "charmap"; |
| 8098 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8099 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8100 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8101 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8102 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8103 | if (PyUnicode_READY(unicode) < 0) |
| 8104 | return -1; |
| 8105 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8106 | /* find all unencodable characters */ |
| 8107 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8108 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8109 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8110 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8111 | val = encoding_map_lookup(ch, mapping); |
| 8112 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8113 | break; |
| 8114 | ++collendpos; |
| 8115 | continue; |
| 8116 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8117 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8118 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8119 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | if (rep==NULL) |
| 8121 | return -1; |
| 8122 | else if (rep!=Py_None) { |
| 8123 | Py_DECREF(rep); |
| 8124 | break; |
| 8125 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8126 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8128 | } |
| 8129 | /* cache callback name lookup |
| 8130 | * (if not done yet, i.e. it's the first error) */ |
| 8131 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8133 | *known_errorHandler = 1; |
| 8134 | else if (!strcmp(errors, "replace")) |
| 8135 | *known_errorHandler = 2; |
| 8136 | else if (!strcmp(errors, "ignore")) |
| 8137 | *known_errorHandler = 3; |
| 8138 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8139 | *known_errorHandler = 4; |
| 8140 | else |
| 8141 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8142 | } |
| 8143 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8144 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8145 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8146 | return -1; |
| 8147 | case 2: /* replace */ |
| 8148 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8149 | x = charmapencode_output('?', mapping, res, respos); |
| 8150 | if (x==enc_EXCEPTION) { |
| 8151 | return -1; |
| 8152 | } |
| 8153 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8154 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8155 | return -1; |
| 8156 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8157 | } |
| 8158 | /* fall through */ |
| 8159 | case 3: /* ignore */ |
| 8160 | *inpos = collendpos; |
| 8161 | break; |
| 8162 | case 4: /* xmlcharrefreplace */ |
| 8163 | /* generate replacement (temporarily (mis)uses p) */ |
| 8164 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | char buffer[2+29+1+1]; |
| 8166 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8167 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | for (cp = buffer; *cp; ++cp) { |
| 8169 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8170 | if (x==enc_EXCEPTION) |
| 8171 | return -1; |
| 8172 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8173 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | return -1; |
| 8175 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8176 | } |
| 8177 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8178 | *inpos = collendpos; |
| 8179 | break; |
| 8180 | default: |
| 8181 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8182 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8183 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8184 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8186 | if (PyBytes_Check(repunicode)) { |
| 8187 | /* Directly copy bytes result to output. */ |
| 8188 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8189 | Py_ssize_t requiredsize; |
| 8190 | repsize = PyBytes_Size(repunicode); |
| 8191 | requiredsize = *respos + repsize; |
| 8192 | if (requiredsize > outsize) |
| 8193 | /* Make room for all additional bytes. */ |
| 8194 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8195 | Py_DECREF(repunicode); |
| 8196 | return -1; |
| 8197 | } |
| 8198 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8199 | PyBytes_AsString(repunicode), repsize); |
| 8200 | *respos += repsize; |
| 8201 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8202 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8203 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8204 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8205 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame^] | 8206 | if (PyUnicode_READY(repunicode) < 0) { |
| 8207 | Py_DECREF(repunicode); |
| 8208 | return -1; |
| 8209 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8210 | repsize = PyUnicode_GET_SIZE(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame^] | 8211 | data = PyUnicode_DATA(repunicode); |
| 8212 | kind = PyUnicode_KIND(repunicode); |
| 8213 | for (index = 0; index < repsize; index++) { |
| 8214 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8215 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame^] | 8217 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | return -1; |
| 8219 | } |
| 8220 | else if (x==enc_FAILED) { |
| 8221 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8222 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | return -1; |
| 8224 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8225 | } |
| 8226 | *inpos = newpos; |
| 8227 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8228 | } |
| 8229 | return 0; |
| 8230 | } |
| 8231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8232 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8233 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8234 | PyObject *mapping, |
| 8235 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8237 | /* output object */ |
| 8238 | PyObject *res = NULL; |
| 8239 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8240 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8241 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8243 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | PyObject *errorHandler = NULL; |
| 8245 | PyObject *exc = NULL; |
| 8246 | /* the following variable is used for caching string comparisons |
| 8247 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8248 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8249 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8250 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8251 | if (PyUnicode_READY(unicode) < 0) |
| 8252 | return NULL; |
| 8253 | size = PyUnicode_GET_LENGTH(unicode); |
| 8254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8255 | /* Default to Latin-1 */ |
| 8256 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8257 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8258 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8259 | /* allocate enough for a simple encoding without |
| 8260 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8261 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8262 | if (res == NULL) |
| 8263 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8264 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8265 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8266 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8267 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8268 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8269 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8270 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | if (x==enc_EXCEPTION) /* error */ |
| 8272 | goto onError; |
| 8273 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8274 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8275 | &exc, |
| 8276 | &known_errorHandler, &errorHandler, errors, |
| 8277 | &res, &respos)) { |
| 8278 | goto onError; |
| 8279 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8280 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8281 | else |
| 8282 | /* done with this character => adjust input position */ |
| 8283 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8286 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8287 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8288 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8289 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8290 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8291 | Py_XDECREF(exc); |
| 8292 | Py_XDECREF(errorHandler); |
| 8293 | return res; |
| 8294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8295 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8296 | Py_XDECREF(res); |
| 8297 | Py_XDECREF(exc); |
| 8298 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | return NULL; |
| 8300 | } |
| 8301 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8302 | /* Deprecated */ |
| 8303 | PyObject * |
| 8304 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8305 | Py_ssize_t size, |
| 8306 | PyObject *mapping, |
| 8307 | const char *errors) |
| 8308 | { |
| 8309 | PyObject *result; |
| 8310 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8311 | if (unicode == NULL) |
| 8312 | return NULL; |
| 8313 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8314 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8315 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8316 | } |
| 8317 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8318 | PyObject * |
| 8319 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8320 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | { |
| 8322 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | PyErr_BadArgument(); |
| 8324 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8326 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | } |
| 8328 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8330 | static void |
| 8331 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8332 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8333 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8334 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8335 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8336 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8337 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8338 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8339 | } |
| 8340 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8341 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8342 | goto onError; |
| 8343 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8344 | goto onError; |
| 8345 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8346 | goto onError; |
| 8347 | return; |
| 8348 | onError: |
| 8349 | Py_DECREF(*exceptionObject); |
| 8350 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | } |
| 8352 | } |
| 8353 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8354 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8355 | static void |
| 8356 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8357 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8358 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8359 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8360 | { |
| 8361 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8362 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8363 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8365 | } |
| 8366 | |
| 8367 | /* error handling callback helper: |
| 8368 | build arguments, call the callback and check the arguments, |
| 8369 | put the result into newpos and return the replacement string, which |
| 8370 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8371 | static PyObject * |
| 8372 | unicode_translate_call_errorhandler(const char *errors, |
| 8373 | PyObject **errorHandler, |
| 8374 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8375 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8376 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8377 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8378 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8379 | 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] | 8380 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8381 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8382 | PyObject *restuple; |
| 8383 | PyObject *resunicode; |
| 8384 | |
| 8385 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8387 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8389 | } |
| 8390 | |
| 8391 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8392 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8393 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8395 | |
| 8396 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8398 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8399 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8400 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8401 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | Py_DECREF(restuple); |
| 8403 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8404 | } |
| 8405 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8406 | &resunicode, &i_newpos)) { |
| 8407 | Py_DECREF(restuple); |
| 8408 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8409 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8410 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8411 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8412 | else |
| 8413 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8414 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8416 | Py_DECREF(restuple); |
| 8417 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8418 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8419 | Py_INCREF(resunicode); |
| 8420 | Py_DECREF(restuple); |
| 8421 | return resunicode; |
| 8422 | } |
| 8423 | |
| 8424 | /* Lookup the character ch in the mapping and put the result in result, |
| 8425 | which must be decrefed by the caller. |
| 8426 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8427 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8428 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8429 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8430 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | PyObject *x; |
| 8432 | |
| 8433 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8434 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8435 | x = PyObject_GetItem(mapping, w); |
| 8436 | Py_DECREF(w); |
| 8437 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8438 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8439 | /* No mapping found means: use 1:1 mapping. */ |
| 8440 | PyErr_Clear(); |
| 8441 | *result = NULL; |
| 8442 | return 0; |
| 8443 | } else |
| 8444 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8445 | } |
| 8446 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8447 | *result = x; |
| 8448 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8449 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8450 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | long value = PyLong_AS_LONG(x); |
| 8452 | long max = PyUnicode_GetMax(); |
| 8453 | if (value < 0 || value > max) { |
| 8454 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8455 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | Py_DECREF(x); |
| 8457 | return -1; |
| 8458 | } |
| 8459 | *result = x; |
| 8460 | return 0; |
| 8461 | } |
| 8462 | else if (PyUnicode_Check(x)) { |
| 8463 | *result = x; |
| 8464 | return 0; |
| 8465 | } |
| 8466 | else { |
| 8467 | /* wrong return value */ |
| 8468 | PyErr_SetString(PyExc_TypeError, |
| 8469 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8470 | Py_DECREF(x); |
| 8471 | return -1; |
| 8472 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8473 | } |
| 8474 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8475 | if not reallocate and adjust various state variables. |
| 8476 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8477 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8478 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8479 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8480 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8481 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8482 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | /* exponentially overallocate to minimize reallocations */ |
| 8484 | if (requiredsize < 2 * oldsize) |
| 8485 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8486 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8487 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8488 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8489 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8490 | } |
| 8491 | return 0; |
| 8492 | } |
| 8493 | /* lookup the character, put the result in the output string and adjust |
| 8494 | various state variables. Return a new reference to the object that |
| 8495 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8496 | undefined (in which case no character was written). |
| 8497 | The called must decref result. |
| 8498 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8499 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8500 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8501 | PyObject *mapping, Py_UCS4 **output, |
| 8502 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8503 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8504 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8505 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8506 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8508 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8510 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8511 | } |
| 8512 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8513 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8514 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | /* 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] | 8516 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8517 | } |
| 8518 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8519 | Py_ssize_t repsize; |
| 8520 | if (PyUnicode_READY(*res) == -1) |
| 8521 | return -1; |
| 8522 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | if (repsize==1) { |
| 8524 | /* 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] | 8525 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8526 | } |
| 8527 | else if (repsize!=0) { |
| 8528 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | Py_ssize_t requiredsize = *opos + |
| 8530 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8531 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8532 | Py_ssize_t i; |
| 8533 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8534 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8535 | for(i = 0; i < repsize; i++) |
| 8536 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8538 | } |
| 8539 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8541 | return 0; |
| 8542 | } |
| 8543 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8544 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8545 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8546 | PyObject *mapping, |
| 8547 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8549 | /* input object */ |
| 8550 | char *idata; |
| 8551 | Py_ssize_t size, i; |
| 8552 | int kind; |
| 8553 | /* output buffer */ |
| 8554 | Py_UCS4 *output = NULL; |
| 8555 | Py_ssize_t osize; |
| 8556 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8557 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8558 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8559 | char *reason = "character maps to <undefined>"; |
| 8560 | PyObject *errorHandler = NULL; |
| 8561 | PyObject *exc = NULL; |
| 8562 | /* the following variable is used for caching string comparisons |
| 8563 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8564 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8565 | int known_errorHandler = -1; |
| 8566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8567 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8568 | PyErr_BadArgument(); |
| 8569 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8570 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8571 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8572 | if (PyUnicode_READY(input) == -1) |
| 8573 | return NULL; |
| 8574 | idata = (char*)PyUnicode_DATA(input); |
| 8575 | kind = PyUnicode_KIND(input); |
| 8576 | size = PyUnicode_GET_LENGTH(input); |
| 8577 | i = 0; |
| 8578 | |
| 8579 | if (size == 0) { |
| 8580 | Py_INCREF(input); |
| 8581 | return input; |
| 8582 | } |
| 8583 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8584 | /* allocate enough for a simple 1:1 translation without |
| 8585 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8586 | osize = size; |
| 8587 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8588 | opos = 0; |
| 8589 | if (output == NULL) { |
| 8590 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8593 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8594 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8595 | /* try to encode it */ |
| 8596 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8597 | if (charmaptranslate_output(input, i, mapping, |
| 8598 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8599 | Py_XDECREF(x); |
| 8600 | goto onError; |
| 8601 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8602 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8603 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8605 | else { /* untranslatable character */ |
| 8606 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8607 | Py_ssize_t repsize; |
| 8608 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8611 | Py_ssize_t collstart = i; |
| 8612 | Py_ssize_t collend = i+1; |
| 8613 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8614 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8615 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | while (collend < size) { |
| 8617 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8618 | goto onError; |
| 8619 | Py_XDECREF(x); |
| 8620 | if (x!=Py_None) |
| 8621 | break; |
| 8622 | ++collend; |
| 8623 | } |
| 8624 | /* cache callback name lookup |
| 8625 | * (if not done yet, i.e. it's the first error) */ |
| 8626 | if (known_errorHandler==-1) { |
| 8627 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8628 | known_errorHandler = 1; |
| 8629 | else if (!strcmp(errors, "replace")) |
| 8630 | known_errorHandler = 2; |
| 8631 | else if (!strcmp(errors, "ignore")) |
| 8632 | known_errorHandler = 3; |
| 8633 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8634 | known_errorHandler = 4; |
| 8635 | else |
| 8636 | known_errorHandler = 0; |
| 8637 | } |
| 8638 | switch (known_errorHandler) { |
| 8639 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8640 | raise_translate_exception(&exc, input, collstart, |
| 8641 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8642 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8643 | case 2: /* replace */ |
| 8644 | /* 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] | 8645 | for (coll = collstart; coll<collend; coll++) |
| 8646 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8647 | /* fall through */ |
| 8648 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8649 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | break; |
| 8651 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | /* generate replacement (temporarily (mis)uses i) */ |
| 8653 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8654 | char buffer[2+29+1+1]; |
| 8655 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8656 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8657 | if (charmaptranslate_makespace(&output, &osize, |
| 8658 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8659 | goto onError; |
| 8660 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8661 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8664 | break; |
| 8665 | default: |
| 8666 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8667 | reason, input, &exc, |
| 8668 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8669 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8670 | goto onError; |
| 8671 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8672 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8673 | if (charmaptranslate_makespace(&output, &osize, |
| 8674 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8675 | Py_DECREF(repunicode); |
| 8676 | goto onError; |
| 8677 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8679 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8680 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8681 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8682 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8683 | } |
| 8684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8685 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8686 | if (!res) |
| 8687 | goto onError; |
| 8688 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8689 | Py_XDECREF(exc); |
| 8690 | Py_XDECREF(errorHandler); |
| 8691 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8692 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8694 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8695 | Py_XDECREF(exc); |
| 8696 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | return NULL; |
| 8698 | } |
| 8699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8700 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8701 | PyObject * |
| 8702 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8703 | Py_ssize_t size, |
| 8704 | PyObject *mapping, |
| 8705 | const char *errors) |
| 8706 | { |
| 8707 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8708 | if (!unicode) |
| 8709 | return NULL; |
| 8710 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8711 | } |
| 8712 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8713 | PyObject * |
| 8714 | PyUnicode_Translate(PyObject *str, |
| 8715 | PyObject *mapping, |
| 8716 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | { |
| 8718 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8720 | str = PyUnicode_FromObject(str); |
| 8721 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8722 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8723 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | Py_DECREF(str); |
| 8725 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | Py_XDECREF(str); |
| 8729 | return NULL; |
| 8730 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8732 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8733 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 | { |
| 8735 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8736 | called as a callback from fixup() which does it already. */ |
| 8737 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8738 | const int kind = PyUnicode_KIND(self); |
| 8739 | void *data = PyUnicode_DATA(self); |
| 8740 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8741 | Py_ssize_t i; |
| 8742 | |
| 8743 | for (i = 0; i < len; ++i) { |
| 8744 | ch = PyUnicode_READ(kind, data, i); |
| 8745 | fixed = 0; |
| 8746 | if (ch > 127) { |
| 8747 | if (Py_UNICODE_ISSPACE(ch)) |
| 8748 | fixed = ' '; |
| 8749 | else { |
| 8750 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8751 | if (decimal >= 0) |
| 8752 | fixed = '0' + decimal; |
| 8753 | } |
| 8754 | if (fixed != 0) { |
| 8755 | if (fixed > maxchar) |
| 8756 | maxchar = fixed; |
| 8757 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8758 | } |
| 8759 | else if (ch > maxchar) |
| 8760 | maxchar = ch; |
| 8761 | } |
| 8762 | else if (ch > maxchar) |
| 8763 | maxchar = ch; |
| 8764 | } |
| 8765 | |
| 8766 | return maxchar; |
| 8767 | } |
| 8768 | |
| 8769 | PyObject * |
| 8770 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8771 | { |
| 8772 | if (!PyUnicode_Check(unicode)) { |
| 8773 | PyErr_BadInternalCall(); |
| 8774 | return NULL; |
| 8775 | } |
| 8776 | if (PyUnicode_READY(unicode) == -1) |
| 8777 | return NULL; |
| 8778 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8779 | /* If the string is already ASCII, just return the same string */ |
| 8780 | Py_INCREF(unicode); |
| 8781 | return unicode; |
| 8782 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8783 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | } |
| 8785 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8786 | PyObject * |
| 8787 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8788 | Py_ssize_t length) |
| 8789 | { |
| 8790 | PyObject *result; |
| 8791 | Py_UNICODE *p; /* write pointer into result */ |
| 8792 | Py_ssize_t i; |
| 8793 | /* Copy to a new string */ |
| 8794 | result = (PyObject *)_PyUnicode_New(length); |
| 8795 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8796 | if (result == NULL) |
| 8797 | return result; |
| 8798 | p = PyUnicode_AS_UNICODE(result); |
| 8799 | /* Iterate over code points */ |
| 8800 | for (i = 0; i < length; i++) { |
| 8801 | Py_UNICODE ch =s[i]; |
| 8802 | if (ch > 127) { |
| 8803 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8804 | if (decimal >= 0) |
| 8805 | p[i] = '0' + decimal; |
| 8806 | } |
| 8807 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8808 | #ifndef DONT_MAKE_RESULT_READY |
| 8809 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8810 | Py_DECREF(result); |
| 8811 | return NULL; |
| 8812 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8813 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8814 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8815 | return result; |
| 8816 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8817 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8818 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8819 | int |
| 8820 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8821 | Py_ssize_t length, |
| 8822 | char *output, |
| 8823 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8824 | { |
| 8825 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8826 | PyObject *errorHandler = NULL; |
| 8827 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8828 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8829 | const char *encoding = "decimal"; |
| 8830 | const char *reason = "invalid decimal Unicode string"; |
| 8831 | /* the following variable is used for caching string comparisons |
| 8832 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8833 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8834 | |
| 8835 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8836 | PyErr_BadArgument(); |
| 8837 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8838 | } |
| 8839 | |
| 8840 | p = s; |
| 8841 | end = s + length; |
| 8842 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8843 | register Py_UNICODE ch = *p; |
| 8844 | int decimal; |
| 8845 | PyObject *repunicode; |
| 8846 | Py_ssize_t repsize; |
| 8847 | Py_ssize_t newpos; |
| 8848 | Py_UNICODE *uni2; |
| 8849 | Py_UNICODE *collstart; |
| 8850 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8851 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8852 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8853 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8854 | ++p; |
| 8855 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8856 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8857 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8858 | if (decimal >= 0) { |
| 8859 | *output++ = '0' + decimal; |
| 8860 | ++p; |
| 8861 | continue; |
| 8862 | } |
| 8863 | if (0 < ch && ch < 256) { |
| 8864 | *output++ = (char)ch; |
| 8865 | ++p; |
| 8866 | continue; |
| 8867 | } |
| 8868 | /* All other characters are considered unencodable */ |
| 8869 | collstart = p; |
| 8870 | collend = p+1; |
| 8871 | while (collend < end) { |
| 8872 | if ((0 < *collend && *collend < 256) || |
| 8873 | !Py_UNICODE_ISSPACE(*collend) || |
| 8874 | Py_UNICODE_TODECIMAL(*collend)) |
| 8875 | break; |
| 8876 | } |
| 8877 | /* cache callback name lookup |
| 8878 | * (if not done yet, i.e. it's the first error) */ |
| 8879 | if (known_errorHandler==-1) { |
| 8880 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8881 | known_errorHandler = 1; |
| 8882 | else if (!strcmp(errors, "replace")) |
| 8883 | known_errorHandler = 2; |
| 8884 | else if (!strcmp(errors, "ignore")) |
| 8885 | known_errorHandler = 3; |
| 8886 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8887 | known_errorHandler = 4; |
| 8888 | else |
| 8889 | known_errorHandler = 0; |
| 8890 | } |
| 8891 | switch (known_errorHandler) { |
| 8892 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8893 | unicode = PyUnicode_FromUnicode(s, length); |
| 8894 | if (unicode == NULL) |
| 8895 | goto onError; |
| 8896 | raise_encode_exception(&exc, encoding, unicode, collstart-s, collend-s, reason); |
| 8897 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8898 | goto onError; |
| 8899 | case 2: /* replace */ |
| 8900 | for (p = collstart; p < collend; ++p) |
| 8901 | *output++ = '?'; |
| 8902 | /* fall through */ |
| 8903 | case 3: /* ignore */ |
| 8904 | p = collend; |
| 8905 | break; |
| 8906 | case 4: /* xmlcharrefreplace */ |
| 8907 | /* generate replacement (temporarily (mis)uses p) */ |
| 8908 | for (p = collstart; p < collend; ++p) |
| 8909 | output += sprintf(output, "&#%d;", (int)*p); |
| 8910 | p = collend; |
| 8911 | break; |
| 8912 | default: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8913 | unicode = PyUnicode_FromUnicode(s, length); |
| 8914 | if (unicode == NULL) |
| 8915 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8916 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8917 | encoding, reason, unicode, &exc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8918 | collstart-s, collend-s, &newpos); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8919 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8920 | if (repunicode == NULL) |
| 8921 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8922 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8923 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8924 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8925 | Py_DECREF(repunicode); |
| 8926 | goto onError; |
| 8927 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8928 | /* generate replacement */ |
| 8929 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8930 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8931 | Py_UNICODE ch = *uni2; |
| 8932 | if (Py_UNICODE_ISSPACE(ch)) |
| 8933 | *output++ = ' '; |
| 8934 | else { |
| 8935 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8936 | if (decimal >= 0) |
| 8937 | *output++ = '0' + decimal; |
| 8938 | else if (0 < ch && ch < 256) |
| 8939 | *output++ = (char)ch; |
| 8940 | else { |
| 8941 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8942 | unicode = PyUnicode_FromUnicode(s, length); |
| 8943 | if (unicode == NULL) |
| 8944 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8945 | raise_encode_exception(&exc, encoding, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8946 | unicode, collstart-s, collend-s, reason); |
| 8947 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8948 | goto onError; |
| 8949 | } |
| 8950 | } |
| 8951 | } |
| 8952 | p = s + newpos; |
| 8953 | Py_DECREF(repunicode); |
| 8954 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8955 | } |
| 8956 | /* 0-terminate the output string */ |
| 8957 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8958 | Py_XDECREF(exc); |
| 8959 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8960 | return 0; |
| 8961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8962 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8963 | Py_XDECREF(exc); |
| 8964 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8965 | return -1; |
| 8966 | } |
| 8967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | /* --- Helpers ------------------------------------------------------------ */ |
| 8969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8971 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8972 | Py_ssize_t start, |
| 8973 | Py_ssize_t end) |
| 8974 | { |
| 8975 | int kind1, kind2, kind; |
| 8976 | void *buf1, *buf2; |
| 8977 | Py_ssize_t len1, len2, result; |
| 8978 | |
| 8979 | kind1 = PyUnicode_KIND(s1); |
| 8980 | kind2 = PyUnicode_KIND(s2); |
| 8981 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8982 | buf1 = PyUnicode_DATA(s1); |
| 8983 | buf2 = PyUnicode_DATA(s2); |
| 8984 | if (kind1 != kind) |
| 8985 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8986 | if (!buf1) |
| 8987 | return -2; |
| 8988 | if (kind2 != kind) |
| 8989 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8990 | if (!buf2) { |
| 8991 | if (kind1 != kind) PyMem_Free(buf1); |
| 8992 | return -2; |
| 8993 | } |
| 8994 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8995 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8996 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8997 | if (direction > 0) { |
| 8998 | switch(kind) { |
| 8999 | case PyUnicode_1BYTE_KIND: |
| 9000 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9001 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9002 | else |
| 9003 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9004 | break; |
| 9005 | case PyUnicode_2BYTE_KIND: |
| 9006 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9007 | break; |
| 9008 | case PyUnicode_4BYTE_KIND: |
| 9009 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9010 | break; |
| 9011 | default: |
| 9012 | assert(0); result = -2; |
| 9013 | } |
| 9014 | } |
| 9015 | else { |
| 9016 | switch(kind) { |
| 9017 | case PyUnicode_1BYTE_KIND: |
| 9018 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9019 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9020 | else |
| 9021 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9022 | break; |
| 9023 | case PyUnicode_2BYTE_KIND: |
| 9024 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9025 | break; |
| 9026 | case PyUnicode_4BYTE_KIND: |
| 9027 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9028 | break; |
| 9029 | default: |
| 9030 | assert(0); result = -2; |
| 9031 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9032 | } |
| 9033 | |
| 9034 | if (kind1 != kind) |
| 9035 | PyMem_Free(buf1); |
| 9036 | if (kind2 != kind) |
| 9037 | PyMem_Free(buf2); |
| 9038 | |
| 9039 | return result; |
| 9040 | } |
| 9041 | |
| 9042 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9043 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9044 | Py_ssize_t n_buffer, |
| 9045 | void *digits, Py_ssize_t n_digits, |
| 9046 | Py_ssize_t min_width, |
| 9047 | const char *grouping, |
| 9048 | const char *thousands_sep) |
| 9049 | { |
| 9050 | switch(kind) { |
| 9051 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9052 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9053 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9054 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9055 | min_width, grouping, thousands_sep); |
| 9056 | else |
| 9057 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9058 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9059 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9060 | case PyUnicode_2BYTE_KIND: |
| 9061 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9062 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9063 | min_width, grouping, thousands_sep); |
| 9064 | case PyUnicode_4BYTE_KIND: |
| 9065 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9066 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9067 | min_width, grouping, thousands_sep); |
| 9068 | } |
| 9069 | assert(0); |
| 9070 | return -1; |
| 9071 | } |
| 9072 | |
| 9073 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9074 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9075 | #define ADJUST_INDICES(start, end, len) \ |
| 9076 | if (end > len) \ |
| 9077 | end = len; \ |
| 9078 | else if (end < 0) { \ |
| 9079 | end += len; \ |
| 9080 | if (end < 0) \ |
| 9081 | end = 0; \ |
| 9082 | } \ |
| 9083 | if (start < 0) { \ |
| 9084 | start += len; \ |
| 9085 | if (start < 0) \ |
| 9086 | start = 0; \ |
| 9087 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9088 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9089 | Py_ssize_t |
| 9090 | PyUnicode_Count(PyObject *str, |
| 9091 | PyObject *substr, |
| 9092 | Py_ssize_t start, |
| 9093 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9094 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9095 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9096 | PyObject* str_obj; |
| 9097 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | int kind1, kind2, kind; |
| 9099 | void *buf1 = NULL, *buf2 = NULL; |
| 9100 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9101 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9102 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9103 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9104 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9105 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9106 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9107 | Py_DECREF(str_obj); |
| 9108 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9109 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9110 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9111 | kind1 = PyUnicode_KIND(str_obj); |
| 9112 | kind2 = PyUnicode_KIND(sub_obj); |
| 9113 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9114 | buf1 = PyUnicode_DATA(str_obj); |
| 9115 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9116 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9117 | if (!buf1) |
| 9118 | goto onError; |
| 9119 | buf2 = PyUnicode_DATA(sub_obj); |
| 9120 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9121 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9122 | if (!buf2) |
| 9123 | goto onError; |
| 9124 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9125 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9126 | |
| 9127 | ADJUST_INDICES(start, end, len1); |
| 9128 | switch(kind) { |
| 9129 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9130 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9131 | result = asciilib_count( |
| 9132 | ((Py_UCS1*)buf1) + start, end - start, |
| 9133 | buf2, len2, PY_SSIZE_T_MAX |
| 9134 | ); |
| 9135 | else |
| 9136 | result = ucs1lib_count( |
| 9137 | ((Py_UCS1*)buf1) + start, end - start, |
| 9138 | buf2, len2, PY_SSIZE_T_MAX |
| 9139 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9140 | break; |
| 9141 | case PyUnicode_2BYTE_KIND: |
| 9142 | result = ucs2lib_count( |
| 9143 | ((Py_UCS2*)buf1) + start, end - start, |
| 9144 | buf2, len2, PY_SSIZE_T_MAX |
| 9145 | ); |
| 9146 | break; |
| 9147 | case PyUnicode_4BYTE_KIND: |
| 9148 | result = ucs4lib_count( |
| 9149 | ((Py_UCS4*)buf1) + start, end - start, |
| 9150 | buf2, len2, PY_SSIZE_T_MAX |
| 9151 | ); |
| 9152 | break; |
| 9153 | default: |
| 9154 | assert(0); result = 0; |
| 9155 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9156 | |
| 9157 | Py_DECREF(sub_obj); |
| 9158 | Py_DECREF(str_obj); |
| 9159 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9160 | if (kind1 != kind) |
| 9161 | PyMem_Free(buf1); |
| 9162 | if (kind2 != kind) |
| 9163 | PyMem_Free(buf2); |
| 9164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9165 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9166 | onError: |
| 9167 | Py_DECREF(sub_obj); |
| 9168 | Py_DECREF(str_obj); |
| 9169 | if (kind1 != kind && buf1) |
| 9170 | PyMem_Free(buf1); |
| 9171 | if (kind2 != kind && buf2) |
| 9172 | PyMem_Free(buf2); |
| 9173 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9174 | } |
| 9175 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9176 | Py_ssize_t |
| 9177 | PyUnicode_Find(PyObject *str, |
| 9178 | PyObject *sub, |
| 9179 | Py_ssize_t start, |
| 9180 | Py_ssize_t end, |
| 9181 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9182 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9183 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9186 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9187 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9188 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9189 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9190 | Py_DECREF(str); |
| 9191 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9192 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9193 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9194 | result = any_find_slice(direction, |
| 9195 | str, sub, start, end |
| 9196 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9198 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9199 | Py_DECREF(sub); |
| 9200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9201 | return result; |
| 9202 | } |
| 9203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9204 | Py_ssize_t |
| 9205 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9206 | Py_ssize_t start, Py_ssize_t end, |
| 9207 | int direction) |
| 9208 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9209 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9210 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9211 | if (PyUnicode_READY(str) == -1) |
| 9212 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9213 | if (start < 0 || end < 0) { |
| 9214 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9215 | return -2; |
| 9216 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9217 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9218 | end = PyUnicode_GET_LENGTH(str); |
| 9219 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9220 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9221 | kind, end-start, ch, direction); |
| 9222 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9223 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9224 | else |
| 9225 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9226 | } |
| 9227 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9228 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9229 | tailmatch(PyObject *self, |
| 9230 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9231 | Py_ssize_t start, |
| 9232 | Py_ssize_t end, |
| 9233 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9234 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9235 | int kind_self; |
| 9236 | int kind_sub; |
| 9237 | void *data_self; |
| 9238 | void *data_sub; |
| 9239 | Py_ssize_t offset; |
| 9240 | Py_ssize_t i; |
| 9241 | Py_ssize_t end_sub; |
| 9242 | |
| 9243 | if (PyUnicode_READY(self) == -1 || |
| 9244 | PyUnicode_READY(substring) == -1) |
| 9245 | return 0; |
| 9246 | |
| 9247 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9248 | return 1; |
| 9249 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9250 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9251 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9252 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9253 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9255 | kind_self = PyUnicode_KIND(self); |
| 9256 | data_self = PyUnicode_DATA(self); |
| 9257 | kind_sub = PyUnicode_KIND(substring); |
| 9258 | data_sub = PyUnicode_DATA(substring); |
| 9259 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9260 | |
| 9261 | if (direction > 0) |
| 9262 | offset = end; |
| 9263 | else |
| 9264 | offset = start; |
| 9265 | |
| 9266 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9267 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9268 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9269 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9270 | /* If both are of the same kind, memcmp is sufficient */ |
| 9271 | if (kind_self == kind_sub) { |
| 9272 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9273 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9274 | data_sub, |
| 9275 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9276 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | } |
| 9278 | /* otherwise we have to compare each character by first accesing it */ |
| 9279 | else { |
| 9280 | /* We do not need to compare 0 and len(substring)-1 because |
| 9281 | the if statement above ensured already that they are equal |
| 9282 | when we end up here. */ |
| 9283 | // TODO: honor direction and do a forward or backwards search |
| 9284 | for (i = 1; i < end_sub; ++i) { |
| 9285 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9286 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9287 | return 0; |
| 9288 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9289 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9290 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9291 | } |
| 9292 | |
| 9293 | return 0; |
| 9294 | } |
| 9295 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9296 | Py_ssize_t |
| 9297 | PyUnicode_Tailmatch(PyObject *str, |
| 9298 | PyObject *substr, |
| 9299 | Py_ssize_t start, |
| 9300 | Py_ssize_t end, |
| 9301 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9303 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9305 | str = PyUnicode_FromObject(str); |
| 9306 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9307 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9308 | substr = PyUnicode_FromObject(substr); |
| 9309 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9310 | Py_DECREF(str); |
| 9311 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9312 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9313 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9314 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9315 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9316 | Py_DECREF(str); |
| 9317 | Py_DECREF(substr); |
| 9318 | return result; |
| 9319 | } |
| 9320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | /* Apply fixfct filter to the Unicode object self and return a |
| 9322 | reference to the modified object */ |
| 9323 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9324 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9325 | fixup(PyObject *self, |
| 9326 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9327 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | PyObject *u; |
| 9329 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9330 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | if (PyUnicode_READY(self) == -1) |
| 9332 | return NULL; |
| 9333 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 9334 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 9335 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9336 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9337 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9338 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9339 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9340 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9342 | /* fix functions return the new maximum character in a string, |
| 9343 | if the kind of the resulting unicode object does not change, |
| 9344 | everything is fine. Otherwise we need to change the string kind |
| 9345 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9346 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | if (maxchar_new == 0) |
| 9348 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9349 | else if (maxchar_new <= 127) |
| 9350 | maxchar_new = 127; |
| 9351 | else if (maxchar_new <= 255) |
| 9352 | maxchar_new = 255; |
| 9353 | else if (maxchar_new <= 65535) |
| 9354 | maxchar_new = 65535; |
| 9355 | else |
| 9356 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9357 | |
| 9358 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9359 | /* fixfct should return TRUE if it modified the buffer. If |
| 9360 | FALSE, return a reference to the original buffer instead |
| 9361 | (to save space, not time) */ |
| 9362 | Py_INCREF(self); |
| 9363 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9364 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9366 | else if (maxchar_new == maxchar_old) { |
| 9367 | return u; |
| 9368 | } |
| 9369 | else { |
| 9370 | /* In case the maximum character changed, we need to |
| 9371 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9372 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9373 | if (v == NULL) { |
| 9374 | Py_DECREF(u); |
| 9375 | return NULL; |
| 9376 | } |
| 9377 | if (maxchar_new > maxchar_old) { |
| 9378 | /* If the maxchar increased so that the kind changed, not all |
| 9379 | characters are representable anymore and we need to fix the |
| 9380 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9381 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9382 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9383 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9384 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9385 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9386 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9387 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9388 | |
| 9389 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9390 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9391 | return v; |
| 9392 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9393 | } |
| 9394 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9395 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9396 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9397 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9398 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9399 | called as a callback from fixup() which does it already. */ |
| 9400 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9401 | const int kind = PyUnicode_KIND(self); |
| 9402 | void *data = PyUnicode_DATA(self); |
| 9403 | int touched = 0; |
| 9404 | Py_UCS4 maxchar = 0; |
| 9405 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9406 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9407 | for (i = 0; i < len; ++i) { |
| 9408 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9409 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9410 | if (up != ch) { |
| 9411 | if (up > maxchar) |
| 9412 | maxchar = up; |
| 9413 | PyUnicode_WRITE(kind, data, i, up); |
| 9414 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9415 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9416 | else if (ch > maxchar) |
| 9417 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9418 | } |
| 9419 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9420 | if (touched) |
| 9421 | return maxchar; |
| 9422 | else |
| 9423 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9426 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9427 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9428 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9429 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9430 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9431 | const int kind = PyUnicode_KIND(self); |
| 9432 | void *data = PyUnicode_DATA(self); |
| 9433 | int touched = 0; |
| 9434 | Py_UCS4 maxchar = 0; |
| 9435 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9437 | for(i = 0; i < len; ++i) { |
| 9438 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9439 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9440 | if (lo != ch) { |
| 9441 | if (lo > maxchar) |
| 9442 | maxchar = lo; |
| 9443 | PyUnicode_WRITE(kind, data, i, lo); |
| 9444 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9445 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9446 | else if (ch > maxchar) |
| 9447 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9448 | } |
| 9449 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | if (touched) |
| 9451 | return maxchar; |
| 9452 | else |
| 9453 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9454 | } |
| 9455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9456 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9457 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9458 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9459 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9460 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9461 | const int kind = PyUnicode_KIND(self); |
| 9462 | void *data = PyUnicode_DATA(self); |
| 9463 | int touched = 0; |
| 9464 | Py_UCS4 maxchar = 0; |
| 9465 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9467 | for(i = 0; i < len; ++i) { |
| 9468 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9469 | Py_UCS4 nu = 0; |
| 9470 | |
| 9471 | if (Py_UNICODE_ISUPPER(ch)) |
| 9472 | nu = Py_UNICODE_TOLOWER(ch); |
| 9473 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9474 | nu = Py_UNICODE_TOUPPER(ch); |
| 9475 | |
| 9476 | if (nu != 0) { |
| 9477 | if (nu > maxchar) |
| 9478 | maxchar = nu; |
| 9479 | PyUnicode_WRITE(kind, data, i, nu); |
| 9480 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | else if (ch > maxchar) |
| 9483 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9486 | if (touched) |
| 9487 | return maxchar; |
| 9488 | else |
| 9489 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9490 | } |
| 9491 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9492 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9493 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9496 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9497 | const int kind = PyUnicode_KIND(self); |
| 9498 | void *data = PyUnicode_DATA(self); |
| 9499 | int touched = 0; |
| 9500 | Py_UCS4 maxchar = 0; |
| 9501 | Py_ssize_t i = 0; |
| 9502 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9503 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9504 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9505 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | |
| 9507 | ch = PyUnicode_READ(kind, data, i); |
| 9508 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9509 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9510 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9511 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9512 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9513 | ++i; |
| 9514 | for(; i < len; ++i) { |
| 9515 | ch = PyUnicode_READ(kind, data, i); |
| 9516 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9517 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9518 | if (lo > maxchar) |
| 9519 | maxchar = lo; |
| 9520 | PyUnicode_WRITE(kind, data, i, lo); |
| 9521 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9522 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9523 | else if (ch > maxchar) |
| 9524 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9525 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9526 | |
| 9527 | if (touched) |
| 9528 | return maxchar; |
| 9529 | else |
| 9530 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9534 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9536 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9537 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9538 | const int kind = PyUnicode_KIND(self); |
| 9539 | void *data = PyUnicode_DATA(self); |
| 9540 | Py_UCS4 maxchar = 0; |
| 9541 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9542 | int previous_is_cased; |
| 9543 | |
| 9544 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9545 | if (len == 1) { |
| 9546 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9547 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9548 | if (ti != ch) { |
| 9549 | PyUnicode_WRITE(kind, data, i, ti); |
| 9550 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9551 | } |
| 9552 | else |
| 9553 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9554 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9555 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9556 | for(; i < len; ++i) { |
| 9557 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9558 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9559 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9560 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9561 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9562 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9563 | nu = Py_UNICODE_TOTITLE(ch); |
| 9564 | |
| 9565 | if (nu > maxchar) |
| 9566 | maxchar = nu; |
| 9567 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9568 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9569 | if (Py_UNICODE_ISLOWER(ch) || |
| 9570 | Py_UNICODE_ISUPPER(ch) || |
| 9571 | Py_UNICODE_ISTITLE(ch)) |
| 9572 | previous_is_cased = 1; |
| 9573 | else |
| 9574 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9575 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9576 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9577 | } |
| 9578 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9579 | PyObject * |
| 9580 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9582 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9583 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9584 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9585 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9586 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9587 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9588 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9589 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9590 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9591 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9592 | int use_memcpy; |
| 9593 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9594 | PyObject *last_obj; |
| 9595 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9596 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9597 | fseq = PySequence_Fast(seq, ""); |
| 9598 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9599 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9600 | } |
| 9601 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9602 | /* NOTE: the following code can't call back into Python code, |
| 9603 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9604 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9605 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9606 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9607 | /* If empty sequence, return u"". */ |
| 9608 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9609 | Py_DECREF(fseq); |
| 9610 | Py_INCREF(unicode_empty); |
| 9611 | res = unicode_empty; |
| 9612 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9613 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9614 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9615 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9616 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9617 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9618 | if (seqlen == 1) { |
| 9619 | if (PyUnicode_CheckExact(items[0])) { |
| 9620 | res = items[0]; |
| 9621 | Py_INCREF(res); |
| 9622 | Py_DECREF(fseq); |
| 9623 | return res; |
| 9624 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9625 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9626 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9627 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9628 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9629 | /* Set up sep and seplen */ |
| 9630 | if (separator == NULL) { |
| 9631 | /* fall back to a blank space separator */ |
| 9632 | sep = PyUnicode_FromOrdinal(' '); |
| 9633 | if (!sep) |
| 9634 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9635 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9636 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9637 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9638 | else { |
| 9639 | if (!PyUnicode_Check(separator)) { |
| 9640 | PyErr_Format(PyExc_TypeError, |
| 9641 | "separator: expected str instance," |
| 9642 | " %.80s found", |
| 9643 | Py_TYPE(separator)->tp_name); |
| 9644 | goto onError; |
| 9645 | } |
| 9646 | if (PyUnicode_READY(separator)) |
| 9647 | goto onError; |
| 9648 | sep = separator; |
| 9649 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9650 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9651 | /* inc refcount to keep this code path symmetric with the |
| 9652 | above case of a blank separator */ |
| 9653 | Py_INCREF(sep); |
| 9654 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9655 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9656 | } |
| 9657 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9658 | /* There are at least two things to join, or else we have a subclass |
| 9659 | * of str in the sequence. |
| 9660 | * Do a pre-pass to figure out the total amount of space we'll |
| 9661 | * need (sz), and see whether all argument are strings. |
| 9662 | */ |
| 9663 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9664 | #ifdef Py_DEBUG |
| 9665 | use_memcpy = 0; |
| 9666 | #else |
| 9667 | use_memcpy = 1; |
| 9668 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9669 | for (i = 0; i < seqlen; i++) { |
| 9670 | const Py_ssize_t old_sz = sz; |
| 9671 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9672 | if (!PyUnicode_Check(item)) { |
| 9673 | PyErr_Format(PyExc_TypeError, |
| 9674 | "sequence item %zd: expected str instance," |
| 9675 | " %.80s found", |
| 9676 | i, Py_TYPE(item)->tp_name); |
| 9677 | goto onError; |
| 9678 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9679 | if (PyUnicode_READY(item) == -1) |
| 9680 | goto onError; |
| 9681 | sz += PyUnicode_GET_LENGTH(item); |
| 9682 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9683 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9684 | if (i != 0) |
| 9685 | sz += seplen; |
| 9686 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9687 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9688 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9689 | goto onError; |
| 9690 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9691 | if (use_memcpy && last_obj != NULL) { |
| 9692 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9693 | use_memcpy = 0; |
| 9694 | } |
| 9695 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9696 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9697 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9698 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9699 | if (res == NULL) |
| 9700 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9701 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9702 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9703 | #ifdef Py_DEBUG |
| 9704 | use_memcpy = 0; |
| 9705 | #else |
| 9706 | if (use_memcpy) { |
| 9707 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9708 | kind = PyUnicode_KIND(res); |
| 9709 | if (seplen != 0) |
| 9710 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9711 | } |
| 9712 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9714 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9715 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9716 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9717 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9718 | if (use_memcpy) { |
| 9719 | Py_MEMCPY(res_data, |
| 9720 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9721 | kind * seplen); |
| 9722 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9723 | } |
| 9724 | else { |
| 9725 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9726 | res_offset += seplen; |
| 9727 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9728 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9729 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9730 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9731 | if (use_memcpy) { |
| 9732 | Py_MEMCPY(res_data, |
| 9733 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9734 | kind * itemlen); |
| 9735 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9736 | } |
| 9737 | else { |
| 9738 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9739 | res_offset += itemlen; |
| 9740 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9741 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9742 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9743 | if (use_memcpy) |
| 9744 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9745 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9746 | else |
| 9747 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9748 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9749 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9750 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9751 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9752 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9754 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9755 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9757 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9758 | return NULL; |
| 9759 | } |
| 9760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9761 | #define FILL(kind, data, value, start, length) \ |
| 9762 | do { \ |
| 9763 | Py_ssize_t i_ = 0; \ |
| 9764 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9765 | switch ((kind)) { \ |
| 9766 | case PyUnicode_1BYTE_KIND: { \ |
| 9767 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9768 | memset(to_, (unsigned char)value, length); \ |
| 9769 | break; \ |
| 9770 | } \ |
| 9771 | case PyUnicode_2BYTE_KIND: { \ |
| 9772 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9773 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9774 | break; \ |
| 9775 | } \ |
| 9776 | default: { \ |
| 9777 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9778 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9779 | break; \ |
| 9780 | } \ |
| 9781 | } \ |
| 9782 | } while (0) |
| 9783 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9784 | static PyObject * |
| 9785 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9786 | Py_ssize_t left, |
| 9787 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9789 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9790 | PyObject *u; |
| 9791 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9792 | int kind; |
| 9793 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9794 | |
| 9795 | if (left < 0) |
| 9796 | left = 0; |
| 9797 | if (right < 0) |
| 9798 | right = 0; |
| 9799 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9800 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9801 | Py_INCREF(self); |
| 9802 | return self; |
| 9803 | } |
| 9804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9805 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9806 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9807 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9808 | return NULL; |
| 9809 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9811 | if (fill > maxchar) |
| 9812 | maxchar = fill; |
| 9813 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9814 | if (!u) |
| 9815 | return NULL; |
| 9816 | |
| 9817 | kind = PyUnicode_KIND(u); |
| 9818 | data = PyUnicode_DATA(u); |
| 9819 | if (left) |
| 9820 | FILL(kind, data, fill, 0, left); |
| 9821 | if (right) |
| 9822 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9823 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9824 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9825 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9826 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9827 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9828 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9829 | PyObject * |
| 9830 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9831 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9832 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9833 | |
| 9834 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9835 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9836 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | switch(PyUnicode_KIND(string)) { |
| 9839 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9840 | if (PyUnicode_IS_ASCII(string)) |
| 9841 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9842 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9843 | PyUnicode_GET_LENGTH(string), keepends); |
| 9844 | else |
| 9845 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9846 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9847 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9848 | break; |
| 9849 | case PyUnicode_2BYTE_KIND: |
| 9850 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9851 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | PyUnicode_GET_LENGTH(string), keepends); |
| 9853 | break; |
| 9854 | case PyUnicode_4BYTE_KIND: |
| 9855 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9856 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9857 | PyUnicode_GET_LENGTH(string), keepends); |
| 9858 | break; |
| 9859 | default: |
| 9860 | assert(0); |
| 9861 | list = 0; |
| 9862 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9863 | Py_DECREF(string); |
| 9864 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9865 | } |
| 9866 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9867 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9868 | split(PyObject *self, |
| 9869 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9870 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9871 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9872 | int kind1, kind2, kind; |
| 9873 | void *buf1, *buf2; |
| 9874 | Py_ssize_t len1, len2; |
| 9875 | PyObject* out; |
| 9876 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9877 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9878 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9879 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9880 | if (PyUnicode_READY(self) == -1) |
| 9881 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9883 | if (substring == NULL) |
| 9884 | switch(PyUnicode_KIND(self)) { |
| 9885 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9886 | if (PyUnicode_IS_ASCII(self)) |
| 9887 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9888 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9889 | PyUnicode_GET_LENGTH(self), maxcount |
| 9890 | ); |
| 9891 | else |
| 9892 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9893 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9894 | PyUnicode_GET_LENGTH(self), maxcount |
| 9895 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | case PyUnicode_2BYTE_KIND: |
| 9897 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9898 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | PyUnicode_GET_LENGTH(self), maxcount |
| 9900 | ); |
| 9901 | case PyUnicode_4BYTE_KIND: |
| 9902 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9903 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | PyUnicode_GET_LENGTH(self), maxcount |
| 9905 | ); |
| 9906 | default: |
| 9907 | assert(0); |
| 9908 | return NULL; |
| 9909 | } |
| 9910 | |
| 9911 | if (PyUnicode_READY(substring) == -1) |
| 9912 | return NULL; |
| 9913 | |
| 9914 | kind1 = PyUnicode_KIND(self); |
| 9915 | kind2 = PyUnicode_KIND(substring); |
| 9916 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9917 | buf1 = PyUnicode_DATA(self); |
| 9918 | buf2 = PyUnicode_DATA(substring); |
| 9919 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9920 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | if (!buf1) |
| 9922 | return NULL; |
| 9923 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9924 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | if (!buf2) { |
| 9926 | if (kind1 != kind) PyMem_Free(buf1); |
| 9927 | return NULL; |
| 9928 | } |
| 9929 | len1 = PyUnicode_GET_LENGTH(self); |
| 9930 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9931 | |
| 9932 | switch(kind) { |
| 9933 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9934 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9935 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9936 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9937 | else |
| 9938 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9939 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9940 | break; |
| 9941 | case PyUnicode_2BYTE_KIND: |
| 9942 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9943 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9944 | break; |
| 9945 | case PyUnicode_4BYTE_KIND: |
| 9946 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9947 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9948 | break; |
| 9949 | default: |
| 9950 | out = NULL; |
| 9951 | } |
| 9952 | if (kind1 != kind) |
| 9953 | PyMem_Free(buf1); |
| 9954 | if (kind2 != kind) |
| 9955 | PyMem_Free(buf2); |
| 9956 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9957 | } |
| 9958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9959 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9960 | rsplit(PyObject *self, |
| 9961 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9962 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9963 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9964 | int kind1, kind2, kind; |
| 9965 | void *buf1, *buf2; |
| 9966 | Py_ssize_t len1, len2; |
| 9967 | PyObject* out; |
| 9968 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9969 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9970 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9971 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | if (PyUnicode_READY(self) == -1) |
| 9973 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9974 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9975 | if (substring == NULL) |
| 9976 | switch(PyUnicode_KIND(self)) { |
| 9977 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9978 | if (PyUnicode_IS_ASCII(self)) |
| 9979 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9980 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9981 | PyUnicode_GET_LENGTH(self), maxcount |
| 9982 | ); |
| 9983 | else |
| 9984 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9985 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9986 | PyUnicode_GET_LENGTH(self), maxcount |
| 9987 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9988 | case PyUnicode_2BYTE_KIND: |
| 9989 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9990 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | PyUnicode_GET_LENGTH(self), maxcount |
| 9992 | ); |
| 9993 | case PyUnicode_4BYTE_KIND: |
| 9994 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9995 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9996 | PyUnicode_GET_LENGTH(self), maxcount |
| 9997 | ); |
| 9998 | default: |
| 9999 | assert(0); |
| 10000 | return NULL; |
| 10001 | } |
| 10002 | |
| 10003 | if (PyUnicode_READY(substring) == -1) |
| 10004 | return NULL; |
| 10005 | |
| 10006 | kind1 = PyUnicode_KIND(self); |
| 10007 | kind2 = PyUnicode_KIND(substring); |
| 10008 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10009 | buf1 = PyUnicode_DATA(self); |
| 10010 | buf2 = PyUnicode_DATA(substring); |
| 10011 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10012 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | if (!buf1) |
| 10014 | return NULL; |
| 10015 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10016 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | if (!buf2) { |
| 10018 | if (kind1 != kind) PyMem_Free(buf1); |
| 10019 | return NULL; |
| 10020 | } |
| 10021 | len1 = PyUnicode_GET_LENGTH(self); |
| 10022 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10023 | |
| 10024 | switch(kind) { |
| 10025 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10026 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10027 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10028 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10029 | else |
| 10030 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10031 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10032 | break; |
| 10033 | case PyUnicode_2BYTE_KIND: |
| 10034 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10035 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10036 | break; |
| 10037 | case PyUnicode_4BYTE_KIND: |
| 10038 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10039 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | break; |
| 10041 | default: |
| 10042 | out = NULL; |
| 10043 | } |
| 10044 | if (kind1 != kind) |
| 10045 | PyMem_Free(buf1); |
| 10046 | if (kind2 != kind) |
| 10047 | PyMem_Free(buf2); |
| 10048 | return out; |
| 10049 | } |
| 10050 | |
| 10051 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10052 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10053 | 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] | 10054 | { |
| 10055 | switch(kind) { |
| 10056 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10057 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10058 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10059 | else |
| 10060 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | case PyUnicode_2BYTE_KIND: |
| 10062 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10063 | case PyUnicode_4BYTE_KIND: |
| 10064 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10065 | } |
| 10066 | assert(0); |
| 10067 | return -1; |
| 10068 | } |
| 10069 | |
| 10070 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10071 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10072 | 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] | 10073 | { |
| 10074 | switch(kind) { |
| 10075 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10076 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10077 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10078 | else |
| 10079 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | case PyUnicode_2BYTE_KIND: |
| 10081 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10082 | case PyUnicode_4BYTE_KIND: |
| 10083 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10084 | } |
| 10085 | assert(0); |
| 10086 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10087 | } |
| 10088 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10089 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10090 | replace(PyObject *self, PyObject *str1, |
| 10091 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10092 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | PyObject *u; |
| 10094 | char *sbuf = PyUnicode_DATA(self); |
| 10095 | char *buf1 = PyUnicode_DATA(str1); |
| 10096 | char *buf2 = PyUnicode_DATA(str2); |
| 10097 | int srelease = 0, release1 = 0, release2 = 0; |
| 10098 | int skind = PyUnicode_KIND(self); |
| 10099 | int kind1 = PyUnicode_KIND(str1); |
| 10100 | int kind2 = PyUnicode_KIND(str2); |
| 10101 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10102 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10103 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10104 | int mayshrink; |
| 10105 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10106 | |
| 10107 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10108 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10110 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10111 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10112 | if (str1 == str2) |
| 10113 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10114 | if (skind < kind1) |
| 10115 | /* substring too wide to be present */ |
| 10116 | goto nothing; |
| 10117 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10118 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10119 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10120 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10121 | result string. */ |
| 10122 | mayshrink = (maxchar_str2 < maxchar); |
| 10123 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10124 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10126 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10127 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10129 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10131 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10132 | Py_UCS4 u1, u2; |
| 10133 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10135 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10136 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10137 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10139 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10140 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10142 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | rkind = PyUnicode_KIND(u); |
| 10144 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10145 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10146 | if (--maxcount < 0) |
| 10147 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10148 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10149 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10150 | } |
| 10151 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | int rkind = skind; |
| 10153 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10154 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 | if (kind1 < rkind) { |
| 10156 | /* widen substring */ |
| 10157 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10158 | if (!buf1) goto error; |
| 10159 | release1 = 1; |
| 10160 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10161 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10162 | if (i < 0) |
| 10163 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | if (rkind > kind2) { |
| 10165 | /* widen replacement */ |
| 10166 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10167 | if (!buf2) goto error; |
| 10168 | release2 = 1; |
| 10169 | } |
| 10170 | else if (rkind < kind2) { |
| 10171 | /* widen self and buf1 */ |
| 10172 | rkind = kind2; |
| 10173 | if (release1) PyMem_Free(buf1); |
| 10174 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10175 | if (!sbuf) goto error; |
| 10176 | srelease = 1; |
| 10177 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10178 | if (!buf1) goto error; |
| 10179 | release1 = 1; |
| 10180 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10181 | u = PyUnicode_New(slen, maxchar); |
| 10182 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10183 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10184 | assert(PyUnicode_KIND(u) == rkind); |
| 10185 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10186 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10187 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10188 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10189 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10191 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10192 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10193 | |
| 10194 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10195 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10196 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10197 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10198 | if (i == -1) |
| 10199 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10200 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10201 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10202 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10203 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10205 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10206 | } |
| 10207 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | Py_ssize_t n, i, j, ires; |
| 10209 | Py_ssize_t product, new_size; |
| 10210 | int rkind = skind; |
| 10211 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10214 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10216 | if (!buf1) goto error; |
| 10217 | release1 = 1; |
| 10218 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10219 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10220 | if (n == 0) |
| 10221 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10223 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10224 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10225 | if (!buf2) goto error; |
| 10226 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10227 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10228 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10229 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | rkind = kind2; |
| 10231 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10232 | if (!sbuf) goto error; |
| 10233 | srelease = 1; |
| 10234 | if (release1) PyMem_Free(buf1); |
| 10235 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10236 | if (!buf1) goto error; |
| 10237 | release1 = 1; |
| 10238 | } |
| 10239 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10240 | PyUnicode_GET_LENGTH(str1))); */ |
| 10241 | product = n * (len2-len1); |
| 10242 | if ((product / (len2-len1)) != n) { |
| 10243 | PyErr_SetString(PyExc_OverflowError, |
| 10244 | "replace string is too long"); |
| 10245 | goto error; |
| 10246 | } |
| 10247 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10248 | if (new_size == 0) { |
| 10249 | Py_INCREF(unicode_empty); |
| 10250 | u = unicode_empty; |
| 10251 | goto done; |
| 10252 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10253 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10254 | PyErr_SetString(PyExc_OverflowError, |
| 10255 | "replace string is too long"); |
| 10256 | goto error; |
| 10257 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10258 | u = PyUnicode_New(new_size, maxchar); |
| 10259 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10260 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10261 | assert(PyUnicode_KIND(u) == rkind); |
| 10262 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | ires = i = 0; |
| 10264 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10265 | while (n-- > 0) { |
| 10266 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10267 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10268 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10269 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10270 | if (j == -1) |
| 10271 | break; |
| 10272 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10273 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10274 | memcpy(res + rkind * ires, |
| 10275 | sbuf + rkind * i, |
| 10276 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10278 | } |
| 10279 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10281 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10283 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10285 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10287 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10289 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10290 | memcpy(res + rkind * ires, |
| 10291 | sbuf + rkind * i, |
| 10292 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10293 | } |
| 10294 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10295 | /* interleave */ |
| 10296 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10297 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10299 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10300 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10301 | if (--n <= 0) |
| 10302 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10303 | memcpy(res + rkind * ires, |
| 10304 | sbuf + rkind * i, |
| 10305 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10306 | ires++; |
| 10307 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10308 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10309 | memcpy(res + rkind * ires, |
| 10310 | sbuf + rkind * i, |
| 10311 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10312 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10313 | } |
| 10314 | |
| 10315 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10316 | unicode_adjust_maxchar(&u); |
| 10317 | if (u == NULL) |
| 10318 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10319 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10320 | |
| 10321 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10322 | if (srelease) |
| 10323 | PyMem_FREE(sbuf); |
| 10324 | if (release1) |
| 10325 | PyMem_FREE(buf1); |
| 10326 | if (release2) |
| 10327 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10328 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10329 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10330 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10331 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10332 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10333 | if (srelease) |
| 10334 | PyMem_FREE(sbuf); |
| 10335 | if (release1) |
| 10336 | PyMem_FREE(buf1); |
| 10337 | if (release2) |
| 10338 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10339 | if (PyUnicode_CheckExact(self)) { |
| 10340 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10341 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10342 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10343 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10344 | error: |
| 10345 | if (srelease && sbuf) |
| 10346 | PyMem_FREE(sbuf); |
| 10347 | if (release1 && buf1) |
| 10348 | PyMem_FREE(buf1); |
| 10349 | if (release2 && buf2) |
| 10350 | PyMem_FREE(buf2); |
| 10351 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10352 | } |
| 10353 | |
| 10354 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10356 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10357 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10358 | \n\ |
| 10359 | 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] | 10360 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10361 | |
| 10362 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10363 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10364 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10365 | return fixup(self, fixtitle); |
| 10366 | } |
| 10367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10368 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10369 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | \n\ |
| 10371 | 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] | 10372 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10373 | |
| 10374 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10375 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10376 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | return fixup(self, fixcapitalize); |
| 10378 | } |
| 10379 | |
| 10380 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10381 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10382 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10383 | \n\ |
| 10384 | 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] | 10385 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10386 | |
| 10387 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10388 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | { |
| 10390 | PyObject *list; |
| 10391 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10392 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | /* Split into words */ |
| 10395 | list = split(self, NULL, -1); |
| 10396 | if (!list) |
| 10397 | return NULL; |
| 10398 | |
| 10399 | /* Capitalize each word */ |
| 10400 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10401 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10402 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10403 | if (item == NULL) |
| 10404 | goto onError; |
| 10405 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10406 | PyList_SET_ITEM(list, i, item); |
| 10407 | } |
| 10408 | |
| 10409 | /* Join the words to form a new string */ |
| 10410 | item = PyUnicode_Join(NULL, list); |
| 10411 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10412 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10413 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10414 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10415 | } |
| 10416 | #endif |
| 10417 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10418 | /* Argument converter. Coerces to a single unicode character */ |
| 10419 | |
| 10420 | static int |
| 10421 | convert_uc(PyObject *obj, void *addr) |
| 10422 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10423 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10424 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10425 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10426 | uniobj = PyUnicode_FromObject(obj); |
| 10427 | if (uniobj == NULL) { |
| 10428 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10429 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10430 | return 0; |
| 10431 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10433 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10434 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10435 | Py_DECREF(uniobj); |
| 10436 | return 0; |
| 10437 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10438 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10439 | Py_DECREF(uniobj); |
| 10440 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10441 | } |
| 10442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10443 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10444 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10445 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10446 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10447 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10448 | |
| 10449 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10450 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10451 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10452 | Py_ssize_t marg, left; |
| 10453 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10454 | Py_UCS4 fillchar = ' '; |
| 10455 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10456 | 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] | 10457 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10458 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10459 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10460 | return NULL; |
| 10461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10462 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10463 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10464 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10465 | } |
| 10466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10467 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10468 | left = marg / 2 + (marg & width & 1); |
| 10469 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10470 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10474 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10475 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10476 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10477 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | int kind1, kind2; |
| 10479 | void *data1, *data2; |
| 10480 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10481 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10482 | kind1 = PyUnicode_KIND(str1); |
| 10483 | kind2 = PyUnicode_KIND(str2); |
| 10484 | data1 = PyUnicode_DATA(str1); |
| 10485 | data2 = PyUnicode_DATA(str2); |
| 10486 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10487 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10490 | Py_UCS4 c1, c2; |
| 10491 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10492 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10493 | |
| 10494 | if (c1 != c2) |
| 10495 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10496 | } |
| 10497 | |
| 10498 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10499 | } |
| 10500 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10501 | int |
| 10502 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10503 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10504 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10505 | if (PyUnicode_READY(left) == -1 || |
| 10506 | PyUnicode_READY(right) == -1) |
| 10507 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10508 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10510 | PyErr_Format(PyExc_TypeError, |
| 10511 | "Can't compare %.100s and %.100s", |
| 10512 | left->ob_type->tp_name, |
| 10513 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10514 | return -1; |
| 10515 | } |
| 10516 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10517 | int |
| 10518 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10519 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | Py_ssize_t i; |
| 10521 | int kind; |
| 10522 | void *data; |
| 10523 | Py_UCS4 chr; |
| 10524 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10525 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10526 | if (PyUnicode_READY(uni) == -1) |
| 10527 | return -1; |
| 10528 | kind = PyUnicode_KIND(uni); |
| 10529 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10530 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10531 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10532 | if (chr != str[i]) |
| 10533 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10534 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10535 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10536 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10537 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10538 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10539 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10540 | return 0; |
| 10541 | } |
| 10542 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10544 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10545 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10547 | PyObject * |
| 10548 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10549 | { |
| 10550 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10551 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10552 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10553 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10554 | if (PyUnicode_READY(left) == -1 || |
| 10555 | PyUnicode_READY(right) == -1) |
| 10556 | return NULL; |
| 10557 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10558 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10559 | if (op == Py_EQ) { |
| 10560 | Py_INCREF(Py_False); |
| 10561 | return Py_False; |
| 10562 | } |
| 10563 | if (op == Py_NE) { |
| 10564 | Py_INCREF(Py_True); |
| 10565 | return Py_True; |
| 10566 | } |
| 10567 | } |
| 10568 | if (left == right) |
| 10569 | result = 0; |
| 10570 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10571 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10572 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10573 | /* Convert the return value to a Boolean */ |
| 10574 | switch (op) { |
| 10575 | case Py_EQ: |
| 10576 | v = TEST_COND(result == 0); |
| 10577 | break; |
| 10578 | case Py_NE: |
| 10579 | v = TEST_COND(result != 0); |
| 10580 | break; |
| 10581 | case Py_LE: |
| 10582 | v = TEST_COND(result <= 0); |
| 10583 | break; |
| 10584 | case Py_GE: |
| 10585 | v = TEST_COND(result >= 0); |
| 10586 | break; |
| 10587 | case Py_LT: |
| 10588 | v = TEST_COND(result == -1); |
| 10589 | break; |
| 10590 | case Py_GT: |
| 10591 | v = TEST_COND(result == 1); |
| 10592 | break; |
| 10593 | default: |
| 10594 | PyErr_BadArgument(); |
| 10595 | return NULL; |
| 10596 | } |
| 10597 | Py_INCREF(v); |
| 10598 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10599 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10600 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10601 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10602 | } |
| 10603 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10604 | int |
| 10605 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10606 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10607 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | int kind1, kind2, kind; |
| 10609 | void *buf1, *buf2; |
| 10610 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10611 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10612 | |
| 10613 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10614 | sub = PyUnicode_FromObject(element); |
| 10615 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10616 | PyErr_Format(PyExc_TypeError, |
| 10617 | "'in <string>' requires string as left operand, not %s", |
| 10618 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10619 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10620 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10621 | if (PyUnicode_READY(sub) == -1) |
| 10622 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10623 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10624 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10625 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10626 | Py_DECREF(sub); |
| 10627 | return -1; |
| 10628 | } |
| 10629 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10630 | kind1 = PyUnicode_KIND(str); |
| 10631 | kind2 = PyUnicode_KIND(sub); |
| 10632 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10633 | buf1 = PyUnicode_DATA(str); |
| 10634 | buf2 = PyUnicode_DATA(sub); |
| 10635 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10636 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10637 | if (!buf1) { |
| 10638 | Py_DECREF(sub); |
| 10639 | return -1; |
| 10640 | } |
| 10641 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10642 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | if (!buf2) { |
| 10644 | Py_DECREF(sub); |
| 10645 | if (kind1 != kind) PyMem_Free(buf1); |
| 10646 | return -1; |
| 10647 | } |
| 10648 | len1 = PyUnicode_GET_LENGTH(str); |
| 10649 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10650 | |
| 10651 | switch(kind) { |
| 10652 | case PyUnicode_1BYTE_KIND: |
| 10653 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10654 | break; |
| 10655 | case PyUnicode_2BYTE_KIND: |
| 10656 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10657 | break; |
| 10658 | case PyUnicode_4BYTE_KIND: |
| 10659 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10660 | break; |
| 10661 | default: |
| 10662 | result = -1; |
| 10663 | assert(0); |
| 10664 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10665 | |
| 10666 | Py_DECREF(str); |
| 10667 | Py_DECREF(sub); |
| 10668 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10669 | if (kind1 != kind) |
| 10670 | PyMem_Free(buf1); |
| 10671 | if (kind2 != kind) |
| 10672 | PyMem_Free(buf2); |
| 10673 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10674 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10675 | } |
| 10676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10677 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10679 | PyObject * |
| 10680 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10681 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10682 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10683 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10684 | |
| 10685 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10686 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10687 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10688 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10689 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10690 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10691 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10692 | |
| 10693 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10694 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10695 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10697 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10698 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10699 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10701 | } |
| 10702 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10703 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10704 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10705 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10707 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10708 | w = PyUnicode_New( |
| 10709 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10710 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10711 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10712 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10713 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10714 | 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] | 10715 | Py_DECREF(u); |
| 10716 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10717 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10718 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10720 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10721 | Py_XDECREF(u); |
| 10722 | Py_XDECREF(v); |
| 10723 | return NULL; |
| 10724 | } |
| 10725 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10726 | static void |
| 10727 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10728 | { |
| 10729 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10730 | |
| 10731 | assert(PyUnicode_IS_READY(*p_left)); |
| 10732 | assert(PyUnicode_IS_READY(right)); |
| 10733 | |
| 10734 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10735 | right_len = PyUnicode_GET_LENGTH(right); |
| 10736 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10737 | PyErr_SetString(PyExc_OverflowError, |
| 10738 | "strings are too large to concat"); |
| 10739 | goto error; |
| 10740 | } |
| 10741 | new_len = left_len + right_len; |
| 10742 | |
| 10743 | /* Now we own the last reference to 'left', so we can resize it |
| 10744 | * in-place. |
| 10745 | */ |
| 10746 | if (unicode_resize(p_left, new_len) != 0) { |
| 10747 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10748 | * deallocated so it cannot be put back into |
| 10749 | * 'variable'. The MemoryError is raised when there |
| 10750 | * is no value in 'variable', which might (very |
| 10751 | * remotely) be a cause of incompatibilities. |
| 10752 | */ |
| 10753 | goto error; |
| 10754 | } |
| 10755 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10756 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10757 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10758 | return; |
| 10759 | |
| 10760 | error: |
| 10761 | Py_DECREF(*p_left); |
| 10762 | *p_left = NULL; |
| 10763 | } |
| 10764 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10765 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10766 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10767 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10768 | PyObject *left, *res; |
| 10769 | |
| 10770 | if (p_left == NULL) { |
| 10771 | if (!PyErr_Occurred()) |
| 10772 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10773 | return; |
| 10774 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10775 | left = *p_left; |
| 10776 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10777 | if (!PyErr_Occurred()) |
| 10778 | PyErr_BadInternalCall(); |
| 10779 | goto error; |
| 10780 | } |
| 10781 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10782 | if (PyUnicode_READY(left)) |
| 10783 | goto error; |
| 10784 | if (PyUnicode_READY(right)) |
| 10785 | goto error; |
| 10786 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10787 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10788 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10789 | && unicode_resizable(left) |
| 10790 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10791 | || _PyUnicode_WSTR(left) != NULL)) |
| 10792 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10793 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10794 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10795 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10796 | not so different than duplicating the string. */ |
| 10797 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10798 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10799 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10800 | if (p_left != NULL) |
| 10801 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10802 | return; |
| 10803 | } |
| 10804 | } |
| 10805 | |
| 10806 | res = PyUnicode_Concat(left, right); |
| 10807 | if (res == NULL) |
| 10808 | goto error; |
| 10809 | Py_DECREF(left); |
| 10810 | *p_left = res; |
| 10811 | return; |
| 10812 | |
| 10813 | error: |
| 10814 | Py_DECREF(*p_left); |
| 10815 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10816 | } |
| 10817 | |
| 10818 | void |
| 10819 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10820 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10821 | PyUnicode_Append(pleft, right); |
| 10822 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10823 | } |
| 10824 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10825 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10826 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10827 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10828 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10829 | 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] | 10830 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10831 | |
| 10832 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10833 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10834 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10835 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10836 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10837 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10838 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10839 | int kind1, kind2, kind; |
| 10840 | void *buf1, *buf2; |
| 10841 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10842 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10843 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10844 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10845 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10846 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10847 | kind1 = PyUnicode_KIND(self); |
| 10848 | kind2 = PyUnicode_KIND(substring); |
| 10849 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10850 | buf1 = PyUnicode_DATA(self); |
| 10851 | buf2 = PyUnicode_DATA(substring); |
| 10852 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10853 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | if (!buf1) { |
| 10855 | Py_DECREF(substring); |
| 10856 | return NULL; |
| 10857 | } |
| 10858 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10859 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10860 | if (!buf2) { |
| 10861 | Py_DECREF(substring); |
| 10862 | if (kind1 != kind) PyMem_Free(buf1); |
| 10863 | return NULL; |
| 10864 | } |
| 10865 | len1 = PyUnicode_GET_LENGTH(self); |
| 10866 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10867 | |
| 10868 | ADJUST_INDICES(start, end, len1); |
| 10869 | switch(kind) { |
| 10870 | case PyUnicode_1BYTE_KIND: |
| 10871 | iresult = ucs1lib_count( |
| 10872 | ((Py_UCS1*)buf1) + start, end - start, |
| 10873 | buf2, len2, PY_SSIZE_T_MAX |
| 10874 | ); |
| 10875 | break; |
| 10876 | case PyUnicode_2BYTE_KIND: |
| 10877 | iresult = ucs2lib_count( |
| 10878 | ((Py_UCS2*)buf1) + start, end - start, |
| 10879 | buf2, len2, PY_SSIZE_T_MAX |
| 10880 | ); |
| 10881 | break; |
| 10882 | case PyUnicode_4BYTE_KIND: |
| 10883 | iresult = ucs4lib_count( |
| 10884 | ((Py_UCS4*)buf1) + start, end - start, |
| 10885 | buf2, len2, PY_SSIZE_T_MAX |
| 10886 | ); |
| 10887 | break; |
| 10888 | default: |
| 10889 | assert(0); iresult = 0; |
| 10890 | } |
| 10891 | |
| 10892 | result = PyLong_FromSsize_t(iresult); |
| 10893 | |
| 10894 | if (kind1 != kind) |
| 10895 | PyMem_Free(buf1); |
| 10896 | if (kind2 != kind) |
| 10897 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10898 | |
| 10899 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10901 | return result; |
| 10902 | } |
| 10903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10904 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10905 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10906 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10907 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10908 | 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] | 10909 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10910 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10911 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10912 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10913 | |
| 10914 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10915 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10916 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10917 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10918 | char *encoding = NULL; |
| 10919 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10920 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10921 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10922 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10923 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10924 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10925 | } |
| 10926 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10927 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10928 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10929 | \n\ |
| 10930 | 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] | 10931 | 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] | 10932 | |
| 10933 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10934 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10936 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10937 | Py_UCS4 ch; |
| 10938 | PyObject *u; |
| 10939 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10941 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10942 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10943 | |
| 10944 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10945 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10946 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10947 | if (PyUnicode_READY(self) == -1) |
| 10948 | return NULL; |
| 10949 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10950 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10951 | src_len = PyUnicode_GET_LENGTH(self); |
| 10952 | i = j = line_pos = 0; |
| 10953 | kind = PyUnicode_KIND(self); |
| 10954 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10955 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10956 | for (; i < src_len; i++) { |
| 10957 | ch = PyUnicode_READ(kind, src_data, i); |
| 10958 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10959 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10960 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10961 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10962 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10963 | goto overflow; |
| 10964 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10965 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10966 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10969 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10970 | goto overflow; |
| 10971 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10973 | if (ch == '\n' || ch == '\r') |
| 10974 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10976 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10977 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10978 | Py_INCREF(self); |
| 10979 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10980 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10981 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10983 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10984 | if (!u) |
| 10985 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10986 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10987 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10988 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10990 | for (; i < src_len; i++) { |
| 10991 | ch = PyUnicode_READ(kind, src_data, i); |
| 10992 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10993 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10994 | incr = tabsize - (line_pos % tabsize); |
| 10995 | line_pos += incr; |
| 10996 | while (incr--) { |
| 10997 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10998 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10999 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11000 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11001 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11002 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11003 | line_pos++; |
| 11004 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11005 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11006 | if (ch == '\n' || ch == '\r') |
| 11007 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11009 | } |
| 11010 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11011 | #ifndef DONT_MAKE_RESULT_READY |
| 11012 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11013 | Py_DECREF(u); |
| 11014 | return NULL; |
| 11015 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11016 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11017 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11018 | return u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11019 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11020 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11021 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11022 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | } |
| 11024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11025 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11026 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | \n\ |
| 11028 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11029 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | arguments start and end are interpreted as in slice notation.\n\ |
| 11031 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11032 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | |
| 11034 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11035 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11036 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11037 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11038 | Py_ssize_t start; |
| 11039 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11040 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11041 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11042 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11043 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | return NULL; |
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 | if (PyUnicode_READY(self) == -1) |
| 11047 | return NULL; |
| 11048 | if (PyUnicode_READY(substring) == -1) |
| 11049 | return NULL; |
| 11050 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11051 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11052 | |
| 11053 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11055 | if (result == -2) |
| 11056 | return NULL; |
| 11057 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11058 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
| 11061 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11062 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11064 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11065 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11066 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11067 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | } |
| 11069 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11070 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11071 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11072 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11073 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11075 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11076 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11078 | if (_PyUnicode_HASH(self) != -1) |
| 11079 | return _PyUnicode_HASH(self); |
| 11080 | if (PyUnicode_READY(self) == -1) |
| 11081 | return -1; |
| 11082 | len = PyUnicode_GET_LENGTH(self); |
| 11083 | |
| 11084 | /* The hash function as a macro, gets expanded three times below. */ |
| 11085 | #define HASH(P) \ |
| 11086 | x = (Py_uhash_t)*P << 7; \ |
| 11087 | while (--len >= 0) \ |
| 11088 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11089 | |
| 11090 | switch (PyUnicode_KIND(self)) { |
| 11091 | case PyUnicode_1BYTE_KIND: { |
| 11092 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11093 | HASH(c); |
| 11094 | break; |
| 11095 | } |
| 11096 | case PyUnicode_2BYTE_KIND: { |
| 11097 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11098 | HASH(s); |
| 11099 | break; |
| 11100 | } |
| 11101 | default: { |
| 11102 | Py_UCS4 *l; |
| 11103 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11104 | "Impossible switch case in unicode_hash"); |
| 11105 | l = PyUnicode_4BYTE_DATA(self); |
| 11106 | HASH(l); |
| 11107 | break; |
| 11108 | } |
| 11109 | } |
| 11110 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11111 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11112 | if (x == -1) |
| 11113 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11114 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11115 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11117 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11119 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11120 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11121 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11122 | 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] | 11123 | |
| 11124 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11125 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11126 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11127 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11128 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11129 | Py_ssize_t start; |
| 11130 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11131 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11132 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11133 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11134 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | if (PyUnicode_READY(self) == -1) |
| 11137 | return NULL; |
| 11138 | if (PyUnicode_READY(substring) == -1) |
| 11139 | return NULL; |
| 11140 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11141 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | |
| 11143 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11144 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11145 | if (result == -2) |
| 11146 | return NULL; |
| 11147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | if (result < 0) { |
| 11149 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11150 | return NULL; |
| 11151 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11152 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11153 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | } |
| 11155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11156 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11157 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11159 | 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] | 11160 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | |
| 11162 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11163 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11164 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 | Py_ssize_t i, length; |
| 11166 | int kind; |
| 11167 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | int cased; |
| 11169 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | if (PyUnicode_READY(self) == -1) |
| 11171 | return NULL; |
| 11172 | length = PyUnicode_GET_LENGTH(self); |
| 11173 | kind = PyUnicode_KIND(self); |
| 11174 | data = PyUnicode_DATA(self); |
| 11175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (length == 1) |
| 11178 | return PyBool_FromLong( |
| 11179 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11181 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11182 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11183 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11186 | for (i = 0; i < length; i++) { |
| 11187 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11189 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11190 | return PyBool_FromLong(0); |
| 11191 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11192 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11194 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | } |
| 11196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11197 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11198 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11200 | 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] | 11201 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | |
| 11203 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11204 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11206 | Py_ssize_t i, length; |
| 11207 | int kind; |
| 11208 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | int cased; |
| 11210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11211 | if (PyUnicode_READY(self) == -1) |
| 11212 | return NULL; |
| 11213 | length = PyUnicode_GET_LENGTH(self); |
| 11214 | kind = PyUnicode_KIND(self); |
| 11215 | data = PyUnicode_DATA(self); |
| 11216 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11217 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11218 | if (length == 1) |
| 11219 | return PyBool_FromLong( |
| 11220 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11222 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11223 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11224 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11226 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | for (i = 0; i < length; i++) { |
| 11228 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11230 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11231 | return PyBool_FromLong(0); |
| 11232 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11233 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11235 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | } |
| 11237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11238 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11239 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11241 | Return True if S is a titlecased string and there is at least one\n\ |
| 11242 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11243 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11244 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | |
| 11246 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11247 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11248 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11249 | Py_ssize_t i, length; |
| 11250 | int kind; |
| 11251 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | int cased, previous_is_cased; |
| 11253 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11254 | if (PyUnicode_READY(self) == -1) |
| 11255 | return NULL; |
| 11256 | length = PyUnicode_GET_LENGTH(self); |
| 11257 | kind = PyUnicode_KIND(self); |
| 11258 | data = PyUnicode_DATA(self); |
| 11259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11261 | if (length == 1) { |
| 11262 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11263 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11264 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11265 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11267 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11269 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11271 | cased = 0; |
| 11272 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11273 | for (i = 0; i < length; i++) { |
| 11274 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11276 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11277 | if (previous_is_cased) |
| 11278 | return PyBool_FromLong(0); |
| 11279 | previous_is_cased = 1; |
| 11280 | cased = 1; |
| 11281 | } |
| 11282 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11283 | if (!previous_is_cased) |
| 11284 | return PyBool_FromLong(0); |
| 11285 | previous_is_cased = 1; |
| 11286 | cased = 1; |
| 11287 | } |
| 11288 | else |
| 11289 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11290 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11291 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 | } |
| 11293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11294 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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 whitespace\n\ |
| 11298 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11299 | |
| 11300 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11301 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11303 | Py_ssize_t i, length; |
| 11304 | int kind; |
| 11305 | void *data; |
| 11306 | |
| 11307 | if (PyUnicode_READY(self) == -1) |
| 11308 | return NULL; |
| 11309 | length = PyUnicode_GET_LENGTH(self); |
| 11310 | kind = PyUnicode_KIND(self); |
| 11311 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | if (length == 1) |
| 11315 | return PyBool_FromLong( |
| 11316 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11318 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11319 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11320 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11321 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | for (i = 0; i < length; i++) { |
| 11323 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11324 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11325 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11327 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11328 | } |
| 11329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11330 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11331 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11332 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11333 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11334 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11335 | |
| 11336 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11337 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11338 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11339 | Py_ssize_t i, length; |
| 11340 | int kind; |
| 11341 | void *data; |
| 11342 | |
| 11343 | if (PyUnicode_READY(self) == -1) |
| 11344 | return NULL; |
| 11345 | length = PyUnicode_GET_LENGTH(self); |
| 11346 | kind = PyUnicode_KIND(self); |
| 11347 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11348 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11349 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11350 | if (length == 1) |
| 11351 | return PyBool_FromLong( |
| 11352 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11353 | |
| 11354 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11355 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11356 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11357 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11358 | for (i = 0; i < length; i++) { |
| 11359 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11360 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11361 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11362 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11363 | } |
| 11364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11365 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11366 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11367 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11368 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11369 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11370 | |
| 11371 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11372 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11373 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | int kind; |
| 11375 | void *data; |
| 11376 | Py_ssize_t len, i; |
| 11377 | |
| 11378 | if (PyUnicode_READY(self) == -1) |
| 11379 | return NULL; |
| 11380 | |
| 11381 | kind = PyUnicode_KIND(self); |
| 11382 | data = PyUnicode_DATA(self); |
| 11383 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11384 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11385 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11386 | if (len == 1) { |
| 11387 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11388 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11389 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11390 | |
| 11391 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11392 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11393 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11394 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11395 | for (i = 0; i < len; i++) { |
| 11396 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11397 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11398 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11399 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11400 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11401 | } |
| 11402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11403 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11404 | "S.isdecimal() -> 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 decimal 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_isdecimal(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_ISDECIMAL(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_ISDECIMAL(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 | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11438 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11441 | Return True if all characters in S are digits\n\ |
| 11442 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | |
| 11444 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11445 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11446 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11447 | Py_ssize_t i, length; |
| 11448 | int kind; |
| 11449 | void *data; |
| 11450 | |
| 11451 | if (PyUnicode_READY(self) == -1) |
| 11452 | return NULL; |
| 11453 | length = PyUnicode_GET_LENGTH(self); |
| 11454 | kind = PyUnicode_KIND(self); |
| 11455 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | if (length == 1) { |
| 11459 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11460 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11461 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11463 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11465 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11467 | for (i = 0; i < length; i++) { |
| 11468 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11469 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11470 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11471 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11472 | } |
| 11473 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11474 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11475 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11477 | 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] | 11478 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11479 | |
| 11480 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11481 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | Py_ssize_t i, length; |
| 11484 | int kind; |
| 11485 | void *data; |
| 11486 | |
| 11487 | if (PyUnicode_READY(self) == -1) |
| 11488 | return NULL; |
| 11489 | length = PyUnicode_GET_LENGTH(self); |
| 11490 | kind = PyUnicode_KIND(self); |
| 11491 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11494 | if (length == 1) |
| 11495 | return PyBool_FromLong( |
| 11496 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11498 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11500 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11501 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11502 | for (i = 0; i < length; i++) { |
| 11503 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11504 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11506 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | } |
| 11508 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11509 | int |
| 11510 | PyUnicode_IsIdentifier(PyObject *self) |
| 11511 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11512 | int kind; |
| 11513 | void *data; |
| 11514 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11515 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11517 | if (PyUnicode_READY(self) == -1) { |
| 11518 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11519 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11520 | } |
| 11521 | |
| 11522 | /* Special case for empty strings */ |
| 11523 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11524 | return 0; |
| 11525 | kind = PyUnicode_KIND(self); |
| 11526 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11527 | |
| 11528 | /* PEP 3131 says that the first character must be in |
| 11529 | XID_Start and subsequent characters in XID_Continue, |
| 11530 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11531 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11532 | letters, digits, underscore). However, given the current |
| 11533 | definition of XID_Start and XID_Continue, it is sufficient |
| 11534 | to check just for these, except that _ must be allowed |
| 11535 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11537 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11538 | return 0; |
| 11539 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11540 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11542 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11543 | return 1; |
| 11544 | } |
| 11545 | |
| 11546 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11547 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11548 | \n\ |
| 11549 | Return True if S is a valid identifier according\n\ |
| 11550 | to the language definition."); |
| 11551 | |
| 11552 | static PyObject* |
| 11553 | unicode_isidentifier(PyObject *self) |
| 11554 | { |
| 11555 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11556 | } |
| 11557 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11558 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11559 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11560 | \n\ |
| 11561 | Return True if all characters in S are considered\n\ |
| 11562 | printable in repr() or S is empty, False otherwise."); |
| 11563 | |
| 11564 | static PyObject* |
| 11565 | unicode_isprintable(PyObject *self) |
| 11566 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11567 | Py_ssize_t i, length; |
| 11568 | int kind; |
| 11569 | void *data; |
| 11570 | |
| 11571 | if (PyUnicode_READY(self) == -1) |
| 11572 | return NULL; |
| 11573 | length = PyUnicode_GET_LENGTH(self); |
| 11574 | kind = PyUnicode_KIND(self); |
| 11575 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11576 | |
| 11577 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 | if (length == 1) |
| 11579 | return PyBool_FromLong( |
| 11580 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11581 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11582 | for (i = 0; i < length; i++) { |
| 11583 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11584 | Py_RETURN_FALSE; |
| 11585 | } |
| 11586 | } |
| 11587 | Py_RETURN_TRUE; |
| 11588 | } |
| 11589 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11590 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11591 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11592 | \n\ |
| 11593 | 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] | 11594 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | |
| 11596 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11597 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11598 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11599 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11600 | } |
| 11601 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11602 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11603 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11604 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | if (PyUnicode_READY(self) == -1) |
| 11606 | return -1; |
| 11607 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | } |
| 11609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11610 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11611 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11612 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11613 | 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] | 11614 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | |
| 11616 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11617 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11618 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11619 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | Py_UCS4 fillchar = ' '; |
| 11621 | |
| 11622 | if (PyUnicode_READY(self) == -1) |
| 11623 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11624 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11625 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11626 | return NULL; |
| 11627 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11628 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11630 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | } |
| 11632 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11633 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | } |
| 11635 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11636 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11637 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11638 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11639 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11640 | |
| 11641 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11642 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11643 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11644 | return fixup(self, fixlower); |
| 11645 | } |
| 11646 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11647 | #define LEFTSTRIP 0 |
| 11648 | #define RIGHTSTRIP 1 |
| 11649 | #define BOTHSTRIP 2 |
| 11650 | |
| 11651 | /* Arrays indexed by above */ |
| 11652 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11653 | |
| 11654 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11655 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11656 | /* externally visible for str.strip(unicode) */ |
| 11657 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11658 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11659 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11660 | void *data; |
| 11661 | int kind; |
| 11662 | Py_ssize_t i, j, len; |
| 11663 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11664 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11665 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11666 | return NULL; |
| 11667 | |
| 11668 | kind = PyUnicode_KIND(self); |
| 11669 | data = PyUnicode_DATA(self); |
| 11670 | len = PyUnicode_GET_LENGTH(self); |
| 11671 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11672 | PyUnicode_DATA(sepobj), |
| 11673 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11674 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11675 | i = 0; |
| 11676 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11677 | while (i < len && |
| 11678 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11679 | i++; |
| 11680 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11681 | } |
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 | j = len; |
| 11684 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11685 | do { |
| 11686 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11687 | } while (j >= i && |
| 11688 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11689 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11690 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11691 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11692 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11693 | } |
| 11694 | |
| 11695 | PyObject* |
| 11696 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11697 | { |
| 11698 | unsigned char *data; |
| 11699 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11700 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11701 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11702 | if (PyUnicode_READY(self) == -1) |
| 11703 | return NULL; |
| 11704 | |
| 11705 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11706 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11707 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11708 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11709 | if (PyUnicode_CheckExact(self)) { |
| 11710 | Py_INCREF(self); |
| 11711 | return self; |
| 11712 | } |
| 11713 | else |
| 11714 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11715 | } |
| 11716 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11717 | length = end - start; |
| 11718 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11719 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11720 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11721 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11722 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11723 | return NULL; |
| 11724 | } |
| 11725 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11726 | if (PyUnicode_IS_ASCII(self)) { |
| 11727 | kind = PyUnicode_KIND(self); |
| 11728 | data = PyUnicode_1BYTE_DATA(self); |
| 11729 | return unicode_fromascii(data + start, length); |
| 11730 | } |
| 11731 | else { |
| 11732 | kind = PyUnicode_KIND(self); |
| 11733 | data = PyUnicode_1BYTE_DATA(self); |
| 11734 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11735 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11736 | length); |
| 11737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11738 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | |
| 11740 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11741 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11742 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11743 | int kind; |
| 11744 | void *data; |
| 11745 | Py_ssize_t len, i, j; |
| 11746 | |
| 11747 | if (PyUnicode_READY(self) == -1) |
| 11748 | return NULL; |
| 11749 | |
| 11750 | kind = PyUnicode_KIND(self); |
| 11751 | data = PyUnicode_DATA(self); |
| 11752 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11753 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11754 | i = 0; |
| 11755 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11756 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11757 | i++; |
| 11758 | } |
| 11759 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11760 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11761 | j = len; |
| 11762 | if (striptype != LEFTSTRIP) { |
| 11763 | do { |
| 11764 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11765 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11766 | j++; |
| 11767 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11768 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11769 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11770 | } |
| 11771 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11772 | |
| 11773 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11774 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11775 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11776 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11777 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11778 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11779 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11780 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11781 | if (sep != NULL && sep != Py_None) { |
| 11782 | if (PyUnicode_Check(sep)) |
| 11783 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11784 | else { |
| 11785 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11786 | "%s arg must be None or str", |
| 11787 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11788 | return NULL; |
| 11789 | } |
| 11790 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11791 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11792 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11793 | } |
| 11794 | |
| 11795 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11796 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11797 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11798 | \n\ |
| 11799 | Return a copy of the string S with leading and trailing\n\ |
| 11800 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11801 | 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] | 11802 | |
| 11803 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11804 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11805 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11806 | if (PyTuple_GET_SIZE(args) == 0) |
| 11807 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11808 | else |
| 11809 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11810 | } |
| 11811 | |
| 11812 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11813 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11814 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11815 | \n\ |
| 11816 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11817 | 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] | 11818 | |
| 11819 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11820 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11821 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11822 | if (PyTuple_GET_SIZE(args) == 0) |
| 11823 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11824 | else |
| 11825 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11826 | } |
| 11827 | |
| 11828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11829 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11830 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11831 | \n\ |
| 11832 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11833 | 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] | 11834 | |
| 11835 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11836 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11837 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11838 | if (PyTuple_GET_SIZE(args) == 0) |
| 11839 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11840 | else |
| 11841 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11842 | } |
| 11843 | |
| 11844 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11846 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11847 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11848 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11849 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11850 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11851 | if (len < 1) { |
| 11852 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11853 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11854 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11855 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11856 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11857 | /* no repeat, return original string */ |
| 11858 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11859 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11860 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11862 | if (PyUnicode_READY(str) == -1) |
| 11863 | return NULL; |
| 11864 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11865 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11866 | PyErr_SetString(PyExc_OverflowError, |
| 11867 | "repeated string is too long"); |
| 11868 | return NULL; |
| 11869 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11870 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11871 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11872 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11873 | if (!u) |
| 11874 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11875 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
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 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11878 | const int kind = PyUnicode_KIND(str); |
| 11879 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11880 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11881 | if (kind == PyUnicode_1BYTE_KIND) |
| 11882 | memset(to, (unsigned char)fill_char, len); |
| 11883 | else { |
| 11884 | for (n = 0; n < len; ++n) |
| 11885 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11886 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11887 | } |
| 11888 | else { |
| 11889 | /* number of characters copied this far */ |
| 11890 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11891 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11892 | char *to = (char *) PyUnicode_DATA(u); |
| 11893 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11894 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11895 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11896 | n = (done <= nchars-done) ? done : nchars-done; |
| 11897 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11898 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11899 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11900 | } |
| 11901 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11902 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11903 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | } |
| 11905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11906 | PyObject * |
| 11907 | PyUnicode_Replace(PyObject *obj, |
| 11908 | PyObject *subobj, |
| 11909 | PyObject *replobj, |
| 11910 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | { |
| 11912 | PyObject *self; |
| 11913 | PyObject *str1; |
| 11914 | PyObject *str2; |
| 11915 | PyObject *result; |
| 11916 | |
| 11917 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11918 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11919 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11920 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11921 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11922 | Py_DECREF(self); |
| 11923 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | } |
| 11925 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11926 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11927 | Py_DECREF(self); |
| 11928 | Py_DECREF(str1); |
| 11929 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11930 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11931 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11932 | Py_DECREF(self); |
| 11933 | Py_DECREF(str1); |
| 11934 | Py_DECREF(str2); |
| 11935 | return result; |
| 11936 | } |
| 11937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11938 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11939 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11940 | \n\ |
| 11941 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11942 | old replaced by new. If the optional argument count is\n\ |
| 11943 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11944 | |
| 11945 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11946 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11948 | PyObject *str1; |
| 11949 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11950 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | PyObject *result; |
| 11952 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11953 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11954 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11956 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | str1 = PyUnicode_FromObject(str1); |
| 11958 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11959 | return NULL; |
| 11960 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11961 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11962 | Py_DECREF(str1); |
| 11963 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11964 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11965 | |
| 11966 | result = replace(self, str1, str2, maxcount); |
| 11967 | |
| 11968 | Py_DECREF(str1); |
| 11969 | Py_DECREF(str2); |
| 11970 | return result; |
| 11971 | } |
| 11972 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11973 | static PyObject * |
| 11974 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11975 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11976 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11977 | Py_ssize_t isize; |
| 11978 | Py_ssize_t osize, squote, dquote, i, o; |
| 11979 | Py_UCS4 max, quote; |
| 11980 | int ikind, okind; |
| 11981 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11984 | return NULL; |
| 11985 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11986 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11987 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11988 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11989 | /* Compute length of output, quote characters, and |
| 11990 | maximum character */ |
| 11991 | osize = 2; /* quotes */ |
| 11992 | max = 127; |
| 11993 | squote = dquote = 0; |
| 11994 | ikind = PyUnicode_KIND(unicode); |
| 11995 | for (i = 0; i < isize; i++) { |
| 11996 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11997 | switch (ch) { |
| 11998 | case '\'': squote++; osize++; break; |
| 11999 | case '"': dquote++; osize++; break; |
| 12000 | case '\\': case '\t': case '\r': case '\n': |
| 12001 | osize += 2; break; |
| 12002 | default: |
| 12003 | /* Fast-path ASCII */ |
| 12004 | if (ch < ' ' || ch == 0x7f) |
| 12005 | osize += 4; /* \xHH */ |
| 12006 | else if (ch < 0x7f) |
| 12007 | osize++; |
| 12008 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12009 | osize++; |
| 12010 | max = ch > max ? ch : max; |
| 12011 | } |
| 12012 | else if (ch < 0x100) |
| 12013 | osize += 4; /* \xHH */ |
| 12014 | else if (ch < 0x10000) |
| 12015 | osize += 6; /* \uHHHH */ |
| 12016 | else |
| 12017 | osize += 10; /* \uHHHHHHHH */ |
| 12018 | } |
| 12019 | } |
| 12020 | |
| 12021 | quote = '\''; |
| 12022 | if (squote) { |
| 12023 | if (dquote) |
| 12024 | /* Both squote and dquote present. Use squote, |
| 12025 | and escape them */ |
| 12026 | osize += squote; |
| 12027 | else |
| 12028 | quote = '"'; |
| 12029 | } |
| 12030 | |
| 12031 | repr = PyUnicode_New(osize, max); |
| 12032 | if (repr == NULL) |
| 12033 | return NULL; |
| 12034 | okind = PyUnicode_KIND(repr); |
| 12035 | odata = PyUnicode_DATA(repr); |
| 12036 | |
| 12037 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12038 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12039 | |
| 12040 | for (i = 0, o = 1; i < isize; i++) { |
| 12041 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12042 | |
| 12043 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12044 | if ((ch == quote) || (ch == '\\')) { |
| 12045 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12046 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12047 | continue; |
| 12048 | } |
| 12049 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12051 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12052 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12053 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12054 | } |
| 12055 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12056 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12057 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12058 | } |
| 12059 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12060 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12061 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12062 | } |
| 12063 | |
| 12064 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12065 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12066 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12067 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12068 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12069 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12070 | } |
| 12071 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12072 | /* Copy ASCII characters as-is */ |
| 12073 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12074 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12075 | } |
| 12076 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12077 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12078 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12079 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12080 | (categories Z* and C* except ASCII space) |
| 12081 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12082 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12083 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12084 | if (ch <= 0xff) { |
| 12085 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12086 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12087 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12088 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12089 | } |
| 12090 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | else if (ch >= 0x10000) { |
| 12092 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12093 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12094 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12095 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12096 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12097 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12098 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12099 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12100 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12101 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12102 | } |
| 12103 | /* Map 16-bit characters to '\uxxxx' */ |
| 12104 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12105 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12106 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12107 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12108 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12109 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12110 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12111 | } |
| 12112 | } |
| 12113 | /* Copy characters as-is */ |
| 12114 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12115 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12116 | } |
| 12117 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12118 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12119 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12120 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12121 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12122 | } |
| 12123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12124 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12125 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12126 | \n\ |
| 12127 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12128 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12129 | arguments start and end are interpreted as in slice notation.\n\ |
| 12130 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12131 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12132 | |
| 12133 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12134 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12135 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12136 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12137 | Py_ssize_t start; |
| 12138 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12139 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12140 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12141 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12142 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12143 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12144 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12145 | if (PyUnicode_READY(self) == -1) |
| 12146 | return NULL; |
| 12147 | if (PyUnicode_READY(substring) == -1) |
| 12148 | return NULL; |
| 12149 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12150 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12151 | |
| 12152 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12153 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12154 | if (result == -2) |
| 12155 | return NULL; |
| 12156 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12157 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12158 | } |
| 12159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12160 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12161 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12162 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12163 | 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] | 12164 | |
| 12165 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12166 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12167 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12168 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12169 | Py_ssize_t start; |
| 12170 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12171 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12172 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12173 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12174 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12175 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12177 | if (PyUnicode_READY(self) == -1) |
| 12178 | return NULL; |
| 12179 | if (PyUnicode_READY(substring) == -1) |
| 12180 | return NULL; |
| 12181 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12182 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12183 | |
| 12184 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12185 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12186 | if (result == -2) |
| 12187 | return NULL; |
| 12188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12189 | if (result < 0) { |
| 12190 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12191 | return NULL; |
| 12192 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12193 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12194 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | } |
| 12196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12197 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12198 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12200 | 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] | 12201 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | |
| 12203 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12204 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12205 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12206 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12207 | Py_UCS4 fillchar = ' '; |
| 12208 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12209 | 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] | 12210 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12211 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12212 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12213 | return NULL; |
| 12214 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12215 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12217 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12218 | } |
| 12219 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12220 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12221 | } |
| 12222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12223 | PyObject * |
| 12224 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12225 | { |
| 12226 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12228 | s = PyUnicode_FromObject(s); |
| 12229 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12230 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12231 | if (sep != NULL) { |
| 12232 | sep = PyUnicode_FromObject(sep); |
| 12233 | if (sep == NULL) { |
| 12234 | Py_DECREF(s); |
| 12235 | return NULL; |
| 12236 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12237 | } |
| 12238 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12239 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12240 | |
| 12241 | Py_DECREF(s); |
| 12242 | Py_XDECREF(sep); |
| 12243 | return result; |
| 12244 | } |
| 12245 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12246 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12247 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | \n\ |
| 12249 | Return a list of the words in S, using sep as the\n\ |
| 12250 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12251 | 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] | 12252 | whitespace string is a separator and empty strings are\n\ |
| 12253 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12254 | |
| 12255 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12256 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | { |
| 12258 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12259 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12260 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12261 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12262 | return NULL; |
| 12263 | |
| 12264 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12265 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12266 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12267 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12269 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12270 | } |
| 12271 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12272 | PyObject * |
| 12273 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12274 | { |
| 12275 | PyObject* str_obj; |
| 12276 | PyObject* sep_obj; |
| 12277 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 | int kind1, kind2, kind; |
| 12279 | void *buf1 = NULL, *buf2 = NULL; |
| 12280 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12281 | |
| 12282 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12283 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12285 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12286 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12287 | Py_DECREF(str_obj); |
| 12288 | return NULL; |
| 12289 | } |
| 12290 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12291 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12292 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12293 | kind = Py_MAX(kind1, kind2); |
| 12294 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12295 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12296 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | if (!buf1) |
| 12298 | goto onError; |
| 12299 | buf2 = PyUnicode_DATA(sep_obj); |
| 12300 | if (kind2 != kind) |
| 12301 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12302 | if (!buf2) |
| 12303 | goto onError; |
| 12304 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12305 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12306 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12307 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12309 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12310 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12311 | else |
| 12312 | 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] | 12313 | break; |
| 12314 | case PyUnicode_2BYTE_KIND: |
| 12315 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12316 | break; |
| 12317 | case PyUnicode_4BYTE_KIND: |
| 12318 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12319 | break; |
| 12320 | default: |
| 12321 | assert(0); |
| 12322 | out = 0; |
| 12323 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12324 | |
| 12325 | Py_DECREF(sep_obj); |
| 12326 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12327 | if (kind1 != kind) |
| 12328 | PyMem_Free(buf1); |
| 12329 | if (kind2 != kind) |
| 12330 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12331 | |
| 12332 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12333 | onError: |
| 12334 | Py_DECREF(sep_obj); |
| 12335 | Py_DECREF(str_obj); |
| 12336 | if (kind1 != kind && buf1) |
| 12337 | PyMem_Free(buf1); |
| 12338 | if (kind2 != kind && buf2) |
| 12339 | PyMem_Free(buf2); |
| 12340 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12341 | } |
| 12342 | |
| 12343 | |
| 12344 | PyObject * |
| 12345 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12346 | { |
| 12347 | PyObject* str_obj; |
| 12348 | PyObject* sep_obj; |
| 12349 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12350 | int kind1, kind2, kind; |
| 12351 | void *buf1 = NULL, *buf2 = NULL; |
| 12352 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12353 | |
| 12354 | str_obj = PyUnicode_FromObject(str_in); |
| 12355 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12356 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12357 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12358 | if (!sep_obj) { |
| 12359 | Py_DECREF(str_obj); |
| 12360 | return NULL; |
| 12361 | } |
| 12362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12363 | kind1 = PyUnicode_KIND(str_in); |
| 12364 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12365 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12366 | buf1 = PyUnicode_DATA(str_in); |
| 12367 | if (kind1 != kind) |
| 12368 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12369 | if (!buf1) |
| 12370 | goto onError; |
| 12371 | buf2 = PyUnicode_DATA(sep_obj); |
| 12372 | if (kind2 != kind) |
| 12373 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12374 | if (!buf2) |
| 12375 | goto onError; |
| 12376 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12377 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12378 | |
| 12379 | switch(PyUnicode_KIND(str_in)) { |
| 12380 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12381 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12382 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12383 | else |
| 12384 | 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] | 12385 | break; |
| 12386 | case PyUnicode_2BYTE_KIND: |
| 12387 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12388 | break; |
| 12389 | case PyUnicode_4BYTE_KIND: |
| 12390 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12391 | break; |
| 12392 | default: |
| 12393 | assert(0); |
| 12394 | out = 0; |
| 12395 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12396 | |
| 12397 | Py_DECREF(sep_obj); |
| 12398 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12399 | if (kind1 != kind) |
| 12400 | PyMem_Free(buf1); |
| 12401 | if (kind2 != kind) |
| 12402 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12403 | |
| 12404 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | onError: |
| 12406 | Py_DECREF(sep_obj); |
| 12407 | Py_DECREF(str_obj); |
| 12408 | if (kind1 != kind && buf1) |
| 12409 | PyMem_Free(buf1); |
| 12410 | if (kind2 != kind && buf2) |
| 12411 | PyMem_Free(buf2); |
| 12412 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12413 | } |
| 12414 | |
| 12415 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12416 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12417 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12418 | 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] | 12419 | 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] | 12420 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12421 | |
| 12422 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12423 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12424 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12425 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12426 | } |
| 12427 | |
| 12428 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12429 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12430 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12431 | 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] | 12432 | 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] | 12433 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12434 | |
| 12435 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12436 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12437 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12438 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12439 | } |
| 12440 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12441 | PyObject * |
| 12442 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12443 | { |
| 12444 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12445 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12446 | s = PyUnicode_FromObject(s); |
| 12447 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12448 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12449 | if (sep != NULL) { |
| 12450 | sep = PyUnicode_FromObject(sep); |
| 12451 | if (sep == NULL) { |
| 12452 | Py_DECREF(s); |
| 12453 | return NULL; |
| 12454 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12455 | } |
| 12456 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12457 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12458 | |
| 12459 | Py_DECREF(s); |
| 12460 | Py_XDECREF(sep); |
| 12461 | return result; |
| 12462 | } |
| 12463 | |
| 12464 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12465 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12466 | \n\ |
| 12467 | Return a list of the words in S, using sep as the\n\ |
| 12468 | delimiter string, starting at the end of the string and\n\ |
| 12469 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12470 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12471 | is a separator."); |
| 12472 | |
| 12473 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12474 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12475 | { |
| 12476 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12477 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12478 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12479 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12480 | return NULL; |
| 12481 | |
| 12482 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12483 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12484 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12485 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12486 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12487 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12488 | } |
| 12489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12490 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12492 | \n\ |
| 12493 | 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] | 12494 | 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] | 12495 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12496 | |
| 12497 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12498 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12499 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12500 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12501 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12502 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12503 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12504 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12505 | return NULL; |
| 12506 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12507 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12508 | } |
| 12509 | |
| 12510 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12511 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12512 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12513 | if (PyUnicode_CheckExact(self)) { |
| 12514 | Py_INCREF(self); |
| 12515 | return self; |
| 12516 | } else |
| 12517 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12518 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | } |
| 12520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12521 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12522 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12523 | \n\ |
| 12524 | 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] | 12525 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12526 | |
| 12527 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12528 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12530 | return fixup(self, fixswapcase); |
| 12531 | } |
| 12532 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12533 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12534 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12535 | \n\ |
| 12536 | Return a translation table usable for str.translate().\n\ |
| 12537 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12538 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12539 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12540 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12541 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12542 | character at the same position in y. If there is a third argument, it\n\ |
| 12543 | must be a string, whose characters will be mapped to None in the result."); |
| 12544 | |
| 12545 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12546 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12547 | { |
| 12548 | PyObject *x, *y = NULL, *z = NULL; |
| 12549 | PyObject *new = NULL, *key, *value; |
| 12550 | Py_ssize_t i = 0; |
| 12551 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12552 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12553 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12554 | return NULL; |
| 12555 | new = PyDict_New(); |
| 12556 | if (!new) |
| 12557 | return NULL; |
| 12558 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12559 | int x_kind, y_kind, z_kind; |
| 12560 | void *x_data, *y_data, *z_data; |
| 12561 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12562 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12563 | if (!PyUnicode_Check(x)) { |
| 12564 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12565 | "be a string if there is a second argument"); |
| 12566 | goto err; |
| 12567 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12568 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12569 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12570 | "arguments must have equal length"); |
| 12571 | goto err; |
| 12572 | } |
| 12573 | /* 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] | 12574 | x_kind = PyUnicode_KIND(x); |
| 12575 | y_kind = PyUnicode_KIND(y); |
| 12576 | x_data = PyUnicode_DATA(x); |
| 12577 | y_data = PyUnicode_DATA(y); |
| 12578 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12579 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12580 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12581 | if (!key || !value) |
| 12582 | goto err; |
| 12583 | res = PyDict_SetItem(new, key, value); |
| 12584 | Py_DECREF(key); |
| 12585 | Py_DECREF(value); |
| 12586 | if (res < 0) |
| 12587 | goto err; |
| 12588 | } |
| 12589 | /* create entries for deleting chars in z */ |
| 12590 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12591 | z_kind = PyUnicode_KIND(z); |
| 12592 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12593 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12594 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12595 | if (!key) |
| 12596 | goto err; |
| 12597 | res = PyDict_SetItem(new, key, Py_None); |
| 12598 | Py_DECREF(key); |
| 12599 | if (res < 0) |
| 12600 | goto err; |
| 12601 | } |
| 12602 | } |
| 12603 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12604 | int kind; |
| 12605 | void *data; |
| 12606 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12607 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12608 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12609 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12610 | "to maketrans it must be a dict"); |
| 12611 | goto err; |
| 12612 | } |
| 12613 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12614 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12615 | if (PyUnicode_Check(key)) { |
| 12616 | /* convert string keys to integer keys */ |
| 12617 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12618 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12619 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12620 | "table must be of length 1"); |
| 12621 | goto err; |
| 12622 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12623 | kind = PyUnicode_KIND(key); |
| 12624 | data = PyUnicode_DATA(key); |
| 12625 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12626 | if (!newkey) |
| 12627 | goto err; |
| 12628 | res = PyDict_SetItem(new, newkey, value); |
| 12629 | Py_DECREF(newkey); |
| 12630 | if (res < 0) |
| 12631 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12632 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12633 | /* just keep integer keys */ |
| 12634 | if (PyDict_SetItem(new, key, value) < 0) |
| 12635 | goto err; |
| 12636 | } else { |
| 12637 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12638 | "be strings or integers"); |
| 12639 | goto err; |
| 12640 | } |
| 12641 | } |
| 12642 | } |
| 12643 | return new; |
| 12644 | err: |
| 12645 | Py_DECREF(new); |
| 12646 | return NULL; |
| 12647 | } |
| 12648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12649 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12650 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12651 | \n\ |
| 12652 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12653 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12654 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12655 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12656 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | |
| 12658 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12659 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12661 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12662 | } |
| 12663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12664 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12665 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12666 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12667 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12668 | |
| 12669 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12670 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12671 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12672 | return fixup(self, fixupper); |
| 12673 | } |
| 12674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12675 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12676 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12677 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12678 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12679 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12680 | |
| 12681 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12682 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12683 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12684 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12685 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12686 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12687 | int kind; |
| 12688 | void *data; |
| 12689 | Py_UCS4 chr; |
| 12690 | |
| 12691 | if (PyUnicode_READY(self) == -1) |
| 12692 | return NULL; |
| 12693 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12694 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12695 | return NULL; |
| 12696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12697 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12698 | if (PyUnicode_CheckExact(self)) { |
| 12699 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12700 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12701 | } |
| 12702 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12703 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12704 | } |
| 12705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12706 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12707 | |
| 12708 | u = pad(self, fill, 0, '0'); |
| 12709 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12710 | if (u == NULL) |
| 12711 | return NULL; |
| 12712 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12713 | kind = PyUnicode_KIND(u); |
| 12714 | data = PyUnicode_DATA(u); |
| 12715 | chr = PyUnicode_READ(kind, data, fill); |
| 12716 | |
| 12717 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12718 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12719 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12720 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | } |
| 12722 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12723 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12724 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12725 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12726 | |
| 12727 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12728 | static PyObject * |
| 12729 | unicode__decimal2ascii(PyObject *self) |
| 12730 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12731 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12732 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12733 | #endif |
| 12734 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12735 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12736 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12738 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12739 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12740 | With optional end, stop comparing S at that position.\n\ |
| 12741 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12742 | |
| 12743 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12744 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12745 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12746 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12747 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12748 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12749 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12750 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12751 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12752 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12753 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12754 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12755 | if (PyTuple_Check(subobj)) { |
| 12756 | Py_ssize_t i; |
| 12757 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12758 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12759 | if (substring == NULL) |
| 12760 | return NULL; |
| 12761 | result = tailmatch(self, substring, start, end, -1); |
| 12762 | Py_DECREF(substring); |
| 12763 | if (result) { |
| 12764 | Py_RETURN_TRUE; |
| 12765 | } |
| 12766 | } |
| 12767 | /* nothing matched */ |
| 12768 | Py_RETURN_FALSE; |
| 12769 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12770 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12771 | if (substring == NULL) { |
| 12772 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12773 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12774 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12775 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12776 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12777 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12778 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12779 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12780 | } |
| 12781 | |
| 12782 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12783 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12784 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12785 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12786 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12787 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12788 | With optional end, stop comparing S at that position.\n\ |
| 12789 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12790 | |
| 12791 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12792 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12793 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12794 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12795 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12796 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12797 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12798 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12799 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12800 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12801 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12803 | if (PyTuple_Check(subobj)) { |
| 12804 | Py_ssize_t i; |
| 12805 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12806 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12807 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12808 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12809 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12810 | result = tailmatch(self, substring, start, end, +1); |
| 12811 | Py_DECREF(substring); |
| 12812 | if (result) { |
| 12813 | Py_RETURN_TRUE; |
| 12814 | } |
| 12815 | } |
| 12816 | Py_RETURN_FALSE; |
| 12817 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12818 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12819 | if (substring == NULL) { |
| 12820 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12821 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12822 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12823 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12824 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12825 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12826 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12827 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12828 | } |
| 12829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12830 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12831 | |
| 12832 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12833 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12834 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12835 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12836 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12837 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12838 | PyDoc_STRVAR(format_map__doc__, |
| 12839 | "S.format_map(mapping) -> str\n\ |
| 12840 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12841 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12842 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12843 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12844 | static PyObject * |
| 12845 | unicode__format__(PyObject* self, PyObject* args) |
| 12846 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12847 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12848 | |
| 12849 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12850 | return NULL; |
| 12851 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12852 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12853 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12854 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12855 | } |
| 12856 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12857 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12858 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12859 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12860 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12861 | |
| 12862 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12863 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12864 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12865 | Py_ssize_t size; |
| 12866 | |
| 12867 | /* If it's a compact object, account for base structure + |
| 12868 | character data. */ |
| 12869 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12870 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12871 | else if (PyUnicode_IS_COMPACT(v)) |
| 12872 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12873 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12874 | else { |
| 12875 | /* If it is a two-block object, account for base object, and |
| 12876 | for character block if present. */ |
| 12877 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12878 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12879 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12880 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12881 | } |
| 12882 | /* 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] | 12883 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12884 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12885 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12886 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12887 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12888 | |
| 12889 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12890 | } |
| 12891 | |
| 12892 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12893 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12894 | |
| 12895 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12896 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12897 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12898 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12899 | if (!copy) |
| 12900 | return NULL; |
| 12901 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12902 | } |
| 12903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12904 | static PyMethodDef unicode_methods[] = { |
| 12905 | |
| 12906 | /* Order is according to common usage: often used methods should |
| 12907 | appear first, since lookup is done sequentially. */ |
| 12908 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12909 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12910 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12911 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12912 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12913 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12914 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12915 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12916 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12917 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12918 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12919 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12920 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12921 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12922 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12923 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12924 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12925 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12926 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12927 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12928 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12929 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12930 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12931 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12932 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12933 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12934 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12935 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12936 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12937 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12938 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12939 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12940 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12941 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12942 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12943 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12944 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12945 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12946 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12947 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12948 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12949 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12950 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12951 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12952 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12953 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12954 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12955 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12956 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12957 | #endif |
| 12958 | |
| 12959 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12960 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12961 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12962 | #endif |
| 12963 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12964 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12965 | {NULL, NULL} |
| 12966 | }; |
| 12967 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12968 | static PyObject * |
| 12969 | unicode_mod(PyObject *v, PyObject *w) |
| 12970 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12971 | if (!PyUnicode_Check(v)) |
| 12972 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12973 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12974 | } |
| 12975 | |
| 12976 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12977 | 0, /*nb_add*/ |
| 12978 | 0, /*nb_subtract*/ |
| 12979 | 0, /*nb_multiply*/ |
| 12980 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12981 | }; |
| 12982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12983 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12984 | (lenfunc) unicode_length, /* sq_length */ |
| 12985 | PyUnicode_Concat, /* sq_concat */ |
| 12986 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12987 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12988 | 0, /* sq_slice */ |
| 12989 | 0, /* sq_ass_item */ |
| 12990 | 0, /* sq_ass_slice */ |
| 12991 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12992 | }; |
| 12993 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12994 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12995 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12996 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12997 | if (PyUnicode_READY(self) == -1) |
| 12998 | return NULL; |
| 12999 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13000 | if (PyIndex_Check(item)) { |
| 13001 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13002 | if (i == -1 && PyErr_Occurred()) |
| 13003 | return NULL; |
| 13004 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13005 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13006 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13007 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13008 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13009 | PyObject *result; |
| 13010 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13011 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13012 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13014 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13015 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13016 | return NULL; |
| 13017 | } |
| 13018 | |
| 13019 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13020 | return PyUnicode_New(0, 0); |
| 13021 | } else if (start == 0 && step == 1 && |
| 13022 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13023 | PyUnicode_CheckExact(self)) { |
| 13024 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13025 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13026 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13027 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13028 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13029 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13030 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13031 | src_kind = PyUnicode_KIND(self); |
| 13032 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13033 | if (!PyUnicode_IS_ASCII(self)) { |
| 13034 | kind_limit = kind_maxchar_limit(src_kind); |
| 13035 | max_char = 0; |
| 13036 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13037 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13038 | if (ch > max_char) { |
| 13039 | max_char = ch; |
| 13040 | if (max_char >= kind_limit) |
| 13041 | break; |
| 13042 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13043 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13044 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13045 | else |
| 13046 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13047 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13048 | if (result == NULL) |
| 13049 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13050 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13051 | dest_data = PyUnicode_DATA(result); |
| 13052 | |
| 13053 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13054 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13055 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13056 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13057 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13058 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13059 | } else { |
| 13060 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13061 | return NULL; |
| 13062 | } |
| 13063 | } |
| 13064 | |
| 13065 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13066 | (lenfunc)unicode_length, /* mp_length */ |
| 13067 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13068 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13069 | }; |
| 13070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13072 | /* Helpers for PyUnicode_Format() */ |
| 13073 | |
| 13074 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13075 | 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] | 13076 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13077 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13078 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13079 | (*p_argidx)++; |
| 13080 | if (arglen < 0) |
| 13081 | return args; |
| 13082 | else |
| 13083 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13084 | } |
| 13085 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13086 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13087 | return NULL; |
| 13088 | } |
| 13089 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13090 | /* 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] | 13091 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13092 | static PyObject * |
| 13093 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13094 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13095 | char *p; |
| 13096 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13097 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13099 | x = PyFloat_AsDouble(v); |
| 13100 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13101 | return NULL; |
| 13102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13103 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13104 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13105 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13106 | p = PyOS_double_to_string(x, type, prec, |
| 13107 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13108 | if (p == NULL) |
| 13109 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13110 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13111 | PyMem_Free(p); |
| 13112 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13113 | } |
| 13114 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13115 | static PyObject* |
| 13116 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13117 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13118 | char *buf; |
| 13119 | int len; |
| 13120 | PyObject *str; /* temporary string object. */ |
| 13121 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13122 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13123 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13124 | if (!str) |
| 13125 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13126 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13127 | Py_DECREF(str); |
| 13128 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13129 | } |
| 13130 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13131 | static Py_UCS4 |
| 13132 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13133 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13134 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13135 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13136 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13137 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13138 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13139 | goto onError; |
| 13140 | } |
| 13141 | else { |
| 13142 | /* Integer input truncated to a character */ |
| 13143 | long x; |
| 13144 | x = PyLong_AsLong(v); |
| 13145 | if (x == -1 && PyErr_Occurred()) |
| 13146 | goto onError; |
| 13147 | |
| 13148 | if (x < 0 || x > 0x10ffff) { |
| 13149 | PyErr_SetString(PyExc_OverflowError, |
| 13150 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13151 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13152 | } |
| 13153 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13154 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13155 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13156 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13157 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13158 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13159 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13160 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13161 | } |
| 13162 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13163 | static int |
| 13164 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13165 | { |
| 13166 | int r; |
| 13167 | assert(count > 0); |
| 13168 | assert(PyUnicode_Check(obj)); |
| 13169 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13170 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13171 | if (repeated == NULL) |
| 13172 | return -1; |
| 13173 | r = _PyAccu_Accumulate(acc, repeated); |
| 13174 | Py_DECREF(repeated); |
| 13175 | return r; |
| 13176 | } |
| 13177 | else { |
| 13178 | do { |
| 13179 | if (_PyAccu_Accumulate(acc, obj)) |
| 13180 | return -1; |
| 13181 | } while (--count); |
| 13182 | return 0; |
| 13183 | } |
| 13184 | } |
| 13185 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13186 | PyObject * |
| 13187 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13188 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13189 | void *fmt; |
| 13190 | int fmtkind; |
| 13191 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13192 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13193 | int r; |
| 13194 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13195 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13196 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13197 | PyObject *temp = NULL; |
| 13198 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13199 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13200 | _PyAccu acc; |
| 13201 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13202 | |
| 13203 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13204 | return NULL; |
| 13205 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13206 | return NULL; |
| 13207 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13208 | return NULL; |
| 13209 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13210 | return NULL; |
| 13211 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13212 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13214 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13215 | PyErr_BadInternalCall(); |
| 13216 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13217 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13218 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13219 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13220 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13221 | if (_PyAccu_Init(&acc)) |
| 13222 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13223 | fmt = PyUnicode_DATA(uformat); |
| 13224 | fmtkind = PyUnicode_KIND(uformat); |
| 13225 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13226 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13228 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13229 | arglen = PyTuple_Size(args); |
| 13230 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13231 | } |
| 13232 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13233 | arglen = -1; |
| 13234 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13235 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13236 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13237 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13238 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13239 | |
| 13240 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13241 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13242 | PyObject *nonfmt; |
| 13243 | Py_ssize_t nonfmtpos; |
| 13244 | nonfmtpos = fmtpos++; |
| 13245 | while (fmtcnt >= 0 && |
| 13246 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13247 | fmtpos++; |
| 13248 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13249 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13250 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13251 | if (nonfmt == NULL) |
| 13252 | goto onError; |
| 13253 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13254 | Py_DECREF(nonfmt); |
| 13255 | if (r) |
| 13256 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13257 | } |
| 13258 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13259 | /* Got a format specifier */ |
| 13260 | int flags = 0; |
| 13261 | Py_ssize_t width = -1; |
| 13262 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13263 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13264 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13265 | int isnumok; |
| 13266 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13267 | void *pbuf = NULL; |
| 13268 | Py_ssize_t pindex, len; |
| 13269 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13271 | fmtpos++; |
| 13272 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13273 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13274 | Py_ssize_t keylen; |
| 13275 | PyObject *key; |
| 13276 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13277 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13278 | if (dict == NULL) { |
| 13279 | PyErr_SetString(PyExc_TypeError, |
| 13280 | "format requires a mapping"); |
| 13281 | goto onError; |
| 13282 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13283 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13284 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13285 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13286 | /* Skip over balanced parentheses */ |
| 13287 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13288 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13289 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13290 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13291 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13292 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13293 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13294 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13295 | if (fmtcnt < 0 || pcount > 0) { |
| 13296 | PyErr_SetString(PyExc_ValueError, |
| 13297 | "incomplete format key"); |
| 13298 | goto onError; |
| 13299 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13300 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13301 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13302 | if (key == NULL) |
| 13303 | goto onError; |
| 13304 | if (args_owned) { |
| 13305 | Py_DECREF(args); |
| 13306 | args_owned = 0; |
| 13307 | } |
| 13308 | args = PyObject_GetItem(dict, key); |
| 13309 | Py_DECREF(key); |
| 13310 | if (args == NULL) { |
| 13311 | goto onError; |
| 13312 | } |
| 13313 | args_owned = 1; |
| 13314 | arglen = -1; |
| 13315 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13316 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13317 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13318 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13319 | case '-': flags |= F_LJUST; continue; |
| 13320 | case '+': flags |= F_SIGN; continue; |
| 13321 | case ' ': flags |= F_BLANK; continue; |
| 13322 | case '#': flags |= F_ALT; continue; |
| 13323 | case '0': flags |= F_ZERO; continue; |
| 13324 | } |
| 13325 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13326 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13327 | if (c == '*') { |
| 13328 | v = getnextarg(args, arglen, &argidx); |
| 13329 | if (v == NULL) |
| 13330 | goto onError; |
| 13331 | if (!PyLong_Check(v)) { |
| 13332 | PyErr_SetString(PyExc_TypeError, |
| 13333 | "* wants int"); |
| 13334 | goto onError; |
| 13335 | } |
| 13336 | width = PyLong_AsLong(v); |
| 13337 | if (width == -1 && PyErr_Occurred()) |
| 13338 | goto onError; |
| 13339 | if (width < 0) { |
| 13340 | flags |= F_LJUST; |
| 13341 | width = -width; |
| 13342 | } |
| 13343 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13344 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13345 | } |
| 13346 | else if (c >= '0' && c <= '9') { |
| 13347 | width = c - '0'; |
| 13348 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13349 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13350 | if (c < '0' || c > '9') |
| 13351 | break; |
| 13352 | if ((width*10) / 10 != width) { |
| 13353 | PyErr_SetString(PyExc_ValueError, |
| 13354 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13355 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13356 | } |
| 13357 | width = width*10 + (c - '0'); |
| 13358 | } |
| 13359 | } |
| 13360 | if (c == '.') { |
| 13361 | prec = 0; |
| 13362 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13363 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13364 | if (c == '*') { |
| 13365 | v = getnextarg(args, arglen, &argidx); |
| 13366 | if (v == NULL) |
| 13367 | goto onError; |
| 13368 | if (!PyLong_Check(v)) { |
| 13369 | PyErr_SetString(PyExc_TypeError, |
| 13370 | "* wants int"); |
| 13371 | goto onError; |
| 13372 | } |
| 13373 | prec = PyLong_AsLong(v); |
| 13374 | if (prec == -1 && PyErr_Occurred()) |
| 13375 | goto onError; |
| 13376 | if (prec < 0) |
| 13377 | prec = 0; |
| 13378 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13379 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13380 | } |
| 13381 | else if (c >= '0' && c <= '9') { |
| 13382 | prec = c - '0'; |
| 13383 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13384 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13385 | if (c < '0' || c > '9') |
| 13386 | break; |
| 13387 | if ((prec*10) / 10 != prec) { |
| 13388 | PyErr_SetString(PyExc_ValueError, |
| 13389 | "prec too big"); |
| 13390 | goto onError; |
| 13391 | } |
| 13392 | prec = prec*10 + (c - '0'); |
| 13393 | } |
| 13394 | } |
| 13395 | } /* prec */ |
| 13396 | if (fmtcnt >= 0) { |
| 13397 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13398 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13399 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13400 | } |
| 13401 | } |
| 13402 | if (fmtcnt < 0) { |
| 13403 | PyErr_SetString(PyExc_ValueError, |
| 13404 | "incomplete format"); |
| 13405 | goto onError; |
| 13406 | } |
| 13407 | if (c != '%') { |
| 13408 | v = getnextarg(args, arglen, &argidx); |
| 13409 | if (v == NULL) |
| 13410 | goto onError; |
| 13411 | } |
| 13412 | sign = 0; |
| 13413 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13414 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13415 | switch (c) { |
| 13416 | |
| 13417 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13418 | _PyAccu_Accumulate(&acc, percent); |
| 13419 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13420 | |
| 13421 | case 's': |
| 13422 | case 'r': |
| 13423 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13424 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13425 | temp = v; |
| 13426 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13427 | } |
| 13428 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13429 | if (c == 's') |
| 13430 | temp = PyObject_Str(v); |
| 13431 | else if (c == 'r') |
| 13432 | temp = PyObject_Repr(v); |
| 13433 | else |
| 13434 | temp = PyObject_ASCII(v); |
| 13435 | if (temp == NULL) |
| 13436 | goto onError; |
| 13437 | if (PyUnicode_Check(temp)) |
| 13438 | /* nothing to do */; |
| 13439 | else { |
| 13440 | Py_DECREF(temp); |
| 13441 | PyErr_SetString(PyExc_TypeError, |
| 13442 | "%s argument has non-string str()"); |
| 13443 | goto onError; |
| 13444 | } |
| 13445 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13446 | if (PyUnicode_READY(temp) == -1) { |
| 13447 | Py_CLEAR(temp); |
| 13448 | goto onError; |
| 13449 | } |
| 13450 | pbuf = PyUnicode_DATA(temp); |
| 13451 | kind = PyUnicode_KIND(temp); |
| 13452 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13453 | if (prec >= 0 && len > prec) |
| 13454 | len = prec; |
| 13455 | break; |
| 13456 | |
| 13457 | case 'i': |
| 13458 | case 'd': |
| 13459 | case 'u': |
| 13460 | case 'o': |
| 13461 | case 'x': |
| 13462 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13463 | isnumok = 0; |
| 13464 | if (PyNumber_Check(v)) { |
| 13465 | PyObject *iobj=NULL; |
| 13466 | |
| 13467 | if (PyLong_Check(v)) { |
| 13468 | iobj = v; |
| 13469 | Py_INCREF(iobj); |
| 13470 | } |
| 13471 | else { |
| 13472 | iobj = PyNumber_Long(v); |
| 13473 | } |
| 13474 | if (iobj!=NULL) { |
| 13475 | if (PyLong_Check(iobj)) { |
| 13476 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13477 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13478 | Py_DECREF(iobj); |
| 13479 | if (!temp) |
| 13480 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13481 | if (PyUnicode_READY(temp) == -1) { |
| 13482 | Py_CLEAR(temp); |
| 13483 | goto onError; |
| 13484 | } |
| 13485 | pbuf = PyUnicode_DATA(temp); |
| 13486 | kind = PyUnicode_KIND(temp); |
| 13487 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13488 | sign = 1; |
| 13489 | } |
| 13490 | else { |
| 13491 | Py_DECREF(iobj); |
| 13492 | } |
| 13493 | } |
| 13494 | } |
| 13495 | if (!isnumok) { |
| 13496 | PyErr_Format(PyExc_TypeError, |
| 13497 | "%%%c format: a number is required, " |
| 13498 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13499 | goto onError; |
| 13500 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13501 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13502 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13503 | fillobj = zero; |
| 13504 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13505 | break; |
| 13506 | |
| 13507 | case 'e': |
| 13508 | case 'E': |
| 13509 | case 'f': |
| 13510 | case 'F': |
| 13511 | case 'g': |
| 13512 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13513 | temp = formatfloat(v, flags, prec, c); |
| 13514 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13515 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13516 | if (PyUnicode_READY(temp) == -1) { |
| 13517 | Py_CLEAR(temp); |
| 13518 | goto onError; |
| 13519 | } |
| 13520 | pbuf = PyUnicode_DATA(temp); |
| 13521 | kind = PyUnicode_KIND(temp); |
| 13522 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13523 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13524 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13525 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13526 | fillobj = zero; |
| 13527 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13528 | break; |
| 13529 | |
| 13530 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13531 | { |
| 13532 | Py_UCS4 ch = formatchar(v); |
| 13533 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13534 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13535 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13536 | if (temp == NULL) |
| 13537 | goto onError; |
| 13538 | pbuf = PyUnicode_DATA(temp); |
| 13539 | kind = PyUnicode_KIND(temp); |
| 13540 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13541 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13542 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13543 | |
| 13544 | default: |
| 13545 | PyErr_Format(PyExc_ValueError, |
| 13546 | "unsupported format character '%c' (0x%x) " |
| 13547 | "at index %zd", |
| 13548 | (31<=c && c<=126) ? (char)c : '?', |
| 13549 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13550 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13551 | goto onError; |
| 13552 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13553 | /* pbuf is initialized here. */ |
| 13554 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13555 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13556 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13557 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13558 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13559 | pindex++; |
| 13560 | } |
| 13561 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13562 | signobj = plus; |
| 13563 | len--; |
| 13564 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13565 | } |
| 13566 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13567 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13568 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13569 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13570 | else |
| 13571 | sign = 0; |
| 13572 | } |
| 13573 | if (width < len) |
| 13574 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13576 | if (fill != ' ') { |
| 13577 | assert(signobj != NULL); |
| 13578 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13579 | goto onError; |
| 13580 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13581 | if (width > len) |
| 13582 | width--; |
| 13583 | } |
| 13584 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13585 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13586 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13587 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13588 | second = get_latin1_char( |
| 13589 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13590 | pindex += 2; |
| 13591 | if (second == NULL || |
| 13592 | _PyAccu_Accumulate(&acc, zero) || |
| 13593 | _PyAccu_Accumulate(&acc, second)) |
| 13594 | goto onError; |
| 13595 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13596 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13597 | width -= 2; |
| 13598 | if (width < 0) |
| 13599 | width = 0; |
| 13600 | len -= 2; |
| 13601 | } |
| 13602 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13603 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13604 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13605 | goto onError; |
| 13606 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13607 | } |
| 13608 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13609 | if (sign) { |
| 13610 | assert(signobj != NULL); |
| 13611 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13612 | goto onError; |
| 13613 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13614 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13615 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13616 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13617 | second = get_latin1_char( |
| 13618 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13619 | pindex += 2; |
| 13620 | if (second == NULL || |
| 13621 | _PyAccu_Accumulate(&acc, zero) || |
| 13622 | _PyAccu_Accumulate(&acc, second)) |
| 13623 | goto onError; |
| 13624 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13625 | } |
| 13626 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13627 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13628 | if (temp != NULL) { |
| 13629 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13630 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13631 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13632 | else { |
| 13633 | const char *p = (const char *) pbuf; |
| 13634 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13635 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13636 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13637 | } |
| 13638 | if (v == NULL) |
| 13639 | goto onError; |
| 13640 | r = _PyAccu_Accumulate(&acc, v); |
| 13641 | Py_DECREF(v); |
| 13642 | if (r) |
| 13643 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13644 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13645 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13646 | if (dict && (argidx < arglen) && c != '%') { |
| 13647 | PyErr_SetString(PyExc_TypeError, |
| 13648 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13649 | goto onError; |
| 13650 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13651 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13652 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13653 | } /* until end */ |
| 13654 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13655 | PyErr_SetString(PyExc_TypeError, |
| 13656 | "not all arguments converted during string formatting"); |
| 13657 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13658 | } |
| 13659 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13660 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13661 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13662 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13663 | } |
| 13664 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13665 | Py_XDECREF(temp); |
| 13666 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13667 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13668 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13669 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13670 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13671 | Py_XDECREF(temp); |
| 13672 | Py_XDECREF(second); |
| 13673 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13674 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13675 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13676 | } |
| 13677 | return NULL; |
| 13678 | } |
| 13679 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13680 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13681 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13682 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13683 | static PyObject * |
| 13684 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13685 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13686 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13687 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13688 | char *encoding = NULL; |
| 13689 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13690 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13691 | if (type != &PyUnicode_Type) |
| 13692 | return unicode_subtype_new(type, args, kwds); |
| 13693 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13694 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13695 | return NULL; |
| 13696 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13697 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13698 | if (encoding == NULL && errors == NULL) |
| 13699 | return PyObject_Str(x); |
| 13700 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13701 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13702 | } |
| 13703 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13704 | static PyObject * |
| 13705 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13706 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13707 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13708 | Py_ssize_t length, char_size; |
| 13709 | int share_wstr, share_utf8; |
| 13710 | unsigned int kind; |
| 13711 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13712 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13713 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13714 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13715 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13716 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13717 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13718 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13719 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13720 | return NULL; |
| 13721 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13722 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13723 | if (self == NULL) { |
| 13724 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13725 | return NULL; |
| 13726 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13727 | kind = PyUnicode_KIND(unicode); |
| 13728 | length = PyUnicode_GET_LENGTH(unicode); |
| 13729 | |
| 13730 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13731 | #ifdef Py_DEBUG |
| 13732 | _PyUnicode_HASH(self) = -1; |
| 13733 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13734 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13735 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13736 | _PyUnicode_STATE(self).interned = 0; |
| 13737 | _PyUnicode_STATE(self).kind = kind; |
| 13738 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13739 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13740 | _PyUnicode_STATE(self).ready = 1; |
| 13741 | _PyUnicode_WSTR(self) = NULL; |
| 13742 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13743 | _PyUnicode_UTF8(self) = NULL; |
| 13744 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13745 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13746 | |
| 13747 | share_utf8 = 0; |
| 13748 | share_wstr = 0; |
| 13749 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13750 | char_size = 1; |
| 13751 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13752 | share_utf8 = 1; |
| 13753 | } |
| 13754 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13755 | char_size = 2; |
| 13756 | if (sizeof(wchar_t) == 2) |
| 13757 | share_wstr = 1; |
| 13758 | } |
| 13759 | else { |
| 13760 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13761 | char_size = 4; |
| 13762 | if (sizeof(wchar_t) == 4) |
| 13763 | share_wstr = 1; |
| 13764 | } |
| 13765 | |
| 13766 | /* Ensure we won't overflow the length. */ |
| 13767 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13768 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13769 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13770 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13771 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13772 | if (data == NULL) { |
| 13773 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13774 | goto onError; |
| 13775 | } |
| 13776 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13777 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13778 | if (share_utf8) { |
| 13779 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13780 | _PyUnicode_UTF8(self) = data; |
| 13781 | } |
| 13782 | if (share_wstr) { |
| 13783 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13784 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13785 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13786 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13787 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13788 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13789 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13790 | #ifdef Py_DEBUG |
| 13791 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13792 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13793 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13794 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13795 | |
| 13796 | onError: |
| 13797 | Py_DECREF(unicode); |
| 13798 | Py_DECREF(self); |
| 13799 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13800 | } |
| 13801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13802 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13803 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13804 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13805 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13806 | encoding defaults to the current default string encoding.\n\ |
| 13807 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13808 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13809 | static PyObject *unicode_iter(PyObject *seq); |
| 13810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13811 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13812 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13813 | "str", /* tp_name */ |
| 13814 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13815 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13816 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13817 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13818 | 0, /* tp_print */ |
| 13819 | 0, /* tp_getattr */ |
| 13820 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13821 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13822 | unicode_repr, /* tp_repr */ |
| 13823 | &unicode_as_number, /* tp_as_number */ |
| 13824 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13825 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13826 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13827 | 0, /* tp_call*/ |
| 13828 | (reprfunc) unicode_str, /* tp_str */ |
| 13829 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13830 | 0, /* tp_setattro */ |
| 13831 | 0, /* tp_as_buffer */ |
| 13832 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13833 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13834 | unicode_doc, /* tp_doc */ |
| 13835 | 0, /* tp_traverse */ |
| 13836 | 0, /* tp_clear */ |
| 13837 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13838 | 0, /* tp_weaklistoffset */ |
| 13839 | unicode_iter, /* tp_iter */ |
| 13840 | 0, /* tp_iternext */ |
| 13841 | unicode_methods, /* tp_methods */ |
| 13842 | 0, /* tp_members */ |
| 13843 | 0, /* tp_getset */ |
| 13844 | &PyBaseObject_Type, /* tp_base */ |
| 13845 | 0, /* tp_dict */ |
| 13846 | 0, /* tp_descr_get */ |
| 13847 | 0, /* tp_descr_set */ |
| 13848 | 0, /* tp_dictoffset */ |
| 13849 | 0, /* tp_init */ |
| 13850 | 0, /* tp_alloc */ |
| 13851 | unicode_new, /* tp_new */ |
| 13852 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13853 | }; |
| 13854 | |
| 13855 | /* Initialize the Unicode implementation */ |
| 13856 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13857 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13858 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13859 | int i; |
| 13860 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13861 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13862 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13863 | 0x000A, /* LINE FEED */ |
| 13864 | 0x000D, /* CARRIAGE RETURN */ |
| 13865 | 0x001C, /* FILE SEPARATOR */ |
| 13866 | 0x001D, /* GROUP SEPARATOR */ |
| 13867 | 0x001E, /* RECORD SEPARATOR */ |
| 13868 | 0x0085, /* NEXT LINE */ |
| 13869 | 0x2028, /* LINE SEPARATOR */ |
| 13870 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13871 | }; |
| 13872 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13873 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13874 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13875 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13876 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13877 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13878 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13879 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13880 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13881 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13882 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13883 | |
| 13884 | /* initialize the linebreak bloom filter */ |
| 13885 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13886 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13887 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13888 | |
| 13889 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13890 | |
| 13891 | #ifdef HAVE_MBCS |
| 13892 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13893 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13894 | PyErr_SetFromWindowsErr(0); |
| 13895 | return -1; |
| 13896 | } |
| 13897 | #endif |
| 13898 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13899 | } |
| 13900 | |
| 13901 | /* Finalize the Unicode implementation */ |
| 13902 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13903 | int |
| 13904 | PyUnicode_ClearFreeList(void) |
| 13905 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13906 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13907 | } |
| 13908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13909 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13910 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13911 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13912 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13913 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13914 | Py_XDECREF(unicode_empty); |
| 13915 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13916 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13917 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13918 | if (unicode_latin1[i]) { |
| 13919 | Py_DECREF(unicode_latin1[i]); |
| 13920 | unicode_latin1[i] = NULL; |
| 13921 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13922 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13923 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13924 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13925 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13926 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13927 | void |
| 13928 | PyUnicode_InternInPlace(PyObject **p) |
| 13929 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13930 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13931 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13932 | #ifdef Py_DEBUG |
| 13933 | assert(s != NULL); |
| 13934 | assert(_PyUnicode_CHECK(s)); |
| 13935 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13936 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13937 | return; |
| 13938 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13939 | /* If it's a subclass, we don't really know what putting |
| 13940 | it in the interned dict might do. */ |
| 13941 | if (!PyUnicode_CheckExact(s)) |
| 13942 | return; |
| 13943 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13944 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13945 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13946 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13947 | return; |
| 13948 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13949 | s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13950 | if (interned == NULL) { |
| 13951 | interned = PyDict_New(); |
| 13952 | if (interned == NULL) { |
| 13953 | PyErr_Clear(); /* Don't leave an exception */ |
| 13954 | return; |
| 13955 | } |
| 13956 | } |
| 13957 | /* It might be that the GetItem call fails even |
| 13958 | though the key is present in the dictionary, |
| 13959 | namely when this happens during a stack overflow. */ |
| 13960 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13961 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13962 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13963 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13964 | if (t) { |
| 13965 | Py_INCREF(t); |
| 13966 | Py_DECREF(*p); |
| 13967 | *p = t; |
| 13968 | return; |
| 13969 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13970 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13971 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13972 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13973 | PyErr_Clear(); |
| 13974 | PyThreadState_GET()->recursion_critical = 0; |
| 13975 | return; |
| 13976 | } |
| 13977 | PyThreadState_GET()->recursion_critical = 0; |
| 13978 | /* The two references in interned are not counted by refcnt. |
| 13979 | The deallocator will take care of this */ |
| 13980 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13981 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13982 | } |
| 13983 | |
| 13984 | void |
| 13985 | PyUnicode_InternImmortal(PyObject **p) |
| 13986 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13987 | PyUnicode_InternInPlace(p); |
| 13988 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 13989 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13990 | Py_INCREF(*p); |
| 13991 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13992 | } |
| 13993 | |
| 13994 | PyObject * |
| 13995 | PyUnicode_InternFromString(const char *cp) |
| 13996 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13997 | PyObject *s = PyUnicode_FromString(cp); |
| 13998 | if (s == NULL) |
| 13999 | return NULL; |
| 14000 | PyUnicode_InternInPlace(&s); |
| 14001 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14002 | } |
| 14003 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14004 | void |
| 14005 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14006 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14007 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14008 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14009 | Py_ssize_t i, n; |
| 14010 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14011 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14012 | if (interned == NULL || !PyDict_Check(interned)) |
| 14013 | return; |
| 14014 | keys = PyDict_Keys(interned); |
| 14015 | if (keys == NULL || !PyList_Check(keys)) { |
| 14016 | PyErr_Clear(); |
| 14017 | return; |
| 14018 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14019 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14020 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14021 | detector, interned unicode strings are not forcibly deallocated; |
| 14022 | rather, we give them their stolen references back, and then clear |
| 14023 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14024 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14025 | n = PyList_GET_SIZE(keys); |
| 14026 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14027 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14028 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14029 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14030 | if (PyUnicode_READY(s) == -1) { |
| 14031 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14032 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14033 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14034 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14035 | case SSTATE_NOT_INTERNED: |
| 14036 | /* XXX Shouldn't happen */ |
| 14037 | break; |
| 14038 | case SSTATE_INTERNED_IMMORTAL: |
| 14039 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14040 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14041 | break; |
| 14042 | case SSTATE_INTERNED_MORTAL: |
| 14043 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14044 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14045 | break; |
| 14046 | default: |
| 14047 | Py_FatalError("Inconsistent interned string state."); |
| 14048 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14049 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14050 | } |
| 14051 | fprintf(stderr, "total size of all interned strings: " |
| 14052 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14053 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14054 | Py_DECREF(keys); |
| 14055 | PyDict_Clear(interned); |
| 14056 | Py_DECREF(interned); |
| 14057 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14058 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14059 | |
| 14060 | |
| 14061 | /********************* Unicode Iterator **************************/ |
| 14062 | |
| 14063 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14064 | PyObject_HEAD |
| 14065 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14066 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14067 | } unicodeiterobject; |
| 14068 | |
| 14069 | static void |
| 14070 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14071 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14072 | _PyObject_GC_UNTRACK(it); |
| 14073 | Py_XDECREF(it->it_seq); |
| 14074 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14075 | } |
| 14076 | |
| 14077 | static int |
| 14078 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14079 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14080 | Py_VISIT(it->it_seq); |
| 14081 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14082 | } |
| 14083 | |
| 14084 | static PyObject * |
| 14085 | unicodeiter_next(unicodeiterobject *it) |
| 14086 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14087 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14088 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14089 | assert(it != NULL); |
| 14090 | seq = it->it_seq; |
| 14091 | if (seq == NULL) |
| 14092 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14093 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14094 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14095 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14096 | int kind = PyUnicode_KIND(seq); |
| 14097 | void *data = PyUnicode_DATA(seq); |
| 14098 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14099 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14100 | if (item != NULL) |
| 14101 | ++it->it_index; |
| 14102 | return item; |
| 14103 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14104 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14105 | Py_DECREF(seq); |
| 14106 | it->it_seq = NULL; |
| 14107 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14108 | } |
| 14109 | |
| 14110 | static PyObject * |
| 14111 | unicodeiter_len(unicodeiterobject *it) |
| 14112 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14113 | Py_ssize_t len = 0; |
| 14114 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14115 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14116 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14117 | } |
| 14118 | |
| 14119 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14120 | |
| 14121 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14122 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14123 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14124 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14125 | }; |
| 14126 | |
| 14127 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14128 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14129 | "str_iterator", /* tp_name */ |
| 14130 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14131 | 0, /* tp_itemsize */ |
| 14132 | /* methods */ |
| 14133 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14134 | 0, /* tp_print */ |
| 14135 | 0, /* tp_getattr */ |
| 14136 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14137 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14138 | 0, /* tp_repr */ |
| 14139 | 0, /* tp_as_number */ |
| 14140 | 0, /* tp_as_sequence */ |
| 14141 | 0, /* tp_as_mapping */ |
| 14142 | 0, /* tp_hash */ |
| 14143 | 0, /* tp_call */ |
| 14144 | 0, /* tp_str */ |
| 14145 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14146 | 0, /* tp_setattro */ |
| 14147 | 0, /* tp_as_buffer */ |
| 14148 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14149 | 0, /* tp_doc */ |
| 14150 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14151 | 0, /* tp_clear */ |
| 14152 | 0, /* tp_richcompare */ |
| 14153 | 0, /* tp_weaklistoffset */ |
| 14154 | PyObject_SelfIter, /* tp_iter */ |
| 14155 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14156 | unicodeiter_methods, /* tp_methods */ |
| 14157 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14158 | }; |
| 14159 | |
| 14160 | static PyObject * |
| 14161 | unicode_iter(PyObject *seq) |
| 14162 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14163 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14164 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14165 | if (!PyUnicode_Check(seq)) { |
| 14166 | PyErr_BadInternalCall(); |
| 14167 | return NULL; |
| 14168 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14169 | if (PyUnicode_READY(seq) == -1) |
| 14170 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14171 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14172 | if (it == NULL) |
| 14173 | return NULL; |
| 14174 | it->it_index = 0; |
| 14175 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14176 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14177 | _PyObject_GC_TRACK(it); |
| 14178 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14179 | } |
| 14180 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14181 | |
| 14182 | size_t |
| 14183 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14184 | { |
| 14185 | int res = 0; |
| 14186 | while(*u++) |
| 14187 | res++; |
| 14188 | return res; |
| 14189 | } |
| 14190 | |
| 14191 | Py_UNICODE* |
| 14192 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14193 | { |
| 14194 | Py_UNICODE *u = s1; |
| 14195 | while ((*u++ = *s2++)); |
| 14196 | return s1; |
| 14197 | } |
| 14198 | |
| 14199 | Py_UNICODE* |
| 14200 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14201 | { |
| 14202 | Py_UNICODE *u = s1; |
| 14203 | while ((*u++ = *s2++)) |
| 14204 | if (n-- == 0) |
| 14205 | break; |
| 14206 | return s1; |
| 14207 | } |
| 14208 | |
| 14209 | Py_UNICODE* |
| 14210 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14211 | { |
| 14212 | Py_UNICODE *u1 = s1; |
| 14213 | u1 += Py_UNICODE_strlen(u1); |
| 14214 | Py_UNICODE_strcpy(u1, s2); |
| 14215 | return s1; |
| 14216 | } |
| 14217 | |
| 14218 | int |
| 14219 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14220 | { |
| 14221 | while (*s1 && *s2 && *s1 == *s2) |
| 14222 | s1++, s2++; |
| 14223 | if (*s1 && *s2) |
| 14224 | return (*s1 < *s2) ? -1 : +1; |
| 14225 | if (*s1) |
| 14226 | return 1; |
| 14227 | if (*s2) |
| 14228 | return -1; |
| 14229 | return 0; |
| 14230 | } |
| 14231 | |
| 14232 | int |
| 14233 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14234 | { |
| 14235 | register Py_UNICODE u1, u2; |
| 14236 | for (; n != 0; n--) { |
| 14237 | u1 = *s1; |
| 14238 | u2 = *s2; |
| 14239 | if (u1 != u2) |
| 14240 | return (u1 < u2) ? -1 : +1; |
| 14241 | if (u1 == '\0') |
| 14242 | return 0; |
| 14243 | s1++; |
| 14244 | s2++; |
| 14245 | } |
| 14246 | return 0; |
| 14247 | } |
| 14248 | |
| 14249 | Py_UNICODE* |
| 14250 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14251 | { |
| 14252 | const Py_UNICODE *p; |
| 14253 | for (p = s; *p; p++) |
| 14254 | if (*p == c) |
| 14255 | return (Py_UNICODE*)p; |
| 14256 | return NULL; |
| 14257 | } |
| 14258 | |
| 14259 | Py_UNICODE* |
| 14260 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14261 | { |
| 14262 | const Py_UNICODE *p; |
| 14263 | p = s + Py_UNICODE_strlen(s); |
| 14264 | while (p != s) { |
| 14265 | p--; |
| 14266 | if (*p == c) |
| 14267 | return (Py_UNICODE*)p; |
| 14268 | } |
| 14269 | return NULL; |
| 14270 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14271 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14272 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14273 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14274 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14275 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14276 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14278 | if (!PyUnicode_Check(unicode)) { |
| 14279 | PyErr_BadArgument(); |
| 14280 | return NULL; |
| 14281 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14282 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14283 | if (u == NULL) |
| 14284 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14285 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14286 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14287 | PyErr_NoMemory(); |
| 14288 | return NULL; |
| 14289 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14290 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14291 | size *= sizeof(Py_UNICODE); |
| 14292 | copy = PyMem_Malloc(size); |
| 14293 | if (copy == NULL) { |
| 14294 | PyErr_NoMemory(); |
| 14295 | return NULL; |
| 14296 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14297 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14298 | return copy; |
| 14299 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14300 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14301 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14302 | to the string.Formatter class implemented in Python. */ |
| 14303 | |
| 14304 | static PyMethodDef _string_methods[] = { |
| 14305 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14306 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14307 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14308 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14309 | {NULL, NULL} |
| 14310 | }; |
| 14311 | |
| 14312 | static struct PyModuleDef _string_module = { |
| 14313 | PyModuleDef_HEAD_INIT, |
| 14314 | "_string", |
| 14315 | PyDoc_STR("string helper module"), |
| 14316 | 0, |
| 14317 | _string_methods, |
| 14318 | NULL, |
| 14319 | NULL, |
| 14320 | NULL, |
| 14321 | NULL |
| 14322 | }; |
| 14323 | |
| 14324 | PyMODINIT_FUNC |
| 14325 | PyInit__string(void) |
| 14326 | { |
| 14327 | return PyModule_Create(&_string_module); |
| 14328 | } |
| 14329 | |
| 14330 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14331 | #ifdef __cplusplus |
| 14332 | } |
| 14333 | #endif |