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 | } |
Victor Stinner | da29cc3 | 2011-11-21 14:31:41 +0100 | [diff] [blame] | 394 | if (maxchar > 0x10FFFF) { |
| 395 | printf("Invalid Unicode string! {"); |
| 396 | for (i=0; i < ascii->length; i++) |
| 397 | { |
| 398 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 399 | if (i) |
| 400 | printf(", U+%04x", ch); |
| 401 | else |
| 402 | printf("U+%04x", ch); |
| 403 | } |
Victor Stinner | 5bbe5e7 | 2011-11-21 22:54:05 +0100 | [diff] [blame] | 404 | printf("} (len=%lu)\n", ascii->length); |
Victor Stinner | da29cc3 | 2011-11-21 14:31:41 +0100 | [diff] [blame] | 405 | abort(); |
| 406 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 407 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 408 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 409 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 410 | assert(maxchar <= 255); |
| 411 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 412 | else |
| 413 | assert(maxchar < 128); |
| 414 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 415 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 416 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 417 | assert(maxchar <= 0xFFFF); |
| 418 | } |
| 419 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 420 | assert(maxchar >= 0x10000); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 421 | assert(maxchar <= 0x10FFFF); |
| 422 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 423 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 424 | return 1; |
| 425 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 426 | #endif |
| 427 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 428 | #ifdef HAVE_MBCS |
| 429 | static OSVERSIONINFOEX winver; |
| 430 | #endif |
| 431 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 432 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 433 | |
| 434 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 435 | to keep things simple, we use a single bitmask, using the least 5 |
| 436 | bits from each unicode characters as the bit index. */ |
| 437 | |
| 438 | /* the linebreak mask is set up by Unicode_Init below */ |
| 439 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 440 | #if LONG_BIT >= 128 |
| 441 | #define BLOOM_WIDTH 128 |
| 442 | #elif LONG_BIT >= 64 |
| 443 | #define BLOOM_WIDTH 64 |
| 444 | #elif LONG_BIT >= 32 |
| 445 | #define BLOOM_WIDTH 32 |
| 446 | #else |
| 447 | #error "LONG_BIT is smaller than 32" |
| 448 | #endif |
| 449 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 450 | #define BLOOM_MASK unsigned long |
| 451 | |
| 452 | static BLOOM_MASK bloom_linebreak; |
| 453 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 454 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 455 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 456 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 457 | #define BLOOM_LINEBREAK(ch) \ |
| 458 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 459 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 461 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 462 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 463 | { |
| 464 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 465 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 466 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 467 | Py_ssize_t i; |
| 468 | |
| 469 | mask = 0; |
| 470 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 471 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 472 | |
| 473 | return mask; |
| 474 | } |
| 475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 476 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 477 | (BLOOM(mask, chr) \ |
| 478 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 480 | /* Compilation of templated routines */ |
| 481 | |
| 482 | #include "stringlib/asciilib.h" |
| 483 | #include "stringlib/fastsearch.h" |
| 484 | #include "stringlib/partition.h" |
| 485 | #include "stringlib/split.h" |
| 486 | #include "stringlib/count.h" |
| 487 | #include "stringlib/find.h" |
| 488 | #include "stringlib/find_max_char.h" |
| 489 | #include "stringlib/localeutil.h" |
| 490 | #include "stringlib/undef.h" |
| 491 | |
| 492 | #include "stringlib/ucs1lib.h" |
| 493 | #include "stringlib/fastsearch.h" |
| 494 | #include "stringlib/partition.h" |
| 495 | #include "stringlib/split.h" |
| 496 | #include "stringlib/count.h" |
| 497 | #include "stringlib/find.h" |
| 498 | #include "stringlib/find_max_char.h" |
| 499 | #include "stringlib/localeutil.h" |
| 500 | #include "stringlib/undef.h" |
| 501 | |
| 502 | #include "stringlib/ucs2lib.h" |
| 503 | #include "stringlib/fastsearch.h" |
| 504 | #include "stringlib/partition.h" |
| 505 | #include "stringlib/split.h" |
| 506 | #include "stringlib/count.h" |
| 507 | #include "stringlib/find.h" |
| 508 | #include "stringlib/find_max_char.h" |
| 509 | #include "stringlib/localeutil.h" |
| 510 | #include "stringlib/undef.h" |
| 511 | |
| 512 | #include "stringlib/ucs4lib.h" |
| 513 | #include "stringlib/fastsearch.h" |
| 514 | #include "stringlib/partition.h" |
| 515 | #include "stringlib/split.h" |
| 516 | #include "stringlib/count.h" |
| 517 | #include "stringlib/find.h" |
| 518 | #include "stringlib/find_max_char.h" |
| 519 | #include "stringlib/localeutil.h" |
| 520 | #include "stringlib/undef.h" |
| 521 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 522 | #include "stringlib/unicodedefs.h" |
| 523 | #include "stringlib/fastsearch.h" |
| 524 | #include "stringlib/count.h" |
| 525 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 526 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 527 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 528 | /* --- Unicode Object ----------------------------------------------------- */ |
| 529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 530 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 531 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 532 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 533 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 534 | Py_ssize_t size, Py_UCS4 ch, |
| 535 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 536 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 537 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 538 | |
| 539 | switch (kind) { |
| 540 | case PyUnicode_1BYTE_KIND: |
| 541 | { |
| 542 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 543 | if (ch1 == ch) |
| 544 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 545 | else |
| 546 | return -1; |
| 547 | } |
| 548 | case PyUnicode_2BYTE_KIND: |
| 549 | { |
| 550 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 551 | if (ch2 == ch) |
| 552 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 553 | else |
| 554 | return -1; |
| 555 | } |
| 556 | case PyUnicode_4BYTE_KIND: |
| 557 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 558 | default: |
| 559 | assert(0); |
| 560 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 561 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 564 | static PyObject* |
| 565 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 566 | { |
| 567 | Py_ssize_t char_size; |
| 568 | Py_ssize_t struct_size; |
| 569 | Py_ssize_t new_size; |
| 570 | int share_wstr; |
| 571 | |
| 572 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 573 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 574 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 575 | struct_size = sizeof(PyASCIIObject); |
| 576 | else |
| 577 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 578 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 579 | |
| 580 | _Py_DEC_REFTOTAL; |
| 581 | _Py_ForgetReference(unicode); |
| 582 | |
| 583 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 584 | PyErr_NoMemory(); |
| 585 | return NULL; |
| 586 | } |
| 587 | new_size = (struct_size + (length + 1) * char_size); |
| 588 | |
| 589 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 590 | if (unicode == NULL) { |
| 591 | PyObject_Del(unicode); |
| 592 | PyErr_NoMemory(); |
| 593 | return NULL; |
| 594 | } |
| 595 | _Py_NewReference(unicode); |
| 596 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 597 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 598 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 599 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 600 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 601 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 602 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 603 | length, 0); |
| 604 | return unicode; |
| 605 | } |
| 606 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 607 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 608 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 609 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 610 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 611 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 612 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 613 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 614 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 615 | |
| 616 | if (PyUnicode_IS_READY(unicode)) { |
| 617 | Py_ssize_t char_size; |
| 618 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 619 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 620 | void *data; |
| 621 | |
| 622 | data = _PyUnicode_DATA_ANY(unicode); |
| 623 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 624 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 625 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 626 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 627 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 628 | { |
| 629 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 630 | _PyUnicode_UTF8(unicode) = NULL; |
| 631 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 632 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 633 | |
| 634 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 635 | PyErr_NoMemory(); |
| 636 | return -1; |
| 637 | } |
| 638 | new_size = (length + 1) * char_size; |
| 639 | |
| 640 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 641 | if (data == NULL) { |
| 642 | PyErr_NoMemory(); |
| 643 | return -1; |
| 644 | } |
| 645 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 646 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 647 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 648 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 649 | } |
| 650 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 651 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 652 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 653 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 654 | _PyUnicode_LENGTH(unicode) = length; |
| 655 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 656 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 657 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 658 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 659 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 660 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 661 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 662 | |
| 663 | /* check for integer overflow */ |
| 664 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 665 | PyErr_NoMemory(); |
| 666 | return -1; |
| 667 | } |
| 668 | wstr = _PyUnicode_WSTR(unicode); |
| 669 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 670 | if (!wstr) { |
| 671 | PyErr_NoMemory(); |
| 672 | return -1; |
| 673 | } |
| 674 | _PyUnicode_WSTR(unicode) = wstr; |
| 675 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 676 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 677 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 681 | static PyObject* |
| 682 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 683 | { |
| 684 | Py_ssize_t copy_length; |
| 685 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 686 | PyObject *copy; |
| 687 | assert(PyUnicode_IS_READY(unicode)); |
| 688 | |
| 689 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 690 | if (copy == NULL) |
| 691 | return NULL; |
| 692 | |
| 693 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 694 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 695 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 696 | } |
| 697 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 698 | PyObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 699 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 700 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 701 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 702 | if (w == NULL) |
| 703 | return NULL; |
| 704 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 705 | copy_length = Py_MIN(copy_length, length); |
| 706 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 707 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 708 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | /* 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] | 713 | Ux0000 terminated; some code (e.g. new_identifier) |
| 714 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 715 | |
| 716 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 717 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 718 | |
| 719 | */ |
| 720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 721 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 722 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 723 | #endif |
| 724 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 725 | static PyUnicodeObject * |
| 726 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 727 | { |
| 728 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 729 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 730 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 731 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 732 | if (length == 0 && unicode_empty != NULL) { |
| 733 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 734 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 737 | /* Ensure we won't overflow the size. */ |
| 738 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 739 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 740 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 741 | if (length < 0) { |
| 742 | PyErr_SetString(PyExc_SystemError, |
| 743 | "Negative size passed to _PyUnicode_New"); |
| 744 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 747 | #ifdef Py_DEBUG |
| 748 | ++unicode_old_new_calls; |
| 749 | #endif |
| 750 | |
| 751 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 752 | if (unicode == NULL) |
| 753 | return NULL; |
| 754 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 755 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 756 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 757 | PyErr_NoMemory(); |
| 758 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 760 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 761 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 762 | * the caller fails before initializing str -- unicode_resize() |
| 763 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 764 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 765 | * We don't want unicode_resize to read uninitialized memory in |
| 766 | * that case. |
| 767 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 768 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 769 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 770 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 771 | _PyUnicode_HASH(unicode) = -1; |
| 772 | _PyUnicode_STATE(unicode).interned = 0; |
| 773 | _PyUnicode_STATE(unicode).kind = 0; |
| 774 | _PyUnicode_STATE(unicode).compact = 0; |
| 775 | _PyUnicode_STATE(unicode).ready = 0; |
| 776 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 777 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 778 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 779 | _PyUnicode_UTF8(unicode) = NULL; |
| 780 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 781 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 782 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 783 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 784 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 785 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 786 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 787 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 788 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 789 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 792 | static const char* |
| 793 | unicode_kind_name(PyObject *unicode) |
| 794 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 795 | /* don't check consistency: unicode_kind_name() is called from |
| 796 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 797 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 798 | { |
| 799 | if (!PyUnicode_IS_READY(unicode)) |
| 800 | return "wstr"; |
| 801 | switch(PyUnicode_KIND(unicode)) |
| 802 | { |
| 803 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 804 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 805 | return "legacy ascii"; |
| 806 | else |
| 807 | return "legacy latin1"; |
| 808 | case PyUnicode_2BYTE_KIND: |
| 809 | return "legacy UCS2"; |
| 810 | case PyUnicode_4BYTE_KIND: |
| 811 | return "legacy UCS4"; |
| 812 | default: |
| 813 | return "<legacy invalid kind>"; |
| 814 | } |
| 815 | } |
| 816 | assert(PyUnicode_IS_READY(unicode)); |
| 817 | switch(PyUnicode_KIND(unicode)) |
| 818 | { |
| 819 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 820 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 821 | return "ascii"; |
| 822 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 823 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 824 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 825 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 826 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 827 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 828 | default: |
| 829 | return "<invalid compact kind>"; |
| 830 | } |
| 831 | } |
| 832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 833 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 834 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 835 | |
| 836 | /* Functions wrapping macros for use in debugger */ |
| 837 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 838 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void *_PyUnicode_compact_data(void *unicode) { |
| 842 | return _PyUnicode_COMPACT_DATA(unicode); |
| 843 | } |
| 844 | void *_PyUnicode_data(void *unicode){ |
| 845 | printf("obj %p\n", unicode); |
| 846 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 847 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 848 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 849 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 850 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 851 | return PyUnicode_DATA(unicode); |
| 852 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 853 | |
| 854 | void |
| 855 | _PyUnicode_Dump(PyObject *op) |
| 856 | { |
| 857 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 858 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 859 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 860 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 861 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 862 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 863 | { |
| 864 | if (ascii->state.ascii) |
| 865 | data = (ascii + 1); |
| 866 | else |
| 867 | data = (compact + 1); |
| 868 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 869 | else |
| 870 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 871 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 872 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 873 | if (ascii->wstr == data) |
| 874 | printf("shared "); |
| 875 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 876 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 877 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 878 | printf(" (%zu), ", compact->wstr_length); |
| 879 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 880 | printf("shared "); |
| 881 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 882 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 883 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 884 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 885 | #endif |
| 886 | |
| 887 | PyObject * |
| 888 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 889 | { |
| 890 | PyObject *obj; |
| 891 | PyCompactUnicodeObject *unicode; |
| 892 | void *data; |
| 893 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 894 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 895 | Py_ssize_t char_size; |
| 896 | Py_ssize_t struct_size; |
| 897 | |
| 898 | /* Optimization for empty strings */ |
| 899 | if (size == 0 && unicode_empty != NULL) { |
| 900 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 901 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | #ifdef Py_DEBUG |
| 905 | ++unicode_new_new_calls; |
| 906 | #endif |
| 907 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 908 | is_ascii = 0; |
| 909 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 910 | struct_size = sizeof(PyCompactUnicodeObject); |
| 911 | if (maxchar < 128) { |
| 912 | kind_state = PyUnicode_1BYTE_KIND; |
| 913 | char_size = 1; |
| 914 | is_ascii = 1; |
| 915 | struct_size = sizeof(PyASCIIObject); |
| 916 | } |
| 917 | else if (maxchar < 256) { |
| 918 | kind_state = PyUnicode_1BYTE_KIND; |
| 919 | char_size = 1; |
| 920 | } |
| 921 | else if (maxchar < 65536) { |
| 922 | kind_state = PyUnicode_2BYTE_KIND; |
| 923 | char_size = 2; |
| 924 | if (sizeof(wchar_t) == 2) |
| 925 | is_sharing = 1; |
| 926 | } |
| 927 | else { |
| 928 | kind_state = PyUnicode_4BYTE_KIND; |
| 929 | char_size = 4; |
| 930 | if (sizeof(wchar_t) == 4) |
| 931 | is_sharing = 1; |
| 932 | } |
| 933 | |
| 934 | /* Ensure we won't overflow the size. */ |
| 935 | if (size < 0) { |
| 936 | PyErr_SetString(PyExc_SystemError, |
| 937 | "Negative size passed to PyUnicode_New"); |
| 938 | return NULL; |
| 939 | } |
| 940 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 941 | return PyErr_NoMemory(); |
| 942 | |
| 943 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 944 | * PyObject_New() so we are able to allocate space for the object and |
| 945 | * it's data buffer. |
| 946 | */ |
| 947 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 948 | if (obj == NULL) |
| 949 | return PyErr_NoMemory(); |
| 950 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 951 | if (obj == NULL) |
| 952 | return NULL; |
| 953 | |
| 954 | unicode = (PyCompactUnicodeObject *)obj; |
| 955 | if (is_ascii) |
| 956 | data = ((PyASCIIObject*)obj) + 1; |
| 957 | else |
| 958 | data = unicode + 1; |
| 959 | _PyUnicode_LENGTH(unicode) = size; |
| 960 | _PyUnicode_HASH(unicode) = -1; |
| 961 | _PyUnicode_STATE(unicode).interned = 0; |
| 962 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 963 | _PyUnicode_STATE(unicode).compact = 1; |
| 964 | _PyUnicode_STATE(unicode).ready = 1; |
| 965 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 966 | if (is_ascii) { |
| 967 | ((char*)data)[size] = 0; |
| 968 | _PyUnicode_WSTR(unicode) = NULL; |
| 969 | } |
| 970 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 971 | ((char*)data)[size] = 0; |
| 972 | _PyUnicode_WSTR(unicode) = NULL; |
| 973 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 975 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 976 | } |
| 977 | else { |
| 978 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 979 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 980 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 981 | ((Py_UCS2*)data)[size] = 0; |
| 982 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 983 | ((Py_UCS4*)data)[size] = 0; |
| 984 | if (is_sharing) { |
| 985 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 986 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 987 | } |
| 988 | else { |
| 989 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 990 | _PyUnicode_WSTR(unicode) = NULL; |
| 991 | } |
| 992 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 993 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 994 | return obj; |
| 995 | } |
| 996 | |
| 997 | #if SIZEOF_WCHAR_T == 2 |
| 998 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 999 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1000 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1001 | |
| 1002 | This function assumes that unicode can hold one more code point than wstr |
| 1003 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1004 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1005 | 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] | 1006 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1007 | { |
| 1008 | const wchar_t *iter; |
| 1009 | Py_UCS4 *ucs4_out; |
| 1010 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1011 | assert(unicode != NULL); |
| 1012 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1013 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1014 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1015 | |
| 1016 | for (iter = begin; iter < end; ) { |
| 1017 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1018 | _PyUnicode_GET_LENGTH(unicode))); |
| 1019 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1020 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1021 | { |
| 1022 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 1023 | iter += 2; |
| 1024 | } |
| 1025 | else { |
| 1026 | *ucs4_out++ = *iter; |
| 1027 | iter++; |
| 1028 | } |
| 1029 | } |
| 1030 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1031 | _PyUnicode_GET_LENGTH(unicode))); |
| 1032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1033 | } |
| 1034 | #endif |
| 1035 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1036 | static int |
| 1037 | _PyUnicode_Dirty(PyObject *unicode) |
| 1038 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1039 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1040 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1041 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1042 | "Cannot modify a string having more than 1 reference"); |
| 1043 | return -1; |
| 1044 | } |
| 1045 | _PyUnicode_DIRTY(unicode); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1049 | static int |
| 1050 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1051 | PyObject *from, Py_ssize_t from_start, |
| 1052 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1054 | unsigned int from_kind, to_kind; |
| 1055 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1056 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1058 | assert(PyUnicode_Check(from)); |
| 1059 | assert(PyUnicode_Check(to)); |
| 1060 | assert(PyUnicode_IS_READY(from)); |
| 1061 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1062 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1063 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1064 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1065 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1066 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1067 | if (how_many == 0) |
| 1068 | return 0; |
| 1069 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1071 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1073 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1075 | #ifdef Py_DEBUG |
| 1076 | if (!check_maxchar |
| 1077 | && (from_kind > to_kind |
| 1078 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1079 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1080 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1081 | Py_UCS4 ch; |
| 1082 | Py_ssize_t i; |
| 1083 | for (i=0; i < how_many; i++) { |
| 1084 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1085 | assert(ch <= to_maxchar); |
| 1086 | } |
| 1087 | } |
| 1088 | #endif |
| 1089 | fast = (from_kind == to_kind); |
| 1090 | if (check_maxchar |
| 1091 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1092 | { |
| 1093 | /* deny latin1 => ascii */ |
| 1094 | fast = 0; |
| 1095 | } |
| 1096 | |
| 1097 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1098 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1099 | (char*)from_data + from_kind * from_start, |
| 1100 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1102 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1103 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1104 | { |
| 1105 | _PyUnicode_CONVERT_BYTES( |
| 1106 | Py_UCS1, Py_UCS2, |
| 1107 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1108 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1109 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1110 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1111 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1112 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1113 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1114 | { |
| 1115 | _PyUnicode_CONVERT_BYTES( |
| 1116 | Py_UCS1, Py_UCS4, |
| 1117 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1118 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1119 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1120 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1121 | } |
| 1122 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1123 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1124 | { |
| 1125 | _PyUnicode_CONVERT_BYTES( |
| 1126 | Py_UCS2, Py_UCS4, |
| 1127 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1128 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1129 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1130 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1131 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1132 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1133 | /* check if max_char(from substring) <= max_char(to) */ |
| 1134 | if (from_kind > to_kind |
| 1135 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1136 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1137 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1138 | /* slow path to check for character overflow */ |
| 1139 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1140 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1141 | Py_ssize_t i; |
| 1142 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1143 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1144 | for (i=0; i < how_many; i++) { |
| 1145 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1146 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1147 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1148 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1149 | #else |
| 1150 | if (!check_maxchar) { |
| 1151 | for (i=0; i < how_many; i++) { |
| 1152 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1153 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1154 | } |
| 1155 | } |
| 1156 | else { |
| 1157 | for (i=0; i < how_many; i++) { |
| 1158 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1159 | if (ch > to_maxchar) |
| 1160 | return 1; |
| 1161 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1162 | } |
| 1163 | } |
| 1164 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1165 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1166 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1167 | assert(0 && "inconsistent state"); |
| 1168 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1169 | } |
| 1170 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static void |
| 1175 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1176 | PyObject *from, Py_ssize_t from_start, |
| 1177 | Py_ssize_t how_many) |
| 1178 | { |
| 1179 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1180 | } |
| 1181 | |
| 1182 | Py_ssize_t |
| 1183 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1184 | PyObject *from, Py_ssize_t from_start, |
| 1185 | Py_ssize_t how_many) |
| 1186 | { |
| 1187 | int err; |
| 1188 | |
| 1189 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1190 | PyErr_BadInternalCall(); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | if (PyUnicode_READY(from)) |
| 1195 | return -1; |
| 1196 | if (PyUnicode_READY(to)) |
| 1197 | return -1; |
| 1198 | |
| 1199 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1200 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1201 | PyErr_Format(PyExc_SystemError, |
| 1202 | "Cannot write %zi characters at %zi " |
| 1203 | "in a string of %zi characters", |
| 1204 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1205 | return -1; |
| 1206 | } |
| 1207 | |
| 1208 | if (how_many == 0) |
| 1209 | return 0; |
| 1210 | |
| 1211 | if (_PyUnicode_Dirty(to)) |
| 1212 | return -1; |
| 1213 | |
| 1214 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1215 | if (err) { |
| 1216 | PyErr_Format(PyExc_SystemError, |
| 1217 | "Cannot copy %s characters " |
| 1218 | "into a string of %s characters", |
| 1219 | unicode_kind_name(from), |
| 1220 | unicode_kind_name(to)); |
| 1221 | return -1; |
| 1222 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1223 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1226 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1227 | correct string length can be computed before converting a string to UCS4. |
| 1228 | This function counts single surrogates as a character and not as a pair. |
| 1229 | |
| 1230 | Return 0 on success, or -1 on error. */ |
| 1231 | static int |
| 1232 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1233 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1234 | { |
| 1235 | const wchar_t *iter; |
| 1236 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1237 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1238 | *num_surrogates = 0; |
| 1239 | *maxchar = 0; |
| 1240 | |
| 1241 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1242 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1243 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1244 | #if SIZEOF_WCHAR_T != 2 |
| 1245 | if (*maxchar >= 0x10000) |
| 1246 | return 0; |
| 1247 | #endif |
| 1248 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1249 | #if SIZEOF_WCHAR_T == 2 |
| 1250 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1251 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1252 | { |
| 1253 | Py_UCS4 surrogate_val; |
| 1254 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1255 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1256 | ++(*num_surrogates); |
| 1257 | if (surrogate_val > *maxchar) |
| 1258 | *maxchar = surrogate_val; |
| 1259 | iter += 2; |
| 1260 | } |
| 1261 | else |
| 1262 | iter++; |
| 1263 | #else |
| 1264 | iter++; |
| 1265 | #endif |
| 1266 | } |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1271 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1272 | #endif |
| 1273 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1274 | static int |
| 1275 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1276 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1277 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1278 | wchar_t *end; |
| 1279 | Py_UCS4 maxchar = 0; |
| 1280 | Py_ssize_t num_surrogates; |
| 1281 | #if SIZEOF_WCHAR_T == 2 |
| 1282 | Py_ssize_t length_wo_surrogates; |
| 1283 | #endif |
| 1284 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1285 | assert(p_obj != NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1286 | unicode = *p_obj; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1287 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1288 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1289 | strings were created using _PyObject_New() and where no canonical |
| 1290 | representation (the str field) has been set yet aka strings |
| 1291 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1292 | assert(_PyUnicode_CHECK(unicode)); |
| 1293 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1294 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1295 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1296 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1297 | /* Actually, it should neither be interned nor be anything else: */ |
| 1298 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1299 | |
| 1300 | #ifdef Py_DEBUG |
| 1301 | ++unicode_ready_calls; |
| 1302 | #endif |
| 1303 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1304 | #ifdef Py_DEBUG |
| 1305 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1306 | #else |
| 1307 | if (replace && Py_REFCNT(unicode) != 1) |
| 1308 | replace = 0; |
| 1309 | #endif |
| 1310 | if (replace) { |
| 1311 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1312 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1313 | /* Optimization for empty strings */ |
| 1314 | if (len == 0) { |
| 1315 | Py_INCREF(unicode_empty); |
| 1316 | Py_DECREF(*p_obj); |
| 1317 | *p_obj = unicode_empty; |
| 1318 | return 0; |
| 1319 | } |
| 1320 | if (len == 1 && wstr[0] < 256) { |
| 1321 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1322 | if (latin1_char == NULL) |
| 1323 | return -1; |
| 1324 | Py_DECREF(*p_obj); |
| 1325 | *p_obj = latin1_char; |
| 1326 | return 0; |
| 1327 | } |
| 1328 | } |
| 1329 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1330 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1331 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1332 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1333 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | |
| 1335 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1336 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1337 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | PyErr_NoMemory(); |
| 1339 | return -1; |
| 1340 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1341 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | _PyUnicode_WSTR(unicode), end, |
| 1343 | PyUnicode_1BYTE_DATA(unicode)); |
| 1344 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1345 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1346 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1347 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1348 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1349 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1350 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1351 | } |
| 1352 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1353 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1354 | _PyUnicode_UTF8(unicode) = NULL; |
| 1355 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1356 | } |
| 1357 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1358 | _PyUnicode_WSTR(unicode) = NULL; |
| 1359 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1360 | } |
| 1361 | /* In this case we might have to convert down from 4-byte native |
| 1362 | wchar_t to 2-byte unicode. */ |
| 1363 | else if (maxchar < 65536) { |
| 1364 | assert(num_surrogates == 0 && |
| 1365 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1366 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1367 | #if SIZEOF_WCHAR_T == 2 |
| 1368 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1369 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1370 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1371 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1372 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1373 | _PyUnicode_UTF8(unicode) = NULL; |
| 1374 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1375 | #else |
| 1376 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1377 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1378 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1379 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1380 | PyErr_NoMemory(); |
| 1381 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1382 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1383 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1384 | _PyUnicode_WSTR(unicode), end, |
| 1385 | PyUnicode_2BYTE_DATA(unicode)); |
| 1386 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1387 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1388 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1389 | _PyUnicode_UTF8(unicode) = NULL; |
| 1390 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1391 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1392 | _PyUnicode_WSTR(unicode) = NULL; |
| 1393 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1394 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | } |
| 1396 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1397 | else { |
| 1398 | #if SIZEOF_WCHAR_T == 2 |
| 1399 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1400 | new normalized 4-byte version. */ |
| 1401 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1402 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1403 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | PyErr_NoMemory(); |
| 1405 | return -1; |
| 1406 | } |
| 1407 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1408 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1409 | _PyUnicode_UTF8(unicode) = NULL; |
| 1410 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1411 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1412 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1413 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1415 | _PyUnicode_WSTR(unicode) = NULL; |
| 1416 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1417 | #else |
| 1418 | assert(num_surrogates == 0); |
| 1419 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1420 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1421 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1422 | _PyUnicode_UTF8(unicode) = NULL; |
| 1423 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1424 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1425 | #endif |
| 1426 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1427 | } |
| 1428 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1429 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1430 | return 0; |
| 1431 | } |
| 1432 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1433 | int |
| 1434 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1435 | { |
| 1436 | return unicode_ready(op, 1); |
| 1437 | } |
| 1438 | |
| 1439 | int |
| 1440 | _PyUnicode_Ready(PyObject *op) |
| 1441 | { |
| 1442 | return unicode_ready(&op, 0); |
| 1443 | } |
| 1444 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1445 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1446 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1447 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1448 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1449 | case SSTATE_NOT_INTERNED: |
| 1450 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1451 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1452 | case SSTATE_INTERNED_MORTAL: |
| 1453 | /* revive dead object temporarily for DelItem */ |
| 1454 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1455 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1456 | Py_FatalError( |
| 1457 | "deletion of interned string failed"); |
| 1458 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1460 | case SSTATE_INTERNED_IMMORTAL: |
| 1461 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1462 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1463 | default: |
| 1464 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1467 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1469 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1470 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | |
| 1472 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1473 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1474 | } |
| 1475 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1476 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1477 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1478 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1482 | #ifdef Py_DEBUG |
| 1483 | static int |
| 1484 | unicode_is_singleton(PyObject *unicode) |
| 1485 | { |
| 1486 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1487 | if (unicode == unicode_empty) |
| 1488 | return 1; |
| 1489 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1490 | { |
| 1491 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1492 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1493 | return 1; |
| 1494 | } |
| 1495 | return 0; |
| 1496 | } |
| 1497 | #endif |
| 1498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1499 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1500 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1501 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1502 | if (Py_REFCNT(unicode) != 1) |
| 1503 | return 0; |
| 1504 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1505 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1506 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1507 | /* singleton refcount is greater than 1 */ |
| 1508 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1509 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1510 | return 1; |
| 1511 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1512 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1513 | static int |
| 1514 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1515 | { |
| 1516 | PyObject *unicode; |
| 1517 | Py_ssize_t old_length; |
| 1518 | |
| 1519 | assert(p_unicode != NULL); |
| 1520 | unicode = *p_unicode; |
| 1521 | |
| 1522 | assert(unicode != NULL); |
| 1523 | assert(PyUnicode_Check(unicode)); |
| 1524 | assert(0 <= length); |
| 1525 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1526 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1527 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1528 | else |
| 1529 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1530 | if (old_length == length) |
| 1531 | return 0; |
| 1532 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1533 | if (length == 0) { |
| 1534 | Py_DECREF(*p_unicode); |
| 1535 | *p_unicode = unicode_empty; |
| 1536 | Py_INCREF(*p_unicode); |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1540 | if (!unicode_resizable(unicode)) { |
| 1541 | PyObject *copy = resize_copy(unicode, length); |
| 1542 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1543 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1544 | Py_DECREF(*p_unicode); |
| 1545 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1546 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1549 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1550 | *p_unicode = resize_compact(unicode, length); |
| 1551 | if (*p_unicode == NULL) |
| 1552 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1553 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1554 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1555 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1556 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1559 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1560 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1561 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1562 | PyObject *unicode; |
| 1563 | if (p_unicode == NULL) { |
| 1564 | PyErr_BadInternalCall(); |
| 1565 | return -1; |
| 1566 | } |
| 1567 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1568 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1569 | { |
| 1570 | PyErr_BadInternalCall(); |
| 1571 | return -1; |
| 1572 | } |
| 1573 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1574 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1575 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1576 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1577 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1578 | { |
| 1579 | PyObject *result; |
| 1580 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1581 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1582 | return 0; |
| 1583 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1584 | maxchar); |
| 1585 | if (result == NULL) |
| 1586 | return -1; |
| 1587 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1588 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1589 | Py_DECREF(*p_unicode); |
| 1590 | *p_unicode = result; |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | static int |
| 1595 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1596 | Py_UCS4 ch) |
| 1597 | { |
| 1598 | if (unicode_widen(p_unicode, ch) < 0) |
| 1599 | return -1; |
| 1600 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1601 | PyUnicode_DATA(*p_unicode), |
| 1602 | (*pos)++, ch); |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1606 | static PyObject* |
| 1607 | get_latin1_char(unsigned char ch) |
| 1608 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1609 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1610 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1611 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1612 | if (!unicode) |
| 1613 | return NULL; |
| 1614 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1615 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1616 | unicode_latin1[ch] = unicode; |
| 1617 | } |
| 1618 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1619 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1622 | PyObject * |
| 1623 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1624 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1625 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1626 | Py_UCS4 maxchar = 0; |
| 1627 | Py_ssize_t num_surrogates; |
| 1628 | |
| 1629 | if (u == NULL) |
| 1630 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1631 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1632 | /* If the Unicode data is known at construction time, we can apply |
| 1633 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1635 | /* Optimization for empty strings */ |
| 1636 | if (size == 0 && unicode_empty != NULL) { |
| 1637 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1638 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1639 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1641 | /* Single character Unicode objects in the Latin-1 range are |
| 1642 | shared when using this constructor */ |
| 1643 | if (size == 1 && *u < 256) |
| 1644 | return get_latin1_char((unsigned char)*u); |
| 1645 | |
| 1646 | /* If not empty and not single character, copy the Unicode data |
| 1647 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1648 | if (find_maxchar_surrogates(u, u + size, |
| 1649 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1650 | return NULL; |
| 1651 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1652 | unicode = PyUnicode_New(size - num_surrogates, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1653 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1654 | if (!unicode) |
| 1655 | return NULL; |
| 1656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1657 | switch (PyUnicode_KIND(unicode)) { |
| 1658 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1659 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1660 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1661 | break; |
| 1662 | case PyUnicode_2BYTE_KIND: |
| 1663 | #if Py_UNICODE_SIZE == 2 |
| 1664 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1665 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1666 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1668 | #endif |
| 1669 | break; |
| 1670 | case PyUnicode_4BYTE_KIND: |
| 1671 | #if SIZEOF_WCHAR_T == 2 |
| 1672 | /* This is the only case which has to process surrogates, thus |
| 1673 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1674 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1675 | #else |
| 1676 | assert(num_surrogates == 0); |
| 1677 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1678 | #endif |
| 1679 | break; |
| 1680 | default: |
| 1681 | assert(0 && "Impossible state"); |
| 1682 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1683 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1684 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1685 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1688 | PyObject * |
| 1689 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1690 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1691 | if (size < 0) { |
| 1692 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1693 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1694 | return NULL; |
| 1695 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1696 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1697 | /* 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] | 1698 | some optimizations which share commonly used objects. |
| 1699 | Also, this means the input must be UTF-8, so fall back to the |
| 1700 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1701 | if (u != NULL) { |
| 1702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1703 | /* Optimization for empty strings */ |
| 1704 | if (size == 0 && unicode_empty != NULL) { |
| 1705 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1706 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1707 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1708 | |
| 1709 | /* Single characters are shared when using this constructor. |
| 1710 | Restrict to ASCII, since the input must be UTF-8. */ |
Victor Stinner | 9faa384 | 2011-10-23 20:06:00 +0200 | [diff] [blame] | 1711 | if (size == 1 && (unsigned char)*u < 128) |
| 1712 | return get_latin1_char((unsigned char)*u); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1713 | |
| 1714 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1717 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1720 | PyObject * |
| 1721 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1722 | { |
| 1723 | size_t size = strlen(u); |
| 1724 | if (size > PY_SSIZE_T_MAX) { |
| 1725 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1726 | return NULL; |
| 1727 | } |
| 1728 | |
| 1729 | return PyUnicode_FromStringAndSize(u, size); |
| 1730 | } |
| 1731 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1732 | PyObject * |
| 1733 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1734 | { |
| 1735 | if (!id->object) { |
| 1736 | id->object = PyUnicode_FromString(id->string); |
| 1737 | if (!id->object) |
| 1738 | return NULL; |
| 1739 | PyUnicode_InternInPlace(&id->object); |
| 1740 | assert(!id->next); |
| 1741 | id->next = static_strings; |
| 1742 | static_strings = id; |
| 1743 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1744 | return id->object; |
| 1745 | } |
| 1746 | |
| 1747 | void |
| 1748 | _PyUnicode_ClearStaticStrings() |
| 1749 | { |
| 1750 | _Py_Identifier *i; |
| 1751 | for (i = static_strings; i; i = i->next) { |
| 1752 | Py_DECREF(i->object); |
| 1753 | i->object = NULL; |
| 1754 | i->next = NULL; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1758 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1759 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1760 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1761 | PyObject *res; |
| 1762 | #ifdef Py_DEBUG |
| 1763 | const unsigned char *p; |
| 1764 | const unsigned char *end = s + size; |
| 1765 | for (p=s; p < end; p++) { |
| 1766 | assert(*p < 128); |
| 1767 | } |
| 1768 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1769 | if (size == 1) |
| 1770 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1771 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1772 | if (!res) |
| 1773 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1774 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1775 | return res; |
| 1776 | } |
| 1777 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1778 | static Py_UCS4 |
| 1779 | kind_maxchar_limit(unsigned int kind) |
| 1780 | { |
| 1781 | switch(kind) { |
| 1782 | case PyUnicode_1BYTE_KIND: |
| 1783 | return 0x80; |
| 1784 | case PyUnicode_2BYTE_KIND: |
| 1785 | return 0x100; |
| 1786 | case PyUnicode_4BYTE_KIND: |
| 1787 | return 0x10000; |
| 1788 | default: |
| 1789 | assert(0 && "invalid kind"); |
| 1790 | return 0x10ffff; |
| 1791 | } |
| 1792 | } |
| 1793 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1794 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1795 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1796 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1797 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1798 | unsigned char max_char = 127; |
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) |
| 1802 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1803 | max_char = ucs1lib_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; |
| 1807 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1808 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1809 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1812 | static PyObject* |
| 1813 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1814 | { |
| 1815 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1816 | Py_UCS2 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1817 | |
| 1818 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1819 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1820 | return get_latin1_char((unsigned char)u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1821 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1822 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1823 | if (!res) |
| 1824 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1825 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1826 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1827 | else { |
| 1828 | _PyUnicode_CONVERT_BYTES( |
| 1829 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1830 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1831 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1832 | return res; |
| 1833 | } |
| 1834 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1835 | static PyObject* |
| 1836 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1837 | { |
| 1838 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1839 | Py_UCS4 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1840 | |
| 1841 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1842 | if (size == 1 && u[0] < 256) |
| 1843 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1844 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1845 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1846 | if (!res) |
| 1847 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1848 | if (max_char < 256) |
| 1849 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1850 | PyUnicode_1BYTE_DATA(res)); |
| 1851 | else if (max_char < 0x10000) |
| 1852 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1853 | PyUnicode_2BYTE_DATA(res)); |
| 1854 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1855 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1856 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1857 | return res; |
| 1858 | } |
| 1859 | |
| 1860 | PyObject* |
| 1861 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1862 | { |
| 1863 | switch(kind) { |
| 1864 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1865 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1866 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1867 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1868 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1869 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1870 | default: |
| 1871 | assert(0 && "invalid kind"); |
| 1872 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1873 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1874 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1875 | } |
| 1876 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1877 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1878 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1879 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1880 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1881 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1882 | { |
| 1883 | PyObject *unicode, *copy; |
| 1884 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1885 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1886 | unsigned int kind; |
| 1887 | |
| 1888 | assert(p_unicode != NULL); |
| 1889 | unicode = *p_unicode; |
| 1890 | assert(PyUnicode_IS_READY(unicode)); |
| 1891 | if (PyUnicode_IS_ASCII(unicode)) |
| 1892 | return; |
| 1893 | |
| 1894 | len = PyUnicode_GET_LENGTH(unicode); |
| 1895 | kind = PyUnicode_KIND(unicode); |
| 1896 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1897 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1898 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1899 | if (max_char >= 128) |
| 1900 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1901 | } |
| 1902 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1903 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1904 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1905 | if (max_char >= 256) |
| 1906 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1907 | } |
| 1908 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1909 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1910 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1911 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1912 | if (max_char >= 0x10000) |
| 1913 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1914 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1915 | copy = PyUnicode_New(len, max_char); |
| 1916 | copy_characters(copy, 0, unicode, 0, len); |
| 1917 | Py_DECREF(unicode); |
| 1918 | *p_unicode = copy; |
| 1919 | } |
| 1920 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1921 | PyObject* |
| 1922 | PyUnicode_Copy(PyObject *unicode) |
| 1923 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1924 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1925 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1926 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1927 | if (!PyUnicode_Check(unicode)) { |
| 1928 | PyErr_BadInternalCall(); |
| 1929 | return NULL; |
| 1930 | } |
| 1931 | if (PyUnicode_READY(unicode)) |
| 1932 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1933 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1934 | length = PyUnicode_GET_LENGTH(unicode); |
| 1935 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1936 | if (!copy) |
| 1937 | return NULL; |
| 1938 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1939 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1940 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 1941 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1942 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1943 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1947 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1948 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1949 | |
| 1950 | void* |
| 1951 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1952 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1953 | Py_ssize_t len; |
| 1954 | void *result; |
| 1955 | unsigned int skind; |
| 1956 | |
| 1957 | if (PyUnicode_READY(s)) |
| 1958 | return NULL; |
| 1959 | |
| 1960 | len = PyUnicode_GET_LENGTH(s); |
| 1961 | skind = PyUnicode_KIND(s); |
| 1962 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1963 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1964 | return NULL; |
| 1965 | } |
| 1966 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1967 | case PyUnicode_2BYTE_KIND: |
| 1968 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1969 | if (!result) |
| 1970 | return PyErr_NoMemory(); |
| 1971 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1972 | _PyUnicode_CONVERT_BYTES( |
| 1973 | Py_UCS1, Py_UCS2, |
| 1974 | PyUnicode_1BYTE_DATA(s), |
| 1975 | PyUnicode_1BYTE_DATA(s) + len, |
| 1976 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1978 | case PyUnicode_4BYTE_KIND: |
| 1979 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1980 | if (!result) |
| 1981 | return PyErr_NoMemory(); |
| 1982 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1983 | _PyUnicode_CONVERT_BYTES( |
| 1984 | Py_UCS2, Py_UCS4, |
| 1985 | PyUnicode_2BYTE_DATA(s), |
| 1986 | PyUnicode_2BYTE_DATA(s) + len, |
| 1987 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1988 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1989 | else { |
| 1990 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1991 | _PyUnicode_CONVERT_BYTES( |
| 1992 | Py_UCS1, Py_UCS4, |
| 1993 | PyUnicode_1BYTE_DATA(s), |
| 1994 | PyUnicode_1BYTE_DATA(s) + len, |
| 1995 | result); |
| 1996 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1998 | default: |
| 1999 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2000 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2001 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2002 | return NULL; |
| 2003 | } |
| 2004 | |
| 2005 | static Py_UCS4* |
| 2006 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2007 | int copy_null) |
| 2008 | { |
| 2009 | int kind; |
| 2010 | void *data; |
| 2011 | Py_ssize_t len, targetlen; |
| 2012 | if (PyUnicode_READY(string) == -1) |
| 2013 | return NULL; |
| 2014 | kind = PyUnicode_KIND(string); |
| 2015 | data = PyUnicode_DATA(string); |
| 2016 | len = PyUnicode_GET_LENGTH(string); |
| 2017 | targetlen = len; |
| 2018 | if (copy_null) |
| 2019 | targetlen++; |
| 2020 | if (!target) { |
| 2021 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2022 | PyErr_NoMemory(); |
| 2023 | return NULL; |
| 2024 | } |
| 2025 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2026 | if (!target) { |
| 2027 | PyErr_NoMemory(); |
| 2028 | return NULL; |
| 2029 | } |
| 2030 | } |
| 2031 | else { |
| 2032 | if (targetsize < targetlen) { |
| 2033 | PyErr_Format(PyExc_SystemError, |
| 2034 | "string is longer than the buffer"); |
| 2035 | if (copy_null && 0 < targetsize) |
| 2036 | target[0] = 0; |
| 2037 | return NULL; |
| 2038 | } |
| 2039 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2040 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2041 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2042 | _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] | 2043 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2044 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2045 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2046 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2047 | } |
| 2048 | else { |
| 2049 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2050 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2051 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2052 | if (copy_null) |
| 2053 | target[len] = 0; |
| 2054 | return target; |
| 2055 | } |
| 2056 | |
| 2057 | Py_UCS4* |
| 2058 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2059 | int copy_null) |
| 2060 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2061 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2062 | PyErr_BadInternalCall(); |
| 2063 | return NULL; |
| 2064 | } |
| 2065 | return as_ucs4(string, target, targetsize, copy_null); |
| 2066 | } |
| 2067 | |
| 2068 | Py_UCS4* |
| 2069 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2070 | { |
| 2071 | return as_ucs4(string, NULL, 0, 1); |
| 2072 | } |
| 2073 | |
| 2074 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2075 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2076 | PyObject * |
| 2077 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2078 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2079 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2080 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2081 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2082 | PyErr_BadInternalCall(); |
| 2083 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2086 | if (size == -1) { |
| 2087 | size = wcslen(w); |
| 2088 | } |
| 2089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2090 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2093 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2094 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2095 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2096 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2097 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2098 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2099 | *fmt++ = '%'; |
| 2100 | if (width) { |
| 2101 | if (zeropad) |
| 2102 | *fmt++ = '0'; |
| 2103 | fmt += sprintf(fmt, "%d", width); |
| 2104 | } |
| 2105 | if (precision) |
| 2106 | fmt += sprintf(fmt, ".%d", precision); |
| 2107 | if (longflag) |
| 2108 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2109 | else if (longlongflag) { |
| 2110 | /* longlongflag should only ever be nonzero on machines with |
| 2111 | HAVE_LONG_LONG defined */ |
| 2112 | #ifdef HAVE_LONG_LONG |
| 2113 | char *f = PY_FORMAT_LONG_LONG; |
| 2114 | while (*f) |
| 2115 | *fmt++ = *f++; |
| 2116 | #else |
| 2117 | /* we shouldn't ever get here */ |
| 2118 | assert(0); |
| 2119 | *fmt++ = 'l'; |
| 2120 | #endif |
| 2121 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2122 | else if (size_tflag) { |
| 2123 | char *f = PY_FORMAT_SIZE_T; |
| 2124 | while (*f) |
| 2125 | *fmt++ = *f++; |
| 2126 | } |
| 2127 | *fmt++ = c; |
| 2128 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2131 | /* helper for PyUnicode_FromFormatV() */ |
| 2132 | |
| 2133 | static const char* |
| 2134 | parse_format_flags(const char *f, |
| 2135 | int *p_width, int *p_precision, |
| 2136 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2137 | { |
| 2138 | int width, precision, longflag, longlongflag, size_tflag; |
| 2139 | |
| 2140 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2141 | f++; |
| 2142 | width = 0; |
| 2143 | while (Py_ISDIGIT((unsigned)*f)) |
| 2144 | width = (width*10) + *f++ - '0'; |
| 2145 | precision = 0; |
| 2146 | if (*f == '.') { |
| 2147 | f++; |
| 2148 | while (Py_ISDIGIT((unsigned)*f)) |
| 2149 | precision = (precision*10) + *f++ - '0'; |
| 2150 | if (*f == '%') { |
| 2151 | /* "%.3%s" => f points to "3" */ |
| 2152 | f--; |
| 2153 | } |
| 2154 | } |
| 2155 | if (*f == '\0') { |
| 2156 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2157 | f--; |
| 2158 | } |
| 2159 | if (p_width != NULL) |
| 2160 | *p_width = width; |
| 2161 | if (p_precision != NULL) |
| 2162 | *p_precision = precision; |
| 2163 | |
| 2164 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2165 | longflag = 0; |
| 2166 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2167 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2168 | |
| 2169 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2170 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2171 | longflag = 1; |
| 2172 | ++f; |
| 2173 | } |
| 2174 | #ifdef HAVE_LONG_LONG |
| 2175 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2176 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2177 | longlongflag = 1; |
| 2178 | f += 2; |
| 2179 | } |
| 2180 | #endif |
| 2181 | } |
| 2182 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2183 | 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] | 2184 | size_tflag = 1; |
| 2185 | ++f; |
| 2186 | } |
| 2187 | if (p_longflag != NULL) |
| 2188 | *p_longflag = longflag; |
| 2189 | if (p_longlongflag != NULL) |
| 2190 | *p_longlongflag = longlongflag; |
| 2191 | if (p_size_tflag != NULL) |
| 2192 | *p_size_tflag = size_tflag; |
| 2193 | return f; |
| 2194 | } |
| 2195 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2196 | /* maximum number of characters required for output of %ld. 21 characters |
| 2197 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2198 | #define MAX_LONG_CHARS 21 |
| 2199 | /* maximum number of characters required for output of %lld. |
| 2200 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2201 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2202 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2203 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2204 | PyObject * |
| 2205 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2206 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2207 | va_list count; |
| 2208 | Py_ssize_t callcount = 0; |
| 2209 | PyObject **callresults = NULL; |
| 2210 | PyObject **callresult = NULL; |
| 2211 | Py_ssize_t n = 0; |
| 2212 | int width = 0; |
| 2213 | int precision = 0; |
| 2214 | int zeropad; |
| 2215 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2216 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2217 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2218 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2219 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2220 | Py_UCS4 argmaxchar; |
| 2221 | Py_ssize_t numbersize = 0; |
| 2222 | char *numberresults = NULL; |
| 2223 | char *numberresult = NULL; |
| 2224 | Py_ssize_t i; |
| 2225 | int kind; |
| 2226 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2227 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2228 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2229 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2230 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2231 | * 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] | 2232 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2233 | * also estimate a upper bound for all the number formats in the string, |
| 2234 | * 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] | 2235 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2236 | for (f = format; *f; f++) { |
| 2237 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2238 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2239 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2240 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2241 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2242 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2243 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2244 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2245 | #ifdef HAVE_LONG_LONG |
| 2246 | if (longlongflag) { |
| 2247 | if (width < MAX_LONG_LONG_CHARS) |
| 2248 | width = MAX_LONG_LONG_CHARS; |
| 2249 | } |
| 2250 | else |
| 2251 | #endif |
| 2252 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2253 | including sign. Decimal takes the most space. This |
| 2254 | isn't enough for octal. If a width is specified we |
| 2255 | need more (which we allocate later). */ |
| 2256 | if (width < MAX_LONG_CHARS) |
| 2257 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2258 | |
| 2259 | /* account for the size + '\0' to separate numbers |
| 2260 | inside of the numberresults buffer */ |
| 2261 | numbersize += (width + 1); |
| 2262 | } |
| 2263 | } |
| 2264 | else if ((unsigned char)*f > 127) { |
| 2265 | PyErr_Format(PyExc_ValueError, |
| 2266 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2267 | "string, got a non-ASCII byte: 0x%02x", |
| 2268 | (unsigned char)*f); |
| 2269 | return NULL; |
| 2270 | } |
| 2271 | } |
| 2272 | /* step 2: allocate memory for the results of |
| 2273 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2274 | if (callcount) { |
| 2275 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2276 | if (!callresults) { |
| 2277 | PyErr_NoMemory(); |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | callresult = callresults; |
| 2281 | } |
| 2282 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2283 | if (numbersize) { |
| 2284 | numberresults = PyObject_Malloc(numbersize); |
| 2285 | if (!numberresults) { |
| 2286 | PyErr_NoMemory(); |
| 2287 | goto fail; |
| 2288 | } |
| 2289 | numberresult = numberresults; |
| 2290 | } |
| 2291 | |
| 2292 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2293 | for (f = format; *f; f++) { |
| 2294 | if (*f == '%') { |
| 2295 | const char* p; |
| 2296 | int longflag; |
| 2297 | int longlongflag; |
| 2298 | int size_tflag; |
| 2299 | int numprinted; |
| 2300 | |
| 2301 | p = f; |
| 2302 | zeropad = (f[1] == '0'); |
| 2303 | f = parse_format_flags(f, &width, &precision, |
| 2304 | &longflag, &longlongflag, &size_tflag); |
| 2305 | switch (*f) { |
| 2306 | case 'c': |
| 2307 | { |
| 2308 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2309 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2310 | n++; |
| 2311 | break; |
| 2312 | } |
| 2313 | case '%': |
| 2314 | n++; |
| 2315 | break; |
| 2316 | case 'i': |
| 2317 | case 'd': |
| 2318 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2319 | width, precision, *f); |
| 2320 | if (longflag) |
| 2321 | numprinted = sprintf(numberresult, fmt, |
| 2322 | va_arg(count, long)); |
| 2323 | #ifdef HAVE_LONG_LONG |
| 2324 | else if (longlongflag) |
| 2325 | numprinted = sprintf(numberresult, fmt, |
| 2326 | va_arg(count, PY_LONG_LONG)); |
| 2327 | #endif |
| 2328 | else if (size_tflag) |
| 2329 | numprinted = sprintf(numberresult, fmt, |
| 2330 | va_arg(count, Py_ssize_t)); |
| 2331 | else |
| 2332 | numprinted = sprintf(numberresult, fmt, |
| 2333 | va_arg(count, int)); |
| 2334 | n += numprinted; |
| 2335 | /* advance by +1 to skip over the '\0' */ |
| 2336 | numberresult += (numprinted + 1); |
| 2337 | assert(*(numberresult - 1) == '\0'); |
| 2338 | assert(*(numberresult - 2) != '\0'); |
| 2339 | assert(numprinted >= 0); |
| 2340 | assert(numberresult <= numberresults + numbersize); |
| 2341 | break; |
| 2342 | case 'u': |
| 2343 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2344 | width, precision, 'u'); |
| 2345 | if (longflag) |
| 2346 | numprinted = sprintf(numberresult, fmt, |
| 2347 | va_arg(count, unsigned long)); |
| 2348 | #ifdef HAVE_LONG_LONG |
| 2349 | else if (longlongflag) |
| 2350 | numprinted = sprintf(numberresult, fmt, |
| 2351 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2352 | #endif |
| 2353 | else if (size_tflag) |
| 2354 | numprinted = sprintf(numberresult, fmt, |
| 2355 | va_arg(count, size_t)); |
| 2356 | else |
| 2357 | numprinted = sprintf(numberresult, fmt, |
| 2358 | va_arg(count, unsigned int)); |
| 2359 | n += numprinted; |
| 2360 | numberresult += (numprinted + 1); |
| 2361 | assert(*(numberresult - 1) == '\0'); |
| 2362 | assert(*(numberresult - 2) != '\0'); |
| 2363 | assert(numprinted >= 0); |
| 2364 | assert(numberresult <= numberresults + numbersize); |
| 2365 | break; |
| 2366 | case 'x': |
| 2367 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2368 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2369 | n += numprinted; |
| 2370 | numberresult += (numprinted + 1); |
| 2371 | assert(*(numberresult - 1) == '\0'); |
| 2372 | assert(*(numberresult - 2) != '\0'); |
| 2373 | assert(numprinted >= 0); |
| 2374 | assert(numberresult <= numberresults + numbersize); |
| 2375 | break; |
| 2376 | case 'p': |
| 2377 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2378 | /* %p is ill-defined: ensure leading 0x. */ |
| 2379 | if (numberresult[1] == 'X') |
| 2380 | numberresult[1] = 'x'; |
| 2381 | else if (numberresult[1] != 'x') { |
| 2382 | memmove(numberresult + 2, numberresult, |
| 2383 | strlen(numberresult) + 1); |
| 2384 | numberresult[0] = '0'; |
| 2385 | numberresult[1] = 'x'; |
| 2386 | numprinted += 2; |
| 2387 | } |
| 2388 | n += numprinted; |
| 2389 | numberresult += (numprinted + 1); |
| 2390 | assert(*(numberresult - 1) == '\0'); |
| 2391 | assert(*(numberresult - 2) != '\0'); |
| 2392 | assert(numprinted >= 0); |
| 2393 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | case 's': |
| 2396 | { |
| 2397 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2398 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2399 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2400 | if (!str) |
| 2401 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2402 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2403 | unicode objects, there is no need to call ready on them */ |
| 2404 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2405 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2406 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2407 | /* Remember the str and switch to the next slot */ |
| 2408 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2409 | break; |
| 2410 | } |
| 2411 | case 'U': |
| 2412 | { |
| 2413 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2414 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2415 | if (PyUnicode_READY(obj) == -1) |
| 2416 | goto fail; |
| 2417 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2418 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2419 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2420 | break; |
| 2421 | } |
| 2422 | case 'V': |
| 2423 | { |
| 2424 | PyObject *obj = va_arg(count, PyObject *); |
| 2425 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2426 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2427 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2428 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2429 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2430 | if (PyUnicode_READY(obj) == -1) |
| 2431 | goto fail; |
| 2432 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2433 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2434 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2435 | *callresult++ = NULL; |
| 2436 | } |
| 2437 | else { |
| 2438 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2439 | if (!str_obj) |
| 2440 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2441 | if (PyUnicode_READY(str_obj)) { |
| 2442 | Py_DECREF(str_obj); |
| 2443 | goto fail; |
| 2444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2445 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2446 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2447 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2448 | *callresult++ = str_obj; |
| 2449 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2450 | break; |
| 2451 | } |
| 2452 | case 'S': |
| 2453 | { |
| 2454 | PyObject *obj = va_arg(count, PyObject *); |
| 2455 | PyObject *str; |
| 2456 | assert(obj); |
| 2457 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2458 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2459 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2460 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2461 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2462 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2463 | /* Remember the str and switch to the next slot */ |
| 2464 | *callresult++ = str; |
| 2465 | break; |
| 2466 | } |
| 2467 | case 'R': |
| 2468 | { |
| 2469 | PyObject *obj = va_arg(count, PyObject *); |
| 2470 | PyObject *repr; |
| 2471 | assert(obj); |
| 2472 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2473 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2474 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2475 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2476 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2477 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2478 | /* Remember the repr and switch to the next slot */ |
| 2479 | *callresult++ = repr; |
| 2480 | break; |
| 2481 | } |
| 2482 | case 'A': |
| 2483 | { |
| 2484 | PyObject *obj = va_arg(count, PyObject *); |
| 2485 | PyObject *ascii; |
| 2486 | assert(obj); |
| 2487 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2488 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2489 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2490 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2491 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2492 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2493 | /* Remember the repr and switch to the next slot */ |
| 2494 | *callresult++ = ascii; |
| 2495 | break; |
| 2496 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2497 | default: |
| 2498 | /* if we stumble upon an unknown |
| 2499 | formatting code, copy the rest of |
| 2500 | the format string to the output |
| 2501 | string. (we cannot just skip the |
| 2502 | code, since there's no way to know |
| 2503 | what's in the argument list) */ |
| 2504 | n += strlen(p); |
| 2505 | goto expand; |
| 2506 | } |
| 2507 | } else |
| 2508 | n++; |
| 2509 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2510 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2511 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2512 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2513 | we don't have to resize the string. |
| 2514 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2515 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2516 | if (!string) |
| 2517 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2518 | kind = PyUnicode_KIND(string); |
| 2519 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2520 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2521 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2522 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2523 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2524 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2525 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2526 | |
| 2527 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2528 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2529 | /* checking for == because the last argument could be a empty |
| 2530 | string, which causes i to point to end, the assert at the end of |
| 2531 | the loop */ |
| 2532 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2533 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2534 | switch (*f) { |
| 2535 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2536 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2537 | const int ordinal = va_arg(vargs, int); |
| 2538 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2540 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2541 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2542 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2543 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2544 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2545 | case 'p': |
| 2546 | /* unused, since we already have the result */ |
| 2547 | if (*f == 'p') |
| 2548 | (void) va_arg(vargs, void *); |
| 2549 | else |
| 2550 | (void) va_arg(vargs, int); |
| 2551 | /* extract the result from numberresults and append. */ |
| 2552 | for (; *numberresult; ++i, ++numberresult) |
| 2553 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2554 | /* skip over the separating '\0' */ |
| 2555 | assert(*numberresult == '\0'); |
| 2556 | numberresult++; |
| 2557 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2558 | break; |
| 2559 | case 's': |
| 2560 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2561 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2562 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2563 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2564 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2565 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2566 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2567 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2568 | /* We're done with the unicode()/repr() => forget it */ |
| 2569 | Py_DECREF(*callresult); |
| 2570 | /* switch to next unicode()/repr() result */ |
| 2571 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2572 | break; |
| 2573 | } |
| 2574 | case 'U': |
| 2575 | { |
| 2576 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | Py_ssize_t size; |
| 2578 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2579 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2580 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2581 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2582 | break; |
| 2583 | } |
| 2584 | case 'V': |
| 2585 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2586 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2587 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2588 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2589 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | size = PyUnicode_GET_LENGTH(obj); |
| 2591 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2592 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2593 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2594 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2595 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2596 | assert(PyUnicode_KIND(*callresult) <= |
| 2597 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2598 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2599 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2600 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2601 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2602 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2603 | break; |
| 2604 | } |
| 2605 | case 'S': |
| 2606 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2607 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2608 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2609 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2610 | /* unused, since we already have the result */ |
| 2611 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2612 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2613 | copy_characters(string, i, *callresult, 0, size); |
| 2614 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2615 | /* We're done with the unicode()/repr() => forget it */ |
| 2616 | Py_DECREF(*callresult); |
| 2617 | /* switch to next unicode()/repr() result */ |
| 2618 | ++callresult; |
| 2619 | break; |
| 2620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2622 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2623 | break; |
| 2624 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2625 | for (; *p; ++p, ++i) |
| 2626 | PyUnicode_WRITE(kind, data, i, *p); |
| 2627 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2628 | goto end; |
| 2629 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2630 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2631 | else { |
| 2632 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2633 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2634 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2636 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2637 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2638 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2639 | if (callresults) |
| 2640 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2641 | if (numberresults) |
| 2642 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2643 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 2644 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2645 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2646 | if (callresults) { |
| 2647 | PyObject **callresult2 = callresults; |
| 2648 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2649 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2650 | ++callresult2; |
| 2651 | } |
| 2652 | PyObject_Free(callresults); |
| 2653 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | if (numberresults) |
| 2655 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2656 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2659 | PyObject * |
| 2660 | PyUnicode_FromFormat(const char *format, ...) |
| 2661 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2662 | PyObject* ret; |
| 2663 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2664 | |
| 2665 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2666 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2667 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2669 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2670 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2671 | va_end(vargs); |
| 2672 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2675 | #ifdef HAVE_WCHAR_H |
| 2676 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2677 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2678 | convert a Unicode object to a wide character string. |
| 2679 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2680 | - 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] | 2681 | character) required to convert the unicode object. Ignore size argument. |
| 2682 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2683 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2684 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2685 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2686 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2687 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2688 | wchar_t *w, |
| 2689 | Py_ssize_t size) |
| 2690 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2691 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2692 | const wchar_t *wstr; |
| 2693 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2694 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2695 | if (wstr == NULL) |
| 2696 | return -1; |
| 2697 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2698 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2699 | if (size > res) |
| 2700 | size = res + 1; |
| 2701 | else |
| 2702 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2704 | return res; |
| 2705 | } |
| 2706 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2707 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2711 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2712 | wchar_t *w, |
| 2713 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2714 | { |
| 2715 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2716 | PyErr_BadInternalCall(); |
| 2717 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2718 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2719 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2722 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2723 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2724 | Py_ssize_t *size) |
| 2725 | { |
| 2726 | wchar_t* buffer; |
| 2727 | Py_ssize_t buflen; |
| 2728 | |
| 2729 | if (unicode == NULL) { |
| 2730 | PyErr_BadInternalCall(); |
| 2731 | return NULL; |
| 2732 | } |
| 2733 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2734 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2735 | if (buflen == -1) |
| 2736 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2737 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2738 | PyErr_NoMemory(); |
| 2739 | return NULL; |
| 2740 | } |
| 2741 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2742 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2743 | if (buffer == NULL) { |
| 2744 | PyErr_NoMemory(); |
| 2745 | return NULL; |
| 2746 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2747 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2748 | if (buflen == -1) |
| 2749 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2750 | if (size != NULL) |
| 2751 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2752 | return buffer; |
| 2753 | } |
| 2754 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2755 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2757 | PyObject * |
| 2758 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2759 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2760 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2761 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2762 | PyErr_SetString(PyExc_ValueError, |
| 2763 | "chr() arg not in range(0x110000)"); |
| 2764 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2765 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2767 | if (ordinal < 256) |
| 2768 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2770 | v = PyUnicode_New(1, ordinal); |
| 2771 | if (v == NULL) |
| 2772 | return NULL; |
| 2773 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2774 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2775 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2778 | PyObject * |
| 2779 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2781 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2782 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2783 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2784 | if (PyUnicode_READY(obj)) |
| 2785 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2786 | Py_INCREF(obj); |
| 2787 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2788 | } |
| 2789 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2790 | /* For a Unicode subtype that's not a Unicode object, |
| 2791 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2792 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2793 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2794 | PyErr_Format(PyExc_TypeError, |
| 2795 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2796 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2797 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2800 | PyObject * |
| 2801 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2802 | const char *encoding, |
| 2803 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2804 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2805 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2806 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2809 | PyErr_BadInternalCall(); |
| 2810 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2811 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2812 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2813 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2814 | if (PyBytes_Check(obj)) { |
| 2815 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2816 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2817 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2818 | } |
| 2819 | else { |
| 2820 | v = PyUnicode_Decode( |
| 2821 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2822 | encoding, errors); |
| 2823 | } |
| 2824 | return v; |
| 2825 | } |
| 2826 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2827 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2828 | PyErr_SetString(PyExc_TypeError, |
| 2829 | "decoding str is not supported"); |
| 2830 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2831 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2833 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2834 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2835 | PyErr_Format(PyExc_TypeError, |
| 2836 | "coercing to str: need bytes, bytearray " |
| 2837 | "or buffer-like object, %.80s found", |
| 2838 | Py_TYPE(obj)->tp_name); |
| 2839 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2840 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2841 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2842 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2843 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2844 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2845 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2846 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2847 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2848 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2849 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2850 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2853 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2854 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2855 | 1 on success. */ |
| 2856 | static int |
| 2857 | normalize_encoding(const char *encoding, |
| 2858 | char *lower, |
| 2859 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2860 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2861 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2862 | char *l; |
| 2863 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2864 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2865 | if (encoding == NULL) { |
| 2866 | strcpy(lower, "utf-8"); |
| 2867 | return 1; |
| 2868 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2869 | e = encoding; |
| 2870 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2871 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2872 | while (*e) { |
| 2873 | if (l == l_end) |
| 2874 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2875 | if (Py_ISUPPER(*e)) { |
| 2876 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2877 | } |
| 2878 | else if (*e == '_') { |
| 2879 | *l++ = '-'; |
| 2880 | e++; |
| 2881 | } |
| 2882 | else { |
| 2883 | *l++ = *e++; |
| 2884 | } |
| 2885 | } |
| 2886 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2887 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2890 | PyObject * |
| 2891 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2892 | Py_ssize_t size, |
| 2893 | const char *encoding, |
| 2894 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2895 | { |
| 2896 | PyObject *buffer = NULL, *unicode; |
| 2897 | Py_buffer info; |
| 2898 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2899 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2900 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2901 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2902 | if ((strcmp(lower, "utf-8") == 0) || |
| 2903 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2904 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2905 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2906 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2907 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2908 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2909 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2910 | else if (strcmp(lower, "mbcs") == 0) |
| 2911 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2912 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2913 | else if (strcmp(lower, "ascii") == 0) |
| 2914 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2915 | else if (strcmp(lower, "utf-16") == 0) |
| 2916 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2917 | else if (strcmp(lower, "utf-32") == 0) |
| 2918 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2919 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2920 | |
| 2921 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2922 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2923 | 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] | 2924 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2925 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2926 | if (buffer == NULL) |
| 2927 | goto onError; |
| 2928 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2929 | if (unicode == NULL) |
| 2930 | goto onError; |
| 2931 | if (!PyUnicode_Check(unicode)) { |
| 2932 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2933 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2934 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2935 | Py_DECREF(unicode); |
| 2936 | goto onError; |
| 2937 | } |
| 2938 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2939 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2940 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2941 | Py_DECREF(unicode); |
| 2942 | return NULL; |
| 2943 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2944 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2945 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2947 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2948 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2949 | Py_XDECREF(buffer); |
| 2950 | return NULL; |
| 2951 | } |
| 2952 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2953 | PyObject * |
| 2954 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2955 | const char *encoding, |
| 2956 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2957 | { |
| 2958 | PyObject *v; |
| 2959 | |
| 2960 | if (!PyUnicode_Check(unicode)) { |
| 2961 | PyErr_BadArgument(); |
| 2962 | goto onError; |
| 2963 | } |
| 2964 | |
| 2965 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2966 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2967 | |
| 2968 | /* Decode via the codec registry */ |
| 2969 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2970 | if (v == NULL) |
| 2971 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2972 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2973 | return v; |
| 2974 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2975 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2976 | return NULL; |
| 2977 | } |
| 2978 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2979 | PyObject * |
| 2980 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2981 | const char *encoding, |
| 2982 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2983 | { |
| 2984 | PyObject *v; |
| 2985 | |
| 2986 | if (!PyUnicode_Check(unicode)) { |
| 2987 | PyErr_BadArgument(); |
| 2988 | goto onError; |
| 2989 | } |
| 2990 | |
| 2991 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2992 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2993 | |
| 2994 | /* Decode via the codec registry */ |
| 2995 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2996 | if (v == NULL) |
| 2997 | goto onError; |
| 2998 | if (!PyUnicode_Check(v)) { |
| 2999 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3000 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3001 | Py_TYPE(v)->tp_name); |
| 3002 | Py_DECREF(v); |
| 3003 | goto onError; |
| 3004 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3005 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3006 | return v; |
| 3007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3008 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3009 | return NULL; |
| 3010 | } |
| 3011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3012 | PyObject * |
| 3013 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3014 | Py_ssize_t size, |
| 3015 | const char *encoding, |
| 3016 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3017 | { |
| 3018 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3019 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | unicode = PyUnicode_FromUnicode(s, size); |
| 3021 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3022 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3023 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3024 | Py_DECREF(unicode); |
| 3025 | return v; |
| 3026 | } |
| 3027 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3028 | PyObject * |
| 3029 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3030 | const char *encoding, |
| 3031 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3032 | { |
| 3033 | PyObject *v; |
| 3034 | |
| 3035 | if (!PyUnicode_Check(unicode)) { |
| 3036 | PyErr_BadArgument(); |
| 3037 | goto onError; |
| 3038 | } |
| 3039 | |
| 3040 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3041 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3042 | |
| 3043 | /* Encode via the codec registry */ |
| 3044 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3045 | if (v == NULL) |
| 3046 | goto onError; |
| 3047 | return v; |
| 3048 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3049 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3050 | return NULL; |
| 3051 | } |
| 3052 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3053 | PyObject * |
| 3054 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3055 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3056 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3057 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3058 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3059 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3060 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3061 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3062 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3063 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3064 | the Python codec requires to encode at least its own filename. Use the C |
| 3065 | version of the locale codec until the codec registry is initialized and |
| 3066 | the Python codec is loaded. |
| 3067 | |
| 3068 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3069 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3070 | subinterpreters. */ |
| 3071 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3072 | return PyUnicode_AsEncodedString(unicode, |
| 3073 | Py_FileSystemDefaultEncoding, |
| 3074 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3075 | } |
| 3076 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3077 | /* locale encoding with surrogateescape */ |
| 3078 | wchar_t *wchar; |
| 3079 | char *bytes; |
| 3080 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3081 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3082 | |
| 3083 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3084 | if (wchar == NULL) |
| 3085 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3086 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3087 | if (bytes == NULL) { |
| 3088 | if (error_pos != (size_t)-1) { |
| 3089 | char *errmsg = strerror(errno); |
| 3090 | PyObject *exc = NULL; |
| 3091 | if (errmsg == NULL) |
| 3092 | errmsg = "Py_wchar2char() failed"; |
| 3093 | raise_encode_exception(&exc, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 3094 | "filesystemencoding", unicode, |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3095 | error_pos, error_pos+1, |
| 3096 | errmsg); |
| 3097 | Py_XDECREF(exc); |
| 3098 | } |
| 3099 | else |
| 3100 | PyErr_NoMemory(); |
| 3101 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3102 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3103 | } |
| 3104 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3105 | |
| 3106 | bytes_obj = PyBytes_FromString(bytes); |
| 3107 | PyMem_Free(bytes); |
| 3108 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3109 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3110 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3113 | PyObject * |
| 3114 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3115 | const char *encoding, |
| 3116 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3117 | { |
| 3118 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3119 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3121 | if (!PyUnicode_Check(unicode)) { |
| 3122 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3123 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3124 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3125 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3126 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3127 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3128 | if ((strcmp(lower, "utf-8") == 0) || |
| 3129 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3130 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3131 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3132 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3133 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3134 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3135 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3136 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3137 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3138 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3139 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3140 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3141 | else if (strcmp(lower, "mbcs") == 0) |
| 3142 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3143 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3144 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3145 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3146 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3147 | |
| 3148 | /* Encode via the codec registry */ |
| 3149 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3150 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3151 | return NULL; |
| 3152 | |
| 3153 | /* The normal path */ |
| 3154 | if (PyBytes_Check(v)) |
| 3155 | return v; |
| 3156 | |
| 3157 | /* 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] | 3158 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3159 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3160 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3161 | |
| 3162 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3163 | "encoder %s returned bytearray instead of bytes", |
| 3164 | encoding); |
| 3165 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3166 | Py_DECREF(v); |
| 3167 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3168 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3169 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3170 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3171 | Py_DECREF(v); |
| 3172 | return b; |
| 3173 | } |
| 3174 | |
| 3175 | PyErr_Format(PyExc_TypeError, |
| 3176 | "encoder did not return a bytes object (type=%.400s)", |
| 3177 | Py_TYPE(v)->tp_name); |
| 3178 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3179 | return NULL; |
| 3180 | } |
| 3181 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3182 | PyObject * |
| 3183 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3184 | const char *encoding, |
| 3185 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3186 | { |
| 3187 | PyObject *v; |
| 3188 | |
| 3189 | if (!PyUnicode_Check(unicode)) { |
| 3190 | PyErr_BadArgument(); |
| 3191 | goto onError; |
| 3192 | } |
| 3193 | |
| 3194 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3195 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3196 | |
| 3197 | /* Encode via the codec registry */ |
| 3198 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3199 | if (v == NULL) |
| 3200 | goto onError; |
| 3201 | if (!PyUnicode_Check(v)) { |
| 3202 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3203 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3204 | Py_TYPE(v)->tp_name); |
| 3205 | Py_DECREF(v); |
| 3206 | goto onError; |
| 3207 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3209 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3210 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3211 | return NULL; |
| 3212 | } |
| 3213 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3214 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3215 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3216 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3217 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3218 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3219 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3220 | PyObject* |
| 3221 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3222 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3223 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3224 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3225 | #elif defined(__APPLE__) |
| 3226 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3227 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3228 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3229 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3230 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3231 | the Python codec requires to encode at least its own filename. Use the C |
| 3232 | version of the locale codec until the codec registry is initialized and |
| 3233 | the Python codec is loaded. |
| 3234 | |
| 3235 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3236 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3237 | subinterpreters. */ |
| 3238 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3239 | return PyUnicode_Decode(s, size, |
| 3240 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3241 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3242 | } |
| 3243 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3244 | /* locale encoding with surrogateescape */ |
| 3245 | wchar_t *wchar; |
| 3246 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3247 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3248 | |
| 3249 | if (s[size] != '\0' || size != strlen(s)) { |
| 3250 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3251 | return NULL; |
| 3252 | } |
| 3253 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3254 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3255 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3256 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3257 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3258 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3259 | PyMem_Free(wchar); |
| 3260 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3261 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3262 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3265 | |
| 3266 | int |
| 3267 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3268 | { |
| 3269 | PyObject *output = NULL; |
| 3270 | Py_ssize_t size; |
| 3271 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3272 | if (arg == NULL) { |
| 3273 | Py_DECREF(*(PyObject**)addr); |
| 3274 | return 1; |
| 3275 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3276 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3277 | output = arg; |
| 3278 | Py_INCREF(output); |
| 3279 | } |
| 3280 | else { |
| 3281 | arg = PyUnicode_FromObject(arg); |
| 3282 | if (!arg) |
| 3283 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3284 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3285 | Py_DECREF(arg); |
| 3286 | if (!output) |
| 3287 | return 0; |
| 3288 | if (!PyBytes_Check(output)) { |
| 3289 | Py_DECREF(output); |
| 3290 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3291 | return 0; |
| 3292 | } |
| 3293 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3294 | size = PyBytes_GET_SIZE(output); |
| 3295 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3296 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3297 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3298 | Py_DECREF(output); |
| 3299 | return 0; |
| 3300 | } |
| 3301 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3302 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3306 | int |
| 3307 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3308 | { |
| 3309 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3310 | if (arg == NULL) { |
| 3311 | Py_DECREF(*(PyObject**)addr); |
| 3312 | return 1; |
| 3313 | } |
| 3314 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3315 | if (PyUnicode_READY(arg)) |
| 3316 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3317 | output = arg; |
| 3318 | Py_INCREF(output); |
| 3319 | } |
| 3320 | else { |
| 3321 | arg = PyBytes_FromObject(arg); |
| 3322 | if (!arg) |
| 3323 | return 0; |
| 3324 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3325 | PyBytes_GET_SIZE(arg)); |
| 3326 | Py_DECREF(arg); |
| 3327 | if (!output) |
| 3328 | return 0; |
| 3329 | if (!PyUnicode_Check(output)) { |
| 3330 | Py_DECREF(output); |
| 3331 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3332 | return 0; |
| 3333 | } |
| 3334 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3335 | if (PyUnicode_READY(output) < 0) { |
| 3336 | Py_DECREF(output); |
| 3337 | return 0; |
| 3338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3339 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3340 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3341 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3342 | Py_DECREF(output); |
| 3343 | return 0; |
| 3344 | } |
| 3345 | *(PyObject**)addr = output; |
| 3346 | return Py_CLEANUP_SUPPORTED; |
| 3347 | } |
| 3348 | |
| 3349 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3350 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3351 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3352 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3353 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3354 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3355 | if (!PyUnicode_Check(unicode)) { |
| 3356 | PyErr_BadArgument(); |
| 3357 | return NULL; |
| 3358 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3359 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3360 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3361 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3362 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3363 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3364 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3365 | if (bytes == NULL) |
| 3366 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3367 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3368 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3369 | Py_DECREF(bytes); |
| 3370 | return NULL; |
| 3371 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3372 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3373 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3374 | PyBytes_AS_STRING(bytes), |
| 3375 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3376 | Py_DECREF(bytes); |
| 3377 | } |
| 3378 | |
| 3379 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3380 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3381 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3385 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3386 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3387 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3388 | } |
| 3389 | |
| 3390 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3391 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3392 | #endif |
| 3393 | |
| 3394 | |
| 3395 | Py_UNICODE * |
| 3396 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3397 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3398 | const unsigned char *one_byte; |
| 3399 | #if SIZEOF_WCHAR_T == 4 |
| 3400 | const Py_UCS2 *two_bytes; |
| 3401 | #else |
| 3402 | const Py_UCS4 *four_bytes; |
| 3403 | const Py_UCS4 *ucs4_end; |
| 3404 | Py_ssize_t num_surrogates; |
| 3405 | #endif |
| 3406 | wchar_t *w; |
| 3407 | wchar_t *wchar_end; |
| 3408 | |
| 3409 | if (!PyUnicode_Check(unicode)) { |
| 3410 | PyErr_BadArgument(); |
| 3411 | return NULL; |
| 3412 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3413 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3414 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3415 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3416 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3417 | |
| 3418 | #ifdef Py_DEBUG |
| 3419 | ++unicode_as_unicode_calls; |
| 3420 | #endif |
| 3421 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3422 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3423 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3424 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3425 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3426 | num_surrogates = 0; |
| 3427 | |
| 3428 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3429 | if (*four_bytes > 0xFFFF) |
| 3430 | ++num_surrogates; |
| 3431 | } |
| 3432 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3433 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3434 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3435 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3436 | PyErr_NoMemory(); |
| 3437 | return NULL; |
| 3438 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3439 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3440 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3441 | w = _PyUnicode_WSTR(unicode); |
| 3442 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3443 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3444 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3445 | if (*four_bytes > 0xFFFF) { |
| 3446 | /* encode surrogate pair in this case */ |
| 3447 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3448 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3449 | } |
| 3450 | else |
| 3451 | *w = *four_bytes; |
| 3452 | |
| 3453 | if (w > wchar_end) { |
| 3454 | assert(0 && "Miscalculated string end"); |
| 3455 | } |
| 3456 | } |
| 3457 | *w = 0; |
| 3458 | #else |
| 3459 | /* sizeof(wchar_t) == 4 */ |
| 3460 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3461 | "should share memory already."); |
| 3462 | return NULL; |
| 3463 | #endif |
| 3464 | } |
| 3465 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3466 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3467 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3468 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3469 | PyErr_NoMemory(); |
| 3470 | return NULL; |
| 3471 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3472 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3473 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3474 | w = _PyUnicode_WSTR(unicode); |
| 3475 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3476 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3477 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3478 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3479 | for (; w < wchar_end; ++one_byte, ++w) |
| 3480 | *w = *one_byte; |
| 3481 | /* null-terminate the wstr */ |
| 3482 | *w = 0; |
| 3483 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3484 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3485 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3486 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3487 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3488 | *w = *two_bytes; |
| 3489 | /* null-terminate the wstr */ |
| 3490 | *w = 0; |
| 3491 | #else |
| 3492 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3493 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3494 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3495 | Py_FatalError("Impossible unicode object state, wstr " |
| 3496 | "and str should share memory already."); |
| 3497 | return NULL; |
| 3498 | #endif |
| 3499 | } |
| 3500 | else { |
| 3501 | assert(0 && "This should never happen."); |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3506 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3507 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3510 | Py_UNICODE * |
| 3511 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3512 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3513 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3516 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3517 | Py_ssize_t |
| 3518 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3519 | { |
| 3520 | if (!PyUnicode_Check(unicode)) { |
| 3521 | PyErr_BadArgument(); |
| 3522 | goto onError; |
| 3523 | } |
| 3524 | return PyUnicode_GET_SIZE(unicode); |
| 3525 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3526 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3527 | return -1; |
| 3528 | } |
| 3529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3530 | Py_ssize_t |
| 3531 | PyUnicode_GetLength(PyObject *unicode) |
| 3532 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3533 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3534 | PyErr_BadArgument(); |
| 3535 | return -1; |
| 3536 | } |
| 3537 | |
| 3538 | return PyUnicode_GET_LENGTH(unicode); |
| 3539 | } |
| 3540 | |
| 3541 | Py_UCS4 |
| 3542 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3543 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3544 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3545 | PyErr_BadArgument(); |
| 3546 | return (Py_UCS4)-1; |
| 3547 | } |
| 3548 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3549 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3550 | return (Py_UCS4)-1; |
| 3551 | } |
| 3552 | return PyUnicode_READ_CHAR(unicode, index); |
| 3553 | } |
| 3554 | |
| 3555 | int |
| 3556 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3557 | { |
| 3558 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3559 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3560 | return -1; |
| 3561 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3562 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3563 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3564 | return -1; |
| 3565 | } |
| 3566 | if (_PyUnicode_Dirty(unicode)) |
| 3567 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3568 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3569 | index, ch); |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3573 | const char * |
| 3574 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3575 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3576 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3579 | /* create or adjust a UnicodeDecodeError */ |
| 3580 | static void |
| 3581 | make_decode_exception(PyObject **exceptionObject, |
| 3582 | const char *encoding, |
| 3583 | const char *input, Py_ssize_t length, |
| 3584 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3585 | const char *reason) |
| 3586 | { |
| 3587 | if (*exceptionObject == NULL) { |
| 3588 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3589 | encoding, input, length, startpos, endpos, reason); |
| 3590 | } |
| 3591 | else { |
| 3592 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3593 | goto onError; |
| 3594 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3595 | goto onError; |
| 3596 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3597 | goto onError; |
| 3598 | } |
| 3599 | return; |
| 3600 | |
| 3601 | onError: |
| 3602 | Py_DECREF(*exceptionObject); |
| 3603 | *exceptionObject = NULL; |
| 3604 | } |
| 3605 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 | /* error handling callback helper: |
| 3607 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3608 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3609 | and adjust various state variables. |
| 3610 | return 0 on success, -1 on error |
| 3611 | */ |
| 3612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3613 | static int |
| 3614 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3615 | const char *encoding, const char *reason, |
| 3616 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3617 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3618 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3619 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3620 | 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] | 3621 | |
| 3622 | PyObject *restuple = NULL; |
| 3623 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3624 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3625 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3626 | Py_ssize_t requiredsize; |
| 3627 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3628 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3629 | int res = -1; |
| 3630 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3631 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3632 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3633 | else |
| 3634 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3635 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3636 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3637 | *errorHandler = PyCodec_LookupError(errors); |
| 3638 | if (*errorHandler == NULL) |
| 3639 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3642 | make_decode_exception(exceptionObject, |
| 3643 | encoding, |
| 3644 | *input, *inend - *input, |
| 3645 | *startinpos, *endinpos, |
| 3646 | reason); |
| 3647 | if (*exceptionObject == NULL) |
| 3648 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3649 | |
| 3650 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3651 | if (restuple == NULL) |
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 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3654 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3655 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3656 | } |
| 3657 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3658 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3659 | if (PyUnicode_READY(repunicode) < 0) |
| 3660 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3661 | |
| 3662 | /* Copy back the bytes variables, which might have been modified by the |
| 3663 | callback */ |
| 3664 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3665 | if (!inputobj) |
| 3666 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3667 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3668 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3669 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3670 | *input = PyBytes_AS_STRING(inputobj); |
| 3671 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3672 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3673 | /* we can DECREF safely, as the exception has another reference, |
| 3674 | so the object won't go away. */ |
| 3675 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3676 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3677 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3678 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3679 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3680 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3681 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3682 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3683 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3684 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3685 | /* need more space? (at least enough for what we |
| 3686 | have+the replacement+the rest of the string (starting |
| 3687 | at the new input position), so we won't have to check space |
| 3688 | when there are no errors in the rest of the string) */ |
| 3689 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3690 | requiredsize = *outpos + replen + insize-newpos; |
| 3691 | if (requiredsize > outsize) { |
| 3692 | if (requiredsize<2*outsize) |
| 3693 | requiredsize = 2*outsize; |
| 3694 | if (unicode_resize(output, requiredsize) < 0) |
| 3695 | goto onError; |
| 3696 | } |
| 3697 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3698 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3699 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 3700 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3701 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3702 | else { |
| 3703 | wchar_t *repwstr; |
| 3704 | Py_ssize_t repwlen; |
| 3705 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3706 | if (repwstr == NULL) |
| 3707 | goto onError; |
| 3708 | /* need more space? (at least enough for what we |
| 3709 | have+the replacement+the rest of the string (starting |
| 3710 | at the new input position), so we won't have to check space |
| 3711 | when there are no errors in the rest of the string) */ |
| 3712 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3713 | if (requiredsize > outsize) { |
| 3714 | if (requiredsize < 2*outsize) |
| 3715 | requiredsize = 2*outsize; |
| 3716 | if (unicode_resize(output, requiredsize) < 0) |
| 3717 | goto onError; |
| 3718 | } |
| 3719 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3720 | *outpos += repwlen; |
| 3721 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3722 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3723 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3724 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3725 | /* we made it! */ |
| 3726 | res = 0; |
| 3727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3728 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3729 | Py_XDECREF(restuple); |
| 3730 | return res; |
| 3731 | } |
| 3732 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3733 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3734 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3735 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3736 | |
| 3737 | /* Three simple macros defining base-64. */ |
| 3738 | |
| 3739 | /* Is c a base-64 character? */ |
| 3740 | |
| 3741 | #define IS_BASE64(c) \ |
| 3742 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3743 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3744 | ((c) >= '0' && (c) <= '9') || \ |
| 3745 | (c) == '+' || (c) == '/') |
| 3746 | |
| 3747 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3748 | |
| 3749 | #define FROM_BASE64(c) \ |
| 3750 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3751 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3752 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3753 | (c) == '+' ? 62 : 63) |
| 3754 | |
| 3755 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3756 | |
| 3757 | #define TO_BASE64(n) \ |
| 3758 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3759 | |
| 3760 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3761 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3762 | * byte not decoding to itself is the + which begins a base64 |
| 3763 | * string. */ |
| 3764 | |
| 3765 | #define DECODE_DIRECT(c) \ |
| 3766 | ((c) <= 127 && (c) != '+') |
| 3767 | |
| 3768 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3769 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3770 | * the above). See RFC2152. This array identifies these different |
| 3771 | * sets: |
| 3772 | * 0 : "Set D" |
| 3773 | * alphanumeric and '(),-./:? |
| 3774 | * 1 : "Set O" |
| 3775 | * !"#$%&*;<=>@[]^_`{|} |
| 3776 | * 2 : "whitespace" |
| 3777 | * ht nl cr sp |
| 3778 | * 3 : special (must be base64 encoded) |
| 3779 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3780 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3781 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3782 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3783 | char utf7_category[128] = { |
| 3784 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3785 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3786 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3787 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3788 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3789 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3790 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3791 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3792 | /* @ A B C D E F G H I J K L M N O */ |
| 3793 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3794 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3795 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3796 | /* ` a b c d e f g h i j k l m n o */ |
| 3797 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3798 | /* p q r s t u v w x y z { | } ~ del */ |
| 3799 | 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] | 3800 | }; |
| 3801 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3802 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3803 | * answer depends on whether we are encoding set O as itself, and also |
| 3804 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3805 | * clear that the answers to these questions vary between |
| 3806 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3807 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3808 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3809 | ((c) < 128 && (c) > 0 && \ |
| 3810 | ((utf7_category[(c)] == 0) || \ |
| 3811 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3812 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3814 | PyObject * |
| 3815 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3816 | Py_ssize_t size, |
| 3817 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3818 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3819 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3820 | } |
| 3821 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3822 | /* The decoder. The only state we preserve is our read position, |
| 3823 | * i.e. how many characters we have consumed. So if we end in the |
| 3824 | * middle of a shift sequence we have to back off the read position |
| 3825 | * and the output to the beginning of the sequence, otherwise we lose |
| 3826 | * all the shift state (seen bits, number of bits seen, high |
| 3827 | * surrogate). */ |
| 3828 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3829 | PyObject * |
| 3830 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3831 | Py_ssize_t size, |
| 3832 | const char *errors, |
| 3833 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3834 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3835 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3836 | Py_ssize_t startinpos; |
| 3837 | Py_ssize_t endinpos; |
| 3838 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3839 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3840 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3841 | const char *errmsg = ""; |
| 3842 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3843 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3844 | unsigned int base64bits = 0; |
| 3845 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3846 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3847 | PyObject *errorHandler = NULL; |
| 3848 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3849 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3850 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 3851 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3852 | if (!unicode) |
| 3853 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3854 | if (size == 0) { |
| 3855 | if (consumed) |
| 3856 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3857 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3858 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3859 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3860 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3861 | e = s + size; |
| 3862 | |
| 3863 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3864 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3865 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3866 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3867 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3868 | if (inShift) { /* in a base-64 section */ |
| 3869 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3870 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3871 | base64bits += 6; |
| 3872 | s++; |
| 3873 | if (base64bits >= 16) { |
| 3874 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3875 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3876 | base64bits -= 16; |
| 3877 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3878 | if (surrogate) { |
| 3879 | /* expecting a second surrogate */ |
| 3880 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3881 | Py_UCS4 ch2 = (((surrogate & 0x3FF)<<10) |
| 3882 | | (outCh & 0x3FF)) + 0x10000; |
| 3883 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 3884 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3885 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3886 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3887 | } |
| 3888 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3889 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3890 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3891 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3892 | } |
| 3893 | } |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3894 | if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3895 | /* first surrogate */ |
| 3896 | surrogate = outCh; |
| 3897 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3898 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3899 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 3900 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3905 | inShift = 0; |
| 3906 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3907 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3908 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3909 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3910 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3911 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3912 | if (base64bits > 0) { /* left-over bits */ |
| 3913 | if (base64bits >= 6) { |
| 3914 | /* We've seen at least one base-64 character */ |
| 3915 | errmsg = "partial character in shift sequence"; |
| 3916 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3917 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3918 | else { |
| 3919 | /* Some bits remain; they should be zero */ |
| 3920 | if (base64buffer != 0) { |
| 3921 | errmsg = "non-zero padding bits in shift sequence"; |
| 3922 | goto utf7Error; |
| 3923 | } |
| 3924 | } |
| 3925 | } |
| 3926 | if (ch != '-') { |
| 3927 | /* '-' is absorbed; other terminating |
| 3928 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3929 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3930 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3931 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3935 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3936 | s++; /* consume '+' */ |
| 3937 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3938 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3939 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 3940 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3941 | } |
| 3942 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3943 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3944 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3945 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3946 | } |
| 3947 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3948 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3949 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3950 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3951 | s++; |
| 3952 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3953 | else { |
| 3954 | startinpos = s-starts; |
| 3955 | s++; |
| 3956 | errmsg = "unexpected special character"; |
| 3957 | goto utf7Error; |
| 3958 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3959 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3960 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3961 | endinpos = s-starts; |
| 3962 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | errors, &errorHandler, |
| 3964 | "utf7", errmsg, |
| 3965 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3966 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3967 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3970 | /* end of string */ |
| 3971 | |
| 3972 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3973 | /* if we're in an inconsistent state, that's an error */ |
| 3974 | if (surrogate || |
| 3975 | (base64bits >= 6) || |
| 3976 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3977 | endinpos = size; |
| 3978 | if (unicode_decode_call_errorhandler( |
| 3979 | errors, &errorHandler, |
| 3980 | "utf7", "unterminated shift sequence", |
| 3981 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3982 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3983 | goto onError; |
| 3984 | if (s < e) |
| 3985 | goto restart; |
| 3986 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3987 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3988 | |
| 3989 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3990 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3991 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3992 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3993 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3994 | } |
| 3995 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3996 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3997 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3998 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3999 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4000 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4001 | goto onError; |
| 4002 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4003 | Py_XDECREF(errorHandler); |
| 4004 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4005 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4006 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4007 | Py_DECREF(unicode); |
| 4008 | return NULL; |
| 4009 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4010 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4011 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4012 | return unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4013 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4014 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4015 | Py_XDECREF(errorHandler); |
| 4016 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4017 | Py_DECREF(unicode); |
| 4018 | return NULL; |
| 4019 | } |
| 4020 | |
| 4021 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4022 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4023 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4024 | int base64SetO, |
| 4025 | int base64WhiteSpace, |
| 4026 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4027 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4028 | int kind; |
| 4029 | void *data; |
| 4030 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4031 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4032 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4033 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4034 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4035 | unsigned int base64bits = 0; |
| 4036 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4037 | char * out; |
| 4038 | char * start; |
| 4039 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4040 | if (PyUnicode_READY(str) < 0) |
| 4041 | return NULL; |
| 4042 | kind = PyUnicode_KIND(str); |
| 4043 | data = PyUnicode_DATA(str); |
| 4044 | len = PyUnicode_GET_LENGTH(str); |
| 4045 | |
| 4046 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4047 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4048 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4049 | /* It might be possible to tighten this worst case */ |
| 4050 | allocated = 8 * len; |
| 4051 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4052 | return PyErr_NoMemory(); |
| 4053 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4054 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4055 | if (v == NULL) |
| 4056 | return NULL; |
| 4057 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4058 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4059 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4060 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4061 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4062 | if (inShift) { |
| 4063 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4064 | /* shifting out */ |
| 4065 | if (base64bits) { /* output remaining bits */ |
| 4066 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4067 | base64buffer = 0; |
| 4068 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4069 | } |
| 4070 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4071 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4072 | so no '-' is required, except if the character is itself a '-' */ |
| 4073 | if (IS_BASE64(ch) || ch == '-') { |
| 4074 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4075 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4076 | *out++ = (char) ch; |
| 4077 | } |
| 4078 | else { |
| 4079 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4080 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4081 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4082 | else { /* not in a shift sequence */ |
| 4083 | if (ch == '+') { |
| 4084 | *out++ = '+'; |
| 4085 | *out++ = '-'; |
| 4086 | } |
| 4087 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4088 | *out++ = (char) ch; |
| 4089 | } |
| 4090 | else { |
| 4091 | *out++ = '+'; |
| 4092 | inShift = 1; |
| 4093 | goto encode_char; |
| 4094 | } |
| 4095 | } |
| 4096 | continue; |
| 4097 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4098 | if (ch >= 0x10000) { |
| 4099 | /* code first surrogate */ |
| 4100 | base64bits += 16; |
| 4101 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4102 | while (base64bits >= 6) { |
| 4103 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4104 | base64bits -= 6; |
| 4105 | } |
| 4106 | /* prepare second surrogate */ |
| 4107 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4108 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4109 | base64bits += 16; |
| 4110 | base64buffer = (base64buffer << 16) | ch; |
| 4111 | while (base64bits >= 6) { |
| 4112 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4113 | base64bits -= 6; |
| 4114 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4115 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4116 | if (base64bits) |
| 4117 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4118 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4119 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4120 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4121 | return NULL; |
| 4122 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4123 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4124 | PyObject * |
| 4125 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4126 | Py_ssize_t size, |
| 4127 | int base64SetO, |
| 4128 | int base64WhiteSpace, |
| 4129 | const char *errors) |
| 4130 | { |
| 4131 | PyObject *result; |
| 4132 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4133 | if (tmp == NULL) |
| 4134 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4135 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4136 | base64WhiteSpace, errors); |
| 4137 | Py_DECREF(tmp); |
| 4138 | return result; |
| 4139 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4140 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4141 | #undef IS_BASE64 |
| 4142 | #undef FROM_BASE64 |
| 4143 | #undef TO_BASE64 |
| 4144 | #undef DECODE_DIRECT |
| 4145 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4147 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4148 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4149 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4150 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4151 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4152 | illegal prefix. See RFC 3629 for details */ |
| 4153 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4154 | 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] | 4155 | 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] | 4156 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4157 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4158 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4159 | 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] | 4160 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4161 | 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] | 4162 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4163 | 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] | 4164 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4165 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4166 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4167 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4168 | 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] | 4169 | }; |
| 4170 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4171 | PyObject * |
| 4172 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4173 | Py_ssize_t size, |
| 4174 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4175 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4176 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4177 | } |
| 4178 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4179 | #include "stringlib/ucs1lib.h" |
| 4180 | #include "stringlib/codecs.h" |
| 4181 | #include "stringlib/undef.h" |
| 4182 | |
| 4183 | #include "stringlib/ucs2lib.h" |
| 4184 | #include "stringlib/codecs.h" |
| 4185 | #include "stringlib/undef.h" |
| 4186 | |
| 4187 | #include "stringlib/ucs4lib.h" |
| 4188 | #include "stringlib/codecs.h" |
| 4189 | #include "stringlib/undef.h" |
| 4190 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4191 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4192 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4193 | |
| 4194 | /* Mask to quickly check whether a C 'long' contains a |
| 4195 | non-ASCII, UTF8-encoded char. */ |
| 4196 | #if (SIZEOF_LONG == 8) |
| 4197 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4198 | #elif (SIZEOF_LONG == 4) |
| 4199 | # define ASCII_CHAR_MASK 0x80808080L |
| 4200 | #else |
| 4201 | # error C 'long' size should be either 4 or 8! |
| 4202 | #endif |
| 4203 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4204 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4205 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4206 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4207 | This function doesn't check for errors, these checks are performed in |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4208 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4209 | */ |
| 4210 | static Py_UCS4 |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4211 | utf8_max_char_size_and_char_count(const char *s, Py_ssize_t string_size, |
| 4212 | Py_ssize_t *unicode_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4213 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4214 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4215 | const unsigned char *p = (const unsigned char *)s; |
| 4216 | const unsigned char *end = p + string_size; |
| 4217 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4218 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4219 | assert(unicode_size != NULL); |
| 4220 | |
| 4221 | /* By having a cascade of independent loops which fallback onto each |
| 4222 | other, we minimize the amount of work done in the average loop |
| 4223 | iteration, and we also maximize the CPU's ability to predict |
| 4224 | branches correctly (because a given condition will have always the |
| 4225 | same boolean outcome except perhaps in the last iteration of the |
| 4226 | corresponding loop). |
| 4227 | In the general case this brings us rather close to decoding |
| 4228 | performance pre-PEP 393, despite the two-pass decoding. |
| 4229 | |
| 4230 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4231 | character has been encountered. It is actually a pessimization (by |
| 4232 | a significant factor) to use this loop on text with many non-ASCII |
| 4233 | characters, and it is important to avoid bad performance on valid |
| 4234 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4235 | */ |
| 4236 | |
| 4237 | /* ASCII */ |
| 4238 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4239 | /* Only check value if it's not a ASCII char... */ |
| 4240 | if (*p < 0x80) { |
| 4241 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4242 | an explanation. */ |
| 4243 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4244 | /* Help register allocation */ |
| 4245 | register const unsigned char *_p = p; |
| 4246 | while (_p < aligned_end) { |
| 4247 | unsigned long value = *(unsigned long *) _p; |
| 4248 | if (value & ASCII_CHAR_MASK) |
| 4249 | break; |
| 4250 | _p += SIZEOF_LONG; |
| 4251 | char_count += SIZEOF_LONG; |
| 4252 | } |
| 4253 | p = _p; |
| 4254 | if (p == end) |
| 4255 | break; |
| 4256 | } |
| 4257 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4258 | if (*p < 0x80) |
| 4259 | ++char_count; |
| 4260 | else |
| 4261 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4262 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4263 | *unicode_size = char_count; |
| 4264 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4265 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4266 | _ucs1loop: |
| 4267 | for (; p < end; ++p) { |
| 4268 | if (*p < 0xc4) |
| 4269 | char_count += ((*p & 0xc0) != 0x80); |
| 4270 | else |
| 4271 | goto _ucs2loop; |
| 4272 | } |
| 4273 | *unicode_size = char_count; |
| 4274 | return 255; |
| 4275 | |
| 4276 | _ucs2loop: |
| 4277 | for (; p < end; ++p) { |
| 4278 | if (*p < 0xf0) |
| 4279 | char_count += ((*p & 0xc0) != 0x80); |
| 4280 | else |
| 4281 | goto _ucs4loop; |
| 4282 | } |
| 4283 | *unicode_size = char_count; |
| 4284 | return 65535; |
| 4285 | |
| 4286 | _ucs4loop: |
| 4287 | for (; p < end; ++p) { |
| 4288 | char_count += ((*p & 0xc0) != 0x80); |
| 4289 | } |
| 4290 | *unicode_size = char_count; |
| 4291 | return 65537; |
| 4292 | } |
| 4293 | |
| 4294 | /* Called when we encountered some error that wasn't detected in the original |
| 4295 | scan, e.g. an encoded surrogate character. The original maxchar computation |
| 4296 | may have been incorrect, so redo it. */ |
| 4297 | static int |
| 4298 | refit_partial_string(PyObject **unicode, int kind, void *data, Py_ssize_t n) |
| 4299 | { |
| 4300 | PyObject *tmp; |
| 4301 | Py_ssize_t k, maxchar; |
| 4302 | for (k = 0, maxchar = 0; k < n; k++) |
| 4303 | maxchar = Py_MAX(maxchar, PyUnicode_READ(kind, data, k)); |
| 4304 | tmp = PyUnicode_New(PyUnicode_GET_LENGTH(*unicode), maxchar); |
| 4305 | if (tmp == NULL) |
| 4306 | return -1; |
| 4307 | PyUnicode_CopyCharacters(tmp, 0, *unicode, 0, n); |
| 4308 | Py_DECREF(*unicode); |
| 4309 | *unicode = tmp; |
| 4310 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4311 | } |
| 4312 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4313 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
| 4314 | in case of errors. Implicit parameters: unicode, kind, data, has_errors, |
| 4315 | onError. Potential resizing overallocates, so the result needs to shrink |
| 4316 | at the end. |
| 4317 | */ |
| 4318 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4319 | do { \ |
| 4320 | if (has_errors) { \ |
| 4321 | Py_ssize_t pos = index; \ |
| 4322 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4323 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4324 | goto onError; \ |
| 4325 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4326 | goto onError; \ |
| 4327 | } \ |
| 4328 | else \ |
| 4329 | PyUnicode_WRITE(kind, data, index, value); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4330 | } while (0) |
| 4331 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4332 | PyObject * |
| 4333 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4334 | Py_ssize_t size, |
| 4335 | const char *errors, |
| 4336 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4337 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4338 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4339 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4340 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4341 | Py_ssize_t startinpos; |
| 4342 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4343 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4344 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4345 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4346 | PyObject *errorHandler = NULL; |
| 4347 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4348 | Py_UCS4 maxchar = 0; |
| 4349 | Py_ssize_t unicode_size; |
| 4350 | Py_ssize_t i; |
| 4351 | int kind; |
| 4352 | void *data; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4353 | int has_errors = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4355 | if (size == 0) { |
| 4356 | if (consumed) |
| 4357 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4358 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4359 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4360 | maxchar = utf8_max_char_size_and_char_count(s, size, &unicode_size); |
| 4361 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4362 | code below refits and resizes as necessary. */ |
| 4363 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4364 | if (!unicode) |
| 4365 | return NULL; |
| 4366 | /* When the string is ASCII only, just use memcpy and return. |
| 4367 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4368 | sequence at the end of the ASCII block. */ |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4369 | if (maxchar < 128 && size == unicode_size) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4370 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4371 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4372 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4373 | kind = PyUnicode_KIND(unicode); |
| 4374 | data = PyUnicode_DATA(unicode); |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4376 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4377 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4378 | e = s + size; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4379 | switch (kind) { |
| 4380 | case PyUnicode_1BYTE_KIND: |
| 4381 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4382 | break; |
| 4383 | case PyUnicode_2BYTE_KIND: |
| 4384 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4385 | break; |
| 4386 | case PyUnicode_4BYTE_KIND: |
| 4387 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4388 | break; |
| 4389 | } |
| 4390 | if (!has_errors) { |
| 4391 | /* Ensure the unicode size calculation was correct */ |
| 4392 | assert(i == unicode_size); |
| 4393 | assert(s == e); |
| 4394 | if (consumed) |
| 4395 | *consumed = s-starts; |
| 4396 | return unicode; |
| 4397 | } |
| 4398 | /* Fall through to the generic decoding loop for the rest of |
| 4399 | the string */ |
| 4400 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
| 4401 | goto onError; |
| 4402 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4403 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | |
| 4405 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4406 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4407 | |
| 4408 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4409 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4410 | input will consist of an overwhelming majority of ASCII |
| 4411 | characters, we try to optimize for this case by checking |
| 4412 | as many characters as a C 'long' can contain. |
| 4413 | First, check if we can do an aligned read, as most CPUs have |
| 4414 | a penalty for unaligned reads. |
| 4415 | */ |
| 4416 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4417 | /* Help register allocation */ |
| 4418 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4419 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4420 | while (_s < aligned_end) { |
| 4421 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4422 | and do a fast unrolled copy if it only contains ASCII |
| 4423 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4424 | unsigned long value = *(unsigned long *) _s; |
| 4425 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4426 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4427 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4428 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4429 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4430 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4431 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4432 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4433 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4434 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4435 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4436 | #endif |
| 4437 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4438 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4439 | } |
| 4440 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4441 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4442 | if (s == e) |
| 4443 | break; |
| 4444 | ch = (unsigned char)*s; |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4449 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4450 | s++; |
| 4451 | continue; |
| 4452 | } |
| 4453 | |
| 4454 | n = utf8_code_length[ch]; |
| 4455 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4456 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4457 | if (consumed) |
| 4458 | break; |
| 4459 | else { |
| 4460 | errmsg = "unexpected end of data"; |
| 4461 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4462 | endinpos = startinpos+1; |
| 4463 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4464 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4465 | goto utf8Error; |
| 4466 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4467 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4468 | |
| 4469 | switch (n) { |
| 4470 | |
| 4471 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4472 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4473 | startinpos = s-starts; |
| 4474 | endinpos = startinpos+1; |
| 4475 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | |
| 4477 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4478 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4479 | startinpos = s-starts; |
| 4480 | endinpos = startinpos+1; |
| 4481 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4482 | |
| 4483 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4484 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4485 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4486 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4487 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4488 | goto utf8Error; |
| 4489 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4490 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4491 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4492 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4493 | break; |
| 4494 | |
| 4495 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4496 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4497 | will result in surrogates in range d800-dfff. Surrogates are |
| 4498 | not valid UTF-8 so they are rejected. |
| 4499 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4500 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4501 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4502 | (s[2] & 0xc0) != 0x80 || |
| 4503 | ((unsigned char)s[0] == 0xE0 && |
| 4504 | (unsigned char)s[1] < 0xA0) || |
| 4505 | ((unsigned char)s[0] == 0xED && |
| 4506 | (unsigned char)s[1] > 0x9F)) { |
| 4507 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4508 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4509 | endinpos = startinpos + 1; |
| 4510 | |
| 4511 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4512 | continuation byte is s[2], so increment endinpos by 1, |
| 4513 | if not, s[1] is invalid and endinpos doesn't need to |
| 4514 | be incremented. */ |
| 4515 | if ((s[1] & 0xC0) == 0x80) |
| 4516 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4517 | goto utf8Error; |
| 4518 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4520 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4521 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4522 | break; |
| 4523 | |
| 4524 | case 4: |
| 4525 | if ((s[1] & 0xc0) != 0x80 || |
| 4526 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4527 | (s[3] & 0xc0) != 0x80 || |
| 4528 | ((unsigned char)s[0] == 0xF0 && |
| 4529 | (unsigned char)s[1] < 0x90) || |
| 4530 | ((unsigned char)s[0] == 0xF4 && |
| 4531 | (unsigned char)s[1] > 0x8F)) { |
| 4532 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4533 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4534 | endinpos = startinpos + 1; |
| 4535 | if ((s[1] & 0xC0) == 0x80) { |
| 4536 | endinpos++; |
| 4537 | if ((s[2] & 0xC0) == 0x80) |
| 4538 | endinpos++; |
| 4539 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4540 | goto utf8Error; |
| 4541 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4542 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4543 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4544 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4545 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4546 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4547 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4548 | } |
| 4549 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4550 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4551 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4552 | utf8Error: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4553 | if (!has_errors) { |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4554 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4555 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4556 | has_errors = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4557 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4558 | if (unicode_decode_call_errorhandler( |
| 4559 | errors, &errorHandler, |
| 4560 | "utf8", errmsg, |
| 4561 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4562 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4563 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4564 | /* Update data because unicode_decode_call_errorhandler might have |
| 4565 | re-created or resized the unicode object. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4566 | data = PyUnicode_DATA(unicode); |
| 4567 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4568 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4569 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4570 | /* Ensure the unicode_size calculation above was correct: */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4571 | assert(has_errors || i == unicode_size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4572 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4573 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4574 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4575 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4576 | /* Adjust length and ready string when it contained errors and |
| 4577 | is of the old resizable kind. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4578 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4579 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4580 | goto onError; |
| 4581 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4582 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4583 | Py_XDECREF(errorHandler); |
| 4584 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4585 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4586 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4587 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4588 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4589 | Py_XDECREF(errorHandler); |
| 4590 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4591 | Py_DECREF(unicode); |
| 4592 | return NULL; |
| 4593 | } |
| 4594 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4595 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4596 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4597 | #ifdef __APPLE__ |
| 4598 | |
| 4599 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4600 | used to decode the command line arguments on Mac OS X. */ |
| 4601 | |
| 4602 | wchar_t* |
| 4603 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4604 | { |
| 4605 | int n; |
| 4606 | const char *e; |
| 4607 | wchar_t *unicode, *p; |
| 4608 | |
| 4609 | /* Note: size will always be longer than the resulting Unicode |
| 4610 | character count */ |
| 4611 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4612 | PyErr_NoMemory(); |
| 4613 | return NULL; |
| 4614 | } |
| 4615 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4616 | if (!unicode) |
| 4617 | return NULL; |
| 4618 | |
| 4619 | /* Unpack UTF-8 encoded data */ |
| 4620 | p = unicode; |
| 4621 | e = s + size; |
| 4622 | while (s < e) { |
| 4623 | Py_UCS4 ch = (unsigned char)*s; |
| 4624 | |
| 4625 | if (ch < 0x80) { |
| 4626 | *p++ = (wchar_t)ch; |
| 4627 | s++; |
| 4628 | continue; |
| 4629 | } |
| 4630 | |
| 4631 | n = utf8_code_length[ch]; |
| 4632 | if (s + n > e) { |
| 4633 | goto surrogateescape; |
| 4634 | } |
| 4635 | |
| 4636 | switch (n) { |
| 4637 | case 0: |
| 4638 | case 1: |
| 4639 | goto surrogateescape; |
| 4640 | |
| 4641 | case 2: |
| 4642 | if ((s[1] & 0xc0) != 0x80) |
| 4643 | goto surrogateescape; |
| 4644 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4645 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4646 | *p++ = (wchar_t)ch; |
| 4647 | break; |
| 4648 | |
| 4649 | case 3: |
| 4650 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4651 | will result in surrogates in range d800-dfff. Surrogates are |
| 4652 | not valid UTF-8 so they are rejected. |
| 4653 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4654 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4655 | if ((s[1] & 0xc0) != 0x80 || |
| 4656 | (s[2] & 0xc0) != 0x80 || |
| 4657 | ((unsigned char)s[0] == 0xE0 && |
| 4658 | (unsigned char)s[1] < 0xA0) || |
| 4659 | ((unsigned char)s[0] == 0xED && |
| 4660 | (unsigned char)s[1] > 0x9F)) { |
| 4661 | |
| 4662 | goto surrogateescape; |
| 4663 | } |
| 4664 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4665 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4666 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4667 | break; |
| 4668 | |
| 4669 | case 4: |
| 4670 | if ((s[1] & 0xc0) != 0x80 || |
| 4671 | (s[2] & 0xc0) != 0x80 || |
| 4672 | (s[3] & 0xc0) != 0x80 || |
| 4673 | ((unsigned char)s[0] == 0xF0 && |
| 4674 | (unsigned char)s[1] < 0x90) || |
| 4675 | ((unsigned char)s[0] == 0xF4 && |
| 4676 | (unsigned char)s[1] > 0x8F)) { |
| 4677 | goto surrogateescape; |
| 4678 | } |
| 4679 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4680 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4681 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4682 | |
| 4683 | #if SIZEOF_WCHAR_T == 4 |
| 4684 | *p++ = (wchar_t)ch; |
| 4685 | #else |
| 4686 | /* compute and append the two surrogates: */ |
| 4687 | |
| 4688 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4689 | ch -= 0x10000; |
| 4690 | |
| 4691 | /* high surrogate = top 10 bits added to D800 */ |
| 4692 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4693 | |
| 4694 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4695 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4696 | #endif |
| 4697 | break; |
| 4698 | } |
| 4699 | s += n; |
| 4700 | continue; |
| 4701 | |
| 4702 | surrogateescape: |
| 4703 | *p++ = 0xDC00 + ch; |
| 4704 | s++; |
| 4705 | } |
| 4706 | *p = L'\0'; |
| 4707 | return unicode; |
| 4708 | } |
| 4709 | |
| 4710 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4711 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4712 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4713 | |
| 4714 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4715 | and allocate exactly as much space needed at the end. Else allocate the |
| 4716 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4717 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4718 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4719 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4720 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4721 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4722 | #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] | 4723 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4724 | Py_ssize_t i; /* index into s of next input byte */ |
| 4725 | PyObject *result; /* result string object */ |
| 4726 | char *p; /* next free byte in output buffer */ |
| 4727 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4728 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4729 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4730 | PyObject *errorHandler = NULL; |
| 4731 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4732 | int kind; |
| 4733 | void *data; |
| 4734 | Py_ssize_t size; |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4735 | PyObject *rep = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4737 | if (!PyUnicode_Check(unicode)) { |
| 4738 | PyErr_BadArgument(); |
| 4739 | return NULL; |
| 4740 | } |
| 4741 | |
| 4742 | if (PyUnicode_READY(unicode) == -1) |
| 4743 | return NULL; |
| 4744 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4745 | if (PyUnicode_UTF8(unicode)) |
| 4746 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4747 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4748 | |
| 4749 | kind = PyUnicode_KIND(unicode); |
| 4750 | data = PyUnicode_DATA(unicode); |
| 4751 | size = PyUnicode_GET_LENGTH(unicode); |
| 4752 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4753 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4754 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4755 | if (size <= MAX_SHORT_UNICHARS) { |
| 4756 | /* Write into the stack buffer; nallocated can't overflow. |
| 4757 | * At the end, we'll allocate exactly as much heap space as it |
| 4758 | * turns out we need. |
| 4759 | */ |
| 4760 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4761 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4762 | p = stackbuf; |
| 4763 | } |
| 4764 | else { |
| 4765 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4766 | nallocated = size * 4; |
| 4767 | if (nallocated / 4 != size) /* overflow! */ |
| 4768 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4769 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4770 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4771 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4772 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4773 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4774 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4775 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4776 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4777 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4778 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4779 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4780 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4782 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4783 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4784 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4785 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4786 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4787 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4788 | Py_ssize_t repsize, k, startpos; |
| 4789 | startpos = i-1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4790 | rep = unicode_encode_call_errorhandler( |
| 4791 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4792 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4793 | if (!rep) |
| 4794 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4796 | if (PyBytes_Check(rep)) |
| 4797 | repsize = PyBytes_GET_SIZE(rep); |
| 4798 | else |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 4799 | repsize = PyUnicode_GET_LENGTH(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4800 | |
| 4801 | if (repsize > 4) { |
| 4802 | Py_ssize_t offset; |
| 4803 | |
| 4804 | if (result == NULL) |
| 4805 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4806 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4807 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4808 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4809 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4810 | /* integer overflow */ |
| 4811 | PyErr_NoMemory(); |
| 4812 | goto error; |
| 4813 | } |
| 4814 | nallocated += repsize - 4; |
| 4815 | if (result != NULL) { |
| 4816 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4817 | goto error; |
| 4818 | } else { |
| 4819 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4820 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4821 | goto error; |
| 4822 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4823 | } |
| 4824 | p = PyBytes_AS_STRING(result) + offset; |
| 4825 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4827 | if (PyBytes_Check(rep)) { |
| 4828 | char *prep = PyBytes_AS_STRING(rep); |
| 4829 | for(k = repsize; k > 0; k--) |
| 4830 | *p++ = *prep++; |
| 4831 | } else /* rep is unicode */ { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4832 | enum PyUnicode_Kind repkind; |
| 4833 | void *repdata; |
| 4834 | |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4835 | if (PyUnicode_READY(rep) < 0) |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4836 | goto error; |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4837 | repkind = PyUnicode_KIND(rep); |
| 4838 | repdata = PyUnicode_DATA(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4839 | |
| 4840 | for(k=0; k<repsize; k++) { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4841 | Py_UCS4 c = PyUnicode_READ(repkind, repdata, k); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4842 | if (0x80 <= c) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 4843 | raise_encode_exception(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4844 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4845 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4846 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4847 | goto error; |
| 4848 | } |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4849 | *p++ = (char)c; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4850 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4851 | } |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4852 | Py_CLEAR(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4853 | } else if (ch < 0x10000) { |
| 4854 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4855 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4856 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4857 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4858 | /* Encode UCS4 Unicode ordinals */ |
| 4859 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4860 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4861 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4862 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4863 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4864 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4865 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4866 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4867 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4868 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4869 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4870 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4871 | } |
| 4872 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4873 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4874 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4875 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4876 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4877 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4878 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4879 | Py_XDECREF(errorHandler); |
| 4880 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4881 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4882 | error: |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4883 | Py_XDECREF(rep); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4884 | Py_XDECREF(errorHandler); |
| 4885 | Py_XDECREF(exc); |
| 4886 | Py_XDECREF(result); |
| 4887 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4888 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4889 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4892 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4893 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4894 | Py_ssize_t size, |
| 4895 | const char *errors) |
| 4896 | { |
| 4897 | PyObject *v, *unicode; |
| 4898 | |
| 4899 | unicode = PyUnicode_FromUnicode(s, size); |
| 4900 | if (unicode == NULL) |
| 4901 | return NULL; |
| 4902 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4903 | Py_DECREF(unicode); |
| 4904 | return v; |
| 4905 | } |
| 4906 | |
| 4907 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4908 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4909 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4910 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4913 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4914 | |
| 4915 | PyObject * |
| 4916 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4917 | Py_ssize_t size, |
| 4918 | const char *errors, |
| 4919 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4920 | { |
| 4921 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4922 | } |
| 4923 | |
| 4924 | PyObject * |
| 4925 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4926 | Py_ssize_t size, |
| 4927 | const char *errors, |
| 4928 | int *byteorder, |
| 4929 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4930 | { |
| 4931 | const char *starts = s; |
| 4932 | Py_ssize_t startinpos; |
| 4933 | Py_ssize_t endinpos; |
| 4934 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4935 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4936 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4937 | int bo = 0; /* assume native ordering by default */ |
| 4938 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4939 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4940 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4941 | int iorder[] = {0, 1, 2, 3}; |
| 4942 | #else |
| 4943 | int iorder[] = {3, 2, 1, 0}; |
| 4944 | #endif |
| 4945 | PyObject *errorHandler = NULL; |
| 4946 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4947 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4948 | q = (unsigned char *)s; |
| 4949 | e = q + size; |
| 4950 | |
| 4951 | if (byteorder) |
| 4952 | bo = *byteorder; |
| 4953 | |
| 4954 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4955 | byte order setting accordingly. In native mode, the leading BOM |
| 4956 | mark is skipped, in all other modes, it is copied to the output |
| 4957 | stream as-is (giving a ZWNBSP character). */ |
| 4958 | if (bo == 0) { |
| 4959 | if (size >= 4) { |
| 4960 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4961 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4962 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | if (bom == 0x0000FEFF) { |
| 4964 | q += 4; |
| 4965 | bo = -1; |
| 4966 | } |
| 4967 | else if (bom == 0xFFFE0000) { |
| 4968 | q += 4; |
| 4969 | bo = 1; |
| 4970 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4971 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4972 | if (bom == 0x0000FEFF) { |
| 4973 | q += 4; |
| 4974 | bo = 1; |
| 4975 | } |
| 4976 | else if (bom == 0xFFFE0000) { |
| 4977 | q += 4; |
| 4978 | bo = -1; |
| 4979 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4980 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4981 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | if (bo == -1) { |
| 4985 | /* force LE */ |
| 4986 | iorder[0] = 0; |
| 4987 | iorder[1] = 1; |
| 4988 | iorder[2] = 2; |
| 4989 | iorder[3] = 3; |
| 4990 | } |
| 4991 | else if (bo == 1) { |
| 4992 | /* force BE */ |
| 4993 | iorder[0] = 3; |
| 4994 | iorder[1] = 2; |
| 4995 | iorder[2] = 1; |
| 4996 | iorder[3] = 0; |
| 4997 | } |
| 4998 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4999 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5000 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5001 | if (!unicode) |
| 5002 | return NULL; |
| 5003 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5004 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5005 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5006 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5007 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5008 | Py_UCS4 ch; |
| 5009 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5010 | if (e-q<4) { |
| 5011 | if (consumed) |
| 5012 | break; |
| 5013 | errmsg = "truncated data"; |
| 5014 | startinpos = ((const char *)q)-starts; |
| 5015 | endinpos = ((const char *)e)-starts; |
| 5016 | goto utf32Error; |
| 5017 | /* The remaining input chars are ignored if the callback |
| 5018 | chooses to skip the input */ |
| 5019 | } |
| 5020 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5021 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5022 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5023 | if (ch >= 0x110000) |
| 5024 | { |
| 5025 | errmsg = "codepoint not in range(0x110000)"; |
| 5026 | startinpos = ((const char *)q)-starts; |
| 5027 | endinpos = startinpos+4; |
| 5028 | goto utf32Error; |
| 5029 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5030 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5031 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5032 | q += 4; |
| 5033 | continue; |
| 5034 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5035 | if (unicode_decode_call_errorhandler( |
| 5036 | errors, &errorHandler, |
| 5037 | "utf32", errmsg, |
| 5038 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5039 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5040 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | if (byteorder) |
| 5044 | *byteorder = bo; |
| 5045 | |
| 5046 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5048 | |
| 5049 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5050 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | goto onError; |
| 5052 | |
| 5053 | Py_XDECREF(errorHandler); |
| 5054 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5055 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5056 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5057 | Py_DECREF(unicode); |
| 5058 | return NULL; |
| 5059 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5060 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5061 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5062 | return unicode; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5063 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5064 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5065 | Py_DECREF(unicode); |
| 5066 | Py_XDECREF(errorHandler); |
| 5067 | Py_XDECREF(exc); |
| 5068 | return NULL; |
| 5069 | } |
| 5070 | |
| 5071 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5072 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5073 | const char *errors, |
| 5074 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5076 | int kind; |
| 5077 | void *data; |
| 5078 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5079 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5080 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5081 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5082 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5083 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5084 | int iorder[] = {0, 1, 2, 3}; |
| 5085 | #else |
| 5086 | int iorder[] = {3, 2, 1, 0}; |
| 5087 | #endif |
| 5088 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5089 | #define STORECHAR(CH) \ |
| 5090 | do { \ |
| 5091 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5092 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5093 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5094 | p[iorder[0]] = (CH) & 0xff; \ |
| 5095 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5096 | } while(0) |
| 5097 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5098 | if (!PyUnicode_Check(str)) { |
| 5099 | PyErr_BadArgument(); |
| 5100 | return NULL; |
| 5101 | } |
| 5102 | if (PyUnicode_READY(str) < 0) |
| 5103 | return NULL; |
| 5104 | kind = PyUnicode_KIND(str); |
| 5105 | data = PyUnicode_DATA(str); |
| 5106 | len = PyUnicode_GET_LENGTH(str); |
| 5107 | |
| 5108 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5109 | bytesize = nsize * 4; |
| 5110 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5111 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5112 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5113 | if (v == NULL) |
| 5114 | return NULL; |
| 5115 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5116 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5117 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5118 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5119 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5120 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5121 | |
| 5122 | if (byteorder == -1) { |
| 5123 | /* force LE */ |
| 5124 | iorder[0] = 0; |
| 5125 | iorder[1] = 1; |
| 5126 | iorder[2] = 2; |
| 5127 | iorder[3] = 3; |
| 5128 | } |
| 5129 | else if (byteorder == 1) { |
| 5130 | /* force BE */ |
| 5131 | iorder[0] = 3; |
| 5132 | iorder[1] = 2; |
| 5133 | iorder[2] = 1; |
| 5134 | iorder[3] = 0; |
| 5135 | } |
| 5136 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5137 | for (i = 0; i < len; i++) |
| 5138 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5139 | |
| 5140 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5141 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5142 | #undef STORECHAR |
| 5143 | } |
| 5144 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5145 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5146 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5147 | Py_ssize_t size, |
| 5148 | const char *errors, |
| 5149 | int byteorder) |
| 5150 | { |
| 5151 | PyObject *result; |
| 5152 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5153 | if (tmp == NULL) |
| 5154 | return NULL; |
| 5155 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5156 | Py_DECREF(tmp); |
| 5157 | return result; |
| 5158 | } |
| 5159 | |
| 5160 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5161 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5162 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5163 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5166 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5167 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5168 | PyObject * |
| 5169 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5170 | Py_ssize_t size, |
| 5171 | const char *errors, |
| 5172 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5173 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5174 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5175 | } |
| 5176 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5177 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5178 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5179 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5180 | rare in most input. |
| 5181 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5182 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5183 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5184 | #if (SIZEOF_LONG == 8) |
| 5185 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5186 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5187 | #elif (SIZEOF_LONG == 4) |
| 5188 | # define FAST_CHAR_MASK 0x80008000L |
| 5189 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5190 | #else |
| 5191 | # error C 'long' size should be either 4 or 8! |
| 5192 | #endif |
| 5193 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5194 | PyObject * |
| 5195 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5196 | Py_ssize_t size, |
| 5197 | const char *errors, |
| 5198 | int *byteorder, |
| 5199 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5200 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5201 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5202 | Py_ssize_t startinpos; |
| 5203 | Py_ssize_t endinpos; |
| 5204 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5205 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5206 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5207 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5208 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5209 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5210 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5211 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5212 | int ihi = 1, ilo = 0; |
| 5213 | #else |
| 5214 | int ihi = 0, ilo = 1; |
| 5215 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5216 | PyObject *errorHandler = NULL; |
| 5217 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5218 | |
| 5219 | /* Note: size will always be longer than the resulting Unicode |
| 5220 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5221 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5222 | if (!unicode) |
| 5223 | return NULL; |
| 5224 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5225 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5226 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5227 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5228 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5229 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5230 | |
| 5231 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5232 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5233 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5234 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5235 | byte order setting accordingly. In native mode, the leading BOM |
| 5236 | mark is skipped, in all other modes, it is copied to the output |
| 5237 | stream as-is (giving a ZWNBSP character). */ |
| 5238 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5239 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5240 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5241 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5242 | if (bom == 0xFEFF) { |
| 5243 | q += 2; |
| 5244 | bo = -1; |
| 5245 | } |
| 5246 | else if (bom == 0xFFFE) { |
| 5247 | q += 2; |
| 5248 | bo = 1; |
| 5249 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5250 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5251 | if (bom == 0xFEFF) { |
| 5252 | q += 2; |
| 5253 | bo = 1; |
| 5254 | } |
| 5255 | else if (bom == 0xFFFE) { |
| 5256 | q += 2; |
| 5257 | bo = -1; |
| 5258 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5259 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5260 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5261 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5263 | if (bo == -1) { |
| 5264 | /* force LE */ |
| 5265 | ihi = 1; |
| 5266 | ilo = 0; |
| 5267 | } |
| 5268 | else if (bo == 1) { |
| 5269 | /* force BE */ |
| 5270 | ihi = 0; |
| 5271 | ilo = 1; |
| 5272 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5273 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5274 | native_ordering = ilo < ihi; |
| 5275 | #else |
| 5276 | native_ordering = ilo > ihi; |
| 5277 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5278 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5279 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5280 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5281 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5282 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5283 | reads are more expensive, better to defer to another iteration. */ |
| 5284 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5285 | /* Fast path for runs of non-surrogate chars. */ |
| 5286 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5287 | int kind = PyUnicode_KIND(unicode); |
| 5288 | void *data = PyUnicode_DATA(unicode); |
| 5289 | while (_q < aligned_end) { |
| 5290 | unsigned long block = * (unsigned long *) _q; |
| 5291 | unsigned short *pblock = (unsigned short*)█ |
| 5292 | Py_UCS4 maxch; |
| 5293 | if (native_ordering) { |
| 5294 | /* Can use buffer directly */ |
| 5295 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5296 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5297 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5298 | else { |
| 5299 | /* Need to byte-swap */ |
| 5300 | unsigned char *_p = (unsigned char*)pblock; |
| 5301 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5302 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5303 | _p[0] = _q[1]; |
| 5304 | _p[1] = _q[0]; |
| 5305 | _p[2] = _q[3]; |
| 5306 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5307 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5308 | _p[4] = _q[5]; |
| 5309 | _p[5] = _q[4]; |
| 5310 | _p[6] = _q[7]; |
| 5311 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5312 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5313 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5314 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5315 | #if SIZEOF_LONG == 8 |
| 5316 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5317 | #endif |
| 5318 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5319 | if (unicode_widen(&unicode, maxch) < 0) |
| 5320 | goto onError; |
| 5321 | kind = PyUnicode_KIND(unicode); |
| 5322 | data = PyUnicode_DATA(unicode); |
| 5323 | } |
| 5324 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5325 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5326 | #if SIZEOF_LONG == 8 |
| 5327 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5328 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5329 | #endif |
| 5330 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5331 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5332 | q = _q; |
| 5333 | if (q >= e) |
| 5334 | break; |
| 5335 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5337 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5338 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5339 | |
| 5340 | if (ch < 0xD800 || ch > 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5341 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5342 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5343 | continue; |
| 5344 | } |
| 5345 | |
| 5346 | /* UTF-16 code pair: */ |
| 5347 | if (q > e) { |
| 5348 | errmsg = "unexpected end of data"; |
| 5349 | startinpos = (((const char *)q) - 2) - starts; |
| 5350 | endinpos = ((const char *)e) + 1 - starts; |
| 5351 | goto utf16Error; |
| 5352 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5353 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5354 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5355 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5356 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5357 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5358 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5359 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5360 | continue; |
| 5361 | } |
| 5362 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5363 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5364 | startinpos = (((const char *)q)-4)-starts; |
| 5365 | endinpos = startinpos+2; |
| 5366 | goto utf16Error; |
| 5367 | } |
| 5368 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5369 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5370 | errmsg = "illegal encoding"; |
| 5371 | startinpos = (((const char *)q)-2)-starts; |
| 5372 | endinpos = startinpos+2; |
| 5373 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5374 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5375 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5377 | errors, |
| 5378 | &errorHandler, |
| 5379 | "utf16", errmsg, |
| 5380 | &starts, |
| 5381 | (const char **)&e, |
| 5382 | &startinpos, |
| 5383 | &endinpos, |
| 5384 | &exc, |
| 5385 | (const char **)&q, |
| 5386 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5387 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5388 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5390 | /* remaining byte at the end? (size should be even) */ |
| 5391 | if (e == q) { |
| 5392 | if (!consumed) { |
| 5393 | errmsg = "truncated data"; |
| 5394 | startinpos = ((const char *)q) - starts; |
| 5395 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5396 | if (unicode_decode_call_errorhandler( |
| 5397 | errors, |
| 5398 | &errorHandler, |
| 5399 | "utf16", errmsg, |
| 5400 | &starts, |
| 5401 | (const char **)&e, |
| 5402 | &startinpos, |
| 5403 | &endinpos, |
| 5404 | &exc, |
| 5405 | (const char **)&q, |
| 5406 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5407 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5408 | goto onError; |
| 5409 | /* The remaining input chars are ignored if the callback |
| 5410 | chooses to skip the input */ |
| 5411 | } |
| 5412 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5413 | |
| 5414 | if (byteorder) |
| 5415 | *byteorder = bo; |
| 5416 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5417 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5421 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | goto onError; |
| 5423 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5424 | Py_XDECREF(errorHandler); |
| 5425 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5426 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5427 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5431 | Py_XDECREF(errorHandler); |
| 5432 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | return NULL; |
| 5434 | } |
| 5435 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5436 | #undef FAST_CHAR_MASK |
| 5437 | #undef SWAPPED_FAST_CHAR_MASK |
| 5438 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5439 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5440 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5441 | const char *errors, |
| 5442 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5444 | int kind; |
| 5445 | void *data; |
| 5446 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5447 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5448 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5449 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5450 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5451 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5452 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5453 | int ihi = 1, ilo = 0; |
| 5454 | #else |
| 5455 | int ihi = 0, ilo = 1; |
| 5456 | #endif |
| 5457 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5458 | #define STORECHAR(CH) \ |
| 5459 | do { \ |
| 5460 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5461 | p[ilo] = (CH) & 0xff; \ |
| 5462 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5463 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5465 | if (!PyUnicode_Check(str)) { |
| 5466 | PyErr_BadArgument(); |
| 5467 | return NULL; |
| 5468 | } |
| 5469 | if (PyUnicode_READY(str) < 0) |
| 5470 | return NULL; |
| 5471 | kind = PyUnicode_KIND(str); |
| 5472 | data = PyUnicode_DATA(str); |
| 5473 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5474 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5475 | pairs = 0; |
| 5476 | if (kind == PyUnicode_4BYTE_KIND) |
| 5477 | for (i = 0; i < len; i++) |
| 5478 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5479 | pairs++; |
| 5480 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5481 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5482 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5483 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5484 | bytesize = nsize * 2; |
| 5485 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5486 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5487 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5488 | if (v == NULL) |
| 5489 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5490 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5491 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5493 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5494 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5495 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5496 | |
| 5497 | if (byteorder == -1) { |
| 5498 | /* force LE */ |
| 5499 | ihi = 1; |
| 5500 | ilo = 0; |
| 5501 | } |
| 5502 | else if (byteorder == 1) { |
| 5503 | /* force BE */ |
| 5504 | ihi = 0; |
| 5505 | ilo = 1; |
| 5506 | } |
| 5507 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5508 | for (i = 0; i < len; i++) { |
| 5509 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5510 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | if (ch >= 0x10000) { |
| 5512 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5513 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5514 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5515 | STORECHAR(ch); |
| 5516 | if (ch2) |
| 5517 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5518 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5519 | |
| 5520 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5521 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5522 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5525 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5526 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5527 | Py_ssize_t size, |
| 5528 | const char *errors, |
| 5529 | int byteorder) |
| 5530 | { |
| 5531 | PyObject *result; |
| 5532 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5533 | if (tmp == NULL) |
| 5534 | return NULL; |
| 5535 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5536 | Py_DECREF(tmp); |
| 5537 | return result; |
| 5538 | } |
| 5539 | |
| 5540 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5541 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5543 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5547 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5548 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5549 | if all the escapes in the string make it still a valid ASCII string. |
| 5550 | Returns -1 if any escapes were found which cause the string to |
| 5551 | pop out of ASCII range. Otherwise returns the length of the |
| 5552 | required buffer to hold the string. |
| 5553 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5554 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5555 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5556 | { |
| 5557 | const unsigned char *p = (const unsigned char *)s; |
| 5558 | const unsigned char *end = p + size; |
| 5559 | Py_ssize_t length = 0; |
| 5560 | |
| 5561 | if (size < 0) |
| 5562 | return -1; |
| 5563 | |
| 5564 | for (; p < end; ++p) { |
| 5565 | if (*p > 127) { |
| 5566 | /* Non-ASCII */ |
| 5567 | return -1; |
| 5568 | } |
| 5569 | else if (*p != '\\') { |
| 5570 | /* Normal character */ |
| 5571 | ++length; |
| 5572 | } |
| 5573 | else { |
| 5574 | /* Backslash-escape, check next char */ |
| 5575 | ++p; |
| 5576 | /* Escape sequence reaches till end of string or |
| 5577 | non-ASCII follow-up. */ |
| 5578 | if (p >= end || *p > 127) |
| 5579 | return -1; |
| 5580 | switch (*p) { |
| 5581 | case '\n': |
| 5582 | /* backslash + \n result in zero characters */ |
| 5583 | break; |
| 5584 | case '\\': case '\'': case '\"': |
| 5585 | case 'b': case 'f': case 't': |
| 5586 | case 'n': case 'r': case 'v': case 'a': |
| 5587 | ++length; |
| 5588 | break; |
| 5589 | case '0': case '1': case '2': case '3': |
| 5590 | case '4': case '5': case '6': case '7': |
| 5591 | case 'x': case 'u': case 'U': case 'N': |
| 5592 | /* these do not guarantee ASCII characters */ |
| 5593 | return -1; |
| 5594 | default: |
| 5595 | /* count the backslash + the other character */ |
| 5596 | length += 2; |
| 5597 | } |
| 5598 | } |
| 5599 | } |
| 5600 | return length; |
| 5601 | } |
| 5602 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5603 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5604 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5605 | PyObject * |
| 5606 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5607 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5608 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5610 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5611 | Py_ssize_t startinpos; |
| 5612 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5613 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5614 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5616 | char* message; |
| 5617 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5618 | PyObject *errorHandler = NULL; |
| 5619 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5620 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5621 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5622 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5623 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5624 | |
| 5625 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5626 | either the string is pure ASCII with named escapes like \n, etc. |
| 5627 | and we determined it's exact size (common case) |
| 5628 | or it contains \x, \u, ... escape sequences. then we create a |
| 5629 | 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] | 5630 | if (len >= 0) { |
| 5631 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5632 | if (!v) |
| 5633 | goto onError; |
| 5634 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5635 | } |
| 5636 | else { |
| 5637 | /* Escaped strings will always be longer than the resulting |
| 5638 | Unicode string, so we start with size here and then reduce the |
| 5639 | length after conversion to the true value. |
| 5640 | (but if the error callback returns a long replacement string |
| 5641 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5642 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5643 | if (!v) |
| 5644 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5645 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5646 | } |
| 5647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5649 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5650 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | while (s < end) { |
| 5654 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5655 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5656 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5658 | /* The only case in which i == ascii_length is a backslash |
| 5659 | followed by a newline. */ |
| 5660 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5663 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5664 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5665 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | continue; |
| 5667 | } |
| 5668 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5669 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | /* \ - Escapes */ |
| 5671 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5672 | c = *s++; |
| 5673 | if (s > end) |
| 5674 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5675 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5676 | /* The only case in which i == ascii_length is a backslash |
| 5677 | followed by a newline. */ |
| 5678 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5679 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5680 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5683 | #define WRITECHAR(ch) \ |
| 5684 | do { \ |
| 5685 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5686 | goto onError; \ |
| 5687 | }while(0) |
| 5688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5690 | case '\\': WRITECHAR('\\'); break; |
| 5691 | case '\'': WRITECHAR('\''); break; |
| 5692 | case '\"': WRITECHAR('\"'); break; |
| 5693 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5694 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5695 | case 'f': WRITECHAR('\014'); break; |
| 5696 | case 't': WRITECHAR('\t'); break; |
| 5697 | case 'n': WRITECHAR('\n'); break; |
| 5698 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5699 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5700 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5701 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5702 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5704 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 | case '0': case '1': case '2': case '3': |
| 5706 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5707 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5708 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5709 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5710 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5711 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5713 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | break; |
| 5715 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5716 | /* hex escapes */ |
| 5717 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5718 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5719 | digits = 2; |
| 5720 | message = "truncated \\xXX escape"; |
| 5721 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5722 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5723 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5725 | digits = 4; |
| 5726 | message = "truncated \\uXXXX escape"; |
| 5727 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5729 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5730 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5731 | digits = 8; |
| 5732 | message = "truncated \\UXXXXXXXX escape"; |
| 5733 | hexescape: |
| 5734 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5735 | if (s+digits>end) { |
| 5736 | endinpos = size; |
| 5737 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | errors, &errorHandler, |
| 5739 | "unicodeescape", "end of string in escape sequence", |
| 5740 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5741 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5742 | goto onError; |
| 5743 | goto nextByte; |
| 5744 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5745 | for (j = 0; j < digits; ++j) { |
| 5746 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5747 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5748 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5749 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 | errors, &errorHandler, |
| 5751 | "unicodeescape", message, |
| 5752 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5753 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5754 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5755 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5756 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5757 | } |
| 5758 | chr = (chr<<4) & ~0xF; |
| 5759 | if (c >= '0' && c <= '9') |
| 5760 | chr += c - '0'; |
| 5761 | else if (c >= 'a' && c <= 'f') |
| 5762 | chr += 10 + c - 'a'; |
| 5763 | else |
| 5764 | chr += 10 + c - 'A'; |
| 5765 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5766 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5767 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | /* _decoding_error will have already written into the |
| 5769 | target buffer. */ |
| 5770 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5771 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5772 | /* 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] | 5773 | if (chr <= 0x10ffff) { |
| 5774 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5775 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5776 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5777 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5778 | errors, &errorHandler, |
| 5779 | "unicodeescape", "illegal Unicode character", |
| 5780 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5781 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5782 | goto onError; |
| 5783 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5784 | break; |
| 5785 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5786 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5787 | case 'N': |
| 5788 | message = "malformed \\N character escape"; |
| 5789 | if (ucnhash_CAPI == NULL) { |
| 5790 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5791 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5792 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5793 | if (ucnhash_CAPI == NULL) |
| 5794 | goto ucnhashError; |
| 5795 | } |
| 5796 | if (*s == '{') { |
| 5797 | const char *start = s+1; |
| 5798 | /* look for the closing brace */ |
| 5799 | while (*s != '}' && s < end) |
| 5800 | s++; |
| 5801 | if (s > start && s < end && *s == '}') { |
| 5802 | /* found a name. look it up in the unicode database */ |
| 5803 | message = "unknown Unicode character name"; |
| 5804 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5805 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5806 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5807 | goto store; |
| 5808 | } |
| 5809 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5811 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | errors, &errorHandler, |
| 5813 | "unicodeescape", message, |
| 5814 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5815 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5816 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5817 | break; |
| 5818 | |
| 5819 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5820 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5821 | message = "\\ at end of string"; |
| 5822 | s--; |
| 5823 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5824 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | errors, &errorHandler, |
| 5826 | "unicodeescape", message, |
| 5827 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5828 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5829 | goto onError; |
| 5830 | } |
| 5831 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5832 | WRITECHAR('\\'); |
| 5833 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5834 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5835 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5837 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5838 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5839 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5840 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5841 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5842 | if (PyUnicode_Resize(&v, i) < 0) |
| 5843 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5844 | Py_XDECREF(errorHandler); |
| 5845 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5846 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5847 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5848 | Py_DECREF(v); |
| 5849 | return NULL; |
| 5850 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5851 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5852 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5853 | return v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5854 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5855 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5856 | PyErr_SetString( |
| 5857 | PyExc_UnicodeError, |
| 5858 | "\\N escapes not supported (can't load unicodedata module)" |
| 5859 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5860 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5861 | Py_XDECREF(errorHandler); |
| 5862 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5863 | return NULL; |
| 5864 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5867 | Py_XDECREF(errorHandler); |
| 5868 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | return NULL; |
| 5870 | } |
| 5871 | |
| 5872 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5873 | |
| 5874 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5875 | appropriate. |
| 5876 | |
| 5877 | */ |
| 5878 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5879 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5880 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5882 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5883 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5885 | int kind; |
| 5886 | void *data; |
| 5887 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5889 | /* Initial allocation is based on the longest-possible unichr |
| 5890 | escape. |
| 5891 | |
| 5892 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5893 | unichr, so in this case it's the longest unichr escape. In |
| 5894 | narrow (UTF-16) builds this is five chars per source unichr |
| 5895 | since there are two unichrs in the surrogate pair, so in narrow |
| 5896 | (UTF-16) builds it's not the longest unichr escape. |
| 5897 | |
| 5898 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5899 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5900 | escape. |
| 5901 | */ |
| 5902 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5903 | if (!PyUnicode_Check(unicode)) { |
| 5904 | PyErr_BadArgument(); |
| 5905 | return NULL; |
| 5906 | } |
| 5907 | if (PyUnicode_READY(unicode) < 0) |
| 5908 | return NULL; |
| 5909 | len = PyUnicode_GET_LENGTH(unicode); |
| 5910 | kind = PyUnicode_KIND(unicode); |
| 5911 | data = PyUnicode_DATA(unicode); |
| 5912 | switch(kind) { |
| 5913 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5914 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5915 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5916 | } |
| 5917 | |
| 5918 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5919 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5920 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5921 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5922 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5923 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5924 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5926 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5927 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | if (repr == NULL) |
| 5929 | return NULL; |
| 5930 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5931 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5933 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5934 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5935 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5936 | /* Escape backslashes */ |
| 5937 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | *p++ = '\\'; |
| 5939 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5940 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5941 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5942 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5943 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5944 | else if (ch >= 0x10000) { |
| 5945 | *p++ = '\\'; |
| 5946 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5947 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5948 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5949 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5950 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5951 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5952 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5953 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5954 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5956 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5959 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | *p++ = '\\'; |
| 5961 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5962 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5963 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5964 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5965 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5967 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5968 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5969 | else if (ch == '\t') { |
| 5970 | *p++ = '\\'; |
| 5971 | *p++ = 't'; |
| 5972 | } |
| 5973 | else if (ch == '\n') { |
| 5974 | *p++ = '\\'; |
| 5975 | *p++ = 'n'; |
| 5976 | } |
| 5977 | else if (ch == '\r') { |
| 5978 | *p++ = '\\'; |
| 5979 | *p++ = 'r'; |
| 5980 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5981 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5982 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5983 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5985 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5986 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5987 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5988 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | /* Copy everything else as-is */ |
| 5991 | else |
| 5992 | *p++ = (char) ch; |
| 5993 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5995 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5996 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5997 | return NULL; |
| 5998 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6001 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6002 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6003 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6004 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6005 | PyObject *result; |
| 6006 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6007 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6009 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6010 | Py_DECREF(tmp); |
| 6011 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
| 6014 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6016 | PyObject * |
| 6017 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6018 | Py_ssize_t size, |
| 6019 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6021 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6022 | Py_ssize_t startinpos; |
| 6023 | Py_ssize_t endinpos; |
| 6024 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6025 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | const char *end; |
| 6027 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6028 | PyObject *errorHandler = NULL; |
| 6029 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6030 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | /* Escaped strings will always be longer than the resulting |
| 6032 | 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] | 6033 | length after conversion to the true value. (But decoding error |
| 6034 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6035 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6037 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6039 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6040 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | end = s + size; |
| 6042 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6043 | unsigned char c; |
| 6044 | Py_UCS4 x; |
| 6045 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6046 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6049 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6050 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6051 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6053 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | startinpos = s-starts; |
| 6055 | |
| 6056 | /* \u-escapes are only interpreted iff the number of leading |
| 6057 | backslashes if odd */ |
| 6058 | bs = s; |
| 6059 | for (;s < end;) { |
| 6060 | if (*s != '\\') |
| 6061 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6062 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6063 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6064 | } |
| 6065 | if (((s - bs) & 1) == 0 || |
| 6066 | s >= end || |
| 6067 | (*s != 'u' && *s != 'U')) { |
| 6068 | continue; |
| 6069 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6070 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | count = *s=='u' ? 4 : 8; |
| 6072 | s++; |
| 6073 | |
| 6074 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6076 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6077 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6078 | endinpos = s-starts; |
| 6079 | if (unicode_decode_call_errorhandler( |
| 6080 | errors, &errorHandler, |
| 6081 | "rawunicodeescape", "truncated \\uXXXX", |
| 6082 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6083 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6084 | goto onError; |
| 6085 | goto nextByte; |
| 6086 | } |
| 6087 | x = (x<<4) & ~0xF; |
| 6088 | if (c >= '0' && c <= '9') |
| 6089 | x += c - '0'; |
| 6090 | else if (c >= 'a' && c <= 'f') |
| 6091 | x += 10 + c - 'a'; |
| 6092 | else |
| 6093 | x += 10 + c - 'A'; |
| 6094 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6095 | if (x <= 0x10ffff) { |
| 6096 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6097 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6098 | } else { |
| 6099 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6100 | if (unicode_decode_call_errorhandler( |
| 6101 | errors, &errorHandler, |
| 6102 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6104 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6105 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6106 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6107 | nextByte: |
| 6108 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6110 | if (PyUnicode_Resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6111 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6112 | Py_XDECREF(errorHandler); |
| 6113 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6114 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6115 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6116 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6117 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6118 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6119 | Py_XDECREF(errorHandler); |
| 6120 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | return NULL; |
| 6122 | } |
| 6123 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6124 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6125 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6126 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6128 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | char *p; |
| 6130 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6131 | Py_ssize_t expandsize, pos; |
| 6132 | int kind; |
| 6133 | void *data; |
| 6134 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6136 | if (!PyUnicode_Check(unicode)) { |
| 6137 | PyErr_BadArgument(); |
| 6138 | return NULL; |
| 6139 | } |
| 6140 | if (PyUnicode_READY(unicode) < 0) |
| 6141 | return NULL; |
| 6142 | kind = PyUnicode_KIND(unicode); |
| 6143 | data = PyUnicode_DATA(unicode); |
| 6144 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6145 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6146 | switch(kind) { |
| 6147 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6148 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6149 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6150 | } |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6151 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6152 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6153 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6154 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6155 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | if (repr == NULL) |
| 6157 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6158 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6159 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6161 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6162 | for (pos = 0; pos < len; pos++) { |
| 6163 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6164 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6165 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6166 | *p++ = '\\'; |
| 6167 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6168 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6169 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6170 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6171 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6172 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6173 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6174 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6175 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6176 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6177 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6178 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | *p++ = '\\'; |
| 6180 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6181 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6182 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6183 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6184 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6186 | /* Copy everything else as-is */ |
| 6187 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | *p++ = (char) ch; |
| 6189 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6190 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6191 | assert(p > q); |
| 6192 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6193 | return NULL; |
| 6194 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | } |
| 6196 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6197 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6198 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6199 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6201 | PyObject *result; |
| 6202 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6203 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6204 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6205 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6206 | Py_DECREF(tmp); |
| 6207 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6210 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6212 | PyObject * |
| 6213 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6214 | Py_ssize_t size, |
| 6215 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6216 | { |
| 6217 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6218 | Py_ssize_t startinpos; |
| 6219 | Py_ssize_t endinpos; |
| 6220 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6221 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6222 | const char *end; |
| 6223 | const char *reason; |
| 6224 | PyObject *errorHandler = NULL; |
| 6225 | PyObject *exc = NULL; |
| 6226 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6227 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6228 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6229 | 1)) |
| 6230 | return NULL; |
| 6231 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6232 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6233 | 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] | 6234 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6235 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6236 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6237 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6238 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6239 | end = s + size; |
| 6240 | |
| 6241 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6242 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6243 | Py_UCS4 ch; |
| 6244 | /* We copy the raw representation one byte at a time because the |
| 6245 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6246 | ((char *) &uch)[0] = s[0]; |
| 6247 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6248 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6249 | ((char *) &uch)[2] = s[2]; |
| 6250 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6251 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6252 | ch = uch; |
| 6253 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6254 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6255 | some malformed UCS-4 data. */ |
| 6256 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6258 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6260 | end-s < Py_UNICODE_SIZE |
| 6261 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6263 | startinpos = s - starts; |
| 6264 | if (end-s < Py_UNICODE_SIZE) { |
| 6265 | endinpos = end-starts; |
| 6266 | reason = "truncated input"; |
| 6267 | } |
| 6268 | else { |
| 6269 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6270 | reason = "illegal code point (> 0x10FFFF)"; |
| 6271 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6272 | if (unicode_decode_call_errorhandler( |
| 6273 | errors, &errorHandler, |
| 6274 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6275 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6276 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6277 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6278 | continue; |
| 6279 | } |
| 6280 | |
| 6281 | s += Py_UNICODE_SIZE; |
| 6282 | #ifndef Py_UNICODE_WIDE |
| 6283 | if (ch >= 0xD800 && ch <= 0xDBFF && s < end) |
| 6284 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6285 | Py_UNICODE uch2; |
| 6286 | ((char *) &uch2)[0] = s[0]; |
| 6287 | ((char *) &uch2)[1] = s[1]; |
| 6288 | if (uch2 >= 0xDC00 && uch2 <= 0xDFFF) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6289 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6290 | ch = (((uch & 0x3FF)<<10) | (uch2 & 0x3FF)) + 0x10000; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6291 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6292 | } |
| 6293 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6294 | #endif |
| 6295 | |
| 6296 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6297 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6300 | if (PyUnicode_Resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6301 | goto onError; |
| 6302 | Py_XDECREF(errorHandler); |
| 6303 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6304 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6305 | return v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6307 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6308 | Py_XDECREF(v); |
| 6309 | Py_XDECREF(errorHandler); |
| 6310 | Py_XDECREF(exc); |
| 6311 | return NULL; |
| 6312 | } |
| 6313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6315 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6316 | PyObject * |
| 6317 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6318 | Py_ssize_t size, |
| 6319 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6322 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6325 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6326 | static void |
| 6327 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6328 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6329 | PyObject *unicode, |
| 6330 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6331 | const char *reason) |
| 6332 | { |
| 6333 | if (*exceptionObject == NULL) { |
| 6334 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6335 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6336 | encoding, unicode, startpos, endpos, reason); |
| 6337 | } |
| 6338 | else { |
| 6339 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6340 | goto onError; |
| 6341 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6342 | goto onError; |
| 6343 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6344 | goto onError; |
| 6345 | return; |
| 6346 | onError: |
| 6347 | Py_DECREF(*exceptionObject); |
| 6348 | *exceptionObject = NULL; |
| 6349 | } |
| 6350 | } |
| 6351 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6352 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6353 | static void |
| 6354 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6355 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6356 | PyObject *unicode, |
| 6357 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6358 | const char *reason) |
| 6359 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6360 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6361 | encoding, unicode, startpos, endpos, reason); |
| 6362 | if (*exceptionObject != NULL) |
| 6363 | PyCodec_StrictErrors(*exceptionObject); |
| 6364 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6365 | |
| 6366 | /* error handling callback helper: |
| 6367 | build arguments, call the callback and check the arguments, |
| 6368 | put the result into newpos and return the replacement string, which |
| 6369 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6370 | static PyObject * |
| 6371 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6372 | PyObject **errorHandler, |
| 6373 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6374 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6375 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6376 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6377 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6378 | 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] | 6379 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6380 | PyObject *restuple; |
| 6381 | PyObject *resunicode; |
| 6382 | |
| 6383 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6384 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6385 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6386 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6389 | if (PyUnicode_READY(unicode) < 0) |
| 6390 | return NULL; |
| 6391 | len = PyUnicode_GET_LENGTH(unicode); |
| 6392 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6393 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6394 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6395 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6397 | |
| 6398 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6399 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6400 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6401 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6402 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6403 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6404 | Py_DECREF(restuple); |
| 6405 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6406 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6407 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6408 | &resunicode, newpos)) { |
| 6409 | Py_DECREF(restuple); |
| 6410 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6412 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6413 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6414 | Py_DECREF(restuple); |
| 6415 | return NULL; |
| 6416 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6418 | *newpos = len + *newpos; |
| 6419 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6421 | Py_DECREF(restuple); |
| 6422 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6423 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6424 | Py_INCREF(resunicode); |
| 6425 | Py_DECREF(restuple); |
| 6426 | return resunicode; |
| 6427 | } |
| 6428 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6429 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6430 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6431 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6432 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6433 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6434 | /* input state */ |
| 6435 | Py_ssize_t pos=0, size; |
| 6436 | int kind; |
| 6437 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6438 | /* output object */ |
| 6439 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6440 | /* pointer into the output */ |
| 6441 | char *str; |
| 6442 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6443 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6444 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6445 | 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] | 6446 | PyObject *errorHandler = NULL; |
| 6447 | PyObject *exc = NULL; |
| 6448 | /* the following variable is used for caching string comparisons |
| 6449 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6450 | int known_errorHandler = -1; |
| 6451 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6452 | if (PyUnicode_READY(unicode) < 0) |
| 6453 | return NULL; |
| 6454 | size = PyUnicode_GET_LENGTH(unicode); |
| 6455 | kind = PyUnicode_KIND(unicode); |
| 6456 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6457 | /* allocate enough for a simple encoding without |
| 6458 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6459 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6460 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6461 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6462 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6463 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6464 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6465 | ressize = size; |
| 6466 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6467 | while (pos < size) { |
| 6468 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6469 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6470 | /* can we encode this? */ |
| 6471 | if (c<limit) { |
| 6472 | /* no overflow check, because we know that the space is enough */ |
| 6473 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6474 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6475 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6477 | Py_ssize_t requiredsize; |
| 6478 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6479 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6480 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6481 | Py_ssize_t collstart = pos; |
| 6482 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6484 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6485 | ++collend; |
| 6486 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6487 | if (known_errorHandler==-1) { |
| 6488 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6489 | known_errorHandler = 1; |
| 6490 | else if (!strcmp(errors, "replace")) |
| 6491 | known_errorHandler = 2; |
| 6492 | else if (!strcmp(errors, "ignore")) |
| 6493 | known_errorHandler = 3; |
| 6494 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6495 | known_errorHandler = 4; |
| 6496 | else |
| 6497 | known_errorHandler = 0; |
| 6498 | } |
| 6499 | switch (known_errorHandler) { |
| 6500 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6501 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | goto onError; |
| 6503 | case 2: /* replace */ |
| 6504 | while (collstart++<collend) |
| 6505 | *str++ = '?'; /* fall through */ |
| 6506 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6507 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6508 | break; |
| 6509 | case 4: /* xmlcharrefreplace */ |
| 6510 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6511 | /* determine replacement size */ |
| 6512 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6513 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6514 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6516 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6517 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6518 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6519 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6520 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6521 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6522 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 | else |
| 6524 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6525 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6526 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6528 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | repsize += 2+6+1; |
| 6530 | else |
| 6531 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6532 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6534 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | if (requiredsize > ressize) { |
| 6536 | if (requiredsize<2*ressize) |
| 6537 | requiredsize = 2*ressize; |
| 6538 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6539 | goto onError; |
| 6540 | str = PyBytes_AS_STRING(res) + respos; |
| 6541 | ressize = requiredsize; |
| 6542 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6543 | /* generate replacement */ |
| 6544 | for (i = collstart; i < collend; ++i) { |
| 6545 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6547 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | break; |
| 6549 | default: |
| 6550 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6551 | encoding, reason, unicode, &exc, |
| 6552 | collstart, collend, &newpos); |
| 6553 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6554 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6556 | if (PyBytes_Check(repunicode)) { |
| 6557 | /* Directly copy bytes result to output. */ |
| 6558 | repsize = PyBytes_Size(repunicode); |
| 6559 | if (repsize > 1) { |
| 6560 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6561 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6562 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6563 | Py_DECREF(repunicode); |
| 6564 | goto onError; |
| 6565 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6566 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6567 | ressize += repsize-1; |
| 6568 | } |
| 6569 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6570 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6571 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6572 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6573 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6574 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6575 | /* need more space? (at least enough for what we |
| 6576 | have+the replacement+the rest of the string, so |
| 6577 | we won't have to check space for encodable characters) */ |
| 6578 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6579 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6580 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | if (requiredsize > ressize) { |
| 6582 | if (requiredsize<2*ressize) |
| 6583 | requiredsize = 2*ressize; |
| 6584 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6585 | Py_DECREF(repunicode); |
| 6586 | goto onError; |
| 6587 | } |
| 6588 | str = PyBytes_AS_STRING(res) + respos; |
| 6589 | ressize = requiredsize; |
| 6590 | } |
| 6591 | /* check if there is anything unencodable in the replacement |
| 6592 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6593 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6594 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6595 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6596 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6597 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | Py_DECREF(repunicode); |
| 6599 | goto onError; |
| 6600 | } |
| 6601 | *str = (char)c; |
| 6602 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6603 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6604 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6605 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6606 | } |
| 6607 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6608 | /* Resize if we allocated to much */ |
| 6609 | size = str - PyBytes_AS_STRING(res); |
| 6610 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6611 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6612 | if (_PyBytes_Resize(&res, size) < 0) |
| 6613 | goto onError; |
| 6614 | } |
| 6615 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6616 | Py_XDECREF(errorHandler); |
| 6617 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6618 | return res; |
| 6619 | |
| 6620 | onError: |
| 6621 | Py_XDECREF(res); |
| 6622 | Py_XDECREF(errorHandler); |
| 6623 | Py_XDECREF(exc); |
| 6624 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6627 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6628 | PyObject * |
| 6629 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6630 | Py_ssize_t size, |
| 6631 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6633 | PyObject *result; |
| 6634 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6635 | if (unicode == NULL) |
| 6636 | return NULL; |
| 6637 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6638 | Py_DECREF(unicode); |
| 6639 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6642 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6643 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | { |
| 6645 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6646 | PyErr_BadArgument(); |
| 6647 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6649 | if (PyUnicode_READY(unicode) == -1) |
| 6650 | return NULL; |
| 6651 | /* Fast path: if it is a one-byte string, construct |
| 6652 | bytes object directly. */ |
| 6653 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6654 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6655 | PyUnicode_GET_LENGTH(unicode)); |
| 6656 | /* Non-Latin-1 characters present. Defer to above function to |
| 6657 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6658 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6659 | } |
| 6660 | |
| 6661 | PyObject* |
| 6662 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6663 | { |
| 6664 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6669 | PyObject * |
| 6670 | PyUnicode_DecodeASCII(const char *s, |
| 6671 | Py_ssize_t size, |
| 6672 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6674 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6675 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6676 | int kind; |
| 6677 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6678 | Py_ssize_t startinpos; |
| 6679 | Py_ssize_t endinpos; |
| 6680 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6681 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6682 | int has_error; |
| 6683 | const unsigned char *p = (const unsigned char *)s; |
| 6684 | const unsigned char *end = p + size; |
| 6685 | 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] | 6686 | PyObject *errorHandler = NULL; |
| 6687 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6689 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6690 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6691 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6692 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6693 | has_error = 0; |
| 6694 | while (p < end && !has_error) { |
| 6695 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6696 | an explanation. */ |
| 6697 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6698 | /* Help register allocation */ |
| 6699 | register const unsigned char *_p = p; |
| 6700 | while (_p < aligned_end) { |
| 6701 | unsigned long value = *(unsigned long *) _p; |
| 6702 | if (value & ASCII_CHAR_MASK) { |
| 6703 | has_error = 1; |
| 6704 | break; |
| 6705 | } |
| 6706 | _p += SIZEOF_LONG; |
| 6707 | } |
| 6708 | if (_p == end) |
| 6709 | break; |
| 6710 | if (has_error) |
| 6711 | break; |
| 6712 | p = _p; |
| 6713 | } |
| 6714 | if (*p & 0x80) { |
| 6715 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6716 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6717 | } |
| 6718 | else { |
| 6719 | ++p; |
| 6720 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6721 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6722 | if (!has_error) |
| 6723 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6724 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6725 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6726 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6727 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6729 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6730 | kind = PyUnicode_KIND(v); |
| 6731 | data = PyUnicode_DATA(v); |
| 6732 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6733 | e = s + size; |
| 6734 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6735 | register unsigned char c = (unsigned char)*s; |
| 6736 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6737 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6738 | ++s; |
| 6739 | } |
| 6740 | else { |
| 6741 | startinpos = s-starts; |
| 6742 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | if (unicode_decode_call_errorhandler( |
| 6744 | errors, &errorHandler, |
| 6745 | "ascii", "ordinal not in range(128)", |
| 6746 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6747 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6748 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6749 | kind = PyUnicode_KIND(v); |
| 6750 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6753 | if (PyUnicode_Resize(&v, outpos) < 0) |
| 6754 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6755 | Py_XDECREF(errorHandler); |
| 6756 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6757 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6758 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6759 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6760 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6762 | Py_XDECREF(errorHandler); |
| 6763 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | return NULL; |
| 6765 | } |
| 6766 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6767 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6768 | PyObject * |
| 6769 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6770 | Py_ssize_t size, |
| 6771 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6773 | PyObject *result; |
| 6774 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6775 | if (unicode == NULL) |
| 6776 | return NULL; |
| 6777 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6778 | Py_DECREF(unicode); |
| 6779 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | } |
| 6781 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6782 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6783 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | { |
| 6785 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6786 | PyErr_BadArgument(); |
| 6787 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6789 | if (PyUnicode_READY(unicode) == -1) |
| 6790 | return NULL; |
| 6791 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6792 | directly. Else defer to above function to raise the exception. */ |
| 6793 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6794 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6795 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6796 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | PyObject * |
| 6800 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6801 | { |
| 6802 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6805 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6806 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6807 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6808 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6809 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6810 | #define NEED_RETRY |
| 6811 | #endif |
| 6812 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6813 | #ifndef WC_ERR_INVALID_CHARS |
| 6814 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6815 | #endif |
| 6816 | |
| 6817 | static char* |
| 6818 | code_page_name(UINT code_page, PyObject **obj) |
| 6819 | { |
| 6820 | *obj = NULL; |
| 6821 | if (code_page == CP_ACP) |
| 6822 | return "mbcs"; |
| 6823 | if (code_page == CP_UTF7) |
| 6824 | return "CP_UTF7"; |
| 6825 | if (code_page == CP_UTF8) |
| 6826 | return "CP_UTF8"; |
| 6827 | |
| 6828 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6829 | if (*obj == NULL) |
| 6830 | return NULL; |
| 6831 | return PyBytes_AS_STRING(*obj); |
| 6832 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6833 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6834 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6835 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6836 | { |
| 6837 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6838 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6839 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6840 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6841 | return 0; |
| 6842 | |
| 6843 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6844 | if (prev == curr) |
| 6845 | return 1; |
| 6846 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6847 | as it assumes an incomplete character consists of a single |
| 6848 | byte. */ |
| 6849 | if (curr - prev == 2) |
| 6850 | return 1; |
| 6851 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6852 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6853 | return 0; |
| 6854 | } |
| 6855 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6856 | static DWORD |
| 6857 | decode_code_page_flags(UINT code_page) |
| 6858 | { |
| 6859 | if (code_page == CP_UTF7) { |
| 6860 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6861 | return 0; |
| 6862 | } |
| 6863 | else |
| 6864 | return MB_ERR_INVALID_CHARS; |
| 6865 | } |
| 6866 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6867 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6868 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6869 | * mode. |
| 6870 | * |
| 6871 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6872 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6873 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6874 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6875 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6876 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6877 | const char *in, |
| 6878 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6879 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6880 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6881 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6882 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6883 | |
| 6884 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6885 | assert(insize > 0); |
| 6886 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6887 | if (outsize <= 0) |
| 6888 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6889 | |
| 6890 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6891 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6892 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6893 | if (*v == NULL) |
| 6894 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6895 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6896 | } |
| 6897 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6898 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6899 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6900 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6902 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6903 | } |
| 6904 | |
| 6905 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6906 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6907 | if (outsize <= 0) |
| 6908 | goto error; |
| 6909 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6910 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6911 | error: |
| 6912 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6913 | return -2; |
| 6914 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6915 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6918 | /* |
| 6919 | * Decode a byte string from a code page into unicode object with an error |
| 6920 | * handler. |
| 6921 | * |
| 6922 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6923 | * UnicodeDecodeError exception and returns -1 on error. |
| 6924 | */ |
| 6925 | static int |
| 6926 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6927 | PyObject **v, |
| 6928 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6929 | const char *errors) |
| 6930 | { |
| 6931 | const char *startin = in; |
| 6932 | const char *endin = in + size; |
| 6933 | const DWORD flags = decode_code_page_flags(code_page); |
| 6934 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6935 | 2000 English version of the message. */ |
| 6936 | const char *reason = "No mapping for the Unicode character exists " |
| 6937 | "in the target code page."; |
| 6938 | /* each step cannot decode more than 1 character, but a character can be |
| 6939 | represented as a surrogate pair */ |
| 6940 | wchar_t buffer[2], *startout, *out; |
| 6941 | int insize, outsize; |
| 6942 | PyObject *errorHandler = NULL; |
| 6943 | PyObject *exc = NULL; |
| 6944 | PyObject *encoding_obj = NULL; |
| 6945 | char *encoding; |
| 6946 | DWORD err; |
| 6947 | int ret = -1; |
| 6948 | |
| 6949 | assert(size > 0); |
| 6950 | |
| 6951 | encoding = code_page_name(code_page, &encoding_obj); |
| 6952 | if (encoding == NULL) |
| 6953 | return -1; |
| 6954 | |
| 6955 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6956 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6957 | UnicodeDecodeError. */ |
| 6958 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6959 | if (exc != NULL) { |
| 6960 | PyCodec_StrictErrors(exc); |
| 6961 | Py_CLEAR(exc); |
| 6962 | } |
| 6963 | goto error; |
| 6964 | } |
| 6965 | |
| 6966 | if (*v == NULL) { |
| 6967 | /* Create unicode object */ |
| 6968 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6969 | PyErr_NoMemory(); |
| 6970 | goto error; |
| 6971 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6972 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6973 | if (*v == NULL) |
| 6974 | goto error; |
| 6975 | startout = PyUnicode_AS_UNICODE(*v); |
| 6976 | } |
| 6977 | else { |
| 6978 | /* Extend unicode object */ |
| 6979 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6980 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6981 | PyErr_NoMemory(); |
| 6982 | goto error; |
| 6983 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6984 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6985 | goto error; |
| 6986 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6987 | } |
| 6988 | |
| 6989 | /* Decode the byte string character per character */ |
| 6990 | out = startout; |
| 6991 | while (in < endin) |
| 6992 | { |
| 6993 | /* Decode a character */ |
| 6994 | insize = 1; |
| 6995 | do |
| 6996 | { |
| 6997 | outsize = MultiByteToWideChar(code_page, flags, |
| 6998 | in, insize, |
| 6999 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7000 | if (outsize > 0) |
| 7001 | break; |
| 7002 | err = GetLastError(); |
| 7003 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7004 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7005 | { |
| 7006 | PyErr_SetFromWindowsErr(0); |
| 7007 | goto error; |
| 7008 | } |
| 7009 | insize++; |
| 7010 | } |
| 7011 | /* 4=maximum length of a UTF-8 sequence */ |
| 7012 | while (insize <= 4 && (in + insize) <= endin); |
| 7013 | |
| 7014 | if (outsize <= 0) { |
| 7015 | Py_ssize_t startinpos, endinpos, outpos; |
| 7016 | |
| 7017 | startinpos = in - startin; |
| 7018 | endinpos = startinpos + 1; |
| 7019 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7020 | if (unicode_decode_call_errorhandler( |
| 7021 | errors, &errorHandler, |
| 7022 | encoding, reason, |
| 7023 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7024 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7025 | { |
| 7026 | goto error; |
| 7027 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7028 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7029 | } |
| 7030 | else { |
| 7031 | in += insize; |
| 7032 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7033 | out += outsize; |
| 7034 | } |
| 7035 | } |
| 7036 | |
| 7037 | /* write a NUL character at the end */ |
| 7038 | *out = 0; |
| 7039 | |
| 7040 | /* Extend unicode object */ |
| 7041 | outsize = out - startout; |
| 7042 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7043 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7045 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7046 | |
| 7047 | error: |
| 7048 | Py_XDECREF(encoding_obj); |
| 7049 | Py_XDECREF(errorHandler); |
| 7050 | Py_XDECREF(exc); |
| 7051 | return ret; |
| 7052 | } |
| 7053 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7054 | static PyObject * |
| 7055 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7056 | const char *s, Py_ssize_t size, |
| 7057 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7058 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7059 | PyObject *v = NULL; |
| 7060 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7062 | if (code_page < 0) { |
| 7063 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7064 | return NULL; |
| 7065 | } |
| 7066 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7067 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7068 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7069 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7070 | do |
| 7071 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7072 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7073 | if (size > INT_MAX) { |
| 7074 | chunk_size = INT_MAX; |
| 7075 | final = 0; |
| 7076 | done = 0; |
| 7077 | } |
| 7078 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7080 | { |
| 7081 | chunk_size = (int)size; |
| 7082 | final = (consumed == NULL); |
| 7083 | done = 1; |
| 7084 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7085 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7086 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7087 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7088 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7089 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7090 | if (chunk_size == 0 && done) { |
| 7091 | if (v != NULL) |
| 7092 | break; |
| 7093 | Py_INCREF(unicode_empty); |
| 7094 | return unicode_empty; |
| 7095 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7096 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7097 | |
| 7098 | converted = decode_code_page_strict(code_page, &v, |
| 7099 | s, chunk_size); |
| 7100 | if (converted == -2) |
| 7101 | converted = decode_code_page_errors(code_page, &v, |
| 7102 | s, chunk_size, |
| 7103 | errors); |
| 7104 | assert(converted != 0); |
| 7105 | |
| 7106 | if (converted < 0) { |
| 7107 | Py_XDECREF(v); |
| 7108 | return NULL; |
| 7109 | } |
| 7110 | |
| 7111 | if (consumed) |
| 7112 | *consumed += converted; |
| 7113 | |
| 7114 | s += converted; |
| 7115 | size -= converted; |
| 7116 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7117 | |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7118 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7119 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7120 | Py_DECREF(v); |
| 7121 | return NULL; |
| 7122 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7123 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7124 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7125 | return v; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7126 | } |
| 7127 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7128 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7129 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7130 | const char *s, |
| 7131 | Py_ssize_t size, |
| 7132 | const char *errors, |
| 7133 | Py_ssize_t *consumed) |
| 7134 | { |
| 7135 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7136 | } |
| 7137 | |
| 7138 | PyObject * |
| 7139 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7140 | Py_ssize_t size, |
| 7141 | const char *errors, |
| 7142 | Py_ssize_t *consumed) |
| 7143 | { |
| 7144 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7145 | } |
| 7146 | |
| 7147 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7148 | PyUnicode_DecodeMBCS(const char *s, |
| 7149 | Py_ssize_t size, |
| 7150 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7151 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7152 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7153 | } |
| 7154 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7155 | static DWORD |
| 7156 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7157 | { |
| 7158 | if (code_page == CP_UTF8) { |
| 7159 | if (winver.dwMajorVersion >= 6) |
| 7160 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7161 | and later */ |
| 7162 | return WC_ERR_INVALID_CHARS; |
| 7163 | else |
| 7164 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7165 | return 0; |
| 7166 | } |
| 7167 | else if (code_page == CP_UTF7) { |
| 7168 | /* CP_UTF7 only supports flags=0 */ |
| 7169 | return 0; |
| 7170 | } |
| 7171 | else { |
| 7172 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7173 | return 0; |
| 7174 | else |
| 7175 | return WC_NO_BEST_FIT_CHARS; |
| 7176 | } |
| 7177 | } |
| 7178 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7179 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7181 | * mode. |
| 7182 | * |
| 7183 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7184 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7185 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7186 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7187 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7188 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7189 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7190 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7191 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7192 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7193 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7194 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7195 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7196 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7198 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7199 | /* Create a substring so that we can get the UTF-16 representation |
| 7200 | of just the slice under consideration. */ |
| 7201 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7202 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7203 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7204 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7206 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7208 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7209 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7210 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7211 | if (substring == NULL) |
| 7212 | return -1; |
| 7213 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7214 | if (p == NULL) { |
| 7215 | Py_DECREF(substring); |
| 7216 | return -1; |
| 7217 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7218 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7219 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | outsize = WideCharToMultiByte(code_page, flags, |
| 7221 | p, size, |
| 7222 | NULL, 0, |
| 7223 | NULL, pusedDefaultChar); |
| 7224 | if (outsize <= 0) |
| 7225 | goto error; |
| 7226 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7227 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7228 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7229 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7230 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7231 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7232 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7233 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7234 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7235 | if (*outbytes == NULL) { |
| 7236 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7237 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7238 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7239 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7240 | } |
| 7241 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7242 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7243 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7244 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7245 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7246 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7247 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7248 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7249 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7250 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7251 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7252 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7254 | } |
| 7255 | |
| 7256 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7257 | outsize = WideCharToMultiByte(code_page, flags, |
| 7258 | p, size, |
| 7259 | out, outsize, |
| 7260 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7261 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7262 | if (outsize <= 0) |
| 7263 | goto error; |
| 7264 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7265 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7266 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7267 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7268 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7269 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7270 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7271 | return -2; |
| 7272 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7273 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7274 | } |
| 7275 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | /* |
| 7277 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7278 | * error handler. |
| 7279 | * |
| 7280 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7281 | * -1 on other error. |
| 7282 | */ |
| 7283 | static int |
| 7284 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7285 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7286 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7287 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7288 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7289 | Py_ssize_t pos = unicode_offset; |
| 7290 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7291 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7292 | 2000 English version of the message. */ |
| 7293 | const char *reason = "invalid character"; |
| 7294 | /* 4=maximum length of a UTF-8 sequence */ |
| 7295 | char buffer[4]; |
| 7296 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7297 | Py_ssize_t outsize; |
| 7298 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7299 | PyObject *errorHandler = NULL; |
| 7300 | PyObject *exc = NULL; |
| 7301 | PyObject *encoding_obj = NULL; |
| 7302 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7303 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7304 | PyObject *rep; |
| 7305 | int ret = -1; |
| 7306 | |
| 7307 | assert(insize > 0); |
| 7308 | |
| 7309 | encoding = code_page_name(code_page, &encoding_obj); |
| 7310 | if (encoding == NULL) |
| 7311 | return -1; |
| 7312 | |
| 7313 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7314 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7315 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7316 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7317 | if (exc != NULL) { |
| 7318 | PyCodec_StrictErrors(exc); |
| 7319 | Py_DECREF(exc); |
| 7320 | } |
| 7321 | Py_XDECREF(encoding_obj); |
| 7322 | return -1; |
| 7323 | } |
| 7324 | |
| 7325 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7326 | pusedDefaultChar = &usedDefaultChar; |
| 7327 | else |
| 7328 | pusedDefaultChar = NULL; |
| 7329 | |
| 7330 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7331 | PyErr_NoMemory(); |
| 7332 | goto error; |
| 7333 | } |
| 7334 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7335 | |
| 7336 | if (*outbytes == NULL) { |
| 7337 | /* Create string object */ |
| 7338 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7339 | if (*outbytes == NULL) |
| 7340 | goto error; |
| 7341 | out = PyBytes_AS_STRING(*outbytes); |
| 7342 | } |
| 7343 | else { |
| 7344 | /* Extend string object */ |
| 7345 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7346 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7347 | PyErr_NoMemory(); |
| 7348 | goto error; |
| 7349 | } |
| 7350 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7351 | goto error; |
| 7352 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7353 | } |
| 7354 | |
| 7355 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7356 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7357 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7358 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7359 | wchar_t chars[2]; |
| 7360 | int charsize; |
| 7361 | if (ch < 0x10000) { |
| 7362 | chars[0] = (wchar_t)ch; |
| 7363 | charsize = 1; |
| 7364 | } |
| 7365 | else { |
| 7366 | ch -= 0x10000; |
| 7367 | chars[0] = 0xd800 + (ch >> 10); |
| 7368 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7369 | charsize = 2; |
| 7370 | } |
| 7371 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7372 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7373 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7374 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7375 | NULL, pusedDefaultChar); |
| 7376 | if (outsize > 0) { |
| 7377 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7378 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7379 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7380 | memcpy(out, buffer, outsize); |
| 7381 | out += outsize; |
| 7382 | continue; |
| 7383 | } |
| 7384 | } |
| 7385 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7386 | PyErr_SetFromWindowsErr(0); |
| 7387 | goto error; |
| 7388 | } |
| 7389 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7390 | rep = unicode_encode_call_errorhandler( |
| 7391 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7392 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7393 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | if (rep == NULL) |
| 7395 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7396 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7397 | |
| 7398 | if (PyBytes_Check(rep)) { |
| 7399 | outsize = PyBytes_GET_SIZE(rep); |
| 7400 | if (outsize != 1) { |
| 7401 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7402 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7403 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7404 | Py_DECREF(rep); |
| 7405 | goto error; |
| 7406 | } |
| 7407 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7408 | } |
| 7409 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7410 | out += outsize; |
| 7411 | } |
| 7412 | else { |
| 7413 | Py_ssize_t i; |
| 7414 | enum PyUnicode_Kind kind; |
| 7415 | void *data; |
| 7416 | |
| 7417 | if (PyUnicode_READY(rep) < 0) { |
| 7418 | Py_DECREF(rep); |
| 7419 | goto error; |
| 7420 | } |
| 7421 | |
| 7422 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7423 | if (outsize != 1) { |
| 7424 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7425 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7426 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7427 | Py_DECREF(rep); |
| 7428 | goto error; |
| 7429 | } |
| 7430 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7431 | } |
| 7432 | kind = PyUnicode_KIND(rep); |
| 7433 | data = PyUnicode_DATA(rep); |
| 7434 | for (i=0; i < outsize; i++) { |
| 7435 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7436 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7437 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7438 | encoding, unicode, |
| 7439 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7440 | "unable to encode error handler result to ASCII"); |
| 7441 | Py_DECREF(rep); |
| 7442 | goto error; |
| 7443 | } |
| 7444 | *out = (unsigned char)ch; |
| 7445 | out++; |
| 7446 | } |
| 7447 | } |
| 7448 | Py_DECREF(rep); |
| 7449 | } |
| 7450 | /* write a NUL byte */ |
| 7451 | *out = 0; |
| 7452 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7453 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7454 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7455 | goto error; |
| 7456 | ret = 0; |
| 7457 | |
| 7458 | error: |
| 7459 | Py_XDECREF(encoding_obj); |
| 7460 | Py_XDECREF(errorHandler); |
| 7461 | Py_XDECREF(exc); |
| 7462 | return ret; |
| 7463 | } |
| 7464 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7465 | static PyObject * |
| 7466 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7467 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7468 | const char *errors) |
| 7469 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7470 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7471 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7472 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7473 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7474 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7475 | if (PyUnicode_READY(unicode) < 0) |
| 7476 | return NULL; |
| 7477 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7478 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7479 | if (code_page < 0) { |
| 7480 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7481 | return NULL; |
| 7482 | } |
| 7483 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7484 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7485 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7486 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7487 | offset = 0; |
| 7488 | do |
| 7489 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7490 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7491 | /* 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] | 7492 | chunks. */ |
| 7493 | if (len > INT_MAX/2) { |
| 7494 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7495 | done = 0; |
| 7496 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7497 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7498 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7499 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7500 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7501 | done = 1; |
| 7502 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7503 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7504 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7505 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7506 | errors); |
| 7507 | if (ret == -2) |
| 7508 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7509 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7510 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7511 | if (ret < 0) { |
| 7512 | Py_XDECREF(outbytes); |
| 7513 | return NULL; |
| 7514 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7515 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7516 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7517 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7518 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7519 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7520 | return outbytes; |
| 7521 | } |
| 7522 | |
| 7523 | PyObject * |
| 7524 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7525 | Py_ssize_t size, |
| 7526 | const char *errors) |
| 7527 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7528 | PyObject *unicode, *res; |
| 7529 | unicode = PyUnicode_FromUnicode(p, size); |
| 7530 | if (unicode == NULL) |
| 7531 | return NULL; |
| 7532 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7533 | Py_DECREF(unicode); |
| 7534 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7535 | } |
| 7536 | |
| 7537 | PyObject * |
| 7538 | PyUnicode_EncodeCodePage(int code_page, |
| 7539 | PyObject *unicode, |
| 7540 | const char *errors) |
| 7541 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7542 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7543 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7544 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7545 | PyObject * |
| 7546 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7547 | { |
| 7548 | if (!PyUnicode_Check(unicode)) { |
| 7549 | PyErr_BadArgument(); |
| 7550 | return NULL; |
| 7551 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7552 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7553 | } |
| 7554 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7555 | #undef NEED_RETRY |
| 7556 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7557 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7559 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7561 | PyObject * |
| 7562 | PyUnicode_DecodeCharmap(const char *s, |
| 7563 | Py_ssize_t size, |
| 7564 | PyObject *mapping, |
| 7565 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7567 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7568 | Py_ssize_t startinpos; |
| 7569 | Py_ssize_t endinpos; |
| 7570 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7571 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7572 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7573 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7574 | PyObject *errorHandler = NULL; |
| 7575 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | /* Default to Latin-1 */ |
| 7578 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7579 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7581 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7582 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7583 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7585 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7586 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7587 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7588 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7589 | Py_ssize_t maplen; |
| 7590 | enum PyUnicode_Kind kind; |
| 7591 | void *data; |
| 7592 | Py_UCS4 x; |
| 7593 | |
| 7594 | if (PyUnicode_READY(mapping) < 0) |
| 7595 | return NULL; |
| 7596 | |
| 7597 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7598 | data = PyUnicode_DATA(mapping); |
| 7599 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7600 | while (s < e) { |
| 7601 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7603 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7604 | x = PyUnicode_READ(kind, data, ch); |
| 7605 | else |
| 7606 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7608 | if (x == 0xfffe) |
| 7609 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7611 | startinpos = s-starts; |
| 7612 | endinpos = startinpos+1; |
| 7613 | if (unicode_decode_call_errorhandler( |
| 7614 | errors, &errorHandler, |
| 7615 | "charmap", "character maps to <undefined>", |
| 7616 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7617 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | goto onError; |
| 7619 | } |
| 7620 | continue; |
| 7621 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7622 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7623 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7624 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7625 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7626 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7627 | } |
| 7628 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7629 | while (s < e) { |
| 7630 | unsigned char ch = *s; |
| 7631 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7632 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7634 | w = PyLong_FromLong((long)ch); |
| 7635 | if (w == NULL) |
| 7636 | goto onError; |
| 7637 | x = PyObject_GetItem(mapping, w); |
| 7638 | Py_DECREF(w); |
| 7639 | if (x == NULL) { |
| 7640 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7641 | /* No mapping found means: mapping is undefined. */ |
| 7642 | PyErr_Clear(); |
| 7643 | x = Py_None; |
| 7644 | Py_INCREF(x); |
| 7645 | } else |
| 7646 | goto onError; |
| 7647 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7648 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7649 | /* Apply mapping */ |
| 7650 | if (PyLong_Check(x)) { |
| 7651 | long value = PyLong_AS_LONG(x); |
| 7652 | if (value < 0 || value > 65535) { |
| 7653 | PyErr_SetString(PyExc_TypeError, |
| 7654 | "character mapping must be in range(65536)"); |
| 7655 | Py_DECREF(x); |
| 7656 | goto onError; |
| 7657 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7658 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7659 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | } |
| 7661 | else if (x == Py_None) { |
| 7662 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7663 | startinpos = s-starts; |
| 7664 | endinpos = startinpos+1; |
| 7665 | if (unicode_decode_call_errorhandler( |
| 7666 | errors, &errorHandler, |
| 7667 | "charmap", "character maps to <undefined>", |
| 7668 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7669 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7670 | Py_DECREF(x); |
| 7671 | goto onError; |
| 7672 | } |
| 7673 | Py_DECREF(x); |
| 7674 | continue; |
| 7675 | } |
| 7676 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7677 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7678 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7679 | if (PyUnicode_READY(x) < 0) |
| 7680 | goto onError; |
| 7681 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7682 | |
| 7683 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7684 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7685 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7686 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7687 | goto onError; |
| 7688 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7689 | else if (targetsize > 1) { |
| 7690 | /* 1-n mapping */ |
| 7691 | if (targetsize > extrachars) { |
| 7692 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7693 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7694 | (targetsize << 2); |
| 7695 | extrachars += needed; |
| 7696 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7697 | if (PyUnicode_Resize(&v, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7698 | PyUnicode_GET_LENGTH(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7699 | Py_DECREF(x); |
| 7700 | goto onError; |
| 7701 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7703 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7704 | goto onError; |
| 7705 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7706 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7707 | extrachars -= targetsize; |
| 7708 | } |
| 7709 | /* 1-0 mapping: skip the character */ |
| 7710 | } |
| 7711 | else { |
| 7712 | /* wrong return value */ |
| 7713 | PyErr_SetString(PyExc_TypeError, |
| 7714 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7715 | Py_DECREF(x); |
| 7716 | goto onError; |
| 7717 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | Py_DECREF(x); |
| 7719 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7721 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7722 | if (PyUnicode_Resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7723 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7724 | Py_XDECREF(errorHandler); |
| 7725 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7726 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7727 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7728 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7729 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7730 | Py_XDECREF(errorHandler); |
| 7731 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | Py_XDECREF(v); |
| 7733 | return NULL; |
| 7734 | } |
| 7735 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7736 | /* Charmap encoding: the lookup table */ |
| 7737 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7738 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | PyObject_HEAD |
| 7740 | unsigned char level1[32]; |
| 7741 | int count2, count3; |
| 7742 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7743 | }; |
| 7744 | |
| 7745 | static PyObject* |
| 7746 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7747 | { |
| 7748 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7749 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7750 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
| 7753 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7754 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7755 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7756 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7757 | }; |
| 7758 | |
| 7759 | static void |
| 7760 | encoding_map_dealloc(PyObject* o) |
| 7761 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7762 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7766 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7767 | "EncodingMap", /*tp_name*/ |
| 7768 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7769 | 0, /*tp_itemsize*/ |
| 7770 | /* methods */ |
| 7771 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7772 | 0, /*tp_print*/ |
| 7773 | 0, /*tp_getattr*/ |
| 7774 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7775 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | 0, /*tp_repr*/ |
| 7777 | 0, /*tp_as_number*/ |
| 7778 | 0, /*tp_as_sequence*/ |
| 7779 | 0, /*tp_as_mapping*/ |
| 7780 | 0, /*tp_hash*/ |
| 7781 | 0, /*tp_call*/ |
| 7782 | 0, /*tp_str*/ |
| 7783 | 0, /*tp_getattro*/ |
| 7784 | 0, /*tp_setattro*/ |
| 7785 | 0, /*tp_as_buffer*/ |
| 7786 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7787 | 0, /*tp_doc*/ |
| 7788 | 0, /*tp_traverse*/ |
| 7789 | 0, /*tp_clear*/ |
| 7790 | 0, /*tp_richcompare*/ |
| 7791 | 0, /*tp_weaklistoffset*/ |
| 7792 | 0, /*tp_iter*/ |
| 7793 | 0, /*tp_iternext*/ |
| 7794 | encoding_map_methods, /*tp_methods*/ |
| 7795 | 0, /*tp_members*/ |
| 7796 | 0, /*tp_getset*/ |
| 7797 | 0, /*tp_base*/ |
| 7798 | 0, /*tp_dict*/ |
| 7799 | 0, /*tp_descr_get*/ |
| 7800 | 0, /*tp_descr_set*/ |
| 7801 | 0, /*tp_dictoffset*/ |
| 7802 | 0, /*tp_init*/ |
| 7803 | 0, /*tp_alloc*/ |
| 7804 | 0, /*tp_new*/ |
| 7805 | 0, /*tp_free*/ |
| 7806 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7807 | }; |
| 7808 | |
| 7809 | PyObject* |
| 7810 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7811 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7812 | PyObject *result; |
| 7813 | struct encoding_map *mresult; |
| 7814 | int i; |
| 7815 | int need_dict = 0; |
| 7816 | unsigned char level1[32]; |
| 7817 | unsigned char level2[512]; |
| 7818 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7819 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7820 | int kind; |
| 7821 | void *data; |
| 7822 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7824 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7825 | PyErr_BadArgument(); |
| 7826 | return NULL; |
| 7827 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7828 | kind = PyUnicode_KIND(string); |
| 7829 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7830 | memset(level1, 0xFF, sizeof level1); |
| 7831 | memset(level2, 0xFF, sizeof level2); |
| 7832 | |
| 7833 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7834 | or if there are non-BMP characters, we need to use |
| 7835 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7836 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7837 | need_dict = 1; |
| 7838 | for (i = 1; i < 256; i++) { |
| 7839 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7840 | ch = PyUnicode_READ(kind, data, i); |
| 7841 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7842 | need_dict = 1; |
| 7843 | break; |
| 7844 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7845 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7846 | /* unmapped character */ |
| 7847 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | l1 = ch >> 11; |
| 7849 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7850 | if (level1[l1] == 0xFF) |
| 7851 | level1[l1] = count2++; |
| 7852 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7853 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | } |
| 7855 | |
| 7856 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7857 | need_dict = 1; |
| 7858 | |
| 7859 | if (need_dict) { |
| 7860 | PyObject *result = PyDict_New(); |
| 7861 | PyObject *key, *value; |
| 7862 | if (!result) |
| 7863 | return NULL; |
| 7864 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7865 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7866 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7867 | if (!key || !value) |
| 7868 | goto failed1; |
| 7869 | if (PyDict_SetItem(result, key, value) == -1) |
| 7870 | goto failed1; |
| 7871 | Py_DECREF(key); |
| 7872 | Py_DECREF(value); |
| 7873 | } |
| 7874 | return result; |
| 7875 | failed1: |
| 7876 | Py_XDECREF(key); |
| 7877 | Py_XDECREF(value); |
| 7878 | Py_DECREF(result); |
| 7879 | return NULL; |
| 7880 | } |
| 7881 | |
| 7882 | /* Create a three-level trie */ |
| 7883 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7884 | 16*count2 + 128*count3 - 1); |
| 7885 | if (!result) |
| 7886 | return PyErr_NoMemory(); |
| 7887 | PyObject_Init(result, &EncodingMapType); |
| 7888 | mresult = (struct encoding_map*)result; |
| 7889 | mresult->count2 = count2; |
| 7890 | mresult->count3 = count3; |
| 7891 | mlevel1 = mresult->level1; |
| 7892 | mlevel2 = mresult->level23; |
| 7893 | mlevel3 = mresult->level23 + 16*count2; |
| 7894 | memcpy(mlevel1, level1, 32); |
| 7895 | memset(mlevel2, 0xFF, 16*count2); |
| 7896 | memset(mlevel3, 0, 128*count3); |
| 7897 | count3 = 0; |
| 7898 | for (i = 1; i < 256; i++) { |
| 7899 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7900 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7901 | /* unmapped character */ |
| 7902 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7903 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7904 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7905 | i2 = 16*mlevel1[o1] + o2; |
| 7906 | if (mlevel2[i2] == 0xFF) |
| 7907 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7908 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7909 | i3 = 128*mlevel2[i2] + o3; |
| 7910 | mlevel3[i3] = i; |
| 7911 | } |
| 7912 | return result; |
| 7913 | } |
| 7914 | |
| 7915 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7916 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7917 | { |
| 7918 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7919 | int l1 = c>>11; |
| 7920 | int l2 = (c>>7) & 0xF; |
| 7921 | int l3 = c & 0x7F; |
| 7922 | int i; |
| 7923 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7924 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7926 | if (c == 0) |
| 7927 | return 0; |
| 7928 | /* level 1*/ |
| 7929 | i = map->level1[l1]; |
| 7930 | if (i == 0xFF) { |
| 7931 | return -1; |
| 7932 | } |
| 7933 | /* level 2*/ |
| 7934 | i = map->level23[16*i+l2]; |
| 7935 | if (i == 0xFF) { |
| 7936 | return -1; |
| 7937 | } |
| 7938 | /* level 3 */ |
| 7939 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7940 | if (i == 0) { |
| 7941 | return -1; |
| 7942 | } |
| 7943 | return i; |
| 7944 | } |
| 7945 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7946 | /* Lookup the character ch in the mapping. If the character |
| 7947 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7948 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7949 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7950 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7952 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7953 | PyObject *x; |
| 7954 | |
| 7955 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7956 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7957 | x = PyObject_GetItem(mapping, w); |
| 7958 | Py_DECREF(w); |
| 7959 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7960 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7961 | /* No mapping found means: mapping is undefined. */ |
| 7962 | PyErr_Clear(); |
| 7963 | x = Py_None; |
| 7964 | Py_INCREF(x); |
| 7965 | return x; |
| 7966 | } else |
| 7967 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7968 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7969 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7970 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7971 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7972 | long value = PyLong_AS_LONG(x); |
| 7973 | if (value < 0 || value > 255) { |
| 7974 | PyErr_SetString(PyExc_TypeError, |
| 7975 | "character mapping must be in range(256)"); |
| 7976 | Py_DECREF(x); |
| 7977 | return NULL; |
| 7978 | } |
| 7979 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7980 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7981 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7982 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7983 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | /* wrong return value */ |
| 7985 | PyErr_Format(PyExc_TypeError, |
| 7986 | "character mapping must return integer, bytes or None, not %.400s", |
| 7987 | x->ob_type->tp_name); |
| 7988 | Py_DECREF(x); |
| 7989 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7990 | } |
| 7991 | } |
| 7992 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7993 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7994 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7995 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7996 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7997 | /* exponentially overallocate to minimize reallocations */ |
| 7998 | if (requiredsize < 2*outsize) |
| 7999 | requiredsize = 2*outsize; |
| 8000 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8001 | return -1; |
| 8002 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8003 | } |
| 8004 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8005 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8006 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8007 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8008 | /* 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] | 8009 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8010 | space is available. Return a new reference to the object that |
| 8011 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8012 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8013 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8014 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8015 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8016 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8017 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8018 | PyObject *rep; |
| 8019 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8020 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8021 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8022 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8023 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8024 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8025 | if (res == -1) |
| 8026 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 | if (outsize<requiredsize) |
| 8028 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8029 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8030 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8031 | outstart[(*outpos)++] = (char)res; |
| 8032 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
| 8035 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8036 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8038 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8039 | Py_DECREF(rep); |
| 8040 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8041 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8042 | if (PyLong_Check(rep)) { |
| 8043 | Py_ssize_t requiredsize = *outpos+1; |
| 8044 | if (outsize<requiredsize) |
| 8045 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8046 | Py_DECREF(rep); |
| 8047 | return enc_EXCEPTION; |
| 8048 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8049 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8051 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | else { |
| 8053 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8054 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8055 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8056 | if (outsize<requiredsize) |
| 8057 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8058 | Py_DECREF(rep); |
| 8059 | return enc_EXCEPTION; |
| 8060 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8061 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | memcpy(outstart + *outpos, repchars, repsize); |
| 8063 | *outpos += repsize; |
| 8064 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8065 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8066 | Py_DECREF(rep); |
| 8067 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
| 8070 | /* handle an error in PyUnicode_EncodeCharmap |
| 8071 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8072 | static int |
| 8073 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8074 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8075 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8076 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8077 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8078 | { |
| 8079 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8080 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8081 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8082 | enum PyUnicode_Kind kind; |
| 8083 | void *data; |
| 8084 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8085 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8086 | Py_ssize_t collstartpos = *inpos; |
| 8087 | Py_ssize_t collendpos = *inpos+1; |
| 8088 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8089 | char *encoding = "charmap"; |
| 8090 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8091 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8092 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8093 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8094 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8095 | if (PyUnicode_READY(unicode) < 0) |
| 8096 | return -1; |
| 8097 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8098 | /* find all unencodable characters */ |
| 8099 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8100 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8101 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8102 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8103 | val = encoding_map_lookup(ch, mapping); |
| 8104 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8105 | break; |
| 8106 | ++collendpos; |
| 8107 | continue; |
| 8108 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8109 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8110 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8111 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8112 | if (rep==NULL) |
| 8113 | return -1; |
| 8114 | else if (rep!=Py_None) { |
| 8115 | Py_DECREF(rep); |
| 8116 | break; |
| 8117 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8118 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8119 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | } |
| 8121 | /* cache callback name lookup |
| 8122 | * (if not done yet, i.e. it's the first error) */ |
| 8123 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8124 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8125 | *known_errorHandler = 1; |
| 8126 | else if (!strcmp(errors, "replace")) |
| 8127 | *known_errorHandler = 2; |
| 8128 | else if (!strcmp(errors, "ignore")) |
| 8129 | *known_errorHandler = 3; |
| 8130 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8131 | *known_errorHandler = 4; |
| 8132 | else |
| 8133 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8134 | } |
| 8135 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8136 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8137 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8138 | return -1; |
| 8139 | case 2: /* replace */ |
| 8140 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8141 | x = charmapencode_output('?', mapping, res, respos); |
| 8142 | if (x==enc_EXCEPTION) { |
| 8143 | return -1; |
| 8144 | } |
| 8145 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8146 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8147 | return -1; |
| 8148 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8149 | } |
| 8150 | /* fall through */ |
| 8151 | case 3: /* ignore */ |
| 8152 | *inpos = collendpos; |
| 8153 | break; |
| 8154 | case 4: /* xmlcharrefreplace */ |
| 8155 | /* generate replacement (temporarily (mis)uses p) */ |
| 8156 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8157 | char buffer[2+29+1+1]; |
| 8158 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8159 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | for (cp = buffer; *cp; ++cp) { |
| 8161 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8162 | if (x==enc_EXCEPTION) |
| 8163 | return -1; |
| 8164 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8165 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8166 | return -1; |
| 8167 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8168 | } |
| 8169 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8170 | *inpos = collendpos; |
| 8171 | break; |
| 8172 | default: |
| 8173 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8174 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8176 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8177 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8178 | if (PyBytes_Check(repunicode)) { |
| 8179 | /* Directly copy bytes result to output. */ |
| 8180 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8181 | Py_ssize_t requiredsize; |
| 8182 | repsize = PyBytes_Size(repunicode); |
| 8183 | requiredsize = *respos + repsize; |
| 8184 | if (requiredsize > outsize) |
| 8185 | /* Make room for all additional bytes. */ |
| 8186 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8187 | Py_DECREF(repunicode); |
| 8188 | return -1; |
| 8189 | } |
| 8190 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8191 | PyBytes_AsString(repunicode), repsize); |
| 8192 | *respos += repsize; |
| 8193 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8194 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8195 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8196 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8197 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8198 | if (PyUnicode_READY(repunicode) < 0) { |
| 8199 | Py_DECREF(repunicode); |
| 8200 | return -1; |
| 8201 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8202 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8203 | data = PyUnicode_DATA(repunicode); |
| 8204 | kind = PyUnicode_KIND(repunicode); |
| 8205 | for (index = 0; index < repsize; index++) { |
| 8206 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8207 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8209 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | return -1; |
| 8211 | } |
| 8212 | else if (x==enc_FAILED) { |
| 8213 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8214 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8215 | return -1; |
| 8216 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8217 | } |
| 8218 | *inpos = newpos; |
| 8219 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8220 | } |
| 8221 | return 0; |
| 8222 | } |
| 8223 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8224 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8225 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8226 | PyObject *mapping, |
| 8227 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8229 | /* output object */ |
| 8230 | PyObject *res = NULL; |
| 8231 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8232 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8233 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8234 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8235 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8236 | PyObject *errorHandler = NULL; |
| 8237 | PyObject *exc = NULL; |
| 8238 | /* the following variable is used for caching string comparisons |
| 8239 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8240 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8241 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8243 | if (PyUnicode_READY(unicode) < 0) |
| 8244 | return NULL; |
| 8245 | size = PyUnicode_GET_LENGTH(unicode); |
| 8246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8247 | /* Default to Latin-1 */ |
| 8248 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8249 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8250 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8251 | /* allocate enough for a simple encoding without |
| 8252 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8253 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8254 | if (res == NULL) |
| 8255 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8256 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8257 | return res; |
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 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8260 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8262 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | if (x==enc_EXCEPTION) /* error */ |
| 8264 | goto onError; |
| 8265 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8266 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8267 | &exc, |
| 8268 | &known_errorHandler, &errorHandler, errors, |
| 8269 | &res, &respos)) { |
| 8270 | goto onError; |
| 8271 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8272 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8273 | else |
| 8274 | /* done with this character => adjust input position */ |
| 8275 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8276 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8278 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8279 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8280 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8281 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8282 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8283 | Py_XDECREF(exc); |
| 8284 | Py_XDECREF(errorHandler); |
| 8285 | return res; |
| 8286 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8288 | Py_XDECREF(res); |
| 8289 | Py_XDECREF(exc); |
| 8290 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8291 | return NULL; |
| 8292 | } |
| 8293 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8294 | /* Deprecated */ |
| 8295 | PyObject * |
| 8296 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8297 | Py_ssize_t size, |
| 8298 | PyObject *mapping, |
| 8299 | const char *errors) |
| 8300 | { |
| 8301 | PyObject *result; |
| 8302 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8303 | if (unicode == NULL) |
| 8304 | return NULL; |
| 8305 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8306 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8307 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8308 | } |
| 8309 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8310 | PyObject * |
| 8311 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8312 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8313 | { |
| 8314 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8315 | PyErr_BadArgument(); |
| 8316 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8317 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8318 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8319 | } |
| 8320 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8321 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8322 | static void |
| 8323 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8324 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8325 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8326 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8328 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8329 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8330 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | } |
| 8332 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8334 | goto onError; |
| 8335 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8336 | goto onError; |
| 8337 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8338 | goto onError; |
| 8339 | return; |
| 8340 | onError: |
| 8341 | Py_DECREF(*exceptionObject); |
| 8342 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | } |
| 8344 | } |
| 8345 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8346 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8347 | static void |
| 8348 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8349 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8350 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8351 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | { |
| 8353 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8354 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8355 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8356 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8357 | } |
| 8358 | |
| 8359 | /* error handling callback helper: |
| 8360 | build arguments, call the callback and check the arguments, |
| 8361 | put the result into newpos and return the replacement string, which |
| 8362 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8363 | static PyObject * |
| 8364 | unicode_translate_call_errorhandler(const char *errors, |
| 8365 | PyObject **errorHandler, |
| 8366 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8367 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8368 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8369 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8371 | 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] | 8372 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8373 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8374 | PyObject *restuple; |
| 8375 | PyObject *resunicode; |
| 8376 | |
| 8377 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8379 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | } |
| 8382 | |
| 8383 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8384 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8385 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8387 | |
| 8388 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8389 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8390 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8392 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8393 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | Py_DECREF(restuple); |
| 8395 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8396 | } |
| 8397 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8398 | &resunicode, &i_newpos)) { |
| 8399 | Py_DECREF(restuple); |
| 8400 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8401 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8402 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8403 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8404 | else |
| 8405 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8406 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8407 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8408 | Py_DECREF(restuple); |
| 8409 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8410 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8411 | Py_INCREF(resunicode); |
| 8412 | Py_DECREF(restuple); |
| 8413 | return resunicode; |
| 8414 | } |
| 8415 | |
| 8416 | /* Lookup the character ch in the mapping and put the result in result, |
| 8417 | which must be decrefed by the caller. |
| 8418 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8419 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8420 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8421 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8422 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8423 | PyObject *x; |
| 8424 | |
| 8425 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8426 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8427 | x = PyObject_GetItem(mapping, w); |
| 8428 | Py_DECREF(w); |
| 8429 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8431 | /* No mapping found means: use 1:1 mapping. */ |
| 8432 | PyErr_Clear(); |
| 8433 | *result = NULL; |
| 8434 | return 0; |
| 8435 | } else |
| 8436 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8437 | } |
| 8438 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | *result = x; |
| 8440 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8441 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8442 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | long value = PyLong_AS_LONG(x); |
| 8444 | long max = PyUnicode_GetMax(); |
| 8445 | if (value < 0 || value > max) { |
| 8446 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8447 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | Py_DECREF(x); |
| 8449 | return -1; |
| 8450 | } |
| 8451 | *result = x; |
| 8452 | return 0; |
| 8453 | } |
| 8454 | else if (PyUnicode_Check(x)) { |
| 8455 | *result = x; |
| 8456 | return 0; |
| 8457 | } |
| 8458 | else { |
| 8459 | /* wrong return value */ |
| 8460 | PyErr_SetString(PyExc_TypeError, |
| 8461 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8462 | Py_DECREF(x); |
| 8463 | return -1; |
| 8464 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8465 | } |
| 8466 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8467 | if not reallocate and adjust various state variables. |
| 8468 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8469 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8471 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8472 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8473 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8474 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8475 | /* exponentially overallocate to minimize reallocations */ |
| 8476 | if (requiredsize < 2 * oldsize) |
| 8477 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8478 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8479 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8480 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8481 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8482 | } |
| 8483 | return 0; |
| 8484 | } |
| 8485 | /* lookup the character, put the result in the output string and adjust |
| 8486 | various state variables. Return a new reference to the object that |
| 8487 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8488 | undefined (in which case no character was written). |
| 8489 | The called must decref result. |
| 8490 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8491 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8492 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8493 | PyObject *mapping, Py_UCS4 **output, |
| 8494 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8495 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8496 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8497 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8498 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8500 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8501 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8503 | } |
| 8504 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8505 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8506 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 | /* 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] | 8508 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8509 | } |
| 8510 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | Py_ssize_t repsize; |
| 8512 | if (PyUnicode_READY(*res) == -1) |
| 8513 | return -1; |
| 8514 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | if (repsize==1) { |
| 8516 | /* 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] | 8517 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | } |
| 8519 | else if (repsize!=0) { |
| 8520 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8521 | Py_ssize_t requiredsize = *opos + |
| 8522 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8524 | Py_ssize_t i; |
| 8525 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8526 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8527 | for(i = 0; i < repsize; i++) |
| 8528 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8530 | } |
| 8531 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8533 | return 0; |
| 8534 | } |
| 8535 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8536 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8537 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8538 | PyObject *mapping, |
| 8539 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8540 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | /* input object */ |
| 8542 | char *idata; |
| 8543 | Py_ssize_t size, i; |
| 8544 | int kind; |
| 8545 | /* output buffer */ |
| 8546 | Py_UCS4 *output = NULL; |
| 8547 | Py_ssize_t osize; |
| 8548 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8549 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8550 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8551 | char *reason = "character maps to <undefined>"; |
| 8552 | PyObject *errorHandler = NULL; |
| 8553 | PyObject *exc = NULL; |
| 8554 | /* the following variable is used for caching string comparisons |
| 8555 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8556 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8557 | int known_errorHandler = -1; |
| 8558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8559 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | PyErr_BadArgument(); |
| 8561 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8562 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8563 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8564 | if (PyUnicode_READY(input) == -1) |
| 8565 | return NULL; |
| 8566 | idata = (char*)PyUnicode_DATA(input); |
| 8567 | kind = PyUnicode_KIND(input); |
| 8568 | size = PyUnicode_GET_LENGTH(input); |
| 8569 | i = 0; |
| 8570 | |
| 8571 | if (size == 0) { |
| 8572 | Py_INCREF(input); |
| 8573 | return input; |
| 8574 | } |
| 8575 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8576 | /* allocate enough for a simple 1:1 translation without |
| 8577 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | osize = size; |
| 8579 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8580 | opos = 0; |
| 8581 | if (output == NULL) { |
| 8582 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8583 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8584 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8586 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8587 | /* try to encode it */ |
| 8588 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8589 | if (charmaptranslate_output(input, i, mapping, |
| 8590 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | Py_XDECREF(x); |
| 8592 | goto onError; |
| 8593 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8594 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8595 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8596 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8597 | else { /* untranslatable character */ |
| 8598 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8599 | Py_ssize_t repsize; |
| 8600 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8602 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8603 | Py_ssize_t collstart = i; |
| 8604 | Py_ssize_t collend = i+1; |
| 8605 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | while (collend < size) { |
| 8609 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | goto onError; |
| 8611 | Py_XDECREF(x); |
| 8612 | if (x!=Py_None) |
| 8613 | break; |
| 8614 | ++collend; |
| 8615 | } |
| 8616 | /* cache callback name lookup |
| 8617 | * (if not done yet, i.e. it's the first error) */ |
| 8618 | if (known_errorHandler==-1) { |
| 8619 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8620 | known_errorHandler = 1; |
| 8621 | else if (!strcmp(errors, "replace")) |
| 8622 | known_errorHandler = 2; |
| 8623 | else if (!strcmp(errors, "ignore")) |
| 8624 | known_errorHandler = 3; |
| 8625 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8626 | known_errorHandler = 4; |
| 8627 | else |
| 8628 | known_errorHandler = 0; |
| 8629 | } |
| 8630 | switch (known_errorHandler) { |
| 8631 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | raise_translate_exception(&exc, input, collstart, |
| 8633 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8634 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8635 | case 2: /* replace */ |
| 8636 | /* 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] | 8637 | for (coll = collstart; coll<collend; coll++) |
| 8638 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8639 | /* fall through */ |
| 8640 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8642 | break; |
| 8643 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8644 | /* generate replacement (temporarily (mis)uses i) */ |
| 8645 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8646 | char buffer[2+29+1+1]; |
| 8647 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8648 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8649 | if (charmaptranslate_makespace(&output, &osize, |
| 8650 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | goto onError; |
| 8652 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8653 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8654 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8655 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8656 | break; |
| 8657 | default: |
| 8658 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | reason, input, &exc, |
| 8660 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8661 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | goto onError; |
| 8663 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8665 | if (charmaptranslate_makespace(&output, &osize, |
| 8666 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8667 | Py_DECREF(repunicode); |
| 8668 | goto onError; |
| 8669 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8670 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8671 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8672 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8673 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8674 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8675 | } |
| 8676 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8678 | if (!res) |
| 8679 | goto onError; |
| 8680 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8681 | Py_XDECREF(exc); |
| 8682 | Py_XDECREF(errorHandler); |
| 8683 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8684 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8685 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8686 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8687 | Py_XDECREF(exc); |
| 8688 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8689 | return NULL; |
| 8690 | } |
| 8691 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8692 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8693 | PyObject * |
| 8694 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8695 | Py_ssize_t size, |
| 8696 | PyObject *mapping, |
| 8697 | const char *errors) |
| 8698 | { |
| 8699 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8700 | if (!unicode) |
| 8701 | return NULL; |
| 8702 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8703 | } |
| 8704 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8705 | PyObject * |
| 8706 | PyUnicode_Translate(PyObject *str, |
| 8707 | PyObject *mapping, |
| 8708 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8709 | { |
| 8710 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8712 | str = PyUnicode_FromObject(str); |
| 8713 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8714 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8715 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | Py_DECREF(str); |
| 8717 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8718 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8720 | Py_XDECREF(str); |
| 8721 | return NULL; |
| 8722 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8723 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8724 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8725 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8726 | { |
| 8727 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8728 | called as a callback from fixup() which does it already. */ |
| 8729 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8730 | const int kind = PyUnicode_KIND(self); |
| 8731 | void *data = PyUnicode_DATA(self); |
| 8732 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8733 | Py_ssize_t i; |
| 8734 | |
| 8735 | for (i = 0; i < len; ++i) { |
| 8736 | ch = PyUnicode_READ(kind, data, i); |
| 8737 | fixed = 0; |
| 8738 | if (ch > 127) { |
| 8739 | if (Py_UNICODE_ISSPACE(ch)) |
| 8740 | fixed = ' '; |
| 8741 | else { |
| 8742 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8743 | if (decimal >= 0) |
| 8744 | fixed = '0' + decimal; |
| 8745 | } |
| 8746 | if (fixed != 0) { |
| 8747 | if (fixed > maxchar) |
| 8748 | maxchar = fixed; |
| 8749 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8750 | } |
| 8751 | else if (ch > maxchar) |
| 8752 | maxchar = ch; |
| 8753 | } |
| 8754 | else if (ch > maxchar) |
| 8755 | maxchar = ch; |
| 8756 | } |
| 8757 | |
| 8758 | return maxchar; |
| 8759 | } |
| 8760 | |
| 8761 | PyObject * |
| 8762 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8763 | { |
| 8764 | if (!PyUnicode_Check(unicode)) { |
| 8765 | PyErr_BadInternalCall(); |
| 8766 | return NULL; |
| 8767 | } |
| 8768 | if (PyUnicode_READY(unicode) == -1) |
| 8769 | return NULL; |
| 8770 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8771 | /* If the string is already ASCII, just return the same string */ |
| 8772 | Py_INCREF(unicode); |
| 8773 | return unicode; |
| 8774 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8775 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8776 | } |
| 8777 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8778 | PyObject * |
| 8779 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8780 | Py_ssize_t length) |
| 8781 | { |
| 8782 | PyObject *result; |
| 8783 | Py_UNICODE *p; /* write pointer into result */ |
| 8784 | Py_ssize_t i; |
| 8785 | /* Copy to a new string */ |
| 8786 | result = (PyObject *)_PyUnicode_New(length); |
| 8787 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8788 | if (result == NULL) |
| 8789 | return result; |
| 8790 | p = PyUnicode_AS_UNICODE(result); |
| 8791 | /* Iterate over code points */ |
| 8792 | for (i = 0; i < length; i++) { |
| 8793 | Py_UNICODE ch =s[i]; |
| 8794 | if (ch > 127) { |
| 8795 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8796 | if (decimal >= 0) |
| 8797 | p[i] = '0' + decimal; |
| 8798 | } |
| 8799 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8800 | #ifndef DONT_MAKE_RESULT_READY |
| 8801 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8802 | Py_DECREF(result); |
| 8803 | return NULL; |
| 8804 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8805 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8806 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8807 | return result; |
| 8808 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8809 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8810 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8811 | int |
| 8812 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8813 | Py_ssize_t length, |
| 8814 | char *output, |
| 8815 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8816 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8817 | PyObject *errorHandler = NULL; |
| 8818 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8819 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8820 | const char *encoding = "decimal"; |
| 8821 | const char *reason = "invalid decimal Unicode string"; |
| 8822 | /* the following variable is used for caching string comparisons |
| 8823 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8824 | int known_errorHandler = -1; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8825 | Py_ssize_t i, j; |
| 8826 | enum PyUnicode_Kind kind; |
| 8827 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8828 | |
| 8829 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8830 | PyErr_BadArgument(); |
| 8831 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8832 | } |
| 8833 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8834 | unicode = PyUnicode_FromUnicode(s, length); |
| 8835 | if (unicode == NULL) |
| 8836 | return -1; |
| 8837 | |
| 8838 | if (PyUnicode_READY(unicode) < 0) |
| 8839 | goto onError; |
| 8840 | kind = PyUnicode_KIND(unicode); |
| 8841 | data = PyUnicode_DATA(unicode); |
| 8842 | |
| 8843 | for (i=0; i < length; i++) { |
| 8844 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8845 | int decimal; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8846 | Py_ssize_t startpos, endpos; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8847 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8848 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8849 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8850 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8851 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8852 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8853 | if (decimal >= 0) { |
| 8854 | *output++ = '0' + decimal; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8855 | continue; |
| 8856 | } |
| 8857 | if (0 < ch && ch < 256) { |
| 8858 | *output++ = (char)ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8859 | continue; |
| 8860 | } |
| 8861 | /* All other characters are considered unencodable */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8862 | startpos = i; |
| 8863 | endpos = i+1; |
| 8864 | for (; endpos < length; endpos++) { |
| 8865 | ch = PyUnicode_READ(kind, data, endpos); |
| 8866 | if ((0 < ch && ch < 256) || |
| 8867 | !Py_UNICODE_ISSPACE(ch) || |
| 8868 | Py_UNICODE_TODECIMAL(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8869 | break; |
| 8870 | } |
| 8871 | /* cache callback name lookup |
| 8872 | * (if not done yet, i.e. it's the first error) */ |
| 8873 | if (known_errorHandler==-1) { |
| 8874 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8875 | known_errorHandler = 1; |
| 8876 | else if (!strcmp(errors, "replace")) |
| 8877 | known_errorHandler = 2; |
| 8878 | else if (!strcmp(errors, "ignore")) |
| 8879 | known_errorHandler = 3; |
| 8880 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8881 | known_errorHandler = 4; |
| 8882 | else |
| 8883 | known_errorHandler = 0; |
| 8884 | } |
| 8885 | switch (known_errorHandler) { |
| 8886 | case 1: /* strict */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8887 | raise_encode_exception(&exc, encoding, unicode, startpos, endpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8888 | goto onError; |
| 8889 | case 2: /* replace */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8890 | for (j=startpos; j < endpos; j++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8891 | *output++ = '?'; |
| 8892 | /* fall through */ |
| 8893 | case 3: /* ignore */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8894 | i = endpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8895 | break; |
| 8896 | case 4: /* xmlcharrefreplace */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8897 | /* generate replacement */ |
| 8898 | for (j=startpos; j < endpos; j++) { |
| 8899 | ch = PyUnicode_READ(kind, data, i); |
| 8900 | output += sprintf(output, "&#%d;", (int)ch); |
| 8901 | i++; |
| 8902 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8903 | break; |
| 8904 | default: |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8905 | { |
| 8906 | PyObject *repunicode; |
| 8907 | Py_ssize_t repsize, newpos, k; |
| 8908 | enum PyUnicode_Kind repkind; |
| 8909 | void *repdata; |
| 8910 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8911 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8912 | encoding, reason, unicode, &exc, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8913 | startpos, endpos, &newpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8914 | if (repunicode == NULL) |
| 8915 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8916 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8917 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8918 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8919 | Py_DECREF(repunicode); |
| 8920 | goto onError; |
| 8921 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8922 | if (PyUnicode_READY(repunicode) < 0) { |
| 8923 | Py_DECREF(repunicode); |
| 8924 | goto onError; |
| 8925 | } |
| 8926 | repkind = PyUnicode_KIND(repunicode); |
| 8927 | repdata = PyUnicode_DATA(repunicode); |
| 8928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8929 | /* generate replacement */ |
| 8930 | repsize = PyUnicode_GET_SIZE(repunicode); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8931 | for (k=0; k<repsize; k++) { |
| 8932 | ch = PyUnicode_READ(repkind, repdata, k); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8933 | if (Py_UNICODE_ISSPACE(ch)) |
| 8934 | *output++ = ' '; |
| 8935 | else { |
| 8936 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8937 | if (decimal >= 0) |
| 8938 | *output++ = '0' + decimal; |
| 8939 | else if (0 < ch && ch < 256) |
| 8940 | *output++ = (char)ch; |
| 8941 | else { |
| 8942 | Py_DECREF(repunicode); |
| 8943 | raise_encode_exception(&exc, encoding, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8944 | unicode, startpos, endpos, |
| 8945 | reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | goto onError; |
| 8947 | } |
| 8948 | } |
| 8949 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8950 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8951 | Py_DECREF(repunicode); |
| 8952 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8953 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8954 | } |
| 8955 | /* 0-terminate the output string */ |
| 8956 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8957 | Py_XDECREF(exc); |
| 8958 | Py_XDECREF(errorHandler); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8959 | Py_DECREF(unicode); |
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); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8965 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8966 | return -1; |
| 8967 | } |
| 8968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | /* --- Helpers ------------------------------------------------------------ */ |
| 8970 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8972 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | Py_ssize_t start, |
| 8974 | Py_ssize_t end) |
| 8975 | { |
| 8976 | int kind1, kind2, kind; |
| 8977 | void *buf1, *buf2; |
| 8978 | Py_ssize_t len1, len2, result; |
| 8979 | |
| 8980 | kind1 = PyUnicode_KIND(s1); |
| 8981 | kind2 = PyUnicode_KIND(s2); |
| 8982 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8983 | buf1 = PyUnicode_DATA(s1); |
| 8984 | buf2 = PyUnicode_DATA(s2); |
| 8985 | if (kind1 != kind) |
| 8986 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8987 | if (!buf1) |
| 8988 | return -2; |
| 8989 | if (kind2 != kind) |
| 8990 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8991 | if (!buf2) { |
| 8992 | if (kind1 != kind) PyMem_Free(buf1); |
| 8993 | return -2; |
| 8994 | } |
| 8995 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8996 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8997 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8998 | if (direction > 0) { |
| 8999 | switch(kind) { |
| 9000 | case PyUnicode_1BYTE_KIND: |
| 9001 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9002 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9003 | else |
| 9004 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9005 | break; |
| 9006 | case PyUnicode_2BYTE_KIND: |
| 9007 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9008 | break; |
| 9009 | case PyUnicode_4BYTE_KIND: |
| 9010 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9011 | break; |
| 9012 | default: |
| 9013 | assert(0); result = -2; |
| 9014 | } |
| 9015 | } |
| 9016 | else { |
| 9017 | switch(kind) { |
| 9018 | case PyUnicode_1BYTE_KIND: |
| 9019 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9020 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9021 | else |
| 9022 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9023 | break; |
| 9024 | case PyUnicode_2BYTE_KIND: |
| 9025 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9026 | break; |
| 9027 | case PyUnicode_4BYTE_KIND: |
| 9028 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9029 | break; |
| 9030 | default: |
| 9031 | assert(0); result = -2; |
| 9032 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9033 | } |
| 9034 | |
| 9035 | if (kind1 != kind) |
| 9036 | PyMem_Free(buf1); |
| 9037 | if (kind2 != kind) |
| 9038 | PyMem_Free(buf2); |
| 9039 | |
| 9040 | return result; |
| 9041 | } |
| 9042 | |
| 9043 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9044 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 | Py_ssize_t n_buffer, |
| 9046 | void *digits, Py_ssize_t n_digits, |
| 9047 | Py_ssize_t min_width, |
| 9048 | const char *grouping, |
| 9049 | const char *thousands_sep) |
| 9050 | { |
| 9051 | switch(kind) { |
| 9052 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9053 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9054 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9055 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9056 | min_width, grouping, thousands_sep); |
| 9057 | else |
| 9058 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9059 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9060 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | case PyUnicode_2BYTE_KIND: |
| 9062 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9063 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9064 | min_width, grouping, thousands_sep); |
| 9065 | case PyUnicode_4BYTE_KIND: |
| 9066 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9067 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9068 | min_width, grouping, thousands_sep); |
| 9069 | } |
| 9070 | assert(0); |
| 9071 | return -1; |
| 9072 | } |
| 9073 | |
| 9074 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9075 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9076 | #define ADJUST_INDICES(start, end, len) \ |
| 9077 | if (end > len) \ |
| 9078 | end = len; \ |
| 9079 | else if (end < 0) { \ |
| 9080 | end += len; \ |
| 9081 | if (end < 0) \ |
| 9082 | end = 0; \ |
| 9083 | } \ |
| 9084 | if (start < 0) { \ |
| 9085 | start += len; \ |
| 9086 | if (start < 0) \ |
| 9087 | start = 0; \ |
| 9088 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9089 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9090 | Py_ssize_t |
| 9091 | PyUnicode_Count(PyObject *str, |
| 9092 | PyObject *substr, |
| 9093 | Py_ssize_t start, |
| 9094 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9095 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9096 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9097 | PyObject* str_obj; |
| 9098 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9099 | int kind1, kind2, kind; |
| 9100 | void *buf1 = NULL, *buf2 = NULL; |
| 9101 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9102 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9103 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9104 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9105 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9106 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9107 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9108 | Py_DECREF(str_obj); |
| 9109 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9111 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | kind1 = PyUnicode_KIND(str_obj); |
| 9113 | kind2 = PyUnicode_KIND(sub_obj); |
| 9114 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9115 | buf1 = PyUnicode_DATA(str_obj); |
| 9116 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9117 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | if (!buf1) |
| 9119 | goto onError; |
| 9120 | buf2 = PyUnicode_DATA(sub_obj); |
| 9121 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9122 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9123 | if (!buf2) |
| 9124 | goto onError; |
| 9125 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9126 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9127 | |
| 9128 | ADJUST_INDICES(start, end, len1); |
| 9129 | switch(kind) { |
| 9130 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9131 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9132 | result = asciilib_count( |
| 9133 | ((Py_UCS1*)buf1) + start, end - start, |
| 9134 | buf2, len2, PY_SSIZE_T_MAX |
| 9135 | ); |
| 9136 | else |
| 9137 | result = ucs1lib_count( |
| 9138 | ((Py_UCS1*)buf1) + start, end - start, |
| 9139 | buf2, len2, PY_SSIZE_T_MAX |
| 9140 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9141 | break; |
| 9142 | case PyUnicode_2BYTE_KIND: |
| 9143 | result = ucs2lib_count( |
| 9144 | ((Py_UCS2*)buf1) + start, end - start, |
| 9145 | buf2, len2, PY_SSIZE_T_MAX |
| 9146 | ); |
| 9147 | break; |
| 9148 | case PyUnicode_4BYTE_KIND: |
| 9149 | result = ucs4lib_count( |
| 9150 | ((Py_UCS4*)buf1) + start, end - start, |
| 9151 | buf2, len2, PY_SSIZE_T_MAX |
| 9152 | ); |
| 9153 | break; |
| 9154 | default: |
| 9155 | assert(0); result = 0; |
| 9156 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9157 | |
| 9158 | Py_DECREF(sub_obj); |
| 9159 | Py_DECREF(str_obj); |
| 9160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9161 | if (kind1 != kind) |
| 9162 | PyMem_Free(buf1); |
| 9163 | if (kind2 != kind) |
| 9164 | PyMem_Free(buf2); |
| 9165 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9166 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9167 | onError: |
| 9168 | Py_DECREF(sub_obj); |
| 9169 | Py_DECREF(str_obj); |
| 9170 | if (kind1 != kind && buf1) |
| 9171 | PyMem_Free(buf1); |
| 9172 | if (kind2 != kind && buf2) |
| 9173 | PyMem_Free(buf2); |
| 9174 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9175 | } |
| 9176 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9177 | Py_ssize_t |
| 9178 | PyUnicode_Find(PyObject *str, |
| 9179 | PyObject *sub, |
| 9180 | Py_ssize_t start, |
| 9181 | Py_ssize_t end, |
| 9182 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9183 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9184 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9186 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9187 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9188 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9189 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9190 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9191 | Py_DECREF(str); |
| 9192 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9193 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9194 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9195 | result = any_find_slice(direction, |
| 9196 | str, sub, start, end |
| 9197 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9199 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9200 | Py_DECREF(sub); |
| 9201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9202 | return result; |
| 9203 | } |
| 9204 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9205 | Py_ssize_t |
| 9206 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9207 | Py_ssize_t start, Py_ssize_t end, |
| 9208 | int direction) |
| 9209 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9210 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9211 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9212 | if (PyUnicode_READY(str) == -1) |
| 9213 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9214 | if (start < 0 || end < 0) { |
| 9215 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9216 | return -2; |
| 9217 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9218 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9219 | end = PyUnicode_GET_LENGTH(str); |
| 9220 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9221 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9222 | kind, end-start, ch, direction); |
| 9223 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9224 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9225 | else |
| 9226 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9227 | } |
| 9228 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9229 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9230 | tailmatch(PyObject *self, |
| 9231 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9232 | Py_ssize_t start, |
| 9233 | Py_ssize_t end, |
| 9234 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9235 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9236 | int kind_self; |
| 9237 | int kind_sub; |
| 9238 | void *data_self; |
| 9239 | void *data_sub; |
| 9240 | Py_ssize_t offset; |
| 9241 | Py_ssize_t i; |
| 9242 | Py_ssize_t end_sub; |
| 9243 | |
| 9244 | if (PyUnicode_READY(self) == -1 || |
| 9245 | PyUnicode_READY(substring) == -1) |
| 9246 | return 0; |
| 9247 | |
| 9248 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9249 | return 1; |
| 9250 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9251 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9252 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9253 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9254 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9255 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9256 | kind_self = PyUnicode_KIND(self); |
| 9257 | data_self = PyUnicode_DATA(self); |
| 9258 | kind_sub = PyUnicode_KIND(substring); |
| 9259 | data_sub = PyUnicode_DATA(substring); |
| 9260 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9261 | |
| 9262 | if (direction > 0) |
| 9263 | offset = end; |
| 9264 | else |
| 9265 | offset = start; |
| 9266 | |
| 9267 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9268 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9269 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9270 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9271 | /* If both are of the same kind, memcmp is sufficient */ |
| 9272 | if (kind_self == kind_sub) { |
| 9273 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9274 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9275 | data_sub, |
| 9276 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9277 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9278 | } |
| 9279 | /* otherwise we have to compare each character by first accesing it */ |
| 9280 | else { |
| 9281 | /* We do not need to compare 0 and len(substring)-1 because |
| 9282 | the if statement above ensured already that they are equal |
| 9283 | when we end up here. */ |
| 9284 | // TODO: honor direction and do a forward or backwards search |
| 9285 | for (i = 1; i < end_sub; ++i) { |
| 9286 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9287 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9288 | return 0; |
| 9289 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9290 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9291 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9292 | } |
| 9293 | |
| 9294 | return 0; |
| 9295 | } |
| 9296 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9297 | Py_ssize_t |
| 9298 | PyUnicode_Tailmatch(PyObject *str, |
| 9299 | PyObject *substr, |
| 9300 | Py_ssize_t start, |
| 9301 | Py_ssize_t end, |
| 9302 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9303 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9304 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9306 | str = PyUnicode_FromObject(str); |
| 9307 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9308 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9309 | substr = PyUnicode_FromObject(substr); |
| 9310 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9311 | Py_DECREF(str); |
| 9312 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9313 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9314 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9315 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9316 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9317 | Py_DECREF(str); |
| 9318 | Py_DECREF(substr); |
| 9319 | return result; |
| 9320 | } |
| 9321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9322 | /* Apply fixfct filter to the Unicode object self and return a |
| 9323 | reference to the modified object */ |
| 9324 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9325 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9326 | fixup(PyObject *self, |
| 9327 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9328 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9329 | PyObject *u; |
| 9330 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9331 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9332 | u = PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9334 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9335 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9336 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | /* fix functions return the new maximum character in a string, |
| 9338 | if the kind of the resulting unicode object does not change, |
| 9339 | everything is fine. Otherwise we need to change the string kind |
| 9340 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9341 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9342 | if (maxchar_new == 0) |
| 9343 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9344 | else if (maxchar_new <= 127) |
| 9345 | maxchar_new = 127; |
| 9346 | else if (maxchar_new <= 255) |
| 9347 | maxchar_new = 255; |
| 9348 | else if (maxchar_new <= 65535) |
| 9349 | maxchar_new = 65535; |
| 9350 | else |
| 9351 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9352 | |
| 9353 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9354 | /* fixfct should return TRUE if it modified the buffer. If |
| 9355 | FALSE, return a reference to the original buffer instead |
| 9356 | (to save space, not time) */ |
| 9357 | Py_INCREF(self); |
| 9358 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9359 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9360 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9361 | else if (maxchar_new == maxchar_old) { |
| 9362 | return u; |
| 9363 | } |
| 9364 | else { |
| 9365 | /* In case the maximum character changed, we need to |
| 9366 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9367 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9368 | if (v == NULL) { |
| 9369 | Py_DECREF(u); |
| 9370 | return NULL; |
| 9371 | } |
| 9372 | if (maxchar_new > maxchar_old) { |
| 9373 | /* If the maxchar increased so that the kind changed, not all |
| 9374 | characters are representable anymore and we need to fix the |
| 9375 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9376 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9377 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9378 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9379 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9380 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9381 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9382 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9383 | |
| 9384 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9385 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9386 | return v; |
| 9387 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9388 | } |
| 9389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9391 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9392 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9393 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9394 | called as a callback from fixup() which does it already. */ |
| 9395 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9396 | const int kind = PyUnicode_KIND(self); |
| 9397 | void *data = PyUnicode_DATA(self); |
| 9398 | int touched = 0; |
| 9399 | Py_UCS4 maxchar = 0; |
| 9400 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9402 | for (i = 0; i < len; ++i) { |
| 9403 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9404 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9405 | if (up != ch) { |
| 9406 | if (up > maxchar) |
| 9407 | maxchar = up; |
| 9408 | PyUnicode_WRITE(kind, data, i, up); |
| 9409 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9410 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9411 | else if (ch > maxchar) |
| 9412 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | } |
| 9414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9415 | if (touched) |
| 9416 | return maxchar; |
| 9417 | else |
| 9418 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9419 | } |
| 9420 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9421 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9422 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9423 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9424 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9425 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9426 | const int kind = PyUnicode_KIND(self); |
| 9427 | void *data = PyUnicode_DATA(self); |
| 9428 | int touched = 0; |
| 9429 | Py_UCS4 maxchar = 0; |
| 9430 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9431 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9432 | for(i = 0; i < len; ++i) { |
| 9433 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9434 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9435 | if (lo != ch) { |
| 9436 | if (lo > maxchar) |
| 9437 | maxchar = lo; |
| 9438 | PyUnicode_WRITE(kind, data, i, lo); |
| 9439 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9441 | else if (ch > maxchar) |
| 9442 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9443 | } |
| 9444 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9445 | if (touched) |
| 9446 | return maxchar; |
| 9447 | else |
| 9448 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9449 | } |
| 9450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9451 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9452 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9453 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9454 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9455 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9456 | const int kind = PyUnicode_KIND(self); |
| 9457 | void *data = PyUnicode_DATA(self); |
| 9458 | int touched = 0; |
| 9459 | Py_UCS4 maxchar = 0; |
| 9460 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9462 | for(i = 0; i < len; ++i) { |
| 9463 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9464 | Py_UCS4 nu = 0; |
| 9465 | |
| 9466 | if (Py_UNICODE_ISUPPER(ch)) |
| 9467 | nu = Py_UNICODE_TOLOWER(ch); |
| 9468 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9469 | nu = Py_UNICODE_TOUPPER(ch); |
| 9470 | |
| 9471 | if (nu != 0) { |
| 9472 | if (nu > maxchar) |
| 9473 | maxchar = nu; |
| 9474 | PyUnicode_WRITE(kind, data, i, nu); |
| 9475 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9477 | else if (ch > maxchar) |
| 9478 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | } |
| 9480 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9481 | if (touched) |
| 9482 | return maxchar; |
| 9483 | else |
| 9484 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | } |
| 9486 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9487 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9488 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9489 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9490 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9491 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9492 | const int kind = PyUnicode_KIND(self); |
| 9493 | void *data = PyUnicode_DATA(self); |
| 9494 | int touched = 0; |
| 9495 | Py_UCS4 maxchar = 0; |
| 9496 | Py_ssize_t i = 0; |
| 9497 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9498 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9499 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9500 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | |
| 9502 | ch = PyUnicode_READ(kind, data, i); |
| 9503 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9504 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9505 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9506 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9507 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9508 | ++i; |
| 9509 | for(; i < len; ++i) { |
| 9510 | ch = PyUnicode_READ(kind, data, i); |
| 9511 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9512 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9513 | if (lo > maxchar) |
| 9514 | maxchar = lo; |
| 9515 | PyUnicode_WRITE(kind, data, i, lo); |
| 9516 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9518 | else if (ch > maxchar) |
| 9519 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9520 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9521 | |
| 9522 | if (touched) |
| 9523 | return maxchar; |
| 9524 | else |
| 9525 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9526 | } |
| 9527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9528 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9529 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9530 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9531 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9532 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9533 | const int kind = PyUnicode_KIND(self); |
| 9534 | void *data = PyUnicode_DATA(self); |
| 9535 | Py_UCS4 maxchar = 0; |
| 9536 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | int previous_is_cased; |
| 9538 | |
| 9539 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9540 | if (len == 1) { |
| 9541 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9542 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9543 | if (ti != ch) { |
| 9544 | PyUnicode_WRITE(kind, data, i, ti); |
| 9545 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9546 | } |
| 9547 | else |
| 9548 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9549 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9550 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9551 | for(; i < len; ++i) { |
| 9552 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9553 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9554 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9555 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9556 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9557 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9558 | nu = Py_UNICODE_TOTITLE(ch); |
| 9559 | |
| 9560 | if (nu > maxchar) |
| 9561 | maxchar = nu; |
| 9562 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9563 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9564 | if (Py_UNICODE_ISLOWER(ch) || |
| 9565 | Py_UNICODE_ISUPPER(ch) || |
| 9566 | Py_UNICODE_ISTITLE(ch)) |
| 9567 | previous_is_cased = 1; |
| 9568 | else |
| 9569 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9570 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9571 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9572 | } |
| 9573 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9574 | PyObject * |
| 9575 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9576 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9577 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9578 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9579 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9580 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9581 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9582 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9583 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9584 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9585 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9586 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9587 | int use_memcpy; |
| 9588 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9589 | PyObject *last_obj; |
| 9590 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9591 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9592 | fseq = PySequence_Fast(seq, ""); |
| 9593 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9594 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9595 | } |
| 9596 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9597 | /* NOTE: the following code can't call back into Python code, |
| 9598 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9599 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9600 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9601 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9602 | /* If empty sequence, return u"". */ |
| 9603 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9604 | Py_DECREF(fseq); |
| 9605 | Py_INCREF(unicode_empty); |
| 9606 | res = unicode_empty; |
| 9607 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9608 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9609 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9610 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9611 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9612 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9613 | if (seqlen == 1) { |
| 9614 | if (PyUnicode_CheckExact(items[0])) { |
| 9615 | res = items[0]; |
| 9616 | Py_INCREF(res); |
| 9617 | Py_DECREF(fseq); |
| 9618 | return res; |
| 9619 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9620 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9621 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9622 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9623 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9624 | /* Set up sep and seplen */ |
| 9625 | if (separator == NULL) { |
| 9626 | /* fall back to a blank space separator */ |
| 9627 | sep = PyUnicode_FromOrdinal(' '); |
| 9628 | if (!sep) |
| 9629 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9630 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9631 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9632 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9633 | else { |
| 9634 | if (!PyUnicode_Check(separator)) { |
| 9635 | PyErr_Format(PyExc_TypeError, |
| 9636 | "separator: expected str instance," |
| 9637 | " %.80s found", |
| 9638 | Py_TYPE(separator)->tp_name); |
| 9639 | goto onError; |
| 9640 | } |
| 9641 | if (PyUnicode_READY(separator)) |
| 9642 | goto onError; |
| 9643 | sep = separator; |
| 9644 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9645 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9646 | /* inc refcount to keep this code path symmetric with the |
| 9647 | above case of a blank separator */ |
| 9648 | Py_INCREF(sep); |
| 9649 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9650 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9651 | } |
| 9652 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9653 | /* There are at least two things to join, or else we have a subclass |
| 9654 | * of str in the sequence. |
| 9655 | * Do a pre-pass to figure out the total amount of space we'll |
| 9656 | * need (sz), and see whether all argument are strings. |
| 9657 | */ |
| 9658 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9659 | #ifdef Py_DEBUG |
| 9660 | use_memcpy = 0; |
| 9661 | #else |
| 9662 | use_memcpy = 1; |
| 9663 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9664 | for (i = 0; i < seqlen; i++) { |
| 9665 | const Py_ssize_t old_sz = sz; |
| 9666 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9667 | if (!PyUnicode_Check(item)) { |
| 9668 | PyErr_Format(PyExc_TypeError, |
| 9669 | "sequence item %zd: expected str instance," |
| 9670 | " %.80s found", |
| 9671 | i, Py_TYPE(item)->tp_name); |
| 9672 | goto onError; |
| 9673 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9674 | if (PyUnicode_READY(item) == -1) |
| 9675 | goto onError; |
| 9676 | sz += PyUnicode_GET_LENGTH(item); |
| 9677 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9678 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9679 | if (i != 0) |
| 9680 | sz += seplen; |
| 9681 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9682 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9683 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9684 | goto onError; |
| 9685 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9686 | if (use_memcpy && last_obj != NULL) { |
| 9687 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9688 | use_memcpy = 0; |
| 9689 | } |
| 9690 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9691 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9692 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9693 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9694 | if (res == NULL) |
| 9695 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9696 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9697 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9698 | #ifdef Py_DEBUG |
| 9699 | use_memcpy = 0; |
| 9700 | #else |
| 9701 | if (use_memcpy) { |
| 9702 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9703 | kind = PyUnicode_KIND(res); |
| 9704 | if (seplen != 0) |
| 9705 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9706 | } |
| 9707 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9708 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9709 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9710 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9711 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9712 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9713 | if (use_memcpy) { |
| 9714 | Py_MEMCPY(res_data, |
| 9715 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9716 | kind * seplen); |
| 9717 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9718 | } |
| 9719 | else { |
| 9720 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9721 | res_offset += seplen; |
| 9722 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9723 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9724 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9725 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9726 | if (use_memcpy) { |
| 9727 | Py_MEMCPY(res_data, |
| 9728 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9729 | kind * itemlen); |
| 9730 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9731 | } |
| 9732 | else { |
| 9733 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9734 | res_offset += itemlen; |
| 9735 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9736 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9737 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9738 | if (use_memcpy) |
| 9739 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9740 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9741 | else |
| 9742 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9743 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9744 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9745 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9746 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9747 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9749 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9750 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9751 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9752 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 | return NULL; |
| 9754 | } |
| 9755 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | #define FILL(kind, data, value, start, length) \ |
| 9757 | do { \ |
| 9758 | Py_ssize_t i_ = 0; \ |
| 9759 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9760 | switch ((kind)) { \ |
| 9761 | case PyUnicode_1BYTE_KIND: { \ |
| 9762 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9763 | memset(to_, (unsigned char)value, length); \ |
| 9764 | break; \ |
| 9765 | } \ |
| 9766 | case PyUnicode_2BYTE_KIND: { \ |
| 9767 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9768 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9769 | break; \ |
| 9770 | } \ |
| 9771 | default: { \ |
| 9772 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9773 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9774 | break; \ |
| 9775 | } \ |
| 9776 | } \ |
| 9777 | } while (0) |
| 9778 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9779 | static PyObject * |
| 9780 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9781 | Py_ssize_t left, |
| 9782 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9784 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9785 | PyObject *u; |
| 9786 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9787 | int kind; |
| 9788 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9789 | |
| 9790 | if (left < 0) |
| 9791 | left = 0; |
| 9792 | if (right < 0) |
| 9793 | right = 0; |
| 9794 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9795 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9796 | Py_INCREF(self); |
| 9797 | return self; |
| 9798 | } |
| 9799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9800 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9801 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9802 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9803 | return NULL; |
| 9804 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9805 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9806 | if (fill > maxchar) |
| 9807 | maxchar = fill; |
| 9808 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9809 | if (!u) |
| 9810 | return NULL; |
| 9811 | |
| 9812 | kind = PyUnicode_KIND(u); |
| 9813 | data = PyUnicode_DATA(u); |
| 9814 | if (left) |
| 9815 | FILL(kind, data, fill, 0, left); |
| 9816 | if (right) |
| 9817 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9818 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9819 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9820 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9821 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9822 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9823 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9824 | PyObject * |
| 9825 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9826 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9827 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9828 | |
| 9829 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9830 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9831 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | switch(PyUnicode_KIND(string)) { |
| 9834 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9835 | if (PyUnicode_IS_ASCII(string)) |
| 9836 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9837 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9838 | PyUnicode_GET_LENGTH(string), keepends); |
| 9839 | else |
| 9840 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9841 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9842 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9843 | break; |
| 9844 | case PyUnicode_2BYTE_KIND: |
| 9845 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9846 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 | PyUnicode_GET_LENGTH(string), keepends); |
| 9848 | break; |
| 9849 | case PyUnicode_4BYTE_KIND: |
| 9850 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9851 | string, PyUnicode_4BYTE_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 | default: |
| 9855 | assert(0); |
| 9856 | list = 0; |
| 9857 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9858 | Py_DECREF(string); |
| 9859 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9860 | } |
| 9861 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9862 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9863 | split(PyObject *self, |
| 9864 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9865 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9866 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | int kind1, kind2, kind; |
| 9868 | void *buf1, *buf2; |
| 9869 | Py_ssize_t len1, len2; |
| 9870 | PyObject* out; |
| 9871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9872 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9873 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | if (PyUnicode_READY(self) == -1) |
| 9876 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9877 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9878 | if (substring == NULL) |
| 9879 | switch(PyUnicode_KIND(self)) { |
| 9880 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9881 | if (PyUnicode_IS_ASCII(self)) |
| 9882 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9883 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9884 | PyUnicode_GET_LENGTH(self), maxcount |
| 9885 | ); |
| 9886 | else |
| 9887 | return ucs1lib_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 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | case PyUnicode_2BYTE_KIND: |
| 9892 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9893 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9894 | PyUnicode_GET_LENGTH(self), maxcount |
| 9895 | ); |
| 9896 | case PyUnicode_4BYTE_KIND: |
| 9897 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9898 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | PyUnicode_GET_LENGTH(self), maxcount |
| 9900 | ); |
| 9901 | default: |
| 9902 | assert(0); |
| 9903 | return NULL; |
| 9904 | } |
| 9905 | |
| 9906 | if (PyUnicode_READY(substring) == -1) |
| 9907 | return NULL; |
| 9908 | |
| 9909 | kind1 = PyUnicode_KIND(self); |
| 9910 | kind2 = PyUnicode_KIND(substring); |
| 9911 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9912 | buf1 = PyUnicode_DATA(self); |
| 9913 | buf2 = PyUnicode_DATA(substring); |
| 9914 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9915 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9916 | if (!buf1) |
| 9917 | return NULL; |
| 9918 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9919 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | if (!buf2) { |
| 9921 | if (kind1 != kind) PyMem_Free(buf1); |
| 9922 | return NULL; |
| 9923 | } |
| 9924 | len1 = PyUnicode_GET_LENGTH(self); |
| 9925 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9926 | |
| 9927 | switch(kind) { |
| 9928 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9929 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9930 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9931 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9932 | else |
| 9933 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9934 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9935 | break; |
| 9936 | case PyUnicode_2BYTE_KIND: |
| 9937 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9938 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | break; |
| 9940 | case PyUnicode_4BYTE_KIND: |
| 9941 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9942 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9943 | break; |
| 9944 | default: |
| 9945 | out = NULL; |
| 9946 | } |
| 9947 | if (kind1 != kind) |
| 9948 | PyMem_Free(buf1); |
| 9949 | if (kind2 != kind) |
| 9950 | PyMem_Free(buf2); |
| 9951 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | } |
| 9953 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9954 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9955 | rsplit(PyObject *self, |
| 9956 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9957 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9958 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | int kind1, kind2, kind; |
| 9960 | void *buf1, *buf2; |
| 9961 | Py_ssize_t len1, len2; |
| 9962 | PyObject* out; |
| 9963 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9964 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9965 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9966 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9967 | if (PyUnicode_READY(self) == -1) |
| 9968 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9970 | if (substring == NULL) |
| 9971 | switch(PyUnicode_KIND(self)) { |
| 9972 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9973 | if (PyUnicode_IS_ASCII(self)) |
| 9974 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9975 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9976 | PyUnicode_GET_LENGTH(self), maxcount |
| 9977 | ); |
| 9978 | else |
| 9979 | return ucs1lib_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 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | case PyUnicode_2BYTE_KIND: |
| 9984 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9985 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | PyUnicode_GET_LENGTH(self), maxcount |
| 9987 | ); |
| 9988 | case PyUnicode_4BYTE_KIND: |
| 9989 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9990 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | PyUnicode_GET_LENGTH(self), maxcount |
| 9992 | ); |
| 9993 | default: |
| 9994 | assert(0); |
| 9995 | return NULL; |
| 9996 | } |
| 9997 | |
| 9998 | if (PyUnicode_READY(substring) == -1) |
| 9999 | return NULL; |
| 10000 | |
| 10001 | kind1 = PyUnicode_KIND(self); |
| 10002 | kind2 = PyUnicode_KIND(substring); |
| 10003 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10004 | buf1 = PyUnicode_DATA(self); |
| 10005 | buf2 = PyUnicode_DATA(substring); |
| 10006 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10007 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10008 | if (!buf1) |
| 10009 | return NULL; |
| 10010 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10011 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10012 | if (!buf2) { |
| 10013 | if (kind1 != kind) PyMem_Free(buf1); |
| 10014 | return NULL; |
| 10015 | } |
| 10016 | len1 = PyUnicode_GET_LENGTH(self); |
| 10017 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10018 | |
| 10019 | switch(kind) { |
| 10020 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10021 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10022 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10023 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10024 | else |
| 10025 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10026 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10027 | break; |
| 10028 | case PyUnicode_2BYTE_KIND: |
| 10029 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10030 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10031 | break; |
| 10032 | case PyUnicode_4BYTE_KIND: |
| 10033 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10034 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10035 | break; |
| 10036 | default: |
| 10037 | out = NULL; |
| 10038 | } |
| 10039 | if (kind1 != kind) |
| 10040 | PyMem_Free(buf1); |
| 10041 | if (kind2 != kind) |
| 10042 | PyMem_Free(buf2); |
| 10043 | return out; |
| 10044 | } |
| 10045 | |
| 10046 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10047 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10048 | 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] | 10049 | { |
| 10050 | switch(kind) { |
| 10051 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10052 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10053 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10054 | else |
| 10055 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10056 | case PyUnicode_2BYTE_KIND: |
| 10057 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10058 | case PyUnicode_4BYTE_KIND: |
| 10059 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10060 | } |
| 10061 | assert(0); |
| 10062 | return -1; |
| 10063 | } |
| 10064 | |
| 10065 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10066 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10067 | 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] | 10068 | { |
| 10069 | switch(kind) { |
| 10070 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10071 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10072 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10073 | else |
| 10074 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | case PyUnicode_2BYTE_KIND: |
| 10076 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10077 | case PyUnicode_4BYTE_KIND: |
| 10078 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10079 | } |
| 10080 | assert(0); |
| 10081 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10082 | } |
| 10083 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10084 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | replace(PyObject *self, PyObject *str1, |
| 10086 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10087 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | PyObject *u; |
| 10089 | char *sbuf = PyUnicode_DATA(self); |
| 10090 | char *buf1 = PyUnicode_DATA(str1); |
| 10091 | char *buf2 = PyUnicode_DATA(str2); |
| 10092 | int srelease = 0, release1 = 0, release2 = 0; |
| 10093 | int skind = PyUnicode_KIND(self); |
| 10094 | int kind1 = PyUnicode_KIND(str1); |
| 10095 | int kind2 = PyUnicode_KIND(str2); |
| 10096 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10097 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10098 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10099 | int mayshrink; |
| 10100 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10101 | |
| 10102 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10103 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10105 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10106 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10107 | if (str1 == str2) |
| 10108 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | if (skind < kind1) |
| 10110 | /* substring too wide to be present */ |
| 10111 | goto nothing; |
| 10112 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10113 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10114 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10115 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10116 | result string. */ |
| 10117 | mayshrink = (maxchar_str2 < maxchar); |
| 10118 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10120 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10121 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10122 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10124 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10126 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10127 | Py_UCS4 u1, u2; |
| 10128 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10129 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10130 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10131 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10132 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10133 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10135 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10137 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | rkind = PyUnicode_KIND(u); |
| 10139 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10140 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10141 | if (--maxcount < 0) |
| 10142 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10144 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10145 | } |
| 10146 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10147 | int rkind = skind; |
| 10148 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10149 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | if (kind1 < rkind) { |
| 10151 | /* widen substring */ |
| 10152 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10153 | if (!buf1) goto error; |
| 10154 | release1 = 1; |
| 10155 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10156 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10157 | if (i < 0) |
| 10158 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | if (rkind > kind2) { |
| 10160 | /* widen replacement */ |
| 10161 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10162 | if (!buf2) goto error; |
| 10163 | release2 = 1; |
| 10164 | } |
| 10165 | else if (rkind < kind2) { |
| 10166 | /* widen self and buf1 */ |
| 10167 | rkind = kind2; |
| 10168 | if (release1) PyMem_Free(buf1); |
| 10169 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10170 | if (!sbuf) goto error; |
| 10171 | srelease = 1; |
| 10172 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10173 | if (!buf1) goto error; |
| 10174 | release1 = 1; |
| 10175 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10176 | u = PyUnicode_New(slen, maxchar); |
| 10177 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10179 | assert(PyUnicode_KIND(u) == rkind); |
| 10180 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10181 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10182 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10183 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10184 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10185 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10186 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10187 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10188 | |
| 10189 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10190 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10191 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10192 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10193 | if (i == -1) |
| 10194 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10195 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10196 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10197 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10199 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10200 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10201 | } |
| 10202 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10203 | Py_ssize_t n, i, j, ires; |
| 10204 | Py_ssize_t product, new_size; |
| 10205 | int rkind = skind; |
| 10206 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10207 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10209 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10210 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10211 | if (!buf1) goto error; |
| 10212 | release1 = 1; |
| 10213 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10214 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10215 | if (n == 0) |
| 10216 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10217 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10218 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10219 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10220 | if (!buf2) goto error; |
| 10221 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10222 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10223 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10224 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | rkind = kind2; |
| 10226 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10227 | if (!sbuf) goto error; |
| 10228 | srelease = 1; |
| 10229 | if (release1) PyMem_Free(buf1); |
| 10230 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10231 | if (!buf1) goto error; |
| 10232 | release1 = 1; |
| 10233 | } |
| 10234 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10235 | PyUnicode_GET_LENGTH(str1))); */ |
| 10236 | product = n * (len2-len1); |
| 10237 | if ((product / (len2-len1)) != n) { |
| 10238 | PyErr_SetString(PyExc_OverflowError, |
| 10239 | "replace string is too long"); |
| 10240 | goto error; |
| 10241 | } |
| 10242 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10243 | if (new_size == 0) { |
| 10244 | Py_INCREF(unicode_empty); |
| 10245 | u = unicode_empty; |
| 10246 | goto done; |
| 10247 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10249 | PyErr_SetString(PyExc_OverflowError, |
| 10250 | "replace string is too long"); |
| 10251 | goto error; |
| 10252 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10253 | u = PyUnicode_New(new_size, maxchar); |
| 10254 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10256 | assert(PyUnicode_KIND(u) == rkind); |
| 10257 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | ires = i = 0; |
| 10259 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10260 | while (n-- > 0) { |
| 10261 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10262 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10263 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10264 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10265 | if (j == -1) |
| 10266 | break; |
| 10267 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10268 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10269 | memcpy(res + rkind * ires, |
| 10270 | sbuf + rkind * i, |
| 10271 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10272 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10273 | } |
| 10274 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10275 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10276 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10278 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10280 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10282 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10284 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10285 | memcpy(res + rkind * ires, |
| 10286 | sbuf + rkind * i, |
| 10287 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10288 | } |
| 10289 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10290 | /* interleave */ |
| 10291 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10292 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10294 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10296 | if (--n <= 0) |
| 10297 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10298 | memcpy(res + rkind * ires, |
| 10299 | sbuf + rkind * i, |
| 10300 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | ires++; |
| 10302 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10303 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10304 | memcpy(res + rkind * ires, |
| 10305 | sbuf + rkind * i, |
| 10306 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10307 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10308 | } |
| 10309 | |
| 10310 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10311 | unicode_adjust_maxchar(&u); |
| 10312 | if (u == NULL) |
| 10313 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10314 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10315 | |
| 10316 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | if (srelease) |
| 10318 | PyMem_FREE(sbuf); |
| 10319 | if (release1) |
| 10320 | PyMem_FREE(buf1); |
| 10321 | if (release2) |
| 10322 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10323 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10324 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10325 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10326 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10327 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10328 | if (srelease) |
| 10329 | PyMem_FREE(sbuf); |
| 10330 | if (release1) |
| 10331 | PyMem_FREE(buf1); |
| 10332 | if (release2) |
| 10333 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10334 | if (PyUnicode_CheckExact(self)) { |
| 10335 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10336 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10337 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10338 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | error: |
| 10340 | if (srelease && sbuf) |
| 10341 | PyMem_FREE(sbuf); |
| 10342 | if (release1 && buf1) |
| 10343 | PyMem_FREE(buf1); |
| 10344 | if (release2 && buf2) |
| 10345 | PyMem_FREE(buf2); |
| 10346 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10347 | } |
| 10348 | |
| 10349 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10351 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10352 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10353 | \n\ |
| 10354 | 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] | 10355 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10356 | |
| 10357 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10358 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10359 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10360 | return fixup(self, fixtitle); |
| 10361 | } |
| 10362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10363 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10364 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10365 | \n\ |
| 10366 | 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] | 10367 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10368 | |
| 10369 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10370 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10371 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10372 | return fixup(self, fixcapitalize); |
| 10373 | } |
| 10374 | |
| 10375 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10376 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10377 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10378 | \n\ |
| 10379 | 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] | 10380 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | |
| 10382 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10383 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | { |
| 10385 | PyObject *list; |
| 10386 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10387 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | /* Split into words */ |
| 10390 | list = split(self, NULL, -1); |
| 10391 | if (!list) |
| 10392 | return NULL; |
| 10393 | |
| 10394 | /* Capitalize each word */ |
| 10395 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10396 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10397 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10398 | if (item == NULL) |
| 10399 | goto onError; |
| 10400 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10401 | PyList_SET_ITEM(list, i, item); |
| 10402 | } |
| 10403 | |
| 10404 | /* Join the words to form a new string */ |
| 10405 | item = PyUnicode_Join(NULL, list); |
| 10406 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10407 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10408 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10409 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10410 | } |
| 10411 | #endif |
| 10412 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10413 | /* Argument converter. Coerces to a single unicode character */ |
| 10414 | |
| 10415 | static int |
| 10416 | convert_uc(PyObject *obj, void *addr) |
| 10417 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10419 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10420 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10421 | uniobj = PyUnicode_FromObject(obj); |
| 10422 | if (uniobj == NULL) { |
| 10423 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10424 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10425 | return 0; |
| 10426 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10427 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10428 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10429 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10430 | Py_DECREF(uniobj); |
| 10431 | return 0; |
| 10432 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10433 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10434 | Py_DECREF(uniobj); |
| 10435 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10436 | } |
| 10437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10438 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10439 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10440 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10441 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10442 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10443 | |
| 10444 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10445 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10446 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10447 | Py_ssize_t marg, left; |
| 10448 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | Py_UCS4 fillchar = ' '; |
| 10450 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10451 | 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] | 10452 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10453 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10454 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10455 | return NULL; |
| 10456 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10458 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10459 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10460 | } |
| 10461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10462 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10463 | left = marg / 2 + (marg & width & 1); |
| 10464 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10465 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10466 | } |
| 10467 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10468 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10469 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10470 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10471 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10472 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | int kind1, kind2; |
| 10474 | void *data1, *data2; |
| 10475 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10476 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10477 | kind1 = PyUnicode_KIND(str1); |
| 10478 | kind2 = PyUnicode_KIND(str2); |
| 10479 | data1 = PyUnicode_DATA(str1); |
| 10480 | data2 = PyUnicode_DATA(str2); |
| 10481 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10482 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10484 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10485 | Py_UCS4 c1, c2; |
| 10486 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10487 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10488 | |
| 10489 | if (c1 != c2) |
| 10490 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10491 | } |
| 10492 | |
| 10493 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10494 | } |
| 10495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10496 | int |
| 10497 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10499 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10500 | if (PyUnicode_READY(left) == -1 || |
| 10501 | PyUnicode_READY(right) == -1) |
| 10502 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10503 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10504 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10505 | PyErr_Format(PyExc_TypeError, |
| 10506 | "Can't compare %.100s and %.100s", |
| 10507 | left->ob_type->tp_name, |
| 10508 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10509 | return -1; |
| 10510 | } |
| 10511 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10512 | int |
| 10513 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10514 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10515 | Py_ssize_t i; |
| 10516 | int kind; |
| 10517 | void *data; |
| 10518 | Py_UCS4 chr; |
| 10519 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10520 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10521 | if (PyUnicode_READY(uni) == -1) |
| 10522 | return -1; |
| 10523 | kind = PyUnicode_KIND(uni); |
| 10524 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10525 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10526 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10527 | if (chr != str[i]) |
| 10528 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10529 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10530 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10531 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10532 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10533 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10534 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10535 | return 0; |
| 10536 | } |
| 10537 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10539 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10540 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10541 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10542 | PyObject * |
| 10543 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10544 | { |
| 10545 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10546 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10547 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10548 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | if (PyUnicode_READY(left) == -1 || |
| 10550 | PyUnicode_READY(right) == -1) |
| 10551 | return NULL; |
| 10552 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10553 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10554 | if (op == Py_EQ) { |
| 10555 | Py_INCREF(Py_False); |
| 10556 | return Py_False; |
| 10557 | } |
| 10558 | if (op == Py_NE) { |
| 10559 | Py_INCREF(Py_True); |
| 10560 | return Py_True; |
| 10561 | } |
| 10562 | } |
| 10563 | if (left == right) |
| 10564 | result = 0; |
| 10565 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10566 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10567 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10568 | /* Convert the return value to a Boolean */ |
| 10569 | switch (op) { |
| 10570 | case Py_EQ: |
| 10571 | v = TEST_COND(result == 0); |
| 10572 | break; |
| 10573 | case Py_NE: |
| 10574 | v = TEST_COND(result != 0); |
| 10575 | break; |
| 10576 | case Py_LE: |
| 10577 | v = TEST_COND(result <= 0); |
| 10578 | break; |
| 10579 | case Py_GE: |
| 10580 | v = TEST_COND(result >= 0); |
| 10581 | break; |
| 10582 | case Py_LT: |
| 10583 | v = TEST_COND(result == -1); |
| 10584 | break; |
| 10585 | case Py_GT: |
| 10586 | v = TEST_COND(result == 1); |
| 10587 | break; |
| 10588 | default: |
| 10589 | PyErr_BadArgument(); |
| 10590 | return NULL; |
| 10591 | } |
| 10592 | Py_INCREF(v); |
| 10593 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10594 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10595 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10596 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10597 | } |
| 10598 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10599 | int |
| 10600 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10601 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10602 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10603 | int kind1, kind2, kind; |
| 10604 | void *buf1, *buf2; |
| 10605 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10606 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10607 | |
| 10608 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10609 | sub = PyUnicode_FromObject(element); |
| 10610 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10611 | PyErr_Format(PyExc_TypeError, |
| 10612 | "'in <string>' requires string as left operand, not %s", |
| 10613 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10614 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10616 | if (PyUnicode_READY(sub) == -1) |
| 10617 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10618 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10619 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10620 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10621 | Py_DECREF(sub); |
| 10622 | return -1; |
| 10623 | } |
| 10624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10625 | kind1 = PyUnicode_KIND(str); |
| 10626 | kind2 = PyUnicode_KIND(sub); |
| 10627 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10628 | buf1 = PyUnicode_DATA(str); |
| 10629 | buf2 = PyUnicode_DATA(sub); |
| 10630 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10631 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10632 | if (!buf1) { |
| 10633 | Py_DECREF(sub); |
| 10634 | return -1; |
| 10635 | } |
| 10636 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10637 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10638 | if (!buf2) { |
| 10639 | Py_DECREF(sub); |
| 10640 | if (kind1 != kind) PyMem_Free(buf1); |
| 10641 | return -1; |
| 10642 | } |
| 10643 | len1 = PyUnicode_GET_LENGTH(str); |
| 10644 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10645 | |
| 10646 | switch(kind) { |
| 10647 | case PyUnicode_1BYTE_KIND: |
| 10648 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10649 | break; |
| 10650 | case PyUnicode_2BYTE_KIND: |
| 10651 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10652 | break; |
| 10653 | case PyUnicode_4BYTE_KIND: |
| 10654 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10655 | break; |
| 10656 | default: |
| 10657 | result = -1; |
| 10658 | assert(0); |
| 10659 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10660 | |
| 10661 | Py_DECREF(str); |
| 10662 | Py_DECREF(sub); |
| 10663 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | if (kind1 != kind) |
| 10665 | PyMem_Free(buf1); |
| 10666 | if (kind2 != kind) |
| 10667 | PyMem_Free(buf2); |
| 10668 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10669 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10670 | } |
| 10671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10672 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10673 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10674 | PyObject * |
| 10675 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10676 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10677 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10678 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10679 | |
| 10680 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10681 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10683 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10685 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10686 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10687 | |
| 10688 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10689 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10690 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10692 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10693 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10694 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10695 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | } |
| 10697 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10698 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10699 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10700 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10702 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10703 | w = PyUnicode_New( |
| 10704 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10705 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10707 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10708 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10709 | 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] | 10710 | Py_DECREF(u); |
| 10711 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10712 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10715 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | Py_XDECREF(u); |
| 10717 | Py_XDECREF(v); |
| 10718 | return NULL; |
| 10719 | } |
| 10720 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10721 | static void |
| 10722 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10723 | { |
| 10724 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10725 | |
| 10726 | assert(PyUnicode_IS_READY(*p_left)); |
| 10727 | assert(PyUnicode_IS_READY(right)); |
| 10728 | |
| 10729 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10730 | right_len = PyUnicode_GET_LENGTH(right); |
| 10731 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10732 | PyErr_SetString(PyExc_OverflowError, |
| 10733 | "strings are too large to concat"); |
| 10734 | goto error; |
| 10735 | } |
| 10736 | new_len = left_len + right_len; |
| 10737 | |
| 10738 | /* Now we own the last reference to 'left', so we can resize it |
| 10739 | * in-place. |
| 10740 | */ |
| 10741 | if (unicode_resize(p_left, new_len) != 0) { |
| 10742 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10743 | * deallocated so it cannot be put back into |
| 10744 | * 'variable'. The MemoryError is raised when there |
| 10745 | * is no value in 'variable', which might (very |
| 10746 | * remotely) be a cause of incompatibilities. |
| 10747 | */ |
| 10748 | goto error; |
| 10749 | } |
| 10750 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10751 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10752 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10753 | return; |
| 10754 | |
| 10755 | error: |
| 10756 | Py_DECREF(*p_left); |
| 10757 | *p_left = NULL; |
| 10758 | } |
| 10759 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10760 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10761 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10762 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10763 | PyObject *left, *res; |
| 10764 | |
| 10765 | if (p_left == NULL) { |
| 10766 | if (!PyErr_Occurred()) |
| 10767 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10768 | return; |
| 10769 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10770 | left = *p_left; |
| 10771 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10772 | if (!PyErr_Occurred()) |
| 10773 | PyErr_BadInternalCall(); |
| 10774 | goto error; |
| 10775 | } |
| 10776 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10777 | if (PyUnicode_READY(left)) |
| 10778 | goto error; |
| 10779 | if (PyUnicode_READY(right)) |
| 10780 | goto error; |
| 10781 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10782 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10783 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10784 | && unicode_resizable(left) |
| 10785 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10786 | || _PyUnicode_WSTR(left) != NULL)) |
| 10787 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10788 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10789 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10790 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10791 | not so different than duplicating the string. */ |
| 10792 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10793 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10794 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10795 | if (p_left != NULL) |
| 10796 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10797 | return; |
| 10798 | } |
| 10799 | } |
| 10800 | |
| 10801 | res = PyUnicode_Concat(left, right); |
| 10802 | if (res == NULL) |
| 10803 | goto error; |
| 10804 | Py_DECREF(left); |
| 10805 | *p_left = res; |
| 10806 | return; |
| 10807 | |
| 10808 | error: |
| 10809 | Py_DECREF(*p_left); |
| 10810 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10811 | } |
| 10812 | |
| 10813 | void |
| 10814 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10815 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10816 | PyUnicode_Append(pleft, right); |
| 10817 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10818 | } |
| 10819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10820 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10821 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10822 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10823 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10824 | 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] | 10825 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10826 | |
| 10827 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10828 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10829 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10830 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10831 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10832 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10833 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10834 | int kind1, kind2, kind; |
| 10835 | void *buf1, *buf2; |
| 10836 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10837 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10838 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10839 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10840 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10841 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10842 | kind1 = PyUnicode_KIND(self); |
| 10843 | kind2 = PyUnicode_KIND(substring); |
| 10844 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10845 | buf1 = PyUnicode_DATA(self); |
| 10846 | buf2 = PyUnicode_DATA(substring); |
| 10847 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10848 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10849 | if (!buf1) { |
| 10850 | Py_DECREF(substring); |
| 10851 | return NULL; |
| 10852 | } |
| 10853 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10854 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | if (!buf2) { |
| 10856 | Py_DECREF(substring); |
| 10857 | if (kind1 != kind) PyMem_Free(buf1); |
| 10858 | return NULL; |
| 10859 | } |
| 10860 | len1 = PyUnicode_GET_LENGTH(self); |
| 10861 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10862 | |
| 10863 | ADJUST_INDICES(start, end, len1); |
| 10864 | switch(kind) { |
| 10865 | case PyUnicode_1BYTE_KIND: |
| 10866 | iresult = ucs1lib_count( |
| 10867 | ((Py_UCS1*)buf1) + start, end - start, |
| 10868 | buf2, len2, PY_SSIZE_T_MAX |
| 10869 | ); |
| 10870 | break; |
| 10871 | case PyUnicode_2BYTE_KIND: |
| 10872 | iresult = ucs2lib_count( |
| 10873 | ((Py_UCS2*)buf1) + start, end - start, |
| 10874 | buf2, len2, PY_SSIZE_T_MAX |
| 10875 | ); |
| 10876 | break; |
| 10877 | case PyUnicode_4BYTE_KIND: |
| 10878 | iresult = ucs4lib_count( |
| 10879 | ((Py_UCS4*)buf1) + start, end - start, |
| 10880 | buf2, len2, PY_SSIZE_T_MAX |
| 10881 | ); |
| 10882 | break; |
| 10883 | default: |
| 10884 | assert(0); iresult = 0; |
| 10885 | } |
| 10886 | |
| 10887 | result = PyLong_FromSsize_t(iresult); |
| 10888 | |
| 10889 | if (kind1 != kind) |
| 10890 | PyMem_Free(buf1); |
| 10891 | if (kind2 != kind) |
| 10892 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10893 | |
| 10894 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10895 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10896 | return result; |
| 10897 | } |
| 10898 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10899 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10900 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10901 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10902 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10903 | 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] | 10904 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10905 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10906 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10907 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10908 | |
| 10909 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10910 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10911 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10912 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10913 | char *encoding = NULL; |
| 10914 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10915 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10916 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10917 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10918 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10919 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10920 | } |
| 10921 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10922 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10923 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | \n\ |
| 10925 | 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] | 10926 | 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] | 10927 | |
| 10928 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10929 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10930 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10931 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10932 | Py_UCS4 ch; |
| 10933 | PyObject *u; |
| 10934 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10936 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10937 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | |
| 10939 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10940 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10941 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10942 | if (PyUnicode_READY(self) == -1) |
| 10943 | return NULL; |
| 10944 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10945 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10946 | src_len = PyUnicode_GET_LENGTH(self); |
| 10947 | i = j = line_pos = 0; |
| 10948 | kind = PyUnicode_KIND(self); |
| 10949 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10950 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10951 | for (; i < src_len; i++) { |
| 10952 | ch = PyUnicode_READ(kind, src_data, i); |
| 10953 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10954 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10955 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10956 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10957 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10958 | goto overflow; |
| 10959 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10960 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10961 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10962 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10963 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10964 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10965 | goto overflow; |
| 10966 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10967 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10968 | if (ch == '\n' || ch == '\r') |
| 10969 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10970 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10971 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10972 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10973 | Py_INCREF(self); |
| 10974 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10975 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10978 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10979 | if (!u) |
| 10980 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10981 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10983 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10984 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10985 | for (; i < src_len; i++) { |
| 10986 | ch = PyUnicode_READ(kind, src_data, i); |
| 10987 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10988 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10989 | incr = tabsize - (line_pos % tabsize); |
| 10990 | line_pos += incr; |
| 10991 | while (incr--) { |
| 10992 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10993 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10994 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10995 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10996 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10997 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10998 | line_pos++; |
| 10999 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11000 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11001 | if (ch == '\n' || ch == '\r') |
| 11002 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11004 | } |
| 11005 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11006 | #ifndef DONT_MAKE_RESULT_READY |
| 11007 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11008 | Py_DECREF(u); |
| 11009 | return NULL; |
| 11010 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11011 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11012 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11013 | return u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11014 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11015 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11016 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11017 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11018 | } |
| 11019 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11020 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11021 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11022 | \n\ |
| 11023 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11024 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11025 | arguments start and end are interpreted as in slice notation.\n\ |
| 11026 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11027 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | |
| 11029 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11030 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11032 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11033 | Py_ssize_t start; |
| 11034 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11035 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11036 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11037 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11038 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11040 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11041 | if (PyUnicode_READY(self) == -1) |
| 11042 | return NULL; |
| 11043 | if (PyUnicode_READY(substring) == -1) |
| 11044 | return NULL; |
| 11045 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11046 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11047 | |
| 11048 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11049 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11050 | if (result == -2) |
| 11051 | return NULL; |
| 11052 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11053 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11054 | } |
| 11055 | |
| 11056 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11057 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11059 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11060 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11061 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | } |
| 11064 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11065 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11066 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11067 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11068 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11070 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11071 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 | if (_PyUnicode_HASH(self) != -1) |
| 11074 | return _PyUnicode_HASH(self); |
| 11075 | if (PyUnicode_READY(self) == -1) |
| 11076 | return -1; |
| 11077 | len = PyUnicode_GET_LENGTH(self); |
| 11078 | |
| 11079 | /* The hash function as a macro, gets expanded three times below. */ |
| 11080 | #define HASH(P) \ |
| 11081 | x = (Py_uhash_t)*P << 7; \ |
| 11082 | while (--len >= 0) \ |
| 11083 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11084 | |
| 11085 | switch (PyUnicode_KIND(self)) { |
| 11086 | case PyUnicode_1BYTE_KIND: { |
| 11087 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11088 | HASH(c); |
| 11089 | break; |
| 11090 | } |
| 11091 | case PyUnicode_2BYTE_KIND: { |
| 11092 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11093 | HASH(s); |
| 11094 | break; |
| 11095 | } |
| 11096 | default: { |
| 11097 | Py_UCS4 *l; |
| 11098 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11099 | "Impossible switch case in unicode_hash"); |
| 11100 | l = PyUnicode_4BYTE_DATA(self); |
| 11101 | HASH(l); |
| 11102 | break; |
| 11103 | } |
| 11104 | } |
| 11105 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11106 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11107 | if (x == -1) |
| 11108 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11109 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11110 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11111 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11112 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11114 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11115 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11117 | 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] | 11118 | |
| 11119 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11120 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11121 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11122 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11123 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11124 | Py_ssize_t start; |
| 11125 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11126 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11127 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11128 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11129 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11130 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11131 | if (PyUnicode_READY(self) == -1) |
| 11132 | return NULL; |
| 11133 | if (PyUnicode_READY(substring) == -1) |
| 11134 | return NULL; |
| 11135 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11136 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11137 | |
| 11138 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11139 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11140 | if (result == -2) |
| 11141 | return NULL; |
| 11142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11143 | if (result < 0) { |
| 11144 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11145 | return NULL; |
| 11146 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11147 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11148 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11149 | } |
| 11150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11151 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11152 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11153 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11154 | 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] | 11155 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | |
| 11157 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11158 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11159 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11160 | Py_ssize_t i, length; |
| 11161 | int kind; |
| 11162 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | int cased; |
| 11164 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 | if (PyUnicode_READY(self) == -1) |
| 11166 | return NULL; |
| 11167 | length = PyUnicode_GET_LENGTH(self); |
| 11168 | kind = PyUnicode_KIND(self); |
| 11169 | data = PyUnicode_DATA(self); |
| 11170 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11171 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11172 | if (length == 1) |
| 11173 | return PyBool_FromLong( |
| 11174 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11176 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11178 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11181 | for (i = 0; i < length; i++) { |
| 11182 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11183 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11184 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11185 | return PyBool_FromLong(0); |
| 11186 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11187 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11189 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | } |
| 11191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11192 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11193 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11194 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11195 | 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] | 11196 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11197 | |
| 11198 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11199 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11200 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | Py_ssize_t i, length; |
| 11202 | int kind; |
| 11203 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 | int cased; |
| 11205 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11206 | if (PyUnicode_READY(self) == -1) |
| 11207 | return NULL; |
| 11208 | length = PyUnicode_GET_LENGTH(self); |
| 11209 | kind = PyUnicode_KIND(self); |
| 11210 | data = PyUnicode_DATA(self); |
| 11211 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11213 | if (length == 1) |
| 11214 | return PyBool_FromLong( |
| 11215 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11217 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11218 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11219 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11222 | for (i = 0; i < length; i++) { |
| 11223 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11225 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11226 | return PyBool_FromLong(0); |
| 11227 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11228 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11229 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11230 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | } |
| 11232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11233 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11234 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11236 | Return True if S is a titlecased string and there is at least one\n\ |
| 11237 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11238 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11239 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | |
| 11241 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11242 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11244 | Py_ssize_t i, length; |
| 11245 | int kind; |
| 11246 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | int cased, previous_is_cased; |
| 11248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11249 | if (PyUnicode_READY(self) == -1) |
| 11250 | return NULL; |
| 11251 | length = PyUnicode_GET_LENGTH(self); |
| 11252 | kind = PyUnicode_KIND(self); |
| 11253 | data = PyUnicode_DATA(self); |
| 11254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | if (length == 1) { |
| 11257 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11258 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11259 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11260 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11262 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11263 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11264 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | cased = 0; |
| 11267 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | for (i = 0; i < length; i++) { |
| 11269 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11270 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11271 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11272 | if (previous_is_cased) |
| 11273 | return PyBool_FromLong(0); |
| 11274 | previous_is_cased = 1; |
| 11275 | cased = 1; |
| 11276 | } |
| 11277 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11278 | if (!previous_is_cased) |
| 11279 | return PyBool_FromLong(0); |
| 11280 | previous_is_cased = 1; |
| 11281 | cased = 1; |
| 11282 | } |
| 11283 | else |
| 11284 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11286 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | } |
| 11288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11289 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11290 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11292 | Return True if all characters in S are whitespace\n\ |
| 11293 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | |
| 11295 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11296 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11297 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11298 | Py_ssize_t i, length; |
| 11299 | int kind; |
| 11300 | void *data; |
| 11301 | |
| 11302 | if (PyUnicode_READY(self) == -1) |
| 11303 | return NULL; |
| 11304 | length = PyUnicode_GET_LENGTH(self); |
| 11305 | kind = PyUnicode_KIND(self); |
| 11306 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11308 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11309 | if (length == 1) |
| 11310 | return PyBool_FromLong( |
| 11311 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11312 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11313 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11315 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 | for (i = 0; i < length; i++) { |
| 11318 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11319 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11320 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11322 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | } |
| 11324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11325 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11326 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11327 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11328 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11329 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11330 | |
| 11331 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11332 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11333 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11334 | Py_ssize_t i, length; |
| 11335 | int kind; |
| 11336 | void *data; |
| 11337 | |
| 11338 | if (PyUnicode_READY(self) == -1) |
| 11339 | return NULL; |
| 11340 | length = PyUnicode_GET_LENGTH(self); |
| 11341 | kind = PyUnicode_KIND(self); |
| 11342 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11343 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11344 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | if (length == 1) |
| 11346 | return PyBool_FromLong( |
| 11347 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11348 | |
| 11349 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11350 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11351 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11353 | for (i = 0; i < length; i++) { |
| 11354 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11355 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11356 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11357 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11358 | } |
| 11359 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11360 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11361 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11362 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11363 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11364 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11365 | |
| 11366 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11367 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11368 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11369 | int kind; |
| 11370 | void *data; |
| 11371 | Py_ssize_t len, i; |
| 11372 | |
| 11373 | if (PyUnicode_READY(self) == -1) |
| 11374 | return NULL; |
| 11375 | |
| 11376 | kind = PyUnicode_KIND(self); |
| 11377 | data = PyUnicode_DATA(self); |
| 11378 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11379 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11380 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11381 | if (len == 1) { |
| 11382 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11383 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11384 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11385 | |
| 11386 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11387 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11388 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | for (i = 0; i < len; i++) { |
| 11391 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11392 | if (!Py_UNICODE_ISALNUM(ch)) |
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 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11395 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11396 | } |
| 11397 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11398 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11399 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11400 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11401 | 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] | 11402 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11403 | |
| 11404 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11405 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11407 | Py_ssize_t i, length; |
| 11408 | int kind; |
| 11409 | void *data; |
| 11410 | |
| 11411 | if (PyUnicode_READY(self) == -1) |
| 11412 | return NULL; |
| 11413 | length = PyUnicode_GET_LENGTH(self); |
| 11414 | kind = PyUnicode_KIND(self); |
| 11415 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11417 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11418 | if (length == 1) |
| 11419 | return PyBool_FromLong( |
| 11420 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11422 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11423 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11425 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11426 | for (i = 0; i < length; i++) { |
| 11427 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11428 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11430 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | } |
| 11432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11433 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11434 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11436 | Return True if all characters in S are digits\n\ |
| 11437 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | |
| 11439 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11440 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11442 | Py_ssize_t i, length; |
| 11443 | int kind; |
| 11444 | void *data; |
| 11445 | |
| 11446 | if (PyUnicode_READY(self) == -1) |
| 11447 | return NULL; |
| 11448 | length = PyUnicode_GET_LENGTH(self); |
| 11449 | kind = PyUnicode_KIND(self); |
| 11450 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | if (length == 1) { |
| 11454 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11455 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11456 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11458 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11459 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11460 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11462 | for (i = 0; i < length; i++) { |
| 11463 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11464 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11466 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | } |
| 11468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11469 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11472 | 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] | 11473 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11474 | |
| 11475 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11476 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11478 | Py_ssize_t i, length; |
| 11479 | int kind; |
| 11480 | void *data; |
| 11481 | |
| 11482 | if (PyUnicode_READY(self) == -1) |
| 11483 | return NULL; |
| 11484 | length = PyUnicode_GET_LENGTH(self); |
| 11485 | kind = PyUnicode_KIND(self); |
| 11486 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11488 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 | if (length == 1) |
| 11490 | return PyBool_FromLong( |
| 11491 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11493 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11494 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11495 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11496 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11497 | for (i = 0; i < length; i++) { |
| 11498 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11501 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | } |
| 11503 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11504 | int |
| 11505 | PyUnicode_IsIdentifier(PyObject *self) |
| 11506 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | int kind; |
| 11508 | void *data; |
| 11509 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11510 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11512 | if (PyUnicode_READY(self) == -1) { |
| 11513 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11514 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11515 | } |
| 11516 | |
| 11517 | /* Special case for empty strings */ |
| 11518 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11519 | return 0; |
| 11520 | kind = PyUnicode_KIND(self); |
| 11521 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11522 | |
| 11523 | /* PEP 3131 says that the first character must be in |
| 11524 | XID_Start and subsequent characters in XID_Continue, |
| 11525 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11526 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11527 | letters, digits, underscore). However, given the current |
| 11528 | definition of XID_Start and XID_Continue, it is sufficient |
| 11529 | to check just for these, except that _ must be allowed |
| 11530 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11531 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11532 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11533 | return 0; |
| 11534 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11535 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11537 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11538 | return 1; |
| 11539 | } |
| 11540 | |
| 11541 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11542 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11543 | \n\ |
| 11544 | Return True if S is a valid identifier according\n\ |
| 11545 | to the language definition."); |
| 11546 | |
| 11547 | static PyObject* |
| 11548 | unicode_isidentifier(PyObject *self) |
| 11549 | { |
| 11550 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11551 | } |
| 11552 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11553 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11554 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11555 | \n\ |
| 11556 | Return True if all characters in S are considered\n\ |
| 11557 | printable in repr() or S is empty, False otherwise."); |
| 11558 | |
| 11559 | static PyObject* |
| 11560 | unicode_isprintable(PyObject *self) |
| 11561 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | Py_ssize_t i, length; |
| 11563 | int kind; |
| 11564 | void *data; |
| 11565 | |
| 11566 | if (PyUnicode_READY(self) == -1) |
| 11567 | return NULL; |
| 11568 | length = PyUnicode_GET_LENGTH(self); |
| 11569 | kind = PyUnicode_KIND(self); |
| 11570 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11571 | |
| 11572 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11573 | if (length == 1) |
| 11574 | return PyBool_FromLong( |
| 11575 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11576 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11577 | for (i = 0; i < length; i++) { |
| 11578 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11579 | Py_RETURN_FALSE; |
| 11580 | } |
| 11581 | } |
| 11582 | Py_RETURN_TRUE; |
| 11583 | } |
| 11584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11585 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11586 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11587 | \n\ |
| 11588 | 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] | 11589 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11590 | |
| 11591 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11592 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11594 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | } |
| 11596 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11597 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11598 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11599 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11600 | if (PyUnicode_READY(self) == -1) |
| 11601 | return -1; |
| 11602 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11603 | } |
| 11604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11605 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11606 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11607 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11608 | 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] | 11609 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11610 | |
| 11611 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11612 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11614 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11615 | Py_UCS4 fillchar = ' '; |
| 11616 | |
| 11617 | if (PyUnicode_READY(self) == -1) |
| 11618 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11619 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11620 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11621 | return NULL; |
| 11622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11623 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11624 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11625 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11626 | } |
| 11627 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11628 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | } |
| 11630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11631 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11632 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11633 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11634 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11635 | |
| 11636 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11637 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11638 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 | return fixup(self, fixlower); |
| 11640 | } |
| 11641 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11642 | #define LEFTSTRIP 0 |
| 11643 | #define RIGHTSTRIP 1 |
| 11644 | #define BOTHSTRIP 2 |
| 11645 | |
| 11646 | /* Arrays indexed by above */ |
| 11647 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11648 | |
| 11649 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11650 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11651 | /* externally visible for str.strip(unicode) */ |
| 11652 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11653 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11654 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | void *data; |
| 11656 | int kind; |
| 11657 | Py_ssize_t i, j, len; |
| 11658 | BLOOM_MASK sepmask; |
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 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11661 | return NULL; |
| 11662 | |
| 11663 | kind = PyUnicode_KIND(self); |
| 11664 | data = PyUnicode_DATA(self); |
| 11665 | len = PyUnicode_GET_LENGTH(self); |
| 11666 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11667 | PyUnicode_DATA(sepobj), |
| 11668 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11669 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11670 | i = 0; |
| 11671 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11672 | while (i < len && |
| 11673 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11674 | i++; |
| 11675 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11676 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11677 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11678 | j = len; |
| 11679 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11680 | do { |
| 11681 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | } while (j >= i && |
| 11683 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11684 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11685 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11686 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11687 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11688 | } |
| 11689 | |
| 11690 | PyObject* |
| 11691 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11692 | { |
| 11693 | unsigned char *data; |
| 11694 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11695 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11697 | if (PyUnicode_READY(self) == -1) |
| 11698 | return NULL; |
| 11699 | |
| 11700 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11701 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11702 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11703 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11704 | if (PyUnicode_CheckExact(self)) { |
| 11705 | Py_INCREF(self); |
| 11706 | return self; |
| 11707 | } |
| 11708 | else |
| 11709 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11710 | } |
| 11711 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11712 | length = end - start; |
| 11713 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11714 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11715 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11716 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11717 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11718 | return NULL; |
| 11719 | } |
| 11720 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11721 | if (PyUnicode_IS_ASCII(self)) { |
| 11722 | kind = PyUnicode_KIND(self); |
| 11723 | data = PyUnicode_1BYTE_DATA(self); |
| 11724 | return unicode_fromascii(data + start, length); |
| 11725 | } |
| 11726 | else { |
| 11727 | kind = PyUnicode_KIND(self); |
| 11728 | data = PyUnicode_1BYTE_DATA(self); |
| 11729 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11730 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11731 | length); |
| 11732 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11733 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | |
| 11735 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11736 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11738 | int kind; |
| 11739 | void *data; |
| 11740 | Py_ssize_t len, i, j; |
| 11741 | |
| 11742 | if (PyUnicode_READY(self) == -1) |
| 11743 | return NULL; |
| 11744 | |
| 11745 | kind = PyUnicode_KIND(self); |
| 11746 | data = PyUnicode_DATA(self); |
| 11747 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11748 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11749 | i = 0; |
| 11750 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11751 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11752 | i++; |
| 11753 | } |
| 11754 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11755 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11756 | j = len; |
| 11757 | if (striptype != LEFTSTRIP) { |
| 11758 | do { |
| 11759 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11760 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11761 | j++; |
| 11762 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11763 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11764 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | } |
| 11766 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11767 | |
| 11768 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11769 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11770 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11771 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11772 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11773 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11774 | return NULL; |
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 | if (sep != NULL && sep != Py_None) { |
| 11777 | if (PyUnicode_Check(sep)) |
| 11778 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11779 | else { |
| 11780 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11781 | "%s arg must be None or str", |
| 11782 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11783 | return NULL; |
| 11784 | } |
| 11785 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11786 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11787 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11788 | } |
| 11789 | |
| 11790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11791 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11792 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11793 | \n\ |
| 11794 | Return a copy of the string S with leading and trailing\n\ |
| 11795 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11796 | 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] | 11797 | |
| 11798 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11799 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11800 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11801 | if (PyTuple_GET_SIZE(args) == 0) |
| 11802 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11803 | else |
| 11804 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11805 | } |
| 11806 | |
| 11807 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11808 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11809 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11810 | \n\ |
| 11811 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11812 | 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] | 11813 | |
| 11814 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11815 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11816 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11817 | if (PyTuple_GET_SIZE(args) == 0) |
| 11818 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11819 | else |
| 11820 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11821 | } |
| 11822 | |
| 11823 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11824 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11825 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11826 | \n\ |
| 11827 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11828 | 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] | 11829 | |
| 11830 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11831 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11832 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11833 | if (PyTuple_GET_SIZE(args) == 0) |
| 11834 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11835 | else |
| 11836 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
| 11839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11840 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11841 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11843 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11844 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11846 | if (len < 1) { |
| 11847 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11848 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11849 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11850 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11851 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11852 | /* no repeat, return original string */ |
| 11853 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11854 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11855 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11856 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11857 | if (PyUnicode_READY(str) == -1) |
| 11858 | return NULL; |
| 11859 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11860 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11861 | PyErr_SetString(PyExc_OverflowError, |
| 11862 | "repeated string is too long"); |
| 11863 | return NULL; |
| 11864 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11865 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11866 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11867 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | if (!u) |
| 11869 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11870 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11872 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11873 | const int kind = PyUnicode_KIND(str); |
| 11874 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11875 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11876 | if (kind == PyUnicode_1BYTE_KIND) |
| 11877 | memset(to, (unsigned char)fill_char, len); |
| 11878 | else { |
| 11879 | for (n = 0; n < len; ++n) |
| 11880 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11881 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | } |
| 11883 | else { |
| 11884 | /* number of characters copied this far */ |
| 11885 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11886 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11887 | char *to = (char *) PyUnicode_DATA(u); |
| 11888 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11889 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11890 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11891 | n = (done <= nchars-done) ? done : nchars-done; |
| 11892 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11893 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11894 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11895 | } |
| 11896 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11897 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11898 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11899 | } |
| 11900 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11901 | PyObject * |
| 11902 | PyUnicode_Replace(PyObject *obj, |
| 11903 | PyObject *subobj, |
| 11904 | PyObject *replobj, |
| 11905 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11906 | { |
| 11907 | PyObject *self; |
| 11908 | PyObject *str1; |
| 11909 | PyObject *str2; |
| 11910 | PyObject *result; |
| 11911 | |
| 11912 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11913 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11914 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11915 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11916 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11917 | Py_DECREF(self); |
| 11918 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | } |
| 11920 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11921 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11922 | Py_DECREF(self); |
| 11923 | Py_DECREF(str1); |
| 11924 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11925 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11926 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | Py_DECREF(self); |
| 11928 | Py_DECREF(str1); |
| 11929 | Py_DECREF(str2); |
| 11930 | return result; |
| 11931 | } |
| 11932 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11933 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11934 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11935 | \n\ |
| 11936 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11937 | old replaced by new. If the optional argument count is\n\ |
| 11938 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11939 | |
| 11940 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11943 | PyObject *str1; |
| 11944 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11945 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11946 | PyObject *result; |
| 11947 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11948 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11950 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11951 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11952 | str1 = PyUnicode_FromObject(str1); |
| 11953 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11954 | return NULL; |
| 11955 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11956 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11957 | Py_DECREF(str1); |
| 11958 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11959 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | |
| 11961 | result = replace(self, str1, str2, maxcount); |
| 11962 | |
| 11963 | Py_DECREF(str1); |
| 11964 | Py_DECREF(str2); |
| 11965 | return result; |
| 11966 | } |
| 11967 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11968 | static PyObject * |
| 11969 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11970 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11971 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11972 | Py_ssize_t isize; |
| 11973 | Py_ssize_t osize, squote, dquote, i, o; |
| 11974 | Py_UCS4 max, quote; |
| 11975 | int ikind, okind; |
| 11976 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11978 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11979 | return NULL; |
| 11980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11981 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11982 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | /* Compute length of output, quote characters, and |
| 11985 | maximum character */ |
| 11986 | osize = 2; /* quotes */ |
| 11987 | max = 127; |
| 11988 | squote = dquote = 0; |
| 11989 | ikind = PyUnicode_KIND(unicode); |
| 11990 | for (i = 0; i < isize; i++) { |
| 11991 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11992 | switch (ch) { |
| 11993 | case '\'': squote++; osize++; break; |
| 11994 | case '"': dquote++; osize++; break; |
| 11995 | case '\\': case '\t': case '\r': case '\n': |
| 11996 | osize += 2; break; |
| 11997 | default: |
| 11998 | /* Fast-path ASCII */ |
| 11999 | if (ch < ' ' || ch == 0x7f) |
| 12000 | osize += 4; /* \xHH */ |
| 12001 | else if (ch < 0x7f) |
| 12002 | osize++; |
| 12003 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12004 | osize++; |
| 12005 | max = ch > max ? ch : max; |
| 12006 | } |
| 12007 | else if (ch < 0x100) |
| 12008 | osize += 4; /* \xHH */ |
| 12009 | else if (ch < 0x10000) |
| 12010 | osize += 6; /* \uHHHH */ |
| 12011 | else |
| 12012 | osize += 10; /* \uHHHHHHHH */ |
| 12013 | } |
| 12014 | } |
| 12015 | |
| 12016 | quote = '\''; |
| 12017 | if (squote) { |
| 12018 | if (dquote) |
| 12019 | /* Both squote and dquote present. Use squote, |
| 12020 | and escape them */ |
| 12021 | osize += squote; |
| 12022 | else |
| 12023 | quote = '"'; |
| 12024 | } |
| 12025 | |
| 12026 | repr = PyUnicode_New(osize, max); |
| 12027 | if (repr == NULL) |
| 12028 | return NULL; |
| 12029 | okind = PyUnicode_KIND(repr); |
| 12030 | odata = PyUnicode_DATA(repr); |
| 12031 | |
| 12032 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12033 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12034 | |
| 12035 | for (i = 0, o = 1; i < isize; i++) { |
| 12036 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12037 | |
| 12038 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12039 | if ((ch == quote) || (ch == '\\')) { |
| 12040 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12041 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12042 | continue; |
| 12043 | } |
| 12044 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12045 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12046 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12047 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12048 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12049 | } |
| 12050 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12052 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12053 | } |
| 12054 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12055 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12056 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12057 | } |
| 12058 | |
| 12059 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12060 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12061 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12062 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12063 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12064 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12065 | } |
| 12066 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12067 | /* Copy ASCII characters as-is */ |
| 12068 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12069 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12070 | } |
| 12071 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12072 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12073 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12074 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12075 | (categories Z* and C* except ASCII space) |
| 12076 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12077 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12078 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12079 | if (ch <= 0xff) { |
| 12080 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12081 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12082 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12083 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12084 | } |
| 12085 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12086 | else if (ch >= 0x10000) { |
| 12087 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12088 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12089 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12090 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12091 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12092 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12093 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12094 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12095 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12096 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12097 | } |
| 12098 | /* Map 16-bit characters to '\uxxxx' */ |
| 12099 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12100 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12101 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12102 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12103 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12104 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12105 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12106 | } |
| 12107 | } |
| 12108 | /* Copy characters as-is */ |
| 12109 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12110 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12111 | } |
| 12112 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12113 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12114 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12115 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12116 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12117 | } |
| 12118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12119 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12120 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | \n\ |
| 12122 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12123 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12124 | arguments start and end are interpreted as in slice notation.\n\ |
| 12125 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12126 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12127 | |
| 12128 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12129 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12130 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12131 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12132 | Py_ssize_t start; |
| 12133 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12134 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12135 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12136 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12137 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12138 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12140 | if (PyUnicode_READY(self) == -1) |
| 12141 | return NULL; |
| 12142 | if (PyUnicode_READY(substring) == -1) |
| 12143 | return NULL; |
| 12144 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12145 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12146 | |
| 12147 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12148 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12149 | if (result == -2) |
| 12150 | return NULL; |
| 12151 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12152 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12153 | } |
| 12154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12155 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12156 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12157 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12158 | 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] | 12159 | |
| 12160 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12161 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12162 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12163 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12164 | Py_ssize_t start; |
| 12165 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12166 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12167 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12168 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12169 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12170 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12172 | if (PyUnicode_READY(self) == -1) |
| 12173 | return NULL; |
| 12174 | if (PyUnicode_READY(substring) == -1) |
| 12175 | return NULL; |
| 12176 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12177 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | |
| 12179 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12180 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | if (result == -2) |
| 12182 | return NULL; |
| 12183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12184 | if (result < 0) { |
| 12185 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12186 | return NULL; |
| 12187 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12188 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12189 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12190 | } |
| 12191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12192 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12193 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12194 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12195 | 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] | 12196 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12197 | |
| 12198 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12199 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12200 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12201 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12202 | Py_UCS4 fillchar = ' '; |
| 12203 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12204 | 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] | 12205 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12206 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12207 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | return NULL; |
| 12209 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12210 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12211 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12212 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12213 | } |
| 12214 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12215 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | } |
| 12217 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12218 | PyObject * |
| 12219 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | { |
| 12221 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12223 | s = PyUnicode_FromObject(s); |
| 12224 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12225 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12226 | if (sep != NULL) { |
| 12227 | sep = PyUnicode_FromObject(sep); |
| 12228 | if (sep == NULL) { |
| 12229 | Py_DECREF(s); |
| 12230 | return NULL; |
| 12231 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | } |
| 12233 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12234 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | |
| 12236 | Py_DECREF(s); |
| 12237 | Py_XDECREF(sep); |
| 12238 | return result; |
| 12239 | } |
| 12240 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12241 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12242 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12243 | \n\ |
| 12244 | Return a list of the words in S, using sep as the\n\ |
| 12245 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12246 | 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] | 12247 | whitespace string is a separator and empty strings are\n\ |
| 12248 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 | |
| 12250 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12251 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12252 | { |
| 12253 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12254 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12256 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | return NULL; |
| 12258 | |
| 12259 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12260 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12262 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12264 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12265 | } |
| 12266 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12267 | PyObject * |
| 12268 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12269 | { |
| 12270 | PyObject* str_obj; |
| 12271 | PyObject* sep_obj; |
| 12272 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12273 | int kind1, kind2, kind; |
| 12274 | void *buf1 = NULL, *buf2 = NULL; |
| 12275 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12276 | |
| 12277 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12278 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12279 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12280 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12281 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12282 | Py_DECREF(str_obj); |
| 12283 | return NULL; |
| 12284 | } |
| 12285 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12286 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12287 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12288 | kind = Py_MAX(kind1, kind2); |
| 12289 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12290 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12291 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12292 | if (!buf1) |
| 12293 | goto onError; |
| 12294 | buf2 = PyUnicode_DATA(sep_obj); |
| 12295 | if (kind2 != kind) |
| 12296 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12297 | if (!buf2) |
| 12298 | goto onError; |
| 12299 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12300 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12301 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12302 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12304 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12305 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12306 | else |
| 12307 | 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] | 12308 | break; |
| 12309 | case PyUnicode_2BYTE_KIND: |
| 12310 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12311 | break; |
| 12312 | case PyUnicode_4BYTE_KIND: |
| 12313 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12314 | break; |
| 12315 | default: |
| 12316 | assert(0); |
| 12317 | out = 0; |
| 12318 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12319 | |
| 12320 | Py_DECREF(sep_obj); |
| 12321 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12322 | if (kind1 != kind) |
| 12323 | PyMem_Free(buf1); |
| 12324 | if (kind2 != kind) |
| 12325 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12326 | |
| 12327 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | onError: |
| 12329 | Py_DECREF(sep_obj); |
| 12330 | Py_DECREF(str_obj); |
| 12331 | if (kind1 != kind && buf1) |
| 12332 | PyMem_Free(buf1); |
| 12333 | if (kind2 != kind && buf2) |
| 12334 | PyMem_Free(buf2); |
| 12335 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12336 | } |
| 12337 | |
| 12338 | |
| 12339 | PyObject * |
| 12340 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12341 | { |
| 12342 | PyObject* str_obj; |
| 12343 | PyObject* sep_obj; |
| 12344 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12345 | int kind1, kind2, kind; |
| 12346 | void *buf1 = NULL, *buf2 = NULL; |
| 12347 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12348 | |
| 12349 | str_obj = PyUnicode_FromObject(str_in); |
| 12350 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12351 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12352 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12353 | if (!sep_obj) { |
| 12354 | Py_DECREF(str_obj); |
| 12355 | return NULL; |
| 12356 | } |
| 12357 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12358 | kind1 = PyUnicode_KIND(str_in); |
| 12359 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12360 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12361 | buf1 = PyUnicode_DATA(str_in); |
| 12362 | if (kind1 != kind) |
| 12363 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12364 | if (!buf1) |
| 12365 | goto onError; |
| 12366 | buf2 = PyUnicode_DATA(sep_obj); |
| 12367 | if (kind2 != kind) |
| 12368 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12369 | if (!buf2) |
| 12370 | goto onError; |
| 12371 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12372 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12373 | |
| 12374 | switch(PyUnicode_KIND(str_in)) { |
| 12375 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12376 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12377 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12378 | else |
| 12379 | 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] | 12380 | break; |
| 12381 | case PyUnicode_2BYTE_KIND: |
| 12382 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12383 | break; |
| 12384 | case PyUnicode_4BYTE_KIND: |
| 12385 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12386 | break; |
| 12387 | default: |
| 12388 | assert(0); |
| 12389 | out = 0; |
| 12390 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12391 | |
| 12392 | Py_DECREF(sep_obj); |
| 12393 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12394 | if (kind1 != kind) |
| 12395 | PyMem_Free(buf1); |
| 12396 | if (kind2 != kind) |
| 12397 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12398 | |
| 12399 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | onError: |
| 12401 | Py_DECREF(sep_obj); |
| 12402 | Py_DECREF(str_obj); |
| 12403 | if (kind1 != kind && buf1) |
| 12404 | PyMem_Free(buf1); |
| 12405 | if (kind2 != kind && buf2) |
| 12406 | PyMem_Free(buf2); |
| 12407 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12408 | } |
| 12409 | |
| 12410 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12412 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12413 | 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] | 12414 | 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] | 12415 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12416 | |
| 12417 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12418 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12419 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12420 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12421 | } |
| 12422 | |
| 12423 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12424 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12425 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12426 | 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] | 12427 | 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] | 12428 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12429 | |
| 12430 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12431 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12432 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12433 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12434 | } |
| 12435 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12436 | PyObject * |
| 12437 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12438 | { |
| 12439 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12440 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12441 | s = PyUnicode_FromObject(s); |
| 12442 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12443 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12444 | if (sep != NULL) { |
| 12445 | sep = PyUnicode_FromObject(sep); |
| 12446 | if (sep == NULL) { |
| 12447 | Py_DECREF(s); |
| 12448 | return NULL; |
| 12449 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12450 | } |
| 12451 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12452 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12453 | |
| 12454 | Py_DECREF(s); |
| 12455 | Py_XDECREF(sep); |
| 12456 | return result; |
| 12457 | } |
| 12458 | |
| 12459 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12460 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12461 | \n\ |
| 12462 | Return a list of the words in S, using sep as the\n\ |
| 12463 | delimiter string, starting at the end of the string and\n\ |
| 12464 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12465 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12466 | is a separator."); |
| 12467 | |
| 12468 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12469 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12470 | { |
| 12471 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12472 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12473 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12474 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12475 | return NULL; |
| 12476 | |
| 12477 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12478 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12479 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12480 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12481 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12482 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12483 | } |
| 12484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12485 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12486 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12487 | \n\ |
| 12488 | 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] | 12489 | 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] | 12490 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12491 | |
| 12492 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12493 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12494 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12495 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12496 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12497 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12498 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12499 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12500 | return NULL; |
| 12501 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12502 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12503 | } |
| 12504 | |
| 12505 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12506 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12507 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12508 | if (PyUnicode_CheckExact(self)) { |
| 12509 | Py_INCREF(self); |
| 12510 | return self; |
| 12511 | } else |
| 12512 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12513 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12514 | } |
| 12515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12516 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12517 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12518 | \n\ |
| 12519 | 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] | 12520 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12521 | |
| 12522 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12523 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12524 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | return fixup(self, fixswapcase); |
| 12526 | } |
| 12527 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12528 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12529 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12530 | \n\ |
| 12531 | Return a translation table usable for str.translate().\n\ |
| 12532 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12533 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12534 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12535 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12536 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12537 | character at the same position in y. If there is a third argument, it\n\ |
| 12538 | must be a string, whose characters will be mapped to None in the result."); |
| 12539 | |
| 12540 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12541 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12542 | { |
| 12543 | PyObject *x, *y = NULL, *z = NULL; |
| 12544 | PyObject *new = NULL, *key, *value; |
| 12545 | Py_ssize_t i = 0; |
| 12546 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12547 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12548 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12549 | return NULL; |
| 12550 | new = PyDict_New(); |
| 12551 | if (!new) |
| 12552 | return NULL; |
| 12553 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12554 | int x_kind, y_kind, z_kind; |
| 12555 | void *x_data, *y_data, *z_data; |
| 12556 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12557 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12558 | if (!PyUnicode_Check(x)) { |
| 12559 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12560 | "be a string if there is a second argument"); |
| 12561 | goto err; |
| 12562 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12563 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12564 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12565 | "arguments must have equal length"); |
| 12566 | goto err; |
| 12567 | } |
| 12568 | /* 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] | 12569 | x_kind = PyUnicode_KIND(x); |
| 12570 | y_kind = PyUnicode_KIND(y); |
| 12571 | x_data = PyUnicode_DATA(x); |
| 12572 | y_data = PyUnicode_DATA(y); |
| 12573 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12574 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12575 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12576 | if (!key || !value) |
| 12577 | goto err; |
| 12578 | res = PyDict_SetItem(new, key, value); |
| 12579 | Py_DECREF(key); |
| 12580 | Py_DECREF(value); |
| 12581 | if (res < 0) |
| 12582 | goto err; |
| 12583 | } |
| 12584 | /* create entries for deleting chars in z */ |
| 12585 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12586 | z_kind = PyUnicode_KIND(z); |
| 12587 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12588 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12589 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12590 | if (!key) |
| 12591 | goto err; |
| 12592 | res = PyDict_SetItem(new, key, Py_None); |
| 12593 | Py_DECREF(key); |
| 12594 | if (res < 0) |
| 12595 | goto err; |
| 12596 | } |
| 12597 | } |
| 12598 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12599 | int kind; |
| 12600 | void *data; |
| 12601 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12602 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12603 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12604 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12605 | "to maketrans it must be a dict"); |
| 12606 | goto err; |
| 12607 | } |
| 12608 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12609 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12610 | if (PyUnicode_Check(key)) { |
| 12611 | /* convert string keys to integer keys */ |
| 12612 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12613 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12614 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12615 | "table must be of length 1"); |
| 12616 | goto err; |
| 12617 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12618 | kind = PyUnicode_KIND(key); |
| 12619 | data = PyUnicode_DATA(key); |
| 12620 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12621 | if (!newkey) |
| 12622 | goto err; |
| 12623 | res = PyDict_SetItem(new, newkey, value); |
| 12624 | Py_DECREF(newkey); |
| 12625 | if (res < 0) |
| 12626 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12627 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12628 | /* just keep integer keys */ |
| 12629 | if (PyDict_SetItem(new, key, value) < 0) |
| 12630 | goto err; |
| 12631 | } else { |
| 12632 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12633 | "be strings or integers"); |
| 12634 | goto err; |
| 12635 | } |
| 12636 | } |
| 12637 | } |
| 12638 | return new; |
| 12639 | err: |
| 12640 | Py_DECREF(new); |
| 12641 | return NULL; |
| 12642 | } |
| 12643 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12644 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12645 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12646 | \n\ |
| 12647 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12648 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12649 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12650 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12651 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12652 | |
| 12653 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12654 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12655 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12656 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | } |
| 12658 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12659 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12660 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12661 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12662 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12663 | |
| 12664 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12665 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12666 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | return fixup(self, fixupper); |
| 12668 | } |
| 12669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12670 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12671 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12672 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12673 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12674 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12675 | |
| 12676 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12677 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12678 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12679 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12680 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12681 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12682 | int kind; |
| 12683 | void *data; |
| 12684 | Py_UCS4 chr; |
| 12685 | |
| 12686 | if (PyUnicode_READY(self) == -1) |
| 12687 | return NULL; |
| 12688 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12689 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12690 | return NULL; |
| 12691 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12692 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12693 | if (PyUnicode_CheckExact(self)) { |
| 12694 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12695 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12696 | } |
| 12697 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12698 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | } |
| 12700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12701 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12702 | |
| 12703 | u = pad(self, fill, 0, '0'); |
| 12704 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12705 | if (u == NULL) |
| 12706 | return NULL; |
| 12707 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12708 | kind = PyUnicode_KIND(u); |
| 12709 | data = PyUnicode_DATA(u); |
| 12710 | chr = PyUnicode_READ(kind, data, fill); |
| 12711 | |
| 12712 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12713 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12715 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12716 | } |
| 12717 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12718 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12719 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | |
| 12722 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12723 | static PyObject * |
| 12724 | unicode__decimal2ascii(PyObject *self) |
| 12725 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12727 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | #endif |
| 12729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12730 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12731 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12732 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12733 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12734 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12735 | With optional end, stop comparing S at that position.\n\ |
| 12736 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | |
| 12738 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12739 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12740 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12742 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12743 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12744 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12745 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12746 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12748 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12749 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12750 | if (PyTuple_Check(subobj)) { |
| 12751 | Py_ssize_t i; |
| 12752 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12753 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12754 | if (substring == NULL) |
| 12755 | return NULL; |
| 12756 | result = tailmatch(self, substring, start, end, -1); |
| 12757 | Py_DECREF(substring); |
| 12758 | if (result) { |
| 12759 | Py_RETURN_TRUE; |
| 12760 | } |
| 12761 | } |
| 12762 | /* nothing matched */ |
| 12763 | Py_RETURN_FALSE; |
| 12764 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12765 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12766 | if (substring == NULL) { |
| 12767 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12768 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12769 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12770 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12771 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12772 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12773 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12774 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12775 | } |
| 12776 | |
| 12777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12778 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12779 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12780 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12781 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12782 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12783 | With optional end, stop comparing S at that position.\n\ |
| 12784 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12785 | |
| 12786 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12787 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12788 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12789 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12790 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12791 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12792 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12793 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12794 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12795 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12796 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12797 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12798 | if (PyTuple_Check(subobj)) { |
| 12799 | Py_ssize_t i; |
| 12800 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12801 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12803 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12804 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12805 | result = tailmatch(self, substring, start, end, +1); |
| 12806 | Py_DECREF(substring); |
| 12807 | if (result) { |
| 12808 | Py_RETURN_TRUE; |
| 12809 | } |
| 12810 | } |
| 12811 | Py_RETURN_FALSE; |
| 12812 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12813 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12814 | if (substring == NULL) { |
| 12815 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12816 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12817 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12818 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12819 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12820 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12821 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12822 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12823 | } |
| 12824 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12825 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12826 | |
| 12827 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12828 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12829 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12830 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12831 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12832 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12833 | PyDoc_STRVAR(format_map__doc__, |
| 12834 | "S.format_map(mapping) -> str\n\ |
| 12835 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12836 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12837 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12838 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12839 | static PyObject * |
| 12840 | unicode__format__(PyObject* self, PyObject* args) |
| 12841 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12842 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12843 | |
| 12844 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12845 | return NULL; |
| 12846 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12847 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12848 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12849 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12850 | } |
| 12851 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12852 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12853 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12854 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12855 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12856 | |
| 12857 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12858 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12859 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12860 | Py_ssize_t size; |
| 12861 | |
| 12862 | /* If it's a compact object, account for base structure + |
| 12863 | character data. */ |
| 12864 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12865 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12866 | else if (PyUnicode_IS_COMPACT(v)) |
| 12867 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12868 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12869 | else { |
| 12870 | /* If it is a two-block object, account for base object, and |
| 12871 | for character block if present. */ |
| 12872 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12873 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12874 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12875 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12876 | } |
| 12877 | /* 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] | 12878 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12879 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12880 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12881 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12882 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12883 | |
| 12884 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12885 | } |
| 12886 | |
| 12887 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12888 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12889 | |
| 12890 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12891 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12892 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12893 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12894 | if (!copy) |
| 12895 | return NULL; |
| 12896 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12897 | } |
| 12898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12899 | static PyMethodDef unicode_methods[] = { |
| 12900 | |
| 12901 | /* Order is according to common usage: often used methods should |
| 12902 | appear first, since lookup is done sequentially. */ |
| 12903 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12904 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12905 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12906 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12907 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12908 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12909 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12910 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12911 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12912 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12913 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12914 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12915 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12916 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12917 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12918 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12919 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12920 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12921 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12922 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12923 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12924 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12925 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12926 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12927 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12928 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12929 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12930 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12931 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12932 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12933 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12934 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12935 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12936 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12937 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12938 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12939 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12940 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12941 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12942 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12943 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12944 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12945 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12946 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12947 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12948 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12949 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12950 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12951 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12952 | #endif |
| 12953 | |
| 12954 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12955 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12956 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12957 | #endif |
| 12958 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12959 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12960 | {NULL, NULL} |
| 12961 | }; |
| 12962 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12963 | static PyObject * |
| 12964 | unicode_mod(PyObject *v, PyObject *w) |
| 12965 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12966 | if (!PyUnicode_Check(v)) |
| 12967 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12968 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12969 | } |
| 12970 | |
| 12971 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12972 | 0, /*nb_add*/ |
| 12973 | 0, /*nb_subtract*/ |
| 12974 | 0, /*nb_multiply*/ |
| 12975 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12976 | }; |
| 12977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12978 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12979 | (lenfunc) unicode_length, /* sq_length */ |
| 12980 | PyUnicode_Concat, /* sq_concat */ |
| 12981 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12982 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12983 | 0, /* sq_slice */ |
| 12984 | 0, /* sq_ass_item */ |
| 12985 | 0, /* sq_ass_slice */ |
| 12986 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12987 | }; |
| 12988 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12989 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12990 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12991 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12992 | if (PyUnicode_READY(self) == -1) |
| 12993 | return NULL; |
| 12994 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12995 | if (PyIndex_Check(item)) { |
| 12996 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12997 | if (i == -1 && PyErr_Occurred()) |
| 12998 | return NULL; |
| 12999 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13000 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13001 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13002 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13003 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13004 | PyObject *result; |
| 13005 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13006 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13007 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13009 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13010 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13011 | return NULL; |
| 13012 | } |
| 13013 | |
| 13014 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13015 | return PyUnicode_New(0, 0); |
| 13016 | } else if (start == 0 && step == 1 && |
| 13017 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13018 | PyUnicode_CheckExact(self)) { |
| 13019 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13020 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13021 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13022 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13023 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13024 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13025 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13026 | src_kind = PyUnicode_KIND(self); |
| 13027 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13028 | if (!PyUnicode_IS_ASCII(self)) { |
| 13029 | kind_limit = kind_maxchar_limit(src_kind); |
| 13030 | max_char = 0; |
| 13031 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13032 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13033 | if (ch > max_char) { |
| 13034 | max_char = ch; |
| 13035 | if (max_char >= kind_limit) |
| 13036 | break; |
| 13037 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13038 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13039 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13040 | else |
| 13041 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13042 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13043 | if (result == NULL) |
| 13044 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13045 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13046 | dest_data = PyUnicode_DATA(result); |
| 13047 | |
| 13048 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13049 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13050 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13051 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13052 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13053 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13054 | } else { |
| 13055 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13056 | return NULL; |
| 13057 | } |
| 13058 | } |
| 13059 | |
| 13060 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13061 | (lenfunc)unicode_length, /* mp_length */ |
| 13062 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13063 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13064 | }; |
| 13065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13067 | /* Helpers for PyUnicode_Format() */ |
| 13068 | |
| 13069 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13070 | 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] | 13071 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13072 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13073 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13074 | (*p_argidx)++; |
| 13075 | if (arglen < 0) |
| 13076 | return args; |
| 13077 | else |
| 13078 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13079 | } |
| 13080 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13081 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13082 | return NULL; |
| 13083 | } |
| 13084 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13085 | /* 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] | 13086 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13087 | static PyObject * |
| 13088 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13090 | char *p; |
| 13091 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13092 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13094 | x = PyFloat_AsDouble(v); |
| 13095 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13096 | return NULL; |
| 13097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13098 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13099 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13100 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13101 | p = PyOS_double_to_string(x, type, prec, |
| 13102 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13103 | if (p == NULL) |
| 13104 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13105 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13106 | PyMem_Free(p); |
| 13107 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13108 | } |
| 13109 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13110 | static PyObject* |
| 13111 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13112 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13113 | char *buf; |
| 13114 | int len; |
| 13115 | PyObject *str; /* temporary string object. */ |
| 13116 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13117 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13118 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13119 | if (!str) |
| 13120 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13121 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13122 | Py_DECREF(str); |
| 13123 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13124 | } |
| 13125 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13126 | static Py_UCS4 |
| 13127 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13128 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13129 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13130 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13131 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13132 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13133 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13134 | goto onError; |
| 13135 | } |
| 13136 | else { |
| 13137 | /* Integer input truncated to a character */ |
| 13138 | long x; |
| 13139 | x = PyLong_AsLong(v); |
| 13140 | if (x == -1 && PyErr_Occurred()) |
| 13141 | goto onError; |
| 13142 | |
| 13143 | if (x < 0 || x > 0x10ffff) { |
| 13144 | PyErr_SetString(PyExc_OverflowError, |
| 13145 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13146 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13147 | } |
| 13148 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13149 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13150 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13151 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13152 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13153 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13154 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13155 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13156 | } |
| 13157 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13158 | static int |
| 13159 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13160 | { |
| 13161 | int r; |
| 13162 | assert(count > 0); |
| 13163 | assert(PyUnicode_Check(obj)); |
| 13164 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13165 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13166 | if (repeated == NULL) |
| 13167 | return -1; |
| 13168 | r = _PyAccu_Accumulate(acc, repeated); |
| 13169 | Py_DECREF(repeated); |
| 13170 | return r; |
| 13171 | } |
| 13172 | else { |
| 13173 | do { |
| 13174 | if (_PyAccu_Accumulate(acc, obj)) |
| 13175 | return -1; |
| 13176 | } while (--count); |
| 13177 | return 0; |
| 13178 | } |
| 13179 | } |
| 13180 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13181 | PyObject * |
| 13182 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13183 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13184 | void *fmt; |
| 13185 | int fmtkind; |
| 13186 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13187 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13188 | int r; |
| 13189 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13190 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13191 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13192 | PyObject *temp = NULL; |
| 13193 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13194 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13195 | _PyAccu acc; |
| 13196 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13197 | |
| 13198 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13199 | return NULL; |
| 13200 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13201 | return NULL; |
| 13202 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13203 | return NULL; |
| 13204 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13205 | return NULL; |
| 13206 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13207 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13209 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13210 | PyErr_BadInternalCall(); |
| 13211 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13212 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13213 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13214 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13215 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13216 | if (_PyAccu_Init(&acc)) |
| 13217 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13218 | fmt = PyUnicode_DATA(uformat); |
| 13219 | fmtkind = PyUnicode_KIND(uformat); |
| 13220 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13221 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13223 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13224 | arglen = PyTuple_Size(args); |
| 13225 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13226 | } |
| 13227 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13228 | arglen = -1; |
| 13229 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13230 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13231 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13232 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13233 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13234 | |
| 13235 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13236 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13237 | PyObject *nonfmt; |
| 13238 | Py_ssize_t nonfmtpos; |
| 13239 | nonfmtpos = fmtpos++; |
| 13240 | while (fmtcnt >= 0 && |
| 13241 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13242 | fmtpos++; |
| 13243 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13244 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13245 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13246 | if (nonfmt == NULL) |
| 13247 | goto onError; |
| 13248 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13249 | Py_DECREF(nonfmt); |
| 13250 | if (r) |
| 13251 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13252 | } |
| 13253 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13254 | /* Got a format specifier */ |
| 13255 | int flags = 0; |
| 13256 | Py_ssize_t width = -1; |
| 13257 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13258 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13259 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13260 | int isnumok; |
| 13261 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13262 | void *pbuf = NULL; |
| 13263 | Py_ssize_t pindex, len; |
| 13264 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13265 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13266 | fmtpos++; |
| 13267 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13268 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13269 | Py_ssize_t keylen; |
| 13270 | PyObject *key; |
| 13271 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13272 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13273 | if (dict == NULL) { |
| 13274 | PyErr_SetString(PyExc_TypeError, |
| 13275 | "format requires a mapping"); |
| 13276 | goto onError; |
| 13277 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13278 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13279 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13280 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13281 | /* Skip over balanced parentheses */ |
| 13282 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13283 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13284 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13285 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13286 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13287 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13288 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13289 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13290 | if (fmtcnt < 0 || pcount > 0) { |
| 13291 | PyErr_SetString(PyExc_ValueError, |
| 13292 | "incomplete format key"); |
| 13293 | goto onError; |
| 13294 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13295 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13296 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13297 | if (key == NULL) |
| 13298 | goto onError; |
| 13299 | if (args_owned) { |
| 13300 | Py_DECREF(args); |
| 13301 | args_owned = 0; |
| 13302 | } |
| 13303 | args = PyObject_GetItem(dict, key); |
| 13304 | Py_DECREF(key); |
| 13305 | if (args == NULL) { |
| 13306 | goto onError; |
| 13307 | } |
| 13308 | args_owned = 1; |
| 13309 | arglen = -1; |
| 13310 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13311 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13312 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13313 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13314 | case '-': flags |= F_LJUST; continue; |
| 13315 | case '+': flags |= F_SIGN; continue; |
| 13316 | case ' ': flags |= F_BLANK; continue; |
| 13317 | case '#': flags |= F_ALT; continue; |
| 13318 | case '0': flags |= F_ZERO; continue; |
| 13319 | } |
| 13320 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13321 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13322 | if (c == '*') { |
| 13323 | v = getnextarg(args, arglen, &argidx); |
| 13324 | if (v == NULL) |
| 13325 | goto onError; |
| 13326 | if (!PyLong_Check(v)) { |
| 13327 | PyErr_SetString(PyExc_TypeError, |
| 13328 | "* wants int"); |
| 13329 | goto onError; |
| 13330 | } |
| 13331 | width = PyLong_AsLong(v); |
| 13332 | if (width == -1 && PyErr_Occurred()) |
| 13333 | goto onError; |
| 13334 | if (width < 0) { |
| 13335 | flags |= F_LJUST; |
| 13336 | width = -width; |
| 13337 | } |
| 13338 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13339 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13340 | } |
| 13341 | else if (c >= '0' && c <= '9') { |
| 13342 | width = c - '0'; |
| 13343 | while (--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 | if (c < '0' || c > '9') |
| 13346 | break; |
| 13347 | if ((width*10) / 10 != width) { |
| 13348 | PyErr_SetString(PyExc_ValueError, |
| 13349 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13350 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13351 | } |
| 13352 | width = width*10 + (c - '0'); |
| 13353 | } |
| 13354 | } |
| 13355 | if (c == '.') { |
| 13356 | prec = 0; |
| 13357 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13358 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13359 | if (c == '*') { |
| 13360 | v = getnextarg(args, arglen, &argidx); |
| 13361 | if (v == NULL) |
| 13362 | goto onError; |
| 13363 | if (!PyLong_Check(v)) { |
| 13364 | PyErr_SetString(PyExc_TypeError, |
| 13365 | "* wants int"); |
| 13366 | goto onError; |
| 13367 | } |
| 13368 | prec = PyLong_AsLong(v); |
| 13369 | if (prec == -1 && PyErr_Occurred()) |
| 13370 | goto onError; |
| 13371 | if (prec < 0) |
| 13372 | prec = 0; |
| 13373 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13374 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13375 | } |
| 13376 | else if (c >= '0' && c <= '9') { |
| 13377 | prec = c - '0'; |
| 13378 | while (--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 | if (c < '0' || c > '9') |
| 13381 | break; |
| 13382 | if ((prec*10) / 10 != prec) { |
| 13383 | PyErr_SetString(PyExc_ValueError, |
| 13384 | "prec too big"); |
| 13385 | goto onError; |
| 13386 | } |
| 13387 | prec = prec*10 + (c - '0'); |
| 13388 | } |
| 13389 | } |
| 13390 | } /* prec */ |
| 13391 | if (fmtcnt >= 0) { |
| 13392 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13393 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13394 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13395 | } |
| 13396 | } |
| 13397 | if (fmtcnt < 0) { |
| 13398 | PyErr_SetString(PyExc_ValueError, |
| 13399 | "incomplete format"); |
| 13400 | goto onError; |
| 13401 | } |
| 13402 | if (c != '%') { |
| 13403 | v = getnextarg(args, arglen, &argidx); |
| 13404 | if (v == NULL) |
| 13405 | goto onError; |
| 13406 | } |
| 13407 | sign = 0; |
| 13408 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13409 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13410 | switch (c) { |
| 13411 | |
| 13412 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13413 | _PyAccu_Accumulate(&acc, percent); |
| 13414 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13415 | |
| 13416 | case 's': |
| 13417 | case 'r': |
| 13418 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13419 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13420 | temp = v; |
| 13421 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13422 | } |
| 13423 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13424 | if (c == 's') |
| 13425 | temp = PyObject_Str(v); |
| 13426 | else if (c == 'r') |
| 13427 | temp = PyObject_Repr(v); |
| 13428 | else |
| 13429 | temp = PyObject_ASCII(v); |
| 13430 | if (temp == NULL) |
| 13431 | goto onError; |
| 13432 | if (PyUnicode_Check(temp)) |
| 13433 | /* nothing to do */; |
| 13434 | else { |
| 13435 | Py_DECREF(temp); |
| 13436 | PyErr_SetString(PyExc_TypeError, |
| 13437 | "%s argument has non-string str()"); |
| 13438 | goto onError; |
| 13439 | } |
| 13440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13441 | if (PyUnicode_READY(temp) == -1) { |
| 13442 | Py_CLEAR(temp); |
| 13443 | goto onError; |
| 13444 | } |
| 13445 | pbuf = PyUnicode_DATA(temp); |
| 13446 | kind = PyUnicode_KIND(temp); |
| 13447 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13448 | if (prec >= 0 && len > prec) |
| 13449 | len = prec; |
| 13450 | break; |
| 13451 | |
| 13452 | case 'i': |
| 13453 | case 'd': |
| 13454 | case 'u': |
| 13455 | case 'o': |
| 13456 | case 'x': |
| 13457 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13458 | isnumok = 0; |
| 13459 | if (PyNumber_Check(v)) { |
| 13460 | PyObject *iobj=NULL; |
| 13461 | |
| 13462 | if (PyLong_Check(v)) { |
| 13463 | iobj = v; |
| 13464 | Py_INCREF(iobj); |
| 13465 | } |
| 13466 | else { |
| 13467 | iobj = PyNumber_Long(v); |
| 13468 | } |
| 13469 | if (iobj!=NULL) { |
| 13470 | if (PyLong_Check(iobj)) { |
| 13471 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13472 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13473 | Py_DECREF(iobj); |
| 13474 | if (!temp) |
| 13475 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13476 | if (PyUnicode_READY(temp) == -1) { |
| 13477 | Py_CLEAR(temp); |
| 13478 | goto onError; |
| 13479 | } |
| 13480 | pbuf = PyUnicode_DATA(temp); |
| 13481 | kind = PyUnicode_KIND(temp); |
| 13482 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13483 | sign = 1; |
| 13484 | } |
| 13485 | else { |
| 13486 | Py_DECREF(iobj); |
| 13487 | } |
| 13488 | } |
| 13489 | } |
| 13490 | if (!isnumok) { |
| 13491 | PyErr_Format(PyExc_TypeError, |
| 13492 | "%%%c format: a number is required, " |
| 13493 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13494 | goto onError; |
| 13495 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13496 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13498 | fillobj = zero; |
| 13499 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13500 | break; |
| 13501 | |
| 13502 | case 'e': |
| 13503 | case 'E': |
| 13504 | case 'f': |
| 13505 | case 'F': |
| 13506 | case 'g': |
| 13507 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13508 | temp = formatfloat(v, flags, prec, c); |
| 13509 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13510 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13511 | if (PyUnicode_READY(temp) == -1) { |
| 13512 | Py_CLEAR(temp); |
| 13513 | goto onError; |
| 13514 | } |
| 13515 | pbuf = PyUnicode_DATA(temp); |
| 13516 | kind = PyUnicode_KIND(temp); |
| 13517 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13518 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13519 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13520 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13521 | fillobj = zero; |
| 13522 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13523 | break; |
| 13524 | |
| 13525 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13526 | { |
| 13527 | Py_UCS4 ch = formatchar(v); |
| 13528 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13529 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13530 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13531 | if (temp == NULL) |
| 13532 | goto onError; |
| 13533 | pbuf = PyUnicode_DATA(temp); |
| 13534 | kind = PyUnicode_KIND(temp); |
| 13535 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13536 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13537 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13538 | |
| 13539 | default: |
| 13540 | PyErr_Format(PyExc_ValueError, |
| 13541 | "unsupported format character '%c' (0x%x) " |
| 13542 | "at index %zd", |
| 13543 | (31<=c && c<=126) ? (char)c : '?', |
| 13544 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13545 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13546 | goto onError; |
| 13547 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13548 | /* pbuf is initialized here. */ |
| 13549 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13550 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13551 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13552 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13553 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13554 | pindex++; |
| 13555 | } |
| 13556 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13557 | signobj = plus; |
| 13558 | len--; |
| 13559 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13560 | } |
| 13561 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13562 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13563 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13564 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13565 | else |
| 13566 | sign = 0; |
| 13567 | } |
| 13568 | if (width < len) |
| 13569 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13570 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13571 | if (fill != ' ') { |
| 13572 | assert(signobj != NULL); |
| 13573 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13574 | goto onError; |
| 13575 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13576 | if (width > len) |
| 13577 | width--; |
| 13578 | } |
| 13579 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13580 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13581 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13583 | second = get_latin1_char( |
| 13584 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13585 | pindex += 2; |
| 13586 | if (second == NULL || |
| 13587 | _PyAccu_Accumulate(&acc, zero) || |
| 13588 | _PyAccu_Accumulate(&acc, second)) |
| 13589 | goto onError; |
| 13590 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13591 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13592 | width -= 2; |
| 13593 | if (width < 0) |
| 13594 | width = 0; |
| 13595 | len -= 2; |
| 13596 | } |
| 13597 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13598 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13599 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13600 | goto onError; |
| 13601 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13602 | } |
| 13603 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13604 | if (sign) { |
| 13605 | assert(signobj != NULL); |
| 13606 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13607 | goto onError; |
| 13608 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13609 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13610 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13611 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13612 | second = get_latin1_char( |
| 13613 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13614 | pindex += 2; |
| 13615 | if (second == NULL || |
| 13616 | _PyAccu_Accumulate(&acc, zero) || |
| 13617 | _PyAccu_Accumulate(&acc, second)) |
| 13618 | goto onError; |
| 13619 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13620 | } |
| 13621 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13622 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13623 | if (temp != NULL) { |
| 13624 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13625 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13626 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13627 | else { |
| 13628 | const char *p = (const char *) pbuf; |
| 13629 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13630 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13631 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13632 | } |
| 13633 | if (v == NULL) |
| 13634 | goto onError; |
| 13635 | r = _PyAccu_Accumulate(&acc, v); |
| 13636 | Py_DECREF(v); |
| 13637 | if (r) |
| 13638 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13639 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13640 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13641 | if (dict && (argidx < arglen) && c != '%') { |
| 13642 | PyErr_SetString(PyExc_TypeError, |
| 13643 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13644 | goto onError; |
| 13645 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13646 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13647 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13648 | } /* until end */ |
| 13649 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13650 | PyErr_SetString(PyExc_TypeError, |
| 13651 | "not all arguments converted during string formatting"); |
| 13652 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13653 | } |
| 13654 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13655 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13656 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13657 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13658 | } |
| 13659 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13660 | Py_XDECREF(temp); |
| 13661 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13662 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13664 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13665 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13666 | Py_XDECREF(temp); |
| 13667 | Py_XDECREF(second); |
| 13668 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13669 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13670 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13671 | } |
| 13672 | return NULL; |
| 13673 | } |
| 13674 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13675 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13676 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13677 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13678 | static PyObject * |
| 13679 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13680 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13681 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13682 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13683 | char *encoding = NULL; |
| 13684 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13685 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13686 | if (type != &PyUnicode_Type) |
| 13687 | return unicode_subtype_new(type, args, kwds); |
| 13688 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13689 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13690 | return NULL; |
| 13691 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13692 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13693 | if (encoding == NULL && errors == NULL) |
| 13694 | return PyObject_Str(x); |
| 13695 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13696 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13697 | } |
| 13698 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13699 | static PyObject * |
| 13700 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13701 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13702 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13703 | Py_ssize_t length, char_size; |
| 13704 | int share_wstr, share_utf8; |
| 13705 | unsigned int kind; |
| 13706 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13707 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13708 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13709 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13710 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13711 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13712 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13713 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13714 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13715 | return NULL; |
| 13716 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13717 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13718 | if (self == NULL) { |
| 13719 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13720 | return NULL; |
| 13721 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13722 | kind = PyUnicode_KIND(unicode); |
| 13723 | length = PyUnicode_GET_LENGTH(unicode); |
| 13724 | |
| 13725 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13726 | #ifdef Py_DEBUG |
| 13727 | _PyUnicode_HASH(self) = -1; |
| 13728 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13729 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13730 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13731 | _PyUnicode_STATE(self).interned = 0; |
| 13732 | _PyUnicode_STATE(self).kind = kind; |
| 13733 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13734 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13735 | _PyUnicode_STATE(self).ready = 1; |
| 13736 | _PyUnicode_WSTR(self) = NULL; |
| 13737 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13738 | _PyUnicode_UTF8(self) = NULL; |
| 13739 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13740 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13741 | |
| 13742 | share_utf8 = 0; |
| 13743 | share_wstr = 0; |
| 13744 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13745 | char_size = 1; |
| 13746 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13747 | share_utf8 = 1; |
| 13748 | } |
| 13749 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13750 | char_size = 2; |
| 13751 | if (sizeof(wchar_t) == 2) |
| 13752 | share_wstr = 1; |
| 13753 | } |
| 13754 | else { |
| 13755 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13756 | char_size = 4; |
| 13757 | if (sizeof(wchar_t) == 4) |
| 13758 | share_wstr = 1; |
| 13759 | } |
| 13760 | |
| 13761 | /* Ensure we won't overflow the length. */ |
| 13762 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13763 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13764 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13765 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13766 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13767 | if (data == NULL) { |
| 13768 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13769 | goto onError; |
| 13770 | } |
| 13771 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13772 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13773 | if (share_utf8) { |
| 13774 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13775 | _PyUnicode_UTF8(self) = data; |
| 13776 | } |
| 13777 | if (share_wstr) { |
| 13778 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13779 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13780 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13781 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13782 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13783 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13784 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13785 | #ifdef Py_DEBUG |
| 13786 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13787 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13788 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13789 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13790 | |
| 13791 | onError: |
| 13792 | Py_DECREF(unicode); |
| 13793 | Py_DECREF(self); |
| 13794 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13795 | } |
| 13796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13797 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13798 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13799 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13800 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13801 | encoding defaults to the current default string encoding.\n\ |
| 13802 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13803 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13804 | static PyObject *unicode_iter(PyObject *seq); |
| 13805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13806 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13807 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13808 | "str", /* tp_name */ |
| 13809 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13810 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13811 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13812 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13813 | 0, /* tp_print */ |
| 13814 | 0, /* tp_getattr */ |
| 13815 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13816 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13817 | unicode_repr, /* tp_repr */ |
| 13818 | &unicode_as_number, /* tp_as_number */ |
| 13819 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13820 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13821 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13822 | 0, /* tp_call*/ |
| 13823 | (reprfunc) unicode_str, /* tp_str */ |
| 13824 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13825 | 0, /* tp_setattro */ |
| 13826 | 0, /* tp_as_buffer */ |
| 13827 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13828 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13829 | unicode_doc, /* tp_doc */ |
| 13830 | 0, /* tp_traverse */ |
| 13831 | 0, /* tp_clear */ |
| 13832 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13833 | 0, /* tp_weaklistoffset */ |
| 13834 | unicode_iter, /* tp_iter */ |
| 13835 | 0, /* tp_iternext */ |
| 13836 | unicode_methods, /* tp_methods */ |
| 13837 | 0, /* tp_members */ |
| 13838 | 0, /* tp_getset */ |
| 13839 | &PyBaseObject_Type, /* tp_base */ |
| 13840 | 0, /* tp_dict */ |
| 13841 | 0, /* tp_descr_get */ |
| 13842 | 0, /* tp_descr_set */ |
| 13843 | 0, /* tp_dictoffset */ |
| 13844 | 0, /* tp_init */ |
| 13845 | 0, /* tp_alloc */ |
| 13846 | unicode_new, /* tp_new */ |
| 13847 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13848 | }; |
| 13849 | |
| 13850 | /* Initialize the Unicode implementation */ |
| 13851 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13852 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13853 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13854 | int i; |
| 13855 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13856 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13857 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13858 | 0x000A, /* LINE FEED */ |
| 13859 | 0x000D, /* CARRIAGE RETURN */ |
| 13860 | 0x001C, /* FILE SEPARATOR */ |
| 13861 | 0x001D, /* GROUP SEPARATOR */ |
| 13862 | 0x001E, /* RECORD SEPARATOR */ |
| 13863 | 0x0085, /* NEXT LINE */ |
| 13864 | 0x2028, /* LINE SEPARATOR */ |
| 13865 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13866 | }; |
| 13867 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13868 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13869 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13870 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13871 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13872 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13873 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13874 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13875 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13876 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13877 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13878 | |
| 13879 | /* initialize the linebreak bloom filter */ |
| 13880 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13881 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13882 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13883 | |
| 13884 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13885 | |
| 13886 | #ifdef HAVE_MBCS |
| 13887 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13888 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13889 | PyErr_SetFromWindowsErr(0); |
| 13890 | return -1; |
| 13891 | } |
| 13892 | #endif |
| 13893 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13894 | } |
| 13895 | |
| 13896 | /* Finalize the Unicode implementation */ |
| 13897 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13898 | int |
| 13899 | PyUnicode_ClearFreeList(void) |
| 13900 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13901 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13902 | } |
| 13903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13904 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13905 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13906 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13907 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13908 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13909 | Py_XDECREF(unicode_empty); |
| 13910 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13911 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13912 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13913 | if (unicode_latin1[i]) { |
| 13914 | Py_DECREF(unicode_latin1[i]); |
| 13915 | unicode_latin1[i] = NULL; |
| 13916 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13917 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13918 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13919 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13920 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13921 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13922 | void |
| 13923 | PyUnicode_InternInPlace(PyObject **p) |
| 13924 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13925 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13926 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13927 | #ifdef Py_DEBUG |
| 13928 | assert(s != NULL); |
| 13929 | assert(_PyUnicode_CHECK(s)); |
| 13930 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13931 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13932 | return; |
| 13933 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13934 | /* If it's a subclass, we don't really know what putting |
| 13935 | it in the interned dict might do. */ |
| 13936 | if (!PyUnicode_CheckExact(s)) |
| 13937 | return; |
| 13938 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13939 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13940 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13941 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13942 | return; |
| 13943 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13944 | s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13945 | if (interned == NULL) { |
| 13946 | interned = PyDict_New(); |
| 13947 | if (interned == NULL) { |
| 13948 | PyErr_Clear(); /* Don't leave an exception */ |
| 13949 | return; |
| 13950 | } |
| 13951 | } |
| 13952 | /* It might be that the GetItem call fails even |
| 13953 | though the key is present in the dictionary, |
| 13954 | namely when this happens during a stack overflow. */ |
| 13955 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13956 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13957 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13958 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13959 | if (t) { |
| 13960 | Py_INCREF(t); |
| 13961 | Py_DECREF(*p); |
| 13962 | *p = t; |
| 13963 | return; |
| 13964 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13965 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13966 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13967 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13968 | PyErr_Clear(); |
| 13969 | PyThreadState_GET()->recursion_critical = 0; |
| 13970 | return; |
| 13971 | } |
| 13972 | PyThreadState_GET()->recursion_critical = 0; |
| 13973 | /* The two references in interned are not counted by refcnt. |
| 13974 | The deallocator will take care of this */ |
| 13975 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13976 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13977 | } |
| 13978 | |
| 13979 | void |
| 13980 | PyUnicode_InternImmortal(PyObject **p) |
| 13981 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13982 | PyUnicode_InternInPlace(p); |
| 13983 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 13984 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13985 | Py_INCREF(*p); |
| 13986 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13987 | } |
| 13988 | |
| 13989 | PyObject * |
| 13990 | PyUnicode_InternFromString(const char *cp) |
| 13991 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13992 | PyObject *s = PyUnicode_FromString(cp); |
| 13993 | if (s == NULL) |
| 13994 | return NULL; |
| 13995 | PyUnicode_InternInPlace(&s); |
| 13996 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13997 | } |
| 13998 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13999 | void |
| 14000 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14001 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14002 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14003 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14004 | Py_ssize_t i, n; |
| 14005 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
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 | if (interned == NULL || !PyDict_Check(interned)) |
| 14008 | return; |
| 14009 | keys = PyDict_Keys(interned); |
| 14010 | if (keys == NULL || !PyList_Check(keys)) { |
| 14011 | PyErr_Clear(); |
| 14012 | return; |
| 14013 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14014 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14015 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14016 | detector, interned unicode strings are not forcibly deallocated; |
| 14017 | rather, we give them their stolen references back, and then clear |
| 14018 | and DECREF the interned dict. */ |
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 | n = PyList_GET_SIZE(keys); |
| 14021 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14022 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14023 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14024 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14025 | if (PyUnicode_READY(s) == -1) { |
| 14026 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14027 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14028 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14029 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14030 | case SSTATE_NOT_INTERNED: |
| 14031 | /* XXX Shouldn't happen */ |
| 14032 | break; |
| 14033 | case SSTATE_INTERNED_IMMORTAL: |
| 14034 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14035 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14036 | break; |
| 14037 | case SSTATE_INTERNED_MORTAL: |
| 14038 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14039 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14040 | break; |
| 14041 | default: |
| 14042 | Py_FatalError("Inconsistent interned string state."); |
| 14043 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14044 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14045 | } |
| 14046 | fprintf(stderr, "total size of all interned strings: " |
| 14047 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14048 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14049 | Py_DECREF(keys); |
| 14050 | PyDict_Clear(interned); |
| 14051 | Py_DECREF(interned); |
| 14052 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14053 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14054 | |
| 14055 | |
| 14056 | /********************* Unicode Iterator **************************/ |
| 14057 | |
| 14058 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14059 | PyObject_HEAD |
| 14060 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14061 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14062 | } unicodeiterobject; |
| 14063 | |
| 14064 | static void |
| 14065 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14066 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14067 | _PyObject_GC_UNTRACK(it); |
| 14068 | Py_XDECREF(it->it_seq); |
| 14069 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14070 | } |
| 14071 | |
| 14072 | static int |
| 14073 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14074 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14075 | Py_VISIT(it->it_seq); |
| 14076 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14077 | } |
| 14078 | |
| 14079 | static PyObject * |
| 14080 | unicodeiter_next(unicodeiterobject *it) |
| 14081 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14082 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14083 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14084 | assert(it != NULL); |
| 14085 | seq = it->it_seq; |
| 14086 | if (seq == NULL) |
| 14087 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14088 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14090 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14091 | int kind = PyUnicode_KIND(seq); |
| 14092 | void *data = PyUnicode_DATA(seq); |
| 14093 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14094 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14095 | if (item != NULL) |
| 14096 | ++it->it_index; |
| 14097 | return item; |
| 14098 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14099 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14100 | Py_DECREF(seq); |
| 14101 | it->it_seq = NULL; |
| 14102 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14103 | } |
| 14104 | |
| 14105 | static PyObject * |
| 14106 | unicodeiter_len(unicodeiterobject *it) |
| 14107 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14108 | Py_ssize_t len = 0; |
| 14109 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14110 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14111 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14112 | } |
| 14113 | |
| 14114 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14115 | |
| 14116 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14117 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14118 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14119 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14120 | }; |
| 14121 | |
| 14122 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14123 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14124 | "str_iterator", /* tp_name */ |
| 14125 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14126 | 0, /* tp_itemsize */ |
| 14127 | /* methods */ |
| 14128 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14129 | 0, /* tp_print */ |
| 14130 | 0, /* tp_getattr */ |
| 14131 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14132 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14133 | 0, /* tp_repr */ |
| 14134 | 0, /* tp_as_number */ |
| 14135 | 0, /* tp_as_sequence */ |
| 14136 | 0, /* tp_as_mapping */ |
| 14137 | 0, /* tp_hash */ |
| 14138 | 0, /* tp_call */ |
| 14139 | 0, /* tp_str */ |
| 14140 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14141 | 0, /* tp_setattro */ |
| 14142 | 0, /* tp_as_buffer */ |
| 14143 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14144 | 0, /* tp_doc */ |
| 14145 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14146 | 0, /* tp_clear */ |
| 14147 | 0, /* tp_richcompare */ |
| 14148 | 0, /* tp_weaklistoffset */ |
| 14149 | PyObject_SelfIter, /* tp_iter */ |
| 14150 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14151 | unicodeiter_methods, /* tp_methods */ |
| 14152 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14153 | }; |
| 14154 | |
| 14155 | static PyObject * |
| 14156 | unicode_iter(PyObject *seq) |
| 14157 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14158 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14159 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14160 | if (!PyUnicode_Check(seq)) { |
| 14161 | PyErr_BadInternalCall(); |
| 14162 | return NULL; |
| 14163 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14164 | if (PyUnicode_READY(seq) == -1) |
| 14165 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14166 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14167 | if (it == NULL) |
| 14168 | return NULL; |
| 14169 | it->it_index = 0; |
| 14170 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14171 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14172 | _PyObject_GC_TRACK(it); |
| 14173 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14174 | } |
| 14175 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14176 | |
| 14177 | size_t |
| 14178 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14179 | { |
| 14180 | int res = 0; |
| 14181 | while(*u++) |
| 14182 | res++; |
| 14183 | return res; |
| 14184 | } |
| 14185 | |
| 14186 | Py_UNICODE* |
| 14187 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14188 | { |
| 14189 | Py_UNICODE *u = s1; |
| 14190 | while ((*u++ = *s2++)); |
| 14191 | return s1; |
| 14192 | } |
| 14193 | |
| 14194 | Py_UNICODE* |
| 14195 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14196 | { |
| 14197 | Py_UNICODE *u = s1; |
| 14198 | while ((*u++ = *s2++)) |
| 14199 | if (n-- == 0) |
| 14200 | break; |
| 14201 | return s1; |
| 14202 | } |
| 14203 | |
| 14204 | Py_UNICODE* |
| 14205 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14206 | { |
| 14207 | Py_UNICODE *u1 = s1; |
| 14208 | u1 += Py_UNICODE_strlen(u1); |
| 14209 | Py_UNICODE_strcpy(u1, s2); |
| 14210 | return s1; |
| 14211 | } |
| 14212 | |
| 14213 | int |
| 14214 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14215 | { |
| 14216 | while (*s1 && *s2 && *s1 == *s2) |
| 14217 | s1++, s2++; |
| 14218 | if (*s1 && *s2) |
| 14219 | return (*s1 < *s2) ? -1 : +1; |
| 14220 | if (*s1) |
| 14221 | return 1; |
| 14222 | if (*s2) |
| 14223 | return -1; |
| 14224 | return 0; |
| 14225 | } |
| 14226 | |
| 14227 | int |
| 14228 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14229 | { |
| 14230 | register Py_UNICODE u1, u2; |
| 14231 | for (; n != 0; n--) { |
| 14232 | u1 = *s1; |
| 14233 | u2 = *s2; |
| 14234 | if (u1 != u2) |
| 14235 | return (u1 < u2) ? -1 : +1; |
| 14236 | if (u1 == '\0') |
| 14237 | return 0; |
| 14238 | s1++; |
| 14239 | s2++; |
| 14240 | } |
| 14241 | return 0; |
| 14242 | } |
| 14243 | |
| 14244 | Py_UNICODE* |
| 14245 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14246 | { |
| 14247 | const Py_UNICODE *p; |
| 14248 | for (p = s; *p; p++) |
| 14249 | if (*p == c) |
| 14250 | return (Py_UNICODE*)p; |
| 14251 | return NULL; |
| 14252 | } |
| 14253 | |
| 14254 | Py_UNICODE* |
| 14255 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14256 | { |
| 14257 | const Py_UNICODE *p; |
| 14258 | p = s + Py_UNICODE_strlen(s); |
| 14259 | while (p != s) { |
| 14260 | p--; |
| 14261 | if (*p == c) |
| 14262 | return (Py_UNICODE*)p; |
| 14263 | } |
| 14264 | return NULL; |
| 14265 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14266 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14267 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14268 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14269 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14270 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14271 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14272 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14273 | if (!PyUnicode_Check(unicode)) { |
| 14274 | PyErr_BadArgument(); |
| 14275 | return NULL; |
| 14276 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14277 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14278 | if (u == NULL) |
| 14279 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14280 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14281 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14282 | PyErr_NoMemory(); |
| 14283 | return NULL; |
| 14284 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14285 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14286 | size *= sizeof(Py_UNICODE); |
| 14287 | copy = PyMem_Malloc(size); |
| 14288 | if (copy == NULL) { |
| 14289 | PyErr_NoMemory(); |
| 14290 | return NULL; |
| 14291 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14292 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14293 | return copy; |
| 14294 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14295 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14296 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14297 | to the string.Formatter class implemented in Python. */ |
| 14298 | |
| 14299 | static PyMethodDef _string_methods[] = { |
| 14300 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14301 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14302 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14303 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14304 | {NULL, NULL} |
| 14305 | }; |
| 14306 | |
| 14307 | static struct PyModuleDef _string_module = { |
| 14308 | PyModuleDef_HEAD_INIT, |
| 14309 | "_string", |
| 14310 | PyDoc_STR("string helper module"), |
| 14311 | 0, |
| 14312 | _string_methods, |
| 14313 | NULL, |
| 14314 | NULL, |
| 14315 | NULL, |
| 14316 | NULL |
| 14317 | }; |
| 14318 | |
| 14319 | PyMODINIT_FUNC |
| 14320 | PyInit__string(void) |
| 14321 | { |
| 14322 | return PyModule_Create(&_string_module); |
| 14323 | } |
| 14324 | |
| 14325 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14326 | #ifdef __cplusplus |
| 14327 | } |
| 14328 | #endif |